Skip to content

fix: nvm lazy load で claude CLI が動作しない問題を修正 - #664

Merged
keito4 merged 1 commit into
mainfrom
fix/nvm-lazy-load-claude-cli-v2
Apr 3, 2026
Merged

fix: nvm lazy load で claude CLI が動作しない問題を修正#664
keito4 merged 1 commit into
mainfrom
fix/nvm-lazy-load-claude-cli-v2

Conversation

@keito4

@keito4 keito4 commented Apr 3, 2026

Copy link
Copy Markdown
Owner

概要

PR #662 の nvm 遅延読み込みで claude CLI が起動できなくなった問題を修正。

原因

  1. node/npm/npx をシェル関数でラップ → #!/usr/bin/env node から見えない
  2. nvm alias 解決で defaultnode(シンボリック名)を直接バージョンと解釈 → パスが vnode/bin になり不正

修正内容

  • alias 解決ロジックを廃止し、ls | sort -V | tail -1 で最新バージョンの bin を PATH に追加
  • node/npm/npx のシェル関数ラッパーを廃止(バイナリを直接使用)
  • nvm コマンドのみ遅延読み込みを維持
  • 起動時間: 0.37s(変化なし)

テスト

  • claude --version2.1.91 (Claude Code)
  • node --versionv24.4.0
  • time zsh -i -c exit → 0.37s
  • ✅ pre-commit: Format, Lint, Test 通過

Closes #663

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Updated Node.js environment initialization in shell configuration for more direct access to development tools.
    • Refactored package manager path setup during shell startup.

node/npm/npx のシェル関数ラッパーを廃止し、nvm の最新インストール済み
バージョンの bin ディレクトリを PATH に即座に追加する方式に変更。
nvm alias の再帰的解決(default → node → 実バージョン)が不要になり、
#!/usr/bin/env node を使う CLI ツール(claude 等)が正常に動作する。

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Apr 3, 2026

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Modified shell initialization in nix/home/zsh.nix to remove lazy-load function wrappers for node/npm/npx, instead directly exporting the Node.js bin directory to PATH, ensuring #!/usr/bin/env node shebangs resolve correctly. Added PNPM home configuration to PATH with duplicate prevention.

Changes

Cohort / File(s) Summary
Node.js & PNPM Shell Configuration
nix/home/zsh.nix
Removed node(), npm(), npx() lazy-load function wrappers; added direct PATH export for Node.js bin directory from $NVM_DIR/versions/node/*/bin to enable immediate resolution of shebangs without nvm initialization. Simplified _nvm_lazy_load to only handle nvm function lazy-loading. Added PNPM_HOME export and guarded PATH prepend to prevent duplicates.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

released

Poem

🐰 A hop and a skip, the paths align,
No wrapper functions to intertwine,
The Node lives ready in PATH so bright,
Shebangs resolved on first sight!
PNPM joins the journey—all systems shine!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main fix: resolving the nvm lazy-load issue that prevented the claude CLI from functioning.
Linked Issues check ✅ Passed All coding objectives from issue #663 are implemented: shell-function wrappers removed, latest Node version bin directory added to PATH, nvm lazy-loading preserved for commands only, startup performance maintained.
Out of Scope Changes check ✅ Passed All changes directly address the linked issue #663 objectives; the addition of PNPM_HOME export and PATH prepending in node.zsh is related to the Node.js environment setup scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/nvm-lazy-load-claude-cli-v2

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@keito4

keito4 commented Apr 3, 2026

Copy link
Copy Markdown
Owner Author

🔍 AI Code Review (Local Hook)

Models: Codex (default) / Gemini (default)

🤖 Codex Review

指摘事項:

  1. sort -V はmacOS(BSD sort)で未対応のためエラーになり、NODE_BIN が空のままになって PATH が設定されません。目的の「即座に node を使える」挙動が失敗します。nix/home/zsh.nix:112-115
  2. デフォルト Node.js の選択が「最新インストール済みバージョン」固定になり、nvm alias default.nvmrc による意図したバージョンが無視されます。プロジェクトごとの期待バージョンとズレる可能性が高いです。nix/home/zsh.nix:110-115

判定: patch is incorrect
理由: macOS 環境で sort -V が失敗し PATH 追加が効かず、狙っている即時 node 利用が壊れるため。
信頼度: 0.57


⚠️ 修正が必要です

上記のレビューで問題が指摘されています。修正してからマージしてください。


🤖 Generated by post_pr_ai_review.py hook

@claude

claude Bot commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

Claude finished @keito4's task in 1m 25s —— View job


PR レビュー結果: ❌ REQUEST_CHANGES

  • コンテキスト収集
  • コードレビュー実施
  • フィードバック投稿

概要

アプローチの方向性(node/npm/npx のシェル関数ラッパー廃止)は正しいです。ただし、実装に 2つの major 問題 があります。


🔴 Major: sort -V が macOS (BSD sort) 未対応

場所: nix/home/zsh.nix:114

NODE_BIN=$(ls -d "$NVM_DIR/versions/node"/*/bin 2>/dev/null | sort -V | tail -1)

sort -V は GNU 拡張であり、macOS の BSD sort では サポートされていません。このファイルは nix-darwin(macOS)向けの設定なので、NODE_BIN が空になり PATH が設定されず、修正が完全に無効になります。

Codex レビューでも同様の指摘があります。

修正案: sort -V を廃止し、nvm の alias ファイルを直接読む方式を採用する(後述)。


🔴 Major: nvm alias default を無視して最新版を強制使用

場所: nix/home/zsh.nix:112-115

現在のコードは「最新インストール済みバージョン」を使いますが、ユーザーが nvm alias default <version> で設定したデフォルトバージョンを完全に無視します。

例: defaultv20.19.0、最新インストールが v24.4.0 の場合 → v24.4.0 が使われる(意図と異なる)。

推奨修正: alias チェーンを辿って正しいバージョンを取得する。

# nvm alias default を辿って PATH に追加(macOS/Linux 互換)
if [ -d "$NVM_DIR/versions/node" ] && [ -f "$NVM_DIR/alias/default" ]; then
  _nvm_ver=$(cat "$NVM_DIR/alias/default")
  # alias チェーンを解決(例: "default" → "node" → "v24.4.0")
  while [ -f "$NVM_DIR/alias/$_nvm_ver" ]; do
    _nvm_ver=$(cat "$NVM_DIR/alias/$_nvm_ver")
  done
  if [ -d "$NVM_DIR/versions/node/$_nvm_ver/bin" ]; then
    export PATH="$NVM_DIR/versions/node/$_nvm_ver/bin:$PATH"
  fi
  unset _nvm_ver
fi

このアプローチは:

  • sort -V を使わない(macOS 互換)
  • nvm alias default の設定を尊重
  • ✅ alias チェーン(defaultnodev24.4.0)を正しく解決
  • ls のパース不要

Fix this →


🟡 Minor: PNPM_HOME が macOS パス固定

場所: nix/home/zsh.nix:136

export PNPM_HOME="$HOME/Library/pnpm"

$HOME/Library/pnpm は macOS 固有のパスです。Linux 環境では $HOME/.local/share/pnpm が標準です。nix-darwin 専用であれば問題ないですが、コメントで明示すると意図が伝わりやすくなります。


✅ 良い点

  • node/npm/npx のシェル関数ラッパーを廃止したことで、#!/usr/bin/env node シェバン問題は正しく解決されています
  • nvm コマンドの遅延読み込みは維持されており、起動時間への影響もない
  • _nvm_lazy_loadunset -f nvm 後に nvm.sh をソースして nvm "$@" を呼ぶフローは正しい

まとめ

sort -V の macOS 非互換と nvm alias default を無視する問題を修正してからマージを推奨します。上記の「推奨修正」コードで両方の問題を同時に解決できます。

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f8cb7ffc1e

ℹ️ About Codex in GitHub

Your team has set up Codex to 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 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread nix/home/zsh.nix
# nvm 本体の初期化のみ遅延させる
if [ -d "$NVM_DIR/versions/node" ]; then
# 最新のインストール済みバージョンを使用
NODE_BIN=$(ls -d "$NVM_DIR/versions/node"/*/bin 2>/dev/null | sort -V | tail -1)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Honor nvm default version when priming PATH

Selecting NODE_BIN via sort -V | tail -1 always chooses the highest installed Node, not the user's nvm alias default. In environments with multiple installed versions (for example default pinned to an LTS for project compatibility while a newer major is also installed), this makes node/npm and #!/usr/bin/env node tools run with the wrong runtime until nvm is explicitly invoked, which is a behavioral regression from the prior lazy-loader wrappers.

Useful? React with 👍 / 👎.

@keito4 keito4 self-assigned this Apr 3, 2026
@keito4
keito4 merged commit 8b183f8 into main Apr 3, 2026
7 of 8 checks passed
@keito4
keito4 deleted the fix/nvm-lazy-load-claude-cli-v2 branch April 3, 2026 00:48
@github-actions

github-actions Bot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 1.108.3 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@github-actions github-actions Bot added the released リリース済み label Apr 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released リリース済み

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant