-
Notifications
You must be signed in to change notification settings - Fork 6
Fix pnpm compatibility by replacing build-time path manipulation with runtime detection #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jack-rhodes
wants to merge
3
commits into
get-convex:main
Choose a base branch
from
jack-rhodes:fix/pnpm-compatibility
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,9 @@ import { | |
| jsonToConvex, | ||
| } from "convex/values"; | ||
| import { compareValues } from "./compare.js"; | ||
| import { fileURLToPath } from "url"; | ||
| import { join } from "path"; | ||
| import { existsSync, readdirSync, statSync } from "fs"; | ||
|
|
||
| type FilterJson = | ||
| | { $eq: [FilterJson, FilterJson] } | ||
|
|
@@ -1556,8 +1559,81 @@ function getSyscalls() { | |
| }; | ||
| } | ||
|
|
||
| function findConvexFiles( | ||
| projectRoot: string, | ||
| ): Record<string, () => Promise<any>> { | ||
| const convexDir = join(projectRoot, "convex"); | ||
|
|
||
| if (!existsSync(convexDir)) { | ||
| throw new Error( | ||
| `Could not find "convex" directory at "${convexDir}". ` + | ||
| `Make sure your project has a convex directory, or provide the modules parameter to convexTest().`, | ||
| ); | ||
| } | ||
|
|
||
| const modules: Record<string, () => Promise<any>> = {}; | ||
|
|
||
| function scanDirectory(dir: string, relativePath: string = "") { | ||
| const items = readdirSync(dir); | ||
|
|
||
| for (const item of items) { | ||
| const fullPath = join(dir, item); | ||
| const stat = statSync(fullPath); | ||
|
|
||
| if (stat.isDirectory()) { | ||
| scanDirectory( | ||
| fullPath, | ||
| relativePath ? `${relativePath}/${item}` : item, | ||
| ); | ||
| } else if (stat.isFile() && /\.(js|ts|mjs|mts)$/.test(item)) { | ||
| // Create a relative path from convex directory | ||
| const moduleKey = relativePath | ||
| ? `./convex/${relativePath}/${item}` | ||
| : `./convex/${item}`; | ||
|
|
||
| modules[moduleKey] = async () => { | ||
| return await import(fullPath); | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| scanDirectory(convexDir); | ||
| return modules; | ||
| } | ||
|
|
||
| function findProjectRoot(): string { | ||
| // Split on node_modules - works for npm, yarn, and pnpm in standard setups | ||
| try { | ||
| const currentPath = | ||
| typeof __filename !== "undefined" | ||
| ? __filename | ||
| : fileURLToPath(import.meta.url); | ||
|
|
||
| // For pnpm, we need to find the first node_modules, not the last one | ||
| // because pnpm can have nested node_modules like: | ||
| // /project/node_modules/.pnpm/package@version_deps/node_modules/package/file.js | ||
| const nodeModulesIndex = currentPath.indexOf("/node_modules/"); | ||
| if (nodeModulesIndex !== -1) { | ||
| return currentPath.substring(0, nodeModulesIndex); | ||
| } | ||
| } catch (e) { | ||
| // Fall through to fallback | ||
| } | ||
|
|
||
| // Fallback to current working directory | ||
| return process.cwd(); | ||
| } | ||
|
|
||
| function moduleCache(specifiedModules?: Record<string, () => Promise<any>>) { | ||
| const modules = specifiedModules ?? import.meta.glob("./convex/**/*.*s"); | ||
| const modules = | ||
| specifiedModules ?? | ||
| (() => { | ||
| // Use dynamic module discovery when no modules are specified | ||
| const projectRoot = findProjectRoot(); | ||
| return findConvexFiles(projectRoot); | ||
| })(); | ||
|
|
||
| const prefix = findModulesRoot( | ||
| Object.keys(modules), | ||
| specifiedModules !== undefined, | ||
|
|
@@ -1591,9 +1667,9 @@ function findModulesRoot(modulesPaths: string[], userProvidedModules: boolean) { | |
| (userProvidedModules | ||
| ? "Make sure your `import.meta.glob` includes the files in the " + | ||
| '"_generated" directory' | ||
| : "If your Convex functions aren't defined in a directory " + | ||
| 'called "convex" sibling to your node_modules, ' + | ||
| "provide the second argument to `convexTest`"), | ||
| : "convex-test automatically detected your project root and convex directory. " + | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we could say where we tried to find them (show the project root path e.g.) |
||
| "If your Convex functions are in a non-standard location, " + | ||
| "provide the modules parameter to `convexTest` with your custom `import.meta.glob` pattern."), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ); | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ideally, we could also see if there's a
convex.jsonin the project root that has afunctionspath, which is used to point to custom function locations.