fix(ssh): probe npm via prepended PATH, not colocated with node (#9165) (#9255)

* fix(ssh): probe npm via prepended PATH, not colocated with node (#9165)

The remote Node/npm toolchain gate invoked npm by its absolute path
<nodeBinDir>/npm (POSIX) / npm.cmd (Windows, behind a Test-Path
colocation check). But deploy (commandWithNodePath) runs bare `npm`
with nodeBinDir merely prepended to PATH, so npm can resolve from
anywhere on PATH.

A host whose only resolvable node has npm elsewhere on PATH (e.g. node
symlinked into a dir without npm) deployed fine on v1.4.144, but after
upgrade the candidate is rejected with no fallback → SSH/relay
connection fails to establish.

Make the probe resolve npm exactly the way deploy does — bare
`npm --version` under the same prepended PATH — so it still confirms npm
is runnable (the #8450 concern) without requiring colocation. Windows
now prepends the backslash-form dir (matching deploy) so bare-command
PATH lookup resolves reliably.

* test(ssh): cover split Node npm PATH resolution
This commit is contained in:
Brennan Benson
2026-07-17 19:29:43 -07:00
parent 212b79a03a
commit 5346efcb7f
4 changed files with 55 additions and 18 deletions
@@ -51,12 +51,46 @@ describe('resolveRemoteNodePath', () => {
'/home/u/.nvm/versions/node/v22.22.0/bin/node'
)
expect(execCommandMock.mock.calls[1]![1]).toContain("'/usr/bin/npm' --version")
expect(execCommandMock.mock.calls[1]![1]).toContain("PATH='/usr/bin':$PATH npm --version")
expect(execCommandMock.mock.calls[2]![1]).toContain(
"'/home/u/.nvm/versions/node/v22.22.0/bin/npm' --version"
"PATH='/home/u/.nvm/versions/node/v22.22.0/bin':$PATH npm --version"
)
})
it.runIf(process.platform !== 'win32')(
'accepts npm elsewhere on PATH without probing another Node candidate',
async () => {
const root = mkdtempSync(path.join(os.tmpdir(), 'orca-split-node-npm-'))
try {
const nodePath = path.join(root, 'selected node', 'bin', 'node')
const npmBinDir = path.join(root, 'npm elsewhere', 'bin')
mkdirSync(path.dirname(nodePath), { recursive: true })
mkdirSync(npmBinDir, { recursive: true })
writeFileSync(nodePath, '#!/bin/sh\nprintf "v22.22.0\\n"\n')
writeFileSync(path.join(npmBinDir, 'npm'), '#!/bin/sh\nprintf "11.13.0\\n"\n')
chmodSync(nodePath, 0o755)
chmodSync(path.join(npmBinDir, 'npm'), 0o755)
execCommandMock
.mockResolvedValueOnce(`${nodePath}\n${path.join(root, 'fallback', 'bin', 'node')}\n`)
.mockImplementationOnce((_conn: SshConnection, command: string) =>
Promise.resolve(
execFileSync('/bin/sh', ['-c', command], {
encoding: 'utf8',
env: { HOME: root, PATH: npmBinDir }
})
)
)
await expect(resolveRemoteNodePath(conn)).resolves.toBe(nodePath)
// One inventory exec plus one candidate probe keeps SSH startup work bounded.
expect(execCommandMock).toHaveBeenCalledTimes(2)
} finally {
rmSync(root, { recursive: true, force: true })
}
}
)
it('probes mise install directories', async () => {
execCommandMock
.mockResolvedValueOnce('/home/u/.local/share/mise/installs/node/20/bin/node\n')