Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Implement stack traces override context for fromDockerfile (#959)
Mish Ushakovd877205874d5
diff --git a/.changeset/new-birds-pump.md b/.changeset/new-birds-pump.mdnew file mode 100644index 000000000..e8e11764f--- /dev/null+++ b/.changeset/new-birds-pump.md@@ -0,0 +1,6 @@+---+'@e2b/python-sdk': patch+'e2b': patch+---++docker stack trace contextdiff --git a/packages/js-sdk/src/template/index.ts b/packages/js-sdk/src/template/index.tsindex 5573ed954..d22c8a1c4 100644--- a/packages/js-sdk/src/template/index.ts+++ b/packages/js-sdk/src/template/index.ts@@ -58,6 +58,7 @@ export class TemplateBaseprivate logsRefreshFrequency: number = 200private stackTraces: (string | undefined)[] = []private stackTracesEnabled: boolean = true+ private stackTracesOverride: string | undefined = undefinedconstructor(options?: TemplateOptions) {this.fileContextPath = options?.fileContextPath ?? this.fileContextPath@@ -178,7 +179,11 @@ export class TemplateBase}fromDockerfile(dockerfileContentOrPath: string): TemplateBuilder {- const { baseImage } = parseDockerfile(dockerfileContentOrPath, this)+ const { baseImage } = this.runInStackTraceOverrideContext(+ () => parseDockerfile(dockerfileContentOrPath, this),+ // -1 as we're going up the call stack from the parseDockerfile function+ getCallerFrame(STACK_TRACE_DEPTH - 1)+ )this.baseImage = baseImagethis.baseTemplate = undefined@@ -523,6 +528,11 @@ export class TemplateBasereturn this}+ if (this.stackTracesOverride) {+ this.stackTraces.push(this.stackTracesOverride)+ return this+ }+this.stackTraces.push(getCallerFrame(stackTracesDepth))return this}@@ -561,6 +571,16 @@ export class TemplateBasereturn result}+ private runInStackTraceOverrideContext<T>(+ fn: () => T,+ stackTraceOverride: string | undefined+ ): T {+ this.stackTracesOverride = stackTraceOverride+ const result = fn()+ this.stackTracesOverride = undefined+ return result+ }+/*** Convert the template to JSON representation.*diff --git a/packages/js-sdk/tests/template/stacktrace.test.ts b/packages/js-sdk/tests/template/stacktrace.test.tsindex de91f5675..8c526ecfd 100644--- a/packages/js-sdk/tests/template/stacktrace.test.ts+++ b/packages/js-sdk/tests/template/stacktrace.test.ts@@ -61,25 +61,23 @@ buildTemplateTest('traces on fromImage', async ({ buildTemplate }) => {}, 'fromImage')})-// TODO: uncomment this test when build system is updated-// test('traces on fromTemplate', async () => {-// const templateFrom = Template()-// const template = templateFrom.fromTemplate('this-template-does-not-exist')-// await expectToThrowAndCheckTrace(async () => {-// await buildTemplate(template)-// }, 'fromTemplate')-// })--// TODO: uncomment this test when fromDockerfile is fixed-// buildTemplateTest('traces on fromDockerfile', async ({ buildTemplate }) => {-// const templateFrom = Template()-// const template = templateFrom.fromDockerfile(-// 'FROM ubuntu:22.04\nRUN nonexistent'-// )-// await expectToThrowAndCheckTrace(async () => {-// await buildTemplate(template)-// }, 'fromDockerfile')-// })+buildTemplateTest('traces on fromTemplate', async ({ buildTemplate }) => {+ const templateFrom = Template()+ const template = templateFrom.fromTemplate('this-template-does-not-exist')+ await expectToThrowAndCheckTrace(async () => {+ await buildTemplate(template)+ }, 'fromTemplate')+})++buildTemplateTest('traces on fromDockerfile', async ({ buildTemplate }) => {+ const templateFrom = Template()+ const template = templateFrom.fromDockerfile(+ 'FROM ubuntu:22.04\nRUN nonexistent'+ )+ await expectToThrowAndCheckTrace(async () => {+ await buildTemplate(template)+ }, 'fromDockerfile')+})buildTemplateTest('traces on fromImage registry', async ({ buildTemplate }) => {const templateFrom = Template()diff --git a/packages/python-sdk/e2b/template/main.py b/packages/python-sdk/e2b/template/main.pyindex 215ab2ee8..27c5d4d89 100644--- a/packages/python-sdk/e2b/template/main.py+++ b/packages/python-sdk/e2b/template/main.py@@ -597,6 +597,7 @@ class TemplateBase:self._file_ignore_patterns: List[str] = file_ignore_patterns or []self._stack_traces: List[Union[TracebackType, None]] = []self._stack_traces_enabled: bool = True+ self._stack_traces_override: Optional[Union[TracebackType, None]] = Nonedef skip_cache(self) -> "TemplateBase":"""@@ -625,6 +626,11 @@ class TemplateBase:if not self._stack_traces_enabled:return self+ # Use the override if set, otherwise get the caller frame+ if self._stack_traces_override is not None:+ self._stack_traces.append(self._stack_traces_override)+ return self+stack = get_caller_frame(stack_traces_depth)if stack is None:self._stack_traces.append(None)@@ -673,6 +679,22 @@ class TemplateBase:self._collect_stack_trace(STACK_TRACE_DEPTH + 1)return result+ def _run_in_stack_trace_override_context(+ self, fn, stack_trace_override: Optional[Union[TracebackType, None]]+ ):+ """+ Execute a function with a manual stack trace override.++ :param fn: Function to execute+ :param stack_trace_override: Stack trace to use instead of auto-collecting++ :return: The result of the function+ """+ self._stack_traces_override = stack_trace_override+ result = fn()+ self._stack_traces_override = None+ return result+# Built-in image mixinsdef from_debian_image(self, variant: str = "stable") -> TemplateBuilder:"""@@ -838,8 +860,23 @@ class TemplateBase:# Create a TemplateBuilder first to use its methodsbuilder = TemplateBuilder(self)+ # Get the caller frame to use for stack trace override+ # -1 as we're going up the call stack from the parse_dockerfile function+ caller_frame = get_caller_frame(STACK_TRACE_DEPTH - 1)+ stack_trace_override = None+ if caller_frame is not None:+ stack_trace_override = TracebackType(+ tb_next=None,+ tb_frame=caller_frame,+ tb_lasti=caller_frame.f_lasti,+ tb_lineno=caller_frame.f_lineno,+ )+# Parse the dockerfile using the builder as the interface- base_image = parse_dockerfile(dockerfile_content_or_path, builder)+ base_image = self._run_in_stack_trace_override_context(+ lambda: parse_dockerfile(dockerfile_content_or_path, builder),+ stack_trace_override,+ )self._base_image = base_image# If we should force the next layer and it's a FROM command, invalidate whole templatediff --git a/packages/python-sdk/tests/async/template_async/test_stacktrace.py b/packages/python-sdk/tests/async/template_async/test_stacktrace.pyindex 406785c1f..48e9fea1a 100644--- a/packages/python-sdk/tests/async/template_async/test_stacktrace.py+++ b/packages/python-sdk/tests/async/template_async/test_stacktrace.py@@ -34,20 +34,21 @@ async def test_traces_on_from_image(async_build):await _expect_to_throw_and_check_trace(lambda: async_build(template), "from_image")-# @pytest.mark.skip_debug()-# async def test_traces_on_from_template(async_build):-# template = AsyncTemplate().from_template("this-template-does-not-exist")-# await _expect_to_throw_and_check_trace(lambda: async_build(template), "from_template")---# TODO: uncomment when the from_dockerfile exception handling is fixed-# @pytest.mark.skip_debug()-# async def test_traces_on_from_dockerfile(async_build):-# template = AsyncTemplate()-# template = template.from_dockerfile("FROM ubuntu:22.04\nRUN nonexistent")-# await _expect_to_throw_and_check_trace(-# lambda: async_build(template), "from_dockerfile"-# )[email protected]_debug()+async def test_traces_on_from_template(async_build):+ template = AsyncTemplate().from_template("this-template-does-not-exist")+ await _expect_to_throw_and_check_trace(+ lambda: async_build(template), "from_template"+ )++[email protected]_debug()+async def test_traces_on_from_dockerfile(async_build):+ template = AsyncTemplate()+ template = template.from_dockerfile("FROM ubuntu:22.04\nRUN nonexistent")+ await _expect_to_throw_and_check_trace(+ lambda: async_build(template), "from_dockerfile"+ )@pytest.mark.skip_debug()diff --git a/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py b/packages/python-sdk/tests/sync/template_sync/test_stacktrace.pyindex 528ec4c33..582452c4b 100644--- a/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py+++ b/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py@@ -34,18 +34,17 @@ def test_traces_on_from_image(build):_expect_to_throw_and_check_trace(lambda: build(template), "from_image")-# @pytest.mark.skip_debug()-# def test_traces_on_from_template(build):-# template = Template().from_template("this-template-does-not-exist")-# _expect_to_throw_and_check_trace(lambda: build(template), "from_template")---# TODO: uncomment when the from_dockerfile exception handling is fixed-# @pytest.mark.skip_debug()-# def test_traces_on_from_dockerfile(build):-# template = Template()-# template = template.from_dockerfile("FROM ubuntu:22.04\nRUN nonexistent")-# _expect_to_throw_and_check_trace(lambda: build(template), "from_dockerfile")[email protected]_debug()+def test_traces_on_from_template(build):+ template = Template().from_template("this-template-does-not-exist")+ _expect_to_throw_and_check_trace(lambda: build(template), "from_template")++[email protected]_debug()+def test_traces_on_from_dockerfile(build):+ template = Template()+ template = template.from_dockerfile("FROM ubuntu:22.04\nRUN nonexistent")+ _expect_to_throw_and_check_trace(lambda: build(template), "from_dockerfile")@pytest.mark.skip_debug()