Skip to content

Commit 6b1f45c

Browse files
one-kashCopilot
andauthored
Fix Claude Code CLI detection for npm-local installs (#1978)
* Fix Claude Code CLI detection for npm-local installs `specify check` reports "Claude Code CLI (not found)" for users who installed Claude Code via npm-local (the default installer path, common with nvm). The binary lives at ~/.claude/local/node_modules/.bin/claude which was not checked. Add CLAUDE_NPM_LOCAL_PATH as a second well-known location alongside the existing migrate-installer path. Fixes #550 * Address Copilot review feedback - Remove unused pytest import from test_check_tool.py - Use tmp_path instead of hardcoded /nonexistent/claude for hermetic tests - Simplify redundant exists() + is_file() to just is_file() AI-assisted: Changes applied with Claude Code. * Update tests/test_check_tool.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update tests/test_check_tool.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 8778c26 commit 6b1f45c

File tree

2 files changed

+104
-5
lines changed

2 files changed

+104
-5
lines changed

src/specify_cli/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ def _build_ai_assistant_help() -> str:
345345
SCRIPT_TYPE_CHOICES = {"sh": "POSIX Shell (bash/zsh)", "ps": "PowerShell"}
346346

347347
CLAUDE_LOCAL_PATH = Path.home() / ".claude" / "local" / "claude"
348+
CLAUDE_NPM_LOCAL_PATH = Path.home() / ".claude" / "local" / "node_modules" / ".bin" / "claude"
348349

349350
BANNER = """
350351
███████╗██████╗ ███████╗ ██████╗██╗███████╗██╗ ██╗
@@ -605,13 +606,15 @@ def check_tool(tool: str, tracker: StepTracker = None) -> bool:
605606
Returns:
606607
True if tool is found, False otherwise
607608
"""
608-
# Special handling for Claude CLI after `claude migrate-installer`
609+
# Special handling for Claude CLI local installs
609610
# See: https://github.com/github/spec-kit/issues/123
610-
# The migrate-installer command REMOVES the original executable from PATH
611-
# and creates an alias at ~/.claude/local/claude instead
612-
# This path should be prioritized over other claude executables in PATH
611+
# See: https://github.com/github/spec-kit/issues/550
612+
# Claude Code can be installed in two local paths:
613+
# 1. ~/.claude/local/claude (after `claude migrate-installer`)
614+
# 2. ~/.claude/local/node_modules/.bin/claude (npm-local install, e.g. via nvm)
615+
# Neither path may be on the system PATH, so we check them explicitly.
613616
if tool == "claude":
614-
if CLAUDE_LOCAL_PATH.exists() and CLAUDE_LOCAL_PATH.is_file():
617+
if CLAUDE_LOCAL_PATH.is_file() or CLAUDE_NPM_LOCAL_PATH.is_file():
615618
if tracker:
616619
tracker.complete(tool, "available")
617620
return True

tests/test_check_tool.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""Tests for check_tool() — Claude Code CLI detection across install methods.
2+
3+
Covers issue https://github.com/github/spec-kit/issues/550:
4+
`specify check` reports "Claude Code CLI (not found)" even when claude is
5+
installed via npm-local (the default `claude` installer path).
6+
"""
7+
8+
from unittest.mock import patch, MagicMock
9+
10+
from specify_cli import check_tool
11+
12+
13+
class TestCheckToolClaude:
14+
"""Claude CLI detection must work for all install methods."""
15+
16+
def test_detected_via_migrate_installer_path(self, tmp_path):
17+
"""claude migrate-installer puts binary at ~/.claude/local/claude."""
18+
fake_claude = tmp_path / "claude"
19+
fake_claude.touch()
20+
21+
# Ensure npm-local path is missing so we only exercise migrate-installer path
22+
fake_missing = tmp_path / "nonexistent" / "claude"
23+
24+
with patch("specify_cli.CLAUDE_LOCAL_PATH", fake_claude), \
25+
patch("specify_cli.CLAUDE_NPM_LOCAL_PATH", fake_missing), \
26+
patch("shutil.which", return_value=None):
27+
assert check_tool("claude") is True
28+
29+
def test_detected_via_npm_local_path(self, tmp_path):
30+
"""npm-local install puts binary at ~/.claude/local/node_modules/.bin/claude."""
31+
fake_npm_claude = tmp_path / "node_modules" / ".bin" / "claude"
32+
fake_npm_claude.parent.mkdir(parents=True)
33+
fake_npm_claude.touch()
34+
35+
# Neither the migrate-installer path nor PATH has claude
36+
fake_migrate = tmp_path / "nonexistent" / "claude"
37+
38+
with patch("specify_cli.CLAUDE_LOCAL_PATH", fake_migrate), \
39+
patch("specify_cli.CLAUDE_NPM_LOCAL_PATH", fake_npm_claude), \
40+
patch("shutil.which", return_value=None):
41+
assert check_tool("claude") is True
42+
43+
def test_detected_via_path(self, tmp_path):
44+
"""claude on PATH (global npm install) should still work."""
45+
fake_missing = tmp_path / "nonexistent" / "claude"
46+
47+
with patch("specify_cli.CLAUDE_LOCAL_PATH", fake_missing), \
48+
patch("specify_cli.CLAUDE_NPM_LOCAL_PATH", fake_missing), \
49+
patch("shutil.which", return_value="/usr/local/bin/claude"):
50+
assert check_tool("claude") is True
51+
52+
def test_not_found_when_nowhere(self, tmp_path):
53+
"""Should return False when claude is genuinely not installed."""
54+
fake_missing = tmp_path / "nonexistent" / "claude"
55+
56+
with patch("specify_cli.CLAUDE_LOCAL_PATH", fake_missing), \
57+
patch("specify_cli.CLAUDE_NPM_LOCAL_PATH", fake_missing), \
58+
patch("shutil.which", return_value=None):
59+
assert check_tool("claude") is False
60+
61+
def test_tracker_updated_on_npm_local_detection(self, tmp_path):
62+
"""StepTracker should be marked 'available' for npm-local installs."""
63+
fake_npm_claude = tmp_path / "node_modules" / ".bin" / "claude"
64+
fake_npm_claude.parent.mkdir(parents=True)
65+
fake_npm_claude.touch()
66+
67+
fake_missing = tmp_path / "nonexistent" / "claude"
68+
tracker = MagicMock()
69+
70+
with patch("specify_cli.CLAUDE_LOCAL_PATH", fake_missing), \
71+
patch("specify_cli.CLAUDE_NPM_LOCAL_PATH", fake_npm_claude), \
72+
patch("shutil.which", return_value=None):
73+
result = check_tool("claude", tracker=tracker)
74+
75+
assert result is True
76+
tracker.complete.assert_called_once_with("claude", "available")
77+
78+
79+
class TestCheckToolOther:
80+
"""Non-Claude tools should be unaffected by the fix."""
81+
82+
def test_git_detected_via_path(self):
83+
with patch("shutil.which", return_value="/usr/bin/git"):
84+
assert check_tool("git") is True
85+
86+
def test_missing_tool(self):
87+
with patch("shutil.which", return_value=None):
88+
assert check_tool("nonexistent-tool") is False
89+
90+
def test_kiro_fallback(self):
91+
"""kiro-cli detection should try both kiro-cli and kiro."""
92+
def fake_which(name):
93+
return "/usr/bin/kiro" if name == "kiro" else None
94+
95+
with patch("shutil.which", side_effect=fake_which):
96+
assert check_tool("kiro-cli") is True

0 commit comments

Comments
 (0)