Repository navigation
Conversation
`npm exec` / `npx` required `@npmcli/arborist` and `pacote` at the top of libnpmexec, so running a bin that is already in `node_modules/.bin` loaded about 420 modules (fetchers, make-fetch-happen, sigstore, tuf-js, cacache, tar, ...) that this path never uses. - require arborist and pacote on first use - read the local package.json for `hasPkgBin` with `@npmcli/package-json` directly, which is what pacote's DirFetcher does for a directory spec - with `--call` and no `--package`, run the command without reading the local tree, whose result was never used `npx <local bin>` and `npx -c <cmd>` now load 140 modules instead of 560 before spawning the command. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
… check `npx foo@1.2.3` with foo@1.2.3 already in the npx cache still loads the local tree with Arborist, revalidates the packument with the registry (getManifest uses preferOnline) and loads the npx cache tree, only to run the same bin from the same directory. When every package is a registry spec for an exact version, that version is the one installed in its npx cache directory, and the package is not in the local project (or, for `npx <pkg>`, in the global prefix), run it from there directly. The check reads package.json files and holds the npx cache lock while it does, so it does not race an install into the same directory. Ranges, tags, aliases, git and directory specs, `--prefer-online` and a `before`/`min-release-age` cutoff still take the full lookup. `npm init <initializer>` also stops loading init-package-json and update-workspaces (and through it Arborist) when it only runs `npm exec`. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
ychampion
left a comment
There was a problem hiding this comment.
I reproduced a workspace regression on 9c9c627 with a warm npx cache and npm install --install-strategy=nested. The workspace has the exact requested CLI version and its optional peer installed under packages/app/node_modules.
On 787f152 and the lazy-load parent 834a00f, both npm exec -w app -- tool@1.0.0 and npm exec -w app --package=tool@1.0.0 -- tool run the workspace copy and load the peer. Running those forms from inside the workspace gives the same result. On this head, all four executions start the same version from _npx and the peer is missing. An ordinary direct project install still works.
findInNpxCache checks the root prefix's node_modules. The cached bin directory then takes precedence over the workspace's bin directory. I tried skipping the fast path when resolve(runPath) !== resolve(path). That restored the workspace binary and peer in all four cases, while the standalone warm-cache case still made no registry requests and loaded 141 modules. A regression test can assert the binary's actual path or the presence of the workspace peer.
The npx cache fast path only looked for the package in the project root's node_modules. With `npm exec -w <ws>`, or from inside a workspace, the full lookup also finds the copy in the workspace's node_modules and runs it from the workspace's bin directory, where its peers resolve. The fast path ran the npx cache copy instead. Look in node_modules of every directory from runPath up to the project root, the same directories whose bins come first on the PATH, and take the full lookup if any of them has the package. Reported by @ychampion.
|
Thanks for catching this @ychampion. Fixed in 79458ec. I didn't go with skipping whenever runPath differs, since that would turn it off for any run from a subfolder. It now looks for the package in node_modules from runPath up to the project root, and takes the normal path if it finds it there. Your four cases run the workspace copy with the peer again, and there's a test for it now. Standalone warm cache is still 0 requests and 141 modules. One thing I left as is. From the root, if only a workspace has the package, plain npm finds it but can't run it because it's not on the PATH, and this branch runs the cached one instead. Can change that if you think it should match. |
Fixes #10324
Refs #7295
Depends on #10096, which this branch includes as its first commit. Only the second commit is new here.
npx create-vite@9.2.1 my-appwith create-vite 9.2.1 already in the npx cache still loads the local tree with Arborist, fetches the create-vite packument (getManifestpassespreferOnline: true), loads the npx cache tree, and only then runs the bin it already had.That's 565 modules and one registry round trip on npm 12.2.3.
npm create vite@9.2.1does the same, and also loads Arborist at start-up throughupdate-workspacesinlib/commands/init.js.When every package is a registry spec for an exact version, the answer can't change.
So before any of that,
execnow checks the npx cache directory for those packages.If each one is installed there at exactly that version, and none of them is in the local project or, for
npx <pkg>, in the global prefix, it runs the bin from there.It reads a few
package.jsonfiles with@npmcli/package-json, the way Arborist does, and holds the npx cache lock while it reads, so it doesn't see a half-finished install from another npx.Everything else takes the full lookup as before
node_modules, or installed globally (only checked fornpx <pkg>, as before)--prefer-online, which asks for the registry checkbeforeormin-release-age, which need the packument's publish timesnpm init <initializer>now requiresinit-package-jsonandupdate-workspaceswhere they're used, as the other commands already do for Arborist.With #10096, npx and npm create load about 140 modules instead of 565 and 581, and make no registry request.
With a warm cache
npx create-vite@9.2.1gets about 1.85x faster on Windows and 2.7x on Linux, andnpm create vite@9.2.1about 1.75x on both.--prefer-offline, Node.js 26.7.0npx create-vite@9.2.1 my-app --template react, first version of this change on npm 11.19.0, two runsnpm create vite@9.2.1 my-app -- --template react, npm 11.19.0, two runsRatios are unpatched over patched, medians of 10 interleaved paired runs each with 95% intervals, on a busy shared machine, so the ratios mean more than the milliseconds.
Without
--prefer-offlinethe unpatched run also waits for the registry (1.2 s here), and the patched run doesn't.#10096 on its own doesn't change this case (1.03x, 0.89-1.10), because the full lookup loads Arborist anyway.
One trade-off, stated plainly.
A cached exact version now runs without asking the registry whether it still exists.
If it gets unpublished or blocked later, it keeps running from the npx cache until the cache is cleared, the same as a copy in
node_moduleswould.#10295 makes the same trade for the packument.
--prefer-onlinekeeps the old behaviour.The docs for
npm execsay this in "A note on caching".Tests are in
workspaces/libnpmexec/test/npx-cache-hit.js.They cover the hit (no registry request and no Arborist, with and without
--package), and that--prefer-online,before, a version that isn't cached,@^1.0.0,@latest, a cache entry with a different version, a copy in the project and a global copy all still take the full lookup.The hit tests fail without the change, and the project and global ones fail if the fast path skips those checks.
Coverage stays at 100%.
Related