Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
fix(sdks): handle multi-source COPY/ADD in fromDockerfile (#1355)
Mish Ushakova6bf71a083a5
diff --git a/.changeset/multi-source-copy.md b/.changeset/multi-source-copy.mdnew file mode 100644index 000000000..9a50c017e--- /dev/null+++ b/.changeset/multi-source-copy.md@@ -0,0 +1,6 @@+---+'e2b': patch+'@e2b/python-sdk': patch+---++fix(sdks): `Template.fromDockerfile` / `Template.from_dockerfile` now correctly handle multi-source `COPY`/`ADD` instructions. Previously, only the first source was kept and all intermediate sources were silently dropped; now each source is emitted as its own `copy()` call to the same destination.diff --git a/packages/js-sdk/src/template/dockerfileParser.ts b/packages/js-sdk/src/template/dockerfileParser.tsindex 97d54a572..6ed62b57a 100644--- a/packages/js-sdk/src/template/dockerfileParser.ts+++ b/packages/js-sdk/src/template/dockerfileParser.ts@@ -184,8 +184,10 @@ function handleCopyInstruction(): void {const argumentsData = instruction.getArguments()if (argumentsData && argumentsData.length >= 2) {- const src = argumentsData[0].getValue()const dest = argumentsData[argumentsData.length - 1].getValue()+ const sources = argumentsData+ .slice(0, -1)+ .map((arg: Argument) => arg.getValue())let user: string | undefinedconst flags = instruction.getFlags()@@ -194,7 +196,9 @@ function handleCopyInstruction(user = chownFlag.getValue() ?? undefined}- templateBuilder.copy(src, dest, { user })+ for (const src of sources) {+ templateBuilder.copy(src, dest, { user })+ }}}diff --git a/packages/js-sdk/tests/template/methods/fromDockerfile.test.ts b/packages/js-sdk/tests/template/methods/fromDockerfile.test.tsindex 70e34f742..3b2f7256d 100644--- a/packages/js-sdk/tests/template/methods/fromDockerfile.test.ts+++ b/packages/js-sdk/tests/template/methods/fromDockerfile.test.ts@@ -131,6 +131,48 @@ buildTemplateTest('fromDockerfile with custom user and workdir', () => {)})+buildTemplateTest('fromDockerfile with multi-source COPY', () => {+ const dockerfile = `FROM node:24+COPY file1.txt file2.txt file3.txt /dest/`++ const template = Template().fromDockerfile(dockerfile)++ // After initial USER root and WORKDIR /, the multi-source COPY should+ // expand into one COPY instruction per source.+ // @ts-expect-error - instructions is not a property of TemplateBuilder+ const copyInstructions = template.instructions.filter(+ (i: { type: InstructionType }) => i.type === InstructionType.COPY+ )++ assert.equal(copyInstructions.length, 3)+ assert.equal(copyInstructions[0].args[0], 'file1.txt')+ assert.equal(copyInstructions[0].args[1], '/dest/')+ assert.equal(copyInstructions[1].args[0], 'file2.txt')+ assert.equal(copyInstructions[1].args[1], '/dest/')+ assert.equal(copyInstructions[2].args[0], 'file3.txt')+ assert.equal(copyInstructions[2].args[1], '/dest/')+})++buildTemplateTest('fromDockerfile with multi-source COPY --chown', () => {+ const dockerfile = `FROM node:24+COPY --chown=myuser:mygroup pkg.json pkg-lock.json /app/`++ const template = Template().fromDockerfile(dockerfile)++ // @ts-expect-error - instructions is not a property of TemplateBuilder+ const copyInstructions = template.instructions.filter(+ (i: { type: InstructionType }) => i.type === InstructionType.COPY+ )++ assert.equal(copyInstructions.length, 2)+ assert.equal(copyInstructions[0].args[0], 'pkg.json')+ assert.equal(copyInstructions[0].args[1], '/app/')+ assert.equal(copyInstructions[0].args[2], 'myuser:mygroup')+ assert.equal(copyInstructions[1].args[0], 'pkg-lock.json')+ assert.equal(copyInstructions[1].args[1], '/app/')+ assert.equal(copyInstructions[1].args[2], 'myuser:mygroup')+})+buildTemplateTest('fromDockerfile with COPY --chown', () => {const dockerfile = `FROM node:24COPY --chown=myuser:mygroup app.js /app/diff --git a/packages/python-sdk/e2b/template/dockerfile_parser.py b/packages/python-sdk/e2b/template/dockerfile_parser.pyindex 7c5c34ce4..434501779 100644--- a/packages/python-sdk/e2b/template/dockerfile_parser.py+++ b/packages/python-sdk/e2b/template/dockerfile_parser.py@@ -200,10 +200,11 @@ def _handle_copy_instruction(non_flag_parts.append(part)if len(non_flag_parts) >= 2:- src = non_flag_parts[0]dest = non_flag_parts[-1] # Last part is destination+ sources = non_flag_parts[:-1]- template_builder.copy(src, dest, user=user)+ for src in sources:+ template_builder.copy(src, dest, user=user)def _handle_workdir_instruction(diff --git a/packages/python-sdk/tests/async/template_async/methods/test_from_dockerfile.py b/packages/python-sdk/tests/async/template_async/methods/test_from_dockerfile.pyindex 426ed010c..1c9f2fda0 100644--- a/packages/python-sdk/tests/async/template_async/methods/test_from_dockerfile.py+++ b/packages/python-sdk/tests/async/template_async/methods/test_from_dockerfile.py@@ -66,6 +66,46 @@ async def test_from_dockerfile_with_custom_user_and_workdir():assert template._template._instructions[-1]["args"][0] == "/home/mish"[email protected]_debug()+async def test_from_dockerfile_with_multi_source_copy():+ dockerfile = """FROM node:24+COPY file1.txt file2.txt file3.txt /dest/"""++ template = AsyncTemplate().from_dockerfile(dockerfile)++ instructions = template._template._instructions++ copy_instructions = [i for i in instructions if i["type"] == InstructionType.COPY]++ assert len(copy_instructions) == 3+ assert copy_instructions[0]["args"][0] == "file1.txt"+ assert copy_instructions[0]["args"][1] == "/dest/"+ assert copy_instructions[1]["args"][0] == "file2.txt"+ assert copy_instructions[1]["args"][1] == "/dest/"+ assert copy_instructions[2]["args"][0] == "file3.txt"+ assert copy_instructions[2]["args"][1] == "/dest/"++[email protected]_debug()+async def test_from_dockerfile_with_multi_source_copy_chown():+ dockerfile = """FROM node:24+COPY --chown=myuser:mygroup pkg.json pkg-lock.json /app/"""++ template = AsyncTemplate().from_dockerfile(dockerfile)++ instructions = template._template._instructions++ copy_instructions = [i for i in instructions if i["type"] == InstructionType.COPY]++ assert len(copy_instructions) == 2+ assert copy_instructions[0]["args"][0] == "pkg.json"+ assert copy_instructions[0]["args"][1] == "/app/"+ assert copy_instructions[0]["args"][2] == "myuser:mygroup"+ assert copy_instructions[1]["args"][0] == "pkg-lock.json"+ assert copy_instructions[1]["args"][1] == "/app/"+ assert copy_instructions[1]["args"][2] == "myuser:mygroup"++@pytest.mark.skip_debug()async def test_from_dockerfile_with_copy_chown():dockerfile = """FROM node:24diff --git a/packages/python-sdk/tests/sync/template_sync/methods/test_from_dockerfile.py b/packages/python-sdk/tests/sync/template_sync/methods/test_from_dockerfile.pyindex b8ff1966e..a1db51acf 100644--- a/packages/python-sdk/tests/sync/template_sync/methods/test_from_dockerfile.py+++ b/packages/python-sdk/tests/sync/template_sync/methods/test_from_dockerfile.py@@ -66,6 +66,46 @@ def test_from_dockerfile_with_custom_user_and_workdir():assert template._template._instructions[-1]["args"][0] == "/home/mish"[email protected]_debug()+def test_from_dockerfile_with_multi_source_copy():+ dockerfile = """FROM node:24+COPY file1.txt file2.txt file3.txt /dest/"""++ template = Template().from_dockerfile(dockerfile)++ instructions = template._template._instructions++ copy_instructions = [i for i in instructions if i["type"] == InstructionType.COPY]++ assert len(copy_instructions) == 3+ assert copy_instructions[0]["args"][0] == "file1.txt"+ assert copy_instructions[0]["args"][1] == "/dest/"+ assert copy_instructions[1]["args"][0] == "file2.txt"+ assert copy_instructions[1]["args"][1] == "/dest/"+ assert copy_instructions[2]["args"][0] == "file3.txt"+ assert copy_instructions[2]["args"][1] == "/dest/"++[email protected]_debug()+def test_from_dockerfile_with_multi_source_copy_chown():+ dockerfile = """FROM node:24+COPY --chown=myuser:mygroup pkg.json pkg-lock.json /app/"""++ template = Template().from_dockerfile(dockerfile)++ instructions = template._template._instructions++ copy_instructions = [i for i in instructions if i["type"] == InstructionType.COPY]++ assert len(copy_instructions) == 2+ assert copy_instructions[0]["args"][0] == "pkg.json"+ assert copy_instructions[0]["args"][1] == "/app/"+ assert copy_instructions[0]["args"][2] == "myuser:mygroup"+ assert copy_instructions[1]["args"][0] == "pkg-lock.json"+ assert copy_instructions[1]["args"][1] == "/app/"+ assert copy_instructions[1]["args"][2] == "myuser:mygroup"++@pytest.mark.skip_debug()def test_from_dockerfile_with_copy_chown():dockerfile = """FROM node:24