Skip to content

Commit 343ec48

Browse files
authored
Merge pull request #42 from RomainGehrig/fix/cwd-not-exists
Explicit error if the cwd does not exist
2 parents 5a4f617 + d336a22 commit 343ec48

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
@@ -141,6 +141,11 @@ async def connect(self) -> None:
141141
self._stderr_stream = TextReceiveStream(self._process.stderr)
142142

143143
except FileNotFoundError as e:
144+
# Check if the error comes from the working directory or the CLI
145+
if self._cwd and not Path(self._cwd).exists():
146+
raise CLIConnectionError(
147+
f"Working directory does not exist: {self._cwd}"
148+
) from e
144149
raise CLINotFoundError(f"Claude Code not found at: {self._cli_path}") from e
145150
except Exception as e:
146151
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)