Fix remote Node.js detection for nvm, mise, asdf, and volta (#6037)
* Fix remote Node.js detection for nvm, mise, asdf, and volta Remote Node resolution failed when node was installed via a version manager (nvm with custom NVM_DIR, mise, asdf, volta) or when the user's login shell was zsh/fish rather than bash. Root cause: the SSH exec transport runs every command under /bin/sh, which never sources shell init files. The only init-aware path was a hardcoded `bash -lc` fallback that missed zsh/fish users and was never reached for the newer version managers. nvm was handled by guessing ~/.nvm (breaking custom NVM_DIR), and mise/asdf/volta had no probes at all. There was also no version gate, so nvm's highest-version glob could return Node 8/10/12 and crash the relay on launch. Fix: resolve via the user's own $SHELL as a login shell first (the only path that runs nvm.sh / mise activate / asdf.sh init hooks), then fall back to direct path probes for all major managers (nvm respecting $NVM_DIR, fnm, mise, asdf, volta, n) plus system locations. Every candidate is version-checked against the relay's Node 18+ requirement before being accepted. * Address CodeRabbit review: probes-first, no || short-circuit - Reorder to path-probes first (deterministic, doesn't depend on shell rc-file semantics where bash -lc skips .bashrc and zsh -lc skips .zshrc — exactly where nvm/mise/asdf hooks live). - Join probes with newlines instead of || so an empty `ls | sort -V | tail -1` (exit 0) doesn't mask later probes. - Deduplicate candidate paths before version-checking. - Drop unreachable mock and fix misleading $SHELL-unset test name. - Login shell is now a fallback for custom ~/.profile PATH setups. * Fix remote Node path probing portability Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com> Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
co-authored by
Orca
Jinwoo-H
parent
a0d9505ba5
commit
44935d4ccb
@@ -1,12 +1,20 @@
|
||||
import type { SshConnection } from './ssh-connection'
|
||||
import { execCommand } from './ssh-relay-deploy-helpers'
|
||||
import { shellEscape } from './ssh-connection-utils'
|
||||
import type { RemoteHostPlatform } from './ssh-remote-platform'
|
||||
import { isWindowsRemoteHost, normalizeWindowsRemotePath } from './ssh-remote-platform'
|
||||
import { powerShellCommand } from './ssh-remote-powershell'
|
||||
import { execCommand } from './ssh-relay-deploy-helpers'
|
||||
|
||||
// Why: the relay requires Node.js 18+. Version managers like nvm keep every
|
||||
// installed version on disk, so a naive "highest version" glob can hand back
|
||||
// Node 8/10/12 and crash the relay on launch. Gate every candidate on this.
|
||||
const MIN_NODE_MAJOR = 18
|
||||
|
||||
// Why: the login-shell fallback catches custom PATH setups in ~/.profile that
|
||||
// the path probes don't cover. Interactive configs (conda prompts, etc.) can
|
||||
// hang a login shell, so keep this short.
|
||||
const LOGIN_SHELL_PROBE_TIMEOUT_MS = 8_000
|
||||
|
||||
// Why: non-login SSH shells (the default for `exec`) don't source
|
||||
// .bashrc/.zshrc, so node installed via nvm/fnm/Homebrew isn't in PATH.
|
||||
// We try common locations and fall back to a login-shell `which`.
|
||||
export async function resolveRemoteNodePath(
|
||||
conn: SshConnection,
|
||||
host?: RemoteHostPlatform
|
||||
@@ -15,48 +23,163 @@ export async function resolveRemoteNodePath(
|
||||
return resolveRemoteWindowsNodePath(conn)
|
||||
}
|
||||
|
||||
const script = [
|
||||
'command -v node 2>/dev/null',
|
||||
'command -v /usr/local/bin/node 2>/dev/null',
|
||||
'command -v /opt/homebrew/bin/node 2>/dev/null',
|
||||
// Why: nvm installs into a versioned directory. `ls -1` sorts
|
||||
// alphabetically, which misorders versions (e.g. v9 > v18). Pipe
|
||||
// through `sort -V` (version sort) so we pick the highest version.
|
||||
'ls -1 $HOME/.nvm/versions/node/*/bin/node 2>/dev/null | sort -V | tail -1',
|
||||
'command -v $HOME/.local/bin/node 2>/dev/null',
|
||||
'command -v $HOME/.fnm/aliases/default/bin/node 2>/dev/null'
|
||||
].join(' || ')
|
||||
|
||||
try {
|
||||
const result = await execCommand(conn, script)
|
||||
const nodePath = result.trim().split('\n')[0]
|
||||
if (nodePath) {
|
||||
console.log(`[ssh-relay] Found node at: ${nodePath}`)
|
||||
return nodePath
|
||||
}
|
||||
} catch {
|
||||
// Fall through to login shell attempt
|
||||
// Strategy 1: probe well-known install directories for every common Node
|
||||
// version manager (nvm, fnm, mise, asdf, volta, n) plus system locations.
|
||||
// This doesn't depend on shell startup-file semantics — bash -lc skips
|
||||
// .bashrc and zsh -lc skips .zshrc, but those are exactly the files where
|
||||
// nvm/mise/asdf hooks live. Probing directories directly is deterministic.
|
||||
const probedPath = await tryResolveViaKnownPaths(conn)
|
||||
if (probedPath) {
|
||||
return probedPath
|
||||
}
|
||||
|
||||
// Why: last resort — source the full login profile. This is separated into
|
||||
// its own exec because `bash -lc` can hang on remotes with interactive
|
||||
// shell configs (conda prompts, etc.). If this times out, the error message
|
||||
// from execCommand will tell us it was the login shell attempt.
|
||||
try {
|
||||
console.log('[ssh-relay] Trying login shell to find node...')
|
||||
const result = await execCommand(conn, "bash -lc 'command -v node' 2>/dev/null")
|
||||
const nodePath = result.trim().split('\n')[0]
|
||||
if (nodePath) {
|
||||
console.log(`[ssh-relay] Found node via login shell: ${nodePath}`)
|
||||
return nodePath
|
||||
}
|
||||
} catch {
|
||||
// Fall through
|
||||
// Strategy 2 (fallback): ask the user's login shell. Catches custom PATH
|
||||
// setups in ~/.profile / ~/.bash_profile that the probes don't cover.
|
||||
const loginShellPath = await tryResolveViaLoginShell(conn)
|
||||
if (loginShellPath) {
|
||||
return loginShellPath
|
||||
}
|
||||
|
||||
throwNodeNotFound()
|
||||
}
|
||||
|
||||
// Probe the on-disk install directories of every common Node version manager
|
||||
// plus system package-manager locations. Every probe runs unconditionally so
|
||||
// a missing directory prints nothing rather than short-circuiting later
|
||||
// probes. Returns the first candidate that meets the minimum version.
|
||||
async function tryResolveViaKnownPaths(conn: SshConnection): Promise<string | null> {
|
||||
const script = `
|
||||
command -v node 2>/dev/null
|
||||
nvm_dirs=\${NVM_DIR:-"$HOME/.nvm"}
|
||||
for nvm_file in "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.bashrc" "$HOME/.zprofile" "$HOME/.zshrc"
|
||||
do
|
||||
[ -r "$nvm_file" ] || continue
|
||||
nvm_dir_from_file=$(sed -n 's/^[[:space:]]*export[[:space:]][[:space:]]*NVM_DIR[[:space:]]*=[[:space:]]*//p; s/^[[:space:]]*NVM_DIR[[:space:]]*=[[:space:]]*//p' "$nvm_file" | tail -n 1)
|
||||
case "$nvm_dir_from_file" in
|
||||
\\"*\\") nvm_dir_from_file=\${nvm_dir_from_file#\\"}; nvm_dir_from_file=\${nvm_dir_from_file%%\\"*} ;;
|
||||
\\'*\\') nvm_dir_from_file=\${nvm_dir_from_file#\\'}; nvm_dir_from_file=\${nvm_dir_from_file%%\\'*} ;;
|
||||
*) nvm_dir_from_file=\${nvm_dir_from_file%%[[:space:]]*} ;;
|
||||
esac
|
||||
case "$nvm_dir_from_file" in
|
||||
'$HOME'*) nvm_dir_from_file="$HOME\${nvm_dir_from_file#'$HOME'}" ;;
|
||||
"~/"*) nvm_dir_from_file="$HOME/\${nvm_dir_from_file#\\~/}" ;;
|
||||
esac
|
||||
[ -n "$nvm_dir_from_file" ] && nvm_dirs="$nvm_dirs
|
||||
$nvm_dir_from_file"
|
||||
done
|
||||
printf '%s\\n' "$nvm_dirs" | while IFS= read -r nvm_dir
|
||||
do
|
||||
[ -n "$nvm_dir" ] || continue
|
||||
for candidate in "$nvm_dir"/versions/node/*/bin/node
|
||||
do
|
||||
[ -x "$candidate" ] && printf '%s\\n' "$candidate"
|
||||
done
|
||||
done
|
||||
for candidate in \\
|
||||
/usr/local/bin/node \\
|
||||
/opt/homebrew/bin/node \\
|
||||
"$HOME/.local/bin/node" \\
|
||||
"$HOME/.fnm/aliases/default/bin/node" \\
|
||||
"$HOME/.fnm/node-versions"/*/installation/bin/node \\
|
||||
"$HOME/.local/share/mise/shims/node" \\
|
||||
"$HOME/.local/share/mise/installs/node"/*/bin/node \\
|
||||
"$HOME/.asdf/shims/node" \\
|
||||
"$HOME/.asdf/installs/nodejs"/*/bin/node \\
|
||||
"$HOME/.volta/bin/node" \\
|
||||
/usr/local/n/versions/node/*/bin/node
|
||||
do
|
||||
[ -x "$candidate" ] && printf '%s\\n' "$candidate"
|
||||
done
|
||||
true
|
||||
`
|
||||
|
||||
try {
|
||||
const result = await execCommand(conn, script)
|
||||
const seen = new Set<string>()
|
||||
for (const line of result.split('\n')) {
|
||||
const candidate = line.trim()
|
||||
if (!candidate || seen.has(candidate)) {
|
||||
continue
|
||||
}
|
||||
seen.add(candidate)
|
||||
if (await nodeMeetsVersionRequirement(conn, candidate)) {
|
||||
console.log(`[ssh-relay] Found node via path probe: ${candidate}`)
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Fall through to login shell.
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// Run `command -v node` under the user's login shell, then verify the result
|
||||
// meets the minimum version. Returns null on any failure (shell missing, no
|
||||
// node found, version too old, timeout) so callers fall through to the error.
|
||||
async function tryResolveViaLoginShell(conn: SshConnection): Promise<string | null> {
|
||||
try {
|
||||
// Why: $SHELL is the user's configured login shell (set by chsh / passwd).
|
||||
// Using it — rather than hardcoding bash — means zsh/fish users whose
|
||||
// custom PATH hooks live in profile files get coverage too. We fall back
|
||||
// to sh if $SHELL is unset (rare, e.g. restricted accounts).
|
||||
const shellResult = await execCommand(conn, 'echo "${SHELL:-/bin/sh}"', {
|
||||
timeoutMs: LOGIN_SHELL_PROBE_TIMEOUT_MS
|
||||
})
|
||||
const shell = shellResult.trim().split('\n')[0]
|
||||
if (!shell) {
|
||||
return null
|
||||
}
|
||||
|
||||
const nodePath = await execCommand(conn, buildCommandInShell(shell, 'command -v node'), {
|
||||
wrapCommand: false,
|
||||
timeoutMs: LOGIN_SHELL_PROBE_TIMEOUT_MS
|
||||
})
|
||||
const candidate = nodePath.trim().split('\n')[0]
|
||||
if (!candidate) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (await nodeMeetsVersionRequirement(conn, candidate)) {
|
||||
console.log(`[ssh-relay] Found node via login shell (${shell}): ${candidate}`)
|
||||
return candidate
|
||||
}
|
||||
} catch {
|
||||
// Fall through.
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function buildCommandInShell(shell: string, command: string): string {
|
||||
const shellName = shell.split('/').at(-1)
|
||||
// Why: dash and POSIX sh do not require `-l`; when $SHELL falls back to
|
||||
// /bin/sh, prefer a portable command over login-shell semantics.
|
||||
const mode = shellName === 'sh' || shellName === 'dash' ? '-c' : '-lc'
|
||||
return `${shellEscape(shell)} ${mode} ${shellEscape(command)}`
|
||||
}
|
||||
|
||||
// Returns true if `nodePath` runs and reports Node >= MIN_NODE_MAJOR.
|
||||
// Caches nothing — this runs at most a few times per resolution (one per
|
||||
// candidate), and the exec round-trip dominates.
|
||||
async function nodeMeetsVersionRequirement(
|
||||
conn: SshConnection,
|
||||
nodePath: string
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const versionOutput = await execCommand(conn, `${shellEscape(nodePath)} --version`, {
|
||||
wrapCommand: false
|
||||
})
|
||||
const match = versionOutput.trim().match(/^v?(\d+)/)
|
||||
if (!match) {
|
||||
return false
|
||||
}
|
||||
const major = Number.parseInt(match[1]!, 10)
|
||||
return major >= MIN_NODE_MAJOR
|
||||
} catch {
|
||||
// Binary missing or fails to run — not usable.
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveRemoteWindowsNodePath(conn: SshConnection): Promise<string> {
|
||||
const script = [
|
||||
'$paths = @()',
|
||||
|
||||
Reference in New Issue
Block a user