fix(installer): load nvm before Node.js version check#49673
fix(installer): load nvm before Node.js version check#49673Chenglin97 wants to merge 1 commit intoopenclaw:mainfrom
Conversation
On systems where nvm manages Node.js, the installer was detecting the system-installed Node (e.g. /usr/bin/node v8) because /usr/bin appears before ~/.nvm/versions/node/.../bin on PATH in non-interactive shells. This caused check_node() to fail on the stale system binary and trigger install_node(), which overwrote the user's nvm environment unnecessarily. Fix: add try_load_nvm() that sources ~/.nvm/nvm.sh (with --no-use to avoid side effects) and activates the nvm default alias before check_node runs. If nvm is not installed, try_load_nvm() is a no-op. Fixes openclaw#49556
Greptile SummaryThis PR adds a Key changes:
Minor concern:
Confidence Score: 4/5
Prompt To Fix All With AIThis is a comment left during a code review.
Path: scripts/install.sh
Line: 1401
Comment:
**Inaccurate "already loaded" no-op comment**
The comment states the function is "a no-op if nvm is not installed or **already loaded**," but this isn't true. When nvm is already available in the shell (`command -v nvm` succeeds before this function runs), the guard `[[ -s "${nvm_dir}/nvm.sh" ]]` is still true, so the function will:
1. Re-source `nvm.sh` with `--no-use` (generally idempotent, so harmless)
2. Call `nvm use default --silent`, potentially switching the active Node version
The behavior itself is intentional and fine — you *do* want to ensure the default nvm Node is active — but the comment is misleading. Either update the comment to reflect what actually happens, or add an early-return guard to make the "already loaded" case a true no-op:
```suggestion
# Source nvm before checking Node.js so the installer uses the nvm-managed
# version rather than a system-installed Node that may be too old.
# This is a no-op if nvm is not installed. If nvm is already loaded,
# it ensures the default alias is active.
```
How can I resolve this? If you propose a fix, please make it concise.Last reviewed commit: "fix(installer): load..." |
| try_load_nvm() { | ||
| # Source nvm before checking Node.js so the installer uses the nvm-managed | ||
| # version rather than a system-installed Node that may be too old. | ||
| # This is a no-op if nvm is not installed or already loaded. |
There was a problem hiding this comment.
Inaccurate "already loaded" no-op comment
The comment states the function is "a no-op if nvm is not installed or already loaded," but this isn't true. When nvm is already available in the shell (command -v nvm succeeds before this function runs), the guard [[ -s "${nvm_dir}/nvm.sh" ]] is still true, so the function will:
- Re-source
nvm.shwith--no-use(generally idempotent, so harmless) - Call
nvm use default --silent, potentially switching the active Node version
The behavior itself is intentional and fine — you do want to ensure the default nvm Node is active — but the comment is misleading. Either update the comment to reflect what actually happens, or add an early-return guard to make the "already loaded" case a true no-op:
| # This is a no-op if nvm is not installed or already loaded. | |
| # Source nvm before checking Node.js so the installer uses the nvm-managed | |
| # version rather than a system-installed Node that may be too old. | |
| # This is a no-op if nvm is not installed. If nvm is already loaded, | |
| # it ensures the default alias is active. |
Prompt To Fix With AI
This is a comment left during a code review.
Path: scripts/install.sh
Line: 1401
Comment:
**Inaccurate "already loaded" no-op comment**
The comment states the function is "a no-op if nvm is not installed or **already loaded**," but this isn't true. When nvm is already available in the shell (`command -v nvm` succeeds before this function runs), the guard `[[ -s "${nvm_dir}/nvm.sh" ]]` is still true, so the function will:
1. Re-source `nvm.sh` with `--no-use` (generally idempotent, so harmless)
2. Call `nvm use default --silent`, potentially switching the active Node version
The behavior itself is intentional and fine — you *do* want to ensure the default nvm Node is active — but the comment is misleading. Either update the comment to reflect what actually happens, or add an early-return guard to make the "already loaded" case a true no-op:
```suggestion
# Source nvm before checking Node.js so the installer uses the nvm-managed
# version rather than a system-installed Node that may be too old.
# This is a no-op if nvm is not installed. If nvm is already loaded,
# it ensures the default alias is active.
```
How can I resolve this? If you propose a fix, please make it concise.There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f2c58ff091
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| . "${nvm_dir}/nvm.sh" --no-use | ||
| # Activate the default/current nvm Node version if one is set | ||
| if command -v nvm >/dev/null 2>&1; then | ||
| nvm use default --silent 2>/dev/null || nvm use node --silent 2>/dev/null || true |
There was a problem hiding this comment.
Avoid activating nvm's default Node before Linux installs
If a Linux user has nvm installed but their default/node alias still points to Node 20 or older, this now prepends that stale nvm binary to PATH before check_node() runs. install_node() then installs a compliant system Node.js, but the Linux branch never rewrites PATH, so ensure_default_node_active_shell() still sees the old nvm binary and aborts the installer. Before this change, the same machine would succeed by picking up the newly installed system Node instead of the old nvm default.
Useful? React with 👍 / 👎.
Problem
On systems where nvm manages Node.js, the installer runs in a non-interactive shell (e.g.
curl ... | bash) where nvm is not loaded. The system-installed Node (e.g./usr/bin/nodev8) appeared earlier on PATH, causingcheck_node()to fail on the stale binary and triggerinstall_node()— overwriting the user's nvm environment unnecessarily.Fix
Add a
try_load_nvm()helper that sources${NVM_DIR:-$HOME/.nvm}/nvm.shwith--no-usebeforecheck_noderuns, then activates the nvm default alias. If nvm is not installed, it is a no-op.Testing
/usr/bin/node: before fix → installs Node again; after fix → detects v24 correctly and skips installtry_load_nvmis a no-op, no behavior changeFixes #49556