Repository navigation
Conversation
|
Thank you for opening this PR, supporting PnP would be really nice! |
There was a problem hiding this comment.
Pull request overview
Adds Yarn Plug’n’Play (PnP) support by switching dependency resolution to Node’s module resolver (via createRequire) when static filesystem resolution fails.
Changes:
- Removed the Yarn PnP “unsupported, skip trace” guard so tracing runs under PnP.
- Added a PnP-friendly fallback in
nodeFileTraceresolution usingcreateRequire(parent).resolve. - Made asset relocation resolution context-aware by threading the importer/source id into
safeRequireResolveandprocessRequireCall.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
standaloner/src/trace.ts |
Removes the PnP early-exit and adds createRequire-based fallback resolution for tracing. |
standaloner/src/relocate.ts |
Makes require/resolve context-aware by using createRequire(importer) and passing sourceId through require-call processing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| try { | ||
| return toPosixPath(require.resolve(request, options)); | ||
| const req = importer ? createRequire(importer) : require; | ||
| return toPosixPath(req.resolve(request)); |
There was a problem hiding this comment.
safeRequireResolve calls createRequire(importer) directly. In Vite/Rollup, importer can be decorated with a leading \0 and/or ?query/#hash suffix (e.g. commonjs interop ids); createRequire will throw for \0-prefixed paths, causing the fallback resolve to silently fail. Consider normalizing/sanitizing importer before passing it to createRequire (strip leading \0, remove [?#].*$), similar to the cleanup done in utils/build-externals.ts around the cleanId handling.
There was a problem hiding this comment.
@copilot apply changes based on this feedback
There was a problem hiding this comment.
-
Investigating fallback logic
I'm currently reviewing
src/trace.tsto see if the fallback resolution logic I implemented there, similar to the one increateRequire(parent), needs adjustment. I'll be focusing on theparentvariable within that file's context. -
Verifying resolve paths
My current focus is on
src/relocate.ts. I've determined that@vercel/nft's internal tracing mechanism handles only direct file paths, not the pseudo-paths like\0or hashes found in Vite chunks. Therefore, adjustments tosrc/trace.tsseem unnecessary for now, and thesafeRequireResolvebug report specifically relates tocreateRequire(importer)withinrelocate.ts. I will proceed with testing.
| const resolved = safeRequireResolve(modulePath, sourceId); | ||
| const potentialPath = resolved || path.resolve(dirName, modulePath); | ||
| const ext = path.extname(potentialPath).toLowerCase(); | ||
| if (['.js', '.mjs', '.cjs', '.ts', '.tsx', '.jsx', '.json', ''].includes(ext)) return null; |
There was a problem hiding this comment.
processRequireCall now resolves the specifier with safeRequireResolve(modulePath, sourceId), but the returned resolved path is only used to compute ext and is then discarded; the reference still stores path: modulePath, so later path.resolve(dirName, filePath) will fail for non-relative specifiers (e.g. require("pkg/subpath/binding.node")) even though safeRequireResolve found the real file. Use the resolved path (when available) for the emitted FileReference so the relocator can actually read/emit the asset.
There was a problem hiding this comment.
-
Investigating Relocation Logic
I'm currently examining the
processRequireCallfunction withinrelocate.ts, specifically focusing on howsafeRequireResolveis being utilized for module resolution. My aim is to pinpoint any potential issues with the current implementation concerningmodulePathandsourceId. -
Refining Module Resolution
I'm refining the module resolution within
processRequireCall. My focus is on ensuring that the actual resolved path, not just the original specifier, is correctly used forFileReferencegeneration to prevent downstream resolution failures.
Co-authored-by: Copilot <copilot@github.com>
@nitedani Yes, the require.resolve and the loader file
IDK, probably not, it's all. Copilot reviews and adds 2 suggestions, I will check them. PS:
|
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| throw new Error(`Build output file dist/index.js or dist/index.mjs not found!`); | ||
| } | ||
|
|
||
| const output = execSync(`node ${distEntry}`, { stdio: 'pipe', cwd: tmpDir }).toString(); |
There was a problem hiding this comment.
execSync(node ${distEntry}) will break if distEntry contains spaces/shell-special characters (common under temp directories on some systems). Prefer execFileSync('node', [distEntry], ...) or ensure the path is safely quoted/escaped.
| try { | ||
| const parentFile = parent ? path.resolve(baseDir, parent) : path.join(baseDir, 'index.js'); | ||
| const req = createRequire(parentFile); | ||
| return toPosixPath(req.resolve(id)); |
There was a problem hiding this comment.
In PnP environments nftDefaultResolve is expected to fail frequently, so this fallback becomes the hot path. Creating a new createRequire(parentFile) instance on every miss can add noticeable overhead; consider caching the require object per parentFile (or caching resolve(id,parent) results) to keep trace time reasonable on large projects.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
@nitedani please can you check these commits related to fixes of Copilot's suggestions?
Open points: |
|
I've continued this in #27, which supersedes this PR. @rtritto, your seven commits are in it unchanged, rebased on
With Yarn's default global cache, the package zips sit outside the project, and standaloner's dependency tracer ignores files outside the project. So this PR's fallback copied nothing, not even Both open Copilot points are settled too. The test now runs the output with |
|
Done in #27, based on your commits. |
Fix #6
Description
This PR introduces support for Yarn Plug'n'Play (PnP) by leveraging Node's native createRequire API to allow PnP to properly hook into the module resolution process.
Motivation
Previously,
standalonerrelied heavily on static file-system resolution algorithms (like those built into@vercel/nft). This approach fundamentally breaks in PnP environments where a physicalnode_modulesdirectory is absent. To fix this, we need to delegate dependency resolution to Node's native module system (require.resolve), which Yarn automatically patches at runtime to load dependencies directly from the.yarn/cache.Changes
src/trace.ts:pnpapimodule was detected.resolvehook fornodeFileTrace. If the default@vercel/nftresolver fails to find the file (which is expected in PnP), it now falls back to a context-awarecreateRequire(parentFile).resolve(id). This triggers Yarn's patched resolver to correctly locate the file.src/relocate.ts:safeRequireResolveutility to dynamically accept animporterfile path context.importer/sourceIdthrough the AST walker (processRequireCall) and theresolveIdbundler hook so that dependencies are resolved accurately relative to their importing file.Testing
createRequirelogic.