From 60ec43185aaf52f8dfa5bd06f2ca588df69de376 Mon Sep 17 00:00:00 2001 From: Apeksha_2022 <70309060+Apeksha9389@users.noreply.github.com> Date: Wed, 21 Oct 2026 00:26:13 +0530 Subject: [PATCH] fix(template): quote Bash context working directories --- .changeset/quote-bash-context-cwd.md | 5 +++++ template/server/messaging.py | 5 ++++- tests/test_contexts.py | 24 ++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 .changeset/quote-bash-context-cwd.md diff --git a/.changeset/quote-bash-context-cwd.md b/.changeset/quote-bash-context-cwd.md new file mode 100644 index 00000000..d1c414e4 --- /dev/null +++ b/.changeset/quote-bash-context-cwd.md @@ -0,0 +1,5 @@ +--- +'@e2b/code-interpreter-template': patch +--- + +Apply Bash context working directories correctly when their paths contain single quotes. diff --git a/template/server/messaging.py b/template/server/messaging.py index 7248a5b4..c3e855fc 100644 --- a/template/server/messaging.py +++ b/template/server/messaging.py @@ -1,6 +1,7 @@ import datetime import json import logging +import shlex import uuid import asyncio @@ -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 diff --git a/tests/test_contexts.py b/tests/test_contexts.py index 8c2c4764..c1c74e26 100644 --- a/tests/test_contexts.py +++ b/tests/test_contexts.py @@ -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)" + )