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

Aperta
#2,286 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
53/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
node.js, rust
Ambito
backend, cli

Direzione di ricerca

Start with get_npm_global_prefix and get_global_node_modules_paths in crates/socket-patch-core/src/crawlers/npm_crawler.rs, comparing the Bun probe and warning behavior nearby. Then trace patched_in_other_scope in crates/socket-patch-cli/src/commands/rollback.rs and read the rollback contract in CLI_CONTRACT.md. Run the proposed injected CommandRunner cases and rollback CLI test; done means failed npm probes warn during global scans and project rollback preserves records and blobs when the global copy cannot be checked.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

agent:triaged arch-audit bug priority:p1

[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 2044d9ee (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 [email protected] holds its original files, a manifest record and blobs for a patch of it, and a global node_modules whose [email protected] 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/[email protected] (already original)
Warning: pkg:npm/[email protected]: 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/[email protected] -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/[email protected]
exit=0  global index.js: module.exports = 1

=== npm fails
Rolled back 1 package:
  pkg:npm/[email protected] (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

Lingua principale
Rust
Stelle
9
Fork
2
Merge medio
1g 8h
PR unite (30g)
313

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di SocketDev/socket-patch

Tutte le issue di SocketDev/socket-patch

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.