prune-lockfile (npm workspaces) returns a broken or unpruned lockfile and still succeeds
Current behavior
With npm workspaces, @nx/js:prune-lockfile either writes a lockfile that npm ci rejects, or quietly hands back the root lockfile. The target passes either way.
Three triggers, one app each in the repro below:
nested: its[email protected]sits underapps/nested/node_modulesbecause another workspace owns the root slot, andagent-base@6in the same app needs the rootdebug@4. The output keeps 4.4.3 and drops 2.6.9. In a bigger repo the same setup throwsFollowing packages could not be mapped to the NPM lockfileinstead, and you get the root lockfile back.pinned: pins[email protected], which the rootoverridesrewrite to 2.1.3. Prune can't find 2.1.2 and falls back. It also copies the app's ownoverrides, which npm ignores inside a workspace, and drops the root ones npm actually applied.aliased:"ms-alias": "npm:ms"falls back the same way.
Looking at the source, the pruned graph keeps edges from every workspace project, nestMappedPackages never counts a project node as placed, and rehoistNodes only promotes a nested version when the hoisted one is missing from the pruned graph.
I patched those two locally and hit one more: placement ignores shadowing. path-scurry landed under rimraf, next to rimraf/node_modules/[email protected], and resolved that instead of 7.1.3. On our ~5k-entry lockfile, some apps had up to 17 edges like that.
Expected behavior
npm ci accepts the pruned lockfile, and every package resolves the version it does in the workspace. If Nx can't produce that, the target should fail. Returning the root lockfile with exit 0 means the documented npm --omit=dev -f install re-resolves the tree. For us that would drop the overrides we rely on for CVE pins.
Repro
mkdir -p repro/apps/{nested,other,pinned,aliased} && cd repro
echo '{"private":true,"workspaces":["apps/*"],"devDependencies":{"nx":"23.1.1","@nx/js":"23.1.1"},"overrides":{"ms":"2.1.3"}}' > package.json
echo '{"plugins":[]}' > nx.json
app() { echo "{\"name\":\"$1\",\"version\":\"0.0.1\",\"dependencies\":$2$3,\"nx\":{\"targets\":{\"build\":{\"command\":\"mkdir -p apps/$1/dist\",\"outputs\":[\"{projectRoot}/dist\"]},\"prune-lockfile\":{\"dependsOn\":[\"build\"],\"executor\":\"@nx/js:prune-lockfile\",\"options\":{\"buildTarget\":\"build\"}}}}}" > apps/$1/package.json; }
app nested '{"debug":"2.6.9","agent-base":"6.0.2"}'
app other '{"debug":"4.3.4"}'
app pinned '{"ms":"2.1.2"}' ',"overrides":{"debug":"4.3.4"}'
app aliased '{"ms-alias":"npm:ms"}'
npm install --no-audit --no-fund
export NX_DAEMON=false
for a in nested pinned aliased; do
npx nx run $a:prune-lockfile --skip-nx-cache
(cd apps/$a/dist && npm ci --dry-run --ignore-scripts)
donenested passes, then npm ci fails with lock file's [email protected] does not satisfy [email protected]. pinned logs not found in the root lock file: [email protected], and aliased logs ms-alias@npm:ms. Both of those return all 317 root entries.
Environment
nx 23.1.1, @nx/js 23.1.1, npm 11.8.0, Node 24.13.1, macOS arm64.
Probably the same thing as #28832, which was closed as stale.
Source: nrwl/nx