Skip to content

Commit d336a22

Browse files
committed
Explicit error if the cwd does not exist
Previously was raised as a CLINotFoundError
1 parent adf115f commit d336a22

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/claude_code_sdk/_internal/transport/subprocess_cli.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ async def connect(self) -> None:
136136
self._stderr_stream = TextReceiveStream(self._process.stderr)
137137

138138
except FileNotFoundError as e:
139+
# Check if the error comes from the working directory or the CLI
140+
if self._cwd and not Path(self._cwd).exists():
141+
raise CLIConnectionError(
142+
f"Working directory does not exist: {self._cwd}"
143+
) from e
139144
raise CLINotFoundError(f"Claude Code not found at: {self._cli_path}") from e
140145
except Exception as e:
141146
raise CLIConnectionError(f"Failed to start Claude Code: {e}") from e

tests/test_transport.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,21 @@ def test_receive_messages(self):
132132
# So we just verify the transport can be created and basic structure is correct
133133
assert transport._prompt == "test"
134134
assert transport._cli_path == "/usr/bin/claude"
135+
136+
def test_connect_with_nonexistent_cwd(self):
137+
"""Test that connect raises CLIConnectionError when cwd doesn't exist."""
138+
from claude_code_sdk._errors import CLIConnectionError
139+
140+
async def _test():
141+
transport = SubprocessCLITransport(
142+
prompt="test",
143+
options=ClaudeCodeOptions(cwd="/this/directory/does/not/exist"),
144+
cli_path="/usr/bin/claude",
145+
)
146+
147+
with pytest.raises(CLIConnectionError) as exc_info:
148+
await transport.connect()
149+
150+
assert "/this/directory/does/not/exist" in str(exc_info.value)
151+
152+
anyio.run(_test)

0 commit comments

Comments
 (0)