Skip to content

feat: support Yarn Plug'n'Play (PnP) - #27

Merged
nitedani merged 45 commits into
nitedani:mainfrom
nitedani-agent:support-pnp
Oct 30, 2026
Merged

nitedani merged 45 commits into
nitedani:mainfrom
nitedani-agent:support-pnp

Conversation

@nitedani-agent

@nitedani-agent nitedani-agent commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

ClaudeCodex Claude Opus 5.5 (high effort) wrote this; GPT-6.1-Sol (Codex, high effort) reviewed it.

In a Yarn PnP project, standaloner leaves every external dependency out of the output, so the build succeeds but the app crashes on start. This PR finds those packages through Yarn and copies them, including from Yarn's global cache.

This supersedes #20 by @rtritto. His seven commits are kept here, rebased on main, with him as their author. Thank you for starting this!

Case (Yarn 4, PnP, lodash left external) Observed on main Expected
yarn node build.mjs, then node dist/index.mjs dist/node_modules stays empty, ERR_MODULE_NOT_FOUND: Cannot find package 'lodash' lodash copied, the app runs

The old PnP check never ran, because require is undefined in this ES module (trace.ts#L93-L98).

This PR asks Yarn where each package's folder is and lets Node File Trace (nft) resolve it from there with its usual exports rules. It also copies platform binaries such as @esbuild/linux-x64, workspace and portal: packages, and npm: aliases under their alias name. From #20, asset relocation resolves require() paths from the importing file instead of from standaloner's own folder, in every project.

The build has to run through Yarn (yarn node build.js or yarn <script>). A link: folder without its own package.json isn't copied, because it has no package name to copy it under. A local package without a name is missed when only its exports field leads to its entry file, because @vercel/nft applies exports only to named packages. Two copies of one package that get different peer dependencies still ship as one, as on main without PnP (#28).

Closes #6

What you should see

Each case runs on Node 26, on main (c46848b) and on this PR (193b056).

1. test/pnp, the new CI test:

main:    Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'lodash4'
this PR: esm {"a":1,"b":2} let a = 1; cjs "cjs-parent" lodash-fork linked esm linked esm linked esm fork local "fork-user" AB B A nested-type lodash4-local 4.17.21 1s 3.10.1 4.17.21

2. A monorepo app leaving a workspace with module-sync exports and a portal: package external prints what it prints under Yarn:

main:    Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'lib'
this PR: lib esm sync.mjs function

3. An app under /usr/src/app with esbuild, react-dom and debug external:

main:    0 files copied, Cannot find package 'esbuild'
this PR: 34 files copied, the app runs
Approach: five candidates, C chosen (9/10)
Candidate Rating
A. #20 as is: createRequire(parent).resolve(id) when nft misses 3
B. Ask Yarn for the full resolution with nft's conditions 7
C. Ask Yarn only for the package folder, then let nft resolve from there 9
D. PnP support inside @vercel/nft 4
E. Keep PnP unsupported, make the old check warn 2

A copied nothing with Yarn's global cache on (its default). B had to reimplement nft's exports handling (module-sync and its fallback). C keeps nft's own rules, and nft's resolve option is the extension point its maintainers chose for PnP (vercel/nft#153). Full record and the decisions made while converging: https://gist.github.com/nitedani-agent/fc43f9ced9b5cfad8f87a899eb3a42f6

Loop A: 23 passes on Node 26 and Node 20, the last (on 193b056) found nothing

Each pass ran probes on the head it named. Early passes compared its output with main and with the same project under nodeLinker: node-modules. Later ones also compared it with the app run directly under Yarn. After each fix since 0e8905c, an app leaving express, @babel/core, eslint, webpack and aws-sdk external copied the same 3562 files as with nodeLinker: node-modules (main copies none). Fixed, by area:

  • Where Yarn keeps packages: a project or Yarn's global folder under /usr (12ef853, ebcf7e3), platform binaries such as @esbuild/linux-x64 (8deda65), a workspace with module-sync exports and .pnp.cjs shipped (1b55235), files under a nested {"type":"module"} package.json (c928d81).
  • npm: aliases: one alias name at two versions (3ff8901), an app alias whose name a dependency's package also has (18bef03, 193b056).
  • Local packages under another name: renamed portal: packages (668e665, 34355ec), two local folders with one name (dc3fce0, 65971d6, 0e8905c, 41e7479, 1149d25).
  • A local fork with an npm package's name and version (9ce7397, 9da0b65, 5e63883), the copy the app imports at the top level (c3d47d8), a bundled renamed package's import build (f078f7a).

Below the bar, outside this change: two peer copies of one name and version ship as one, on main too (#28). A local package without a name reached only through exports is missed (nft's self-reference rule). Full reports: https://gist.github.com/nitedani-agent/0b5a256b8c5c076735a66bfc13f9c8d3

Loop B: 20 review rounds, the last four clean, the last one on 193b056

Fixed: a shell command split on paths with spaces (c820760), one alias link per importing package instead of per name (3ff8901), a refactor that records a package imported under another name in one place (2d74ba9), one that reads each import's copy from the folder it resolved to (591719b), and a test that keeps the app's own local utils when a dependency imports another utils (e7bff26). Declined: a CI lane for a project under /usr (it needs root on the runner), and a module-sync test (nft skips module-sync on Node 20, which CI runs). Full reports: https://gist.github.com/nitedani-agent/7ff89740d0e33cc06ca92636cd794436

Fresh read: 7 rounds as nitedani, the last says merge as is (on 193b056)

Code findings fixed: an npm: alias losing its name (33e0fbb, 39e1619), no test requiring a package with conditional exports from CommonJS (3acb228), a renamed portal: dependency crashing (668e665), and the app's npm: alias losing to a dependency's local package (18bef03). Two peer copies shipping as one became #28. The other findings were on this description. Outside this change, on main too: Rolldown bundles a package's import export over module-sync, and a package whose imports map points at another dependency is copied without it. Full reports: https://gist.github.com/nitedani-agent/31131c6e531349a9aae28ee3a7418962

Refactor pass: asked for in https://github.com//pull/27#issuecomment-6501158643, one change (6085568)

A fresh pass over the whole diff on 193b056 rated every file and function. Its one finding: pnpResolve checked whether a package sits in a node_modules folder twice. 6085568 handles an npm package in one early-return branch, and a local package after it, with the same behavior. The re-rating on 6085568 found nothing more. Full reports: https://gist.github.com/nitedani-agent/df0f870446abf38747b3b5c7fda498d1

Gates: install, build and the four test suites exit 0 on 6085568, on Node 20 and Node 26

The CI workflow's steps, run locally because this fork PR's CI waits for workflow approval:

Step Node 20.20.2 Node 26.12.1
pnpm install, pnpm build exit 0 exit 0
test/vite, test/isolated, test/assets (10, 6 and 24 checks) exit 0 exit 0
test/pnp (2 checks) exit 0 exit 0

Corepack is disabled on the machine that ran them, so test/pnp ran Yarn 4 through pnpm dlx behind a corepack shim.

rtritto and others added 17 commits October 29, 2026 04:34
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <copilot@github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Under Yarn PnP there is no node_modules tree, so nft's resolver missed every
external package and its zips, in Yarn's global cache by default, sat outside
nft's base. Ask PnP for the package's folder and let nft's own resolver apply
exports from there, and trace from the filesystem root under PnP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test bundled lodash, so it passed without any tracing. Leave lodash and a
dual import/require package external, check the output runs with plain node
and loads the ESM build, and run it in CI. Remove the unused
test/vite/test/pnp/index.js fixture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under PnP nft traces from the filesystem root, so the system-path filter
also dropped Yarn's virtual, unplugged and local-cache packages of a
project in /usr (Docker's WORKDIR /usr/src/app). Drop /usr paths only
outside the project.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Under PnP each package sits in its own zip or unplugged folder, so the
node_modules walk never found a package's platform binary (esbuild's
@esbuild/linux-x64) and the app crashed on start. Ask PnP for the
optional dependency's folder first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers the platform optionalDependency (@esbuild/<platform>) that has to be
copied from its own unplugged folder under PnP.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The previous commit still dropped Yarn's global cache when it sits under
/usr, since the cache is always outside the project. Skip the /usr filter
under PnP: every PnP package lives in a Yarn folder, and files outside
any package are already skipped. Outside PnP the filter is back to main's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Yarn places these outside any node_modules folder, so nft couldn't find
them from next to the package and the app crashed on start. Resolve them
through PnP with nft's exports conditions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A checkout path with a space split the file: dependencies.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolving them through PnP with nft's conditions missed nft's module-sync
handling, and took esbuild's require.resolve('pnpapi') for a package, so
.pnp.cjs was shipped. Let nft resolve the package as importing itself, or
the path inside its folder, and only for a folder with a package.json.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Covers a package Yarn places outside any node_modules folder, as it does
for workspaces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nitedani-agent and others added 12 commits October 29, 2026 06:28
Yarn PnP keeps an aliased dependency in the folder of the package it
installs, so it was copied under that package's name and a require of the
alias (string-width-cjs in @isaacs/cliui) failed at runtime. Resolve the
installed package and link the alias name to its copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two packages requiring the same alias name for different versions both got
the last one. Link each alias inside its importer's copy, and at the top
level only for the app's own imports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… PnP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…l exports under PnP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…under PnP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d by under PnP

A fork whose package.json keeps the original's name and version no longer
shares one output folder with the original.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A name another folder already took falls back to the package's own name,
and folders are matched by their real path, so a symlinked portal: keeps
its import name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…me is taken under PnP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…er PnP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n of an npm package

Both shared one output folder, so a fork kept at the original's version
mixed its files with the npm package's. The local folder is copied.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
nitedani-agent and others added 15 commits October 29, 2026 08:11
…d version of an npm package"

Copying only the local folder dropped files the npm package's importers
need, which works on main.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d version under PnP

The fork gets a version of its own, so the existing multi-version layout
links each importer to its copy. The app's own import takes the top level.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…by that name under PnP

An app importing both the fork and the npm package (under an alias) got
the npm package for both.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… is known

The name a folder got depended on which import nft resolved first, so a
fork imported as ms and ms2 sometimes lost its ms name to the npm package.
It now takes its package.json name when imported by it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mports under PnP

A local fork at an older version than the npm package lost the top level
to it, so the app's own import got the npm package.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r PnP

relocate.ts's fallback resolved every import with require conditions, so a
renamed portal: package's require build got bundled into ESM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ckages included

The app's npm ms lost the top level to a newer local fork another package
imports.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…has the same one

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… under

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… only sets "type"

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…its name

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nder its name

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nitedani

Copy link
Copy Markdown
Owner

run refactor then merge

@nitedani-agent

Copy link
Copy Markdown
Contributor Author

Claude Claude Opus 5.5 (high effort) wrote this.

I'm refactoring the change now and will post what it changes here by 2026-10-29T19:00Z.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nitedani-agent

Copy link
Copy Markdown
Contributor Author

ClaudeCodex Claude Opus 5.5 (high effort) wrote this; GPT-6.1-Sol (Codex, medium effort) reviewed it.

Done in 6085568: pnpResolve now handles an npm package in one early-return branch, with the same behavior. Tests pass on Node 20 and 26.

This account has read access only, so the merge is yours. CI waits for your approval of the fork's workflow run. I'd approve it and squash-merge once it's green.

@nitedani
nitedani merged commit 121f88a into nitedani:main Oct 30, 2026
1 check passed
nitedani-agent added a commit to nitedani-agent/standaloner that referenced this pull request Oct 30, 2026
…pies

PnP's local-fork version suffix now applies before assignCopies, so a fork and the npm
package of its name and version never share a copy; the PnP app import and npm: alias
links use output copies instead of versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support PnP in trace

3 participants