Skip to content
Open
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 .changeset/quote-bash-context-cwd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@e2b/code-interpreter-template': patch
---

Apply Bash context working directories correctly when their paths contain single quotes.
5 changes: 4 additions & 1 deletion template/server/messaging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import json
import logging
import shlex
import uuid
import asyncio

Expand Down Expand Up @@ -309,7 +310,9 @@ async def change_current_directory(
message_id, f'System.setProperty("user.dir", "{path}");', True
)
elif language == "bash":
request = self._get_execute_request(message_id, f"cd '{path}'", True)
request = self._get_execute_request(
message_id, f"cd {shlex.quote(path)}", True
)
else:
return

Expand Down
24 changes: 24 additions & 0 deletions tests/test_contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,27 @@ def test_contexts_secure_traffic(client_factory):

client.remove_context(context["id"])
assert context["id"] not in [ctx["id"] for ctx in client.list_contexts()]


@pytest.mark.parametrize("name", ["with spaces", "it's working"])
def test_bash_context_cwd_with_quotes(client: CodeInterpreter, name):
setup = client.run_code(
"from pathlib import Path; import tempfile; "
f"cwd = Path(tempfile.mkdtemp()) / {name!r}; "
"cwd.mkdir(); print(cwd)"
)
assert setup.error is None
directory = "".join(setup.stdout).strip()
assert directory.endswith("/" + name)
try:
context = client.create_context(language="bash", cwd=directory)
execution = client.run_code("pwd", context_id=context["id"])
assert execution.error is None
assert "".join(execution.stdout).strip() == directory
finally:
for context in client.list_contexts():
if context["cwd"] == directory:
client.remove_context(context["id"])
client.run_code(
f"import shutil; from pathlib import Path; shutil.rmtree(Path({directory!r}).parent)"
)
Loading