Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions packages/cli/src/utils/installationInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,28 @@ export function getNpmCliPath(
// instead of throwing synchronously — getNpmCliPath is called from a
// non-async site (handleAutoUpdate), and a returned best-effort path lets the
// downstream spawn surface any failure through its 'error' handler.
//
// Some Node version managers (mise, asdf, proto) replace bin/npm with a
// non-JS wrapper script (e.g. a bash shim). fs.realpathSync succeeds on
// these (the file exists), but spawning `node /path/to/bash-wrapper` fails
// with a SyntaxError. Validate the resolved path ends with ".js" before
// returning it; otherwise fall back to the conventional path.
const adjacentNpm = path.join(path.dirname(nodePath), 'npm');
try {
return fs.realpathSync(adjacentNpm);
const resolved = fs.realpathSync(adjacentNpm);
if (resolved.endsWith('.js')) return resolved;
Comment on lines +52 to +53

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] getNpmCliPath has zero direct unit tests — the .endsWith('.js') guard (the core fix) is untested for both its success and rejection branches. The linked issue #7543's triage explicitly requested a test for this case.

Failure scenario: A future refactor simplifies the function back to return fs.realpathSync(adjacentNpm) (the pre-PR one-liner), or tightens the extension check. With no test asserting the non-.js fallback, the mise/asdf/proto SyntaxError regresses silently.

describe('getNpmCliPath', () => {
  it('returns resolved path when it ends with .js', () => { /* ... */ });
  it('falls back to conventional path for non-.js shim', () => { /* ... */ });
  it('falls back when realpathSync throws', () => { /* ... */ });
});

— qwen3.7-max via Qwen Code /review

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion] When the .endsWith('.js') guard rejects a resolved path (non-JS shim from mise/asdf/proto), the function silently falls through to the conventional path with no diagnostic output. A user encountering an auto-update failure would see a generic downstream spawn error with no indication that npm path resolution fell through.

The module already has a debugLogger at line 68 — adding a debug log here would aid diagnosis:

if (resolved.endsWith('.js')) return resolved;
debugLogger(`Resolved npm path does not end with .js, falling back: ${resolved}`);

— qwen3.7-max via Qwen Code /review

} catch {
return path.join(
path.dirname(nodePath),
'..',
'lib',
'node_modules',
'npm',
'bin',
'npm-cli.js',
);
// Fall through to the conventional path below.
}
return path.join(
path.dirname(nodePath),
'..',
'lib',
'node_modules',
'npm',
'bin',
'npm-cli.js',
);
}

const debugLogger = createDebugLogger('INSTALLATION_INFO');
Expand Down