Skip to content

A failed npm root -g probe reads as "no global install": scan -g reports nothing to scan, and rollback drops the record of a copy that is still patched #2286

Backlog

Description

[agent] Filed by the architecture refactor routine (lead L33, run on main at cfc870f).

Kind: bug, an error that lies. A package manager's global-directory probe that fails is read as "this manager has no global install". Bun's probe was taught the difference in #1009 (for #443); npm's was not.

Problem

The npm crawler asks each manager where its global packages are (npm root -g, pnpm root -g, yarn global dir, bun pm ls -g). For npm, pnpm and yarn a probe that gives no answer is dropped without a word ([npm_crawler.rs L3401-L3426](https://github.com/SocketDev/socket-patch/blob/cfc870f55f5910e48e331ae460585ac2dea878a4/crates/socket-patch-core/src/crawlers/npm_crawler.rs#L3401-L3426)): get_npm_global_prefix returns Err, and the caller's if let Ok(..) skips it. "No answer" covers a manager that is not installed, one that exits non-zero, and one that is still running at the 10 s probe budget. On Linux there is no other discovery path for npm's directory.

Bun's probe already tells these apart. resolve_bun_global_prefix returns Ok(None) for "Bun is not in use" and Err for "Bun is in use but its global dir can't be told", "so a global scan can say so instead of reporting a clean, empty result" ([L2500-L2535](https://github.com/SocketDev/socket-patch/blob/cfc870f55f5910e48e331ae460585ac2dea878a4/crates/socket-patch-core/src/crawlers/npm_crawler.rs#L2500-L2535)), and the caller prints Warning: could not determine Bun's global package directory (…), so Bun's global packages were not scanned. That came with 2044d9e (git log -S'fn resolve_bun_global_prefix').

Two things follow for npm.

1. A global scan reports a clean, empty result. scan -g prints No global packages found. and exits 0, with status: success and scannedPackages: 0 under --json and no warning.

2. A project rollback drops the record of a copy that is still patched. The contract says that before a rollback drops a record it checks the other scope, and that "a copy there that still holds the patch's afterHash bytes, or has a patched file that exists but cannot be hashed, keeps the record (with its variant group) and its blobs" (CLI_CONTRACT.md L2494). patched_in_other_scope (rollback.rs L341-L390) crawls the default global install for that. When the probe fails, the crawl finds no copy, so the record leaves the manifest and its blobs are freed. The run says nothing, and the rollback -g the user would run next finds no record.

apply -g and rollback -g are honest over the same failure: apply -g exits 1 (package_not_installed), and rollback -g warns that "a copy this run did not find stays patched until the package is reinstalled".

Evidence

The binary built from main. A project whose left-pad@1.3.0 holds its original files, a manifest record and blobs for a patch of it, and a global node_modules whose left-pad@1.3.0 holds the patched files. PATH holds one npm:

  • answers: a script that prints the global node_modules for root -g;
  • fails: echo "npm ERR! code EACCES" >&2; exit 1;
  • hangs: exec sleep 600.

socket-patch rollback --offline --yes in the project, then socket-patch rollback -g --offline --yes:

=== npm answers
Rolled back 1 package:
  pkg:npm/left-pad@1.3.0 (already original)
Warning: pkg:npm/left-pad@1.3.0: its copy in the global install (…/os-global/lib/node_modules/left-pad) is still patched or could not be verified, so its manifest record was kept; run `socket-patch rollback pkg:npm/left-pad@1.3.0 -g --cwd …/os-proj` to restore it
exit=0 (0s)
manifest records: 1  blobs left: 4  global index.js: module.exports = 2
then: rollback -g
Rolled back 1 package:
  pkg:npm/left-pad@1.3.0
exit=0  global index.js: module.exports = 1

=== npm fails
Rolled back 1 package:
  pkg:npm/left-pad@1.3.0 (already original)

Removed 1 patch from manifest.
Freed 66 B of unused blobs and archives
exit=0 (0s)
manifest records: 0  blobs left: 0  global index.js: module.exports = 2
then: rollback -g
No patches in this project; nothing to roll back.
exit=0  global index.js: module.exports = 2

=== npm hangs
(the same output as "npm fails")
exit=0 (10s)
manifest records: 0  blobs left: 0  global index.js: module.exports = 2

Under --json the first run carries warnings: ["patched_in_other_scope"], removed: 0. The other two carry no warning, removed: 1, bytesFreed: 66, and nothing on stderr.

A global scan on this machine, against a local stand-in for the patch API that offers no patch (--proxy-url, left out below with the token notice), with its real npm (199 global packages) and then with a failing npm, pnpm and yarn first on PATH:

$ socket-patch scan -g --ecosystems npm --mode agent --dry-run
Found 199 packages (199 npm)
No patches available for installed packages.
$ PATH=<failing npm, pnpm, yarn>:$PATH socket-patch scan -g --ecosystems npm --mode agent --dry-run
No global packages found.                                   (exit 0)

scan -g --ecosystems gem gives the same pair with a failing gem and ruby (17 packages, then No global packages found.). I did not trace the gem crawler's probe.

Not checked: how often npm root -g fails or takes over 10 s on a machine whose global install is otherwise in use. The cases run here are a script that exits 1 and one that never returns.

Planned fix

  1. Give npm's probe Bun's shape: Ok(None) when npm is not on PATH, Err(why) when it is and npm root -g gave no usable answer. get_global_node_modules_paths then warns once, as it does for Bun, that npm's global packages were not scanned and names --global-prefix.
  2. In patched_in_other_scope, when the other scope is the global install and npm's directory could not be determined, keep the npm records it was asked about, and say so. That is the rule the function already follows for a file it cannot hash: "keep the record rather than strand it".
  3. Tests: a unit test over an injected CommandRunner for the three answers, and a CLI test that runs the rollback above with a failing npm on PATH and expects the record and blobs to stay.

Two things to settle in the PR:

  • pnpm and yarn. yarn global dir fails for every Yarn Berry install, which has no global command, so a warning on any failed yarn probe would be noise. The first slice is npm alone; pnpm and yarn need their benign failures told from real ones first.
  • The --json code for step 2. patched_in_other_scope names a copy's path, and here no path is known. Reusing it with a different detail, or adding a code, is an addition to warnings[] that the contract does not name yet.

Generated by Claude Code

Activity

  1. added
    bug
    arch-audit
    on Nov 26, 2026
  2. added a commit that references this issue on Nov 26, 2026
  3. mikolalysenko commented on Nov 27, 2026

    @mikolalysenko
    CollaboratorAuthor

    [agent] One real failure state for the pnpm leg, run on main at cfc870f with pnpm 10.28.0. It bears on the open point above (telling pnpm's real failures from benign ones).

    pnpm root -g exits 1 whenever PNPM_HOME is set and that directory is not on PATH. pnpm setup writes both lines to the shell's rc file; a CI step or a Dockerfile that sets PNPM_HOME without the PATH entry has one without the other.

    $ PNPM_HOME=<dir> pnpm root -g                      # <dir> is not on PATH
     ERROR  The configured global bin directory "<dir>" is not in PATH
    (exit 1)
    $ PNPM_HOME=<dir> socket-patch scan --mode agent -g --ecosystems npm
    Found 201 packages (201 npm)
    Applied 1 patch.
    Patched 1 package, fixing 1 vulnerability (1 high).
    (exit 0)
    $ grep -c SOCKET_PATCHED <each global copy of js-tokens 4.0.0>
    0 <dir>/global/5/.pnpm/js-tokens@4.0.0/node_modules/js-tokens/index.js
    1 <Bun's global dir>/node_modules/js-tokens/index.js
    1 <Yarn's global dir>/node_modules/js-tokens/index.js
    

    The scan says nothing about pnpm, and apply --check -g then reports the patch in sync. With <dir> on PATH the same scan patches pnpm's copy too, and that copy is the one Node resolves for the global loose-envify. The three global installs were made with the real tools (pnpm add -g, bun add -g, yarn global add loose-envify@1.4.0) against a stand-in patch API with one patch for js-tokens 4.0.0.

    pnpm's message is on stderr and the exit status is 1, so this failure can be told from a pnpm that is absent.


    Generated by Claude Code

  4. added a commit that references this issue on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedarch-auditbugpriority:p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions