Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/claude_code_sdk/_internal/transport/subprocess_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ async def connect(self) -> None:
self._stderr_stream = TextReceiveStream(self._process.stderr)

except FileNotFoundError as e:
# Check if the error comes from the working directory or the CLI
if self._cwd and not Path(self._cwd).exists():
raise CLIConnectionError(
f"Working directory does not exist: {self._cwd}"
) from e
raise CLINotFoundError(f"Claude Code not found at: {self._cli_path}") from e
except Exception as e:
raise CLIConnectionError(f"Failed to start Claude Code: {e}") from e
Expand Down
18 changes: 18 additions & 0 deletions tests/test_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,21 @@ def test_receive_messages(self):
# So we just verify the transport can be created and basic structure is correct
assert transport._prompt == "test"
assert transport._cli_path == "/usr/bin/claude"

def test_connect_with_nonexistent_cwd(self):
"""Test that connect raises CLIConnectionError when cwd doesn't exist."""
from claude_code_sdk._errors import CLIConnectionError

async def _test():
transport = SubprocessCLITransport(
prompt="test",
options=ClaudeCodeOptions(cwd="/this/directory/does/not/exist"),
cli_path="/usr/bin/claude",
)

with pytest.raises(CLIConnectionError) as exc_info:
await transport.connect()

assert "/this/directory/does/not/exist" in str(exc_info.value)

anyio.run(_test)