Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Render failed layer stack traces on build error (#901)

Mish Ushakov9f9e2a8f20d2
diff --git a/.changeset/nine-suits-drop.md b/.changeset/nine-suits-drop.md
new file mode 100644
index 000000000..2c1bb7451
--- /dev/null
+++ b/.changeset/nine-suits-drop.md
@@ -0,0 +1,6 @@
+---
+'@e2b/python-sdk': patch
+'e2b': patch
+---
+
+added step stacktraces
diff --git a/packages/js-sdk/src/api/index.ts b/packages/js-sdk/src/api/index.ts
index 75ce9ab25..faa38cc0d 100644
--- a/packages/js-sdk/src/api/index.ts
+++ b/packages/js-sdk/src/api/index.ts
@@ -8,7 +8,11 @@ import { createApiLogger } from '../logs'
export function handleApiError(
response: FetchResponse<any, any, any>,
- errorClass: new (message: string) => Error = SandboxError
+ errorClass: new (
+ message: string,
+ stackTrace?: string
+ ) => Error = SandboxError,
+ stackTrace?: string
): Error | undefined {
if (!response.error) {
return
@@ -35,7 +39,7 @@ export function handleApiError(
}
const message = response.error?.message ?? response.error
- return new errorClass(`${response.response.status}: ${message}`)
+ return new errorClass(`${response.response.status}: ${message}`, stackTrace)
}
/**
diff --git a/packages/js-sdk/src/errors.ts b/packages/js-sdk/src/errors.ts
index 15afbb4a9..7c226b8a5 100644
--- a/packages/js-sdk/src/errors.ts
+++ b/packages/js-sdk/src/errors.ts
@@ -11,9 +11,12 @@ export function formatSandboxTimeoutError(message: string) {
* Thrown when general sandbox errors occur.
*/
export class SandboxError extends Error {
- constructor(message: any) {
+ constructor(message?: string, stackTrace?: string) {
super(message)
this.name = 'SandboxError'
+ if (stackTrace) {
+ this.stack = stackTrace
+ }
}
}
@@ -29,8 +32,8 @@ export class SandboxError extends Error {
* The [unknown] error type is sometimes caused by the sandbox timeout when the request is not processed correctly.
*/
export class TimeoutError extends SandboxError {
- constructor(message: string) {
- super(message)
+ constructor(message: string, stackTrace?: string) {
+ super(message, stackTrace)
this.name = 'TimeoutError'
}
}
@@ -39,8 +42,8 @@ export class TimeoutError extends SandboxError {
* Thrown when an invalid argument is provided.
*/
export class InvalidArgumentError extends SandboxError {
- constructor(message: string) {
- super(message)
+ constructor(message: string, stackTrace?: string) {
+ super(message, stackTrace)
this.name = 'InvalidArgumentError'
}
}
@@ -49,8 +52,8 @@ export class InvalidArgumentError extends SandboxError {
* Thrown when there is not enough disk space.
*/
export class NotEnoughSpaceError extends SandboxError {
- constructor(message: string) {
- super(message)
+ constructor(message: string, stackTrace?: string) {
+ super(message, stackTrace)
this.name = 'NotEnoughSpaceError'
}
}
@@ -59,8 +62,8 @@ export class NotEnoughSpaceError extends SandboxError {
* Thrown when a resource is not found.
*/
export class NotFoundError extends SandboxError {
- constructor(message: string) {
- super(message)
+ constructor(message: string, stackTrace?: string) {
+ super(message, stackTrace)
this.name = 'NotFoundError'
}
}
@@ -69,7 +72,7 @@ export class NotFoundError extends SandboxError {
* Thrown when authentication fails.
*/
export class AuthenticationError extends Error {
- constructor(message: any) {
+ constructor(message: string) {
super(message)
this.name = 'AuthenticationError'
}
@@ -79,8 +82,8 @@ export class AuthenticationError extends Error {
* Thrown when the template uses old envd version. It isn't compatible with the new SDK.
*/
export class TemplateError extends SandboxError {
- constructor(message: string) {
- super(message)
+ constructor(message: string, stackTrace?: string) {
+ super(message, stackTrace)
this.name = 'TemplateError'
}
}
@@ -89,7 +92,7 @@ export class TemplateError extends SandboxError {
* Thrown when the API rate limit is exceeded.
*/
export class RateLimitError extends SandboxError {
- constructor(message: any) {
+ constructor(message: string) {
super(message)
this.name = 'RateLimitError'
}
diff --git a/packages/js-sdk/src/template/buildApi.ts b/packages/js-sdk/src/template/buildApi.ts
index ef1a2a57f..2aa4bce34 100644
--- a/packages/js-sdk/src/template/buildApi.ts
+++ b/packages/js-sdk/src/template/buildApi.ts
@@ -1,7 +1,7 @@
import { ApiClient, paths, handleApiError } from '../api'
import { BuildError, FileUploadError } from './errors'
import { LogEntry } from './types'
-import { tarFileStreamUpload } from './utils'
+import { getBuildStepIndex, tarFileStreamUpload } from './utils'
import { stripAnsi } from '../utils'
type RequestBuildInput = {
@@ -59,7 +59,8 @@ export async function requestBuild(
export async function getFileUploadLink(
client: ApiClient,
- { templateID, filesHash }: GetFileUploadLinkInput
+ { templateID, filesHash }: GetFileUploadLinkInput,
+ stackTrace?: string
) {
const fileUploadLinkRes = await client.api.GET(
'/templates/{templateID}/files/{hash}',
@@ -73,44 +74,55 @@ export async function getFileUploadLink(
}
)
- const error = handleApiError(fileUploadLinkRes, FileUploadError)
+ const error = handleApiError(fileUploadLinkRes, FileUploadError, stackTrace)
if (error) {
throw error
}
if (!fileUploadLinkRes.data) {
- throw new FileUploadError('Failed to get file upload link')
+ throw new FileUploadError('Failed to get file upload link', stackTrace)
}
return fileUploadLinkRes.data
}
-export async function uploadFile(options: {
- fileName: string
- fileContextPath: string
- url: string
-}) {
+export async function uploadFile(
+ options: {
+ fileName: string
+ fileContextPath: string
+ url: string
+ },
+ stackTrace?: string
+) {
const { fileName, url, fileContextPath } = options
- const { contentLength, uploadStream } = await tarFileStreamUpload(
- fileName,
- fileContextPath
- )
+ try {
+ const { contentLength, uploadStream } = await tarFileStreamUpload(
+ fileName,
+ fileContextPath
+ )
- // The compiler assumes this is Web fetch API, but it's actually Node.js fetch API
- const res = await fetch(url, {
- method: 'PUT',
- // @ts-expect-error
- body: uploadStream,
- headers: {
- 'Content-Length': contentLength.toString(),
- },
- duplex: 'half',
- })
+ // The compiler assumes this is Web fetch API, but it's actually Node.js fetch API
+ const res = await fetch(url, {
+ method: 'PUT',
+ // @ts-expect-error
+ body: uploadStream,
+ headers: {
+ 'Content-Length': contentLength.toString(),
+ },
+ duplex: 'half',
+ })
- if (!res.ok) {
- throw new FileUploadError(
- `Failed to upload file: ${res.statusText} ${res.status}`
- )
+ if (!res.ok) {
+ throw new FileUploadError(
+ `Failed to upload file: ${res.statusText}`,
+ stackTrace
+ )
+ }
+ } catch (error) {
+ if (error instanceof FileUploadError) {
+ throw error
+ }
+ throw new FileUploadError(`Failed to upload file: ${error}`, stackTrace)
}
}
@@ -175,11 +187,13 @@ export async function waitForBuildFinish(
buildID,
onBuildLogs,
logsRefreshFrequency,
+ stackTraces,
}: {
templateID: string
buildID: string
onBuildLogs?: (logEntry: InstanceType<typeof LogEntry>) => void
logsRefreshFrequency: number
+ stackTraces: (string | undefined)[]
}
): Promise<void> {
let logsOffset = 0
@@ -214,7 +228,19 @@ export async function waitForBuildFinish(
break
}
case 'error': {
- throw new BuildError(buildStatus?.reason?.message ?? 'Unknown error')
+ let stackError: string | undefined
+ if (buildStatus.reason?.step !== undefined) {
+ const step = getBuildStepIndex(
+ buildStatus.reason.step,
+ stackTraces.length
+ )
+ stackError = stackTraces[step]
+ }
+
+ throw new BuildError(
+ buildStatus?.reason?.message ?? 'Unknown error',
+ stackError
+ )
}
}
diff --git a/packages/js-sdk/src/template/consts.ts b/packages/js-sdk/src/template/consts.ts
new file mode 100644
index 000000000..d81a9ca54
--- /dev/null
+++ b/packages/js-sdk/src/template/consts.ts
@@ -0,0 +1,10 @@
+export const FINALIZE_STEP_NAME = 'finalize'
+export const BASE_STEP_NAME = 'base'
+
+/**
+ * Depths explained:
+ * 1. Template
+ * 2. TemplateBase
+ * 3. Caller method (eg. copy(), fromImage(), etc.)
+ */
+export const STACK_TRACE_DEPTH = 3
diff --git a/packages/js-sdk/src/template/dockerfileParser.ts b/packages/js-sdk/src/template/dockerfileParser.ts
index b92d11681..ecdc7f679 100644
--- a/packages/js-sdk/src/template/dockerfileParser.ts
+++ b/packages/js-sdk/src/template/dockerfileParser.ts
@@ -18,11 +18,7 @@ export interface DockerfileParserInterface {
setUser(user: string): DockerfileParserInterface
setEnvs(envs: Record<string, string>): DockerfileParserInterface
runCmd(
- command: string,
- options?: { user?: string }
- ): DockerfileParserInterface
- runCmd(
- commands: string[],
+ commandOrCommands: string | string[],
options?: { user?: string }
): DockerfileParserInterface
copy(
diff --git a/packages/js-sdk/src/template/errors.ts b/packages/js-sdk/src/template/errors.ts
index 57aa027b6..f61e5c2d9 100644
--- a/packages/js-sdk/src/template/errors.ts
+++ b/packages/js-sdk/src/template/errors.ts
@@ -1,13 +1,16 @@
export class BuildError extends Error {
- constructor(message: string) {
+ constructor(message: string, stackTrace?: string) {
super(message)
this.name = 'BuildError'
+ if (stackTrace) {
+ this.stack = stackTrace
+ }
}
}
export class FileUploadError extends BuildError {
- constructor(message: string) {
- super(message)
+ constructor(message: string, stackTrace?: string) {
+ super(message, stackTrace)
this.name = 'FileUploadError'
}
}
diff --git a/packages/js-sdk/src/template/index.ts b/packages/js-sdk/src/template/index.ts
index 6b0f9ad3c..31be28aa2 100644
--- a/packages/js-sdk/src/template/index.ts
+++ b/packages/js-sdk/src/template/index.ts
@@ -17,16 +17,19 @@ import {
CopyItem,
LogEntry,
RegistryConfig,
+ InstructionType,
} from './types'
import {
calculateFilesHash,
getCallerDirectory,
padOctal,
readDockerignore,
+ getCallerFrame,
readGCPServiceAccountJSON,
} from './utils'
import { ConnectionConfig } from '../connectionConfig'
import { ReadyCmd } from './readycmd'
+import { STACK_TRACE_DEPTH } from './consts'
type TemplateOptions = {
fileContextPath?: string
@@ -61,9 +64,11 @@ export class TemplateBase
private forceNextLayer: boolean = false
private instructions: Instruction[] = []
private fileContextPath: string =
- runtime === 'browser' ? '.' : getCallerDirectory() ?? '.'
+ runtime === 'browser' ? '.' : getCallerDirectory(STACK_TRACE_DEPTH) ?? '.'
private ignoreFilePaths: string[] = []
private logsRefreshFrequency: number = 200
+ private stackTraces: (string | undefined)[] = []
+ private stackTracesEnabled: boolean = true
constructor(options?: TemplateOptions) {
this.fileContextPath = options?.fileContextPath ?? this.fileContextPath
@@ -120,6 +125,7 @@ export class TemplateBase
this.force = true
}
+ this.collectStackTrace()
return this
}
@@ -132,6 +138,7 @@ export class TemplateBase
this.force = true
}
+ this.collectStackTrace()
return this
}
@@ -152,6 +159,7 @@ export class TemplateBase
this.force = true
}
+ this.collectStackTrace()
return this
}
@@ -162,13 +170,15 @@ export class TemplateBase
password: string
}
): TemplateBuilder {
- return this.fromImage(image, {
- registryConfig: {
- type: 'registry',
- username: options.username,
- password: options.password,
- },
- })
+ return this.runInNewStackTraceContext(() =>
+ this.fromImage(image, {
+ registryConfig: {
+ type: 'registry',
+ username: options.username,
+ password: options.password,
+ },
+ })
+ )
}
fromAWSRegistry(
@@ -179,14 +189,16 @@ export class TemplateBase
region: string
}
): TemplateBuilder {
- return this.fromImage(image, {
- registryConfig: {
- type: 'aws',
- awsAccessKeyId: options.accessKeyId,
- awsSecretAccessKey: options.secretAccessKey,
- awsRegion: options.region,
- },
- })
+ return this.runInNewStackTraceContext(() =>
+ this.fromImage(image, {
+ registryConfig: {
+ type: 'aws',
+ awsAccessKeyId: options.accessKeyId,
+ awsSecretAccessKey: options.secretAccessKey,
+ awsRegion: options.region,
+ },
+ })
+ )
}
fromGCPRegistry(
@@ -195,15 +207,17 @@ export class TemplateBase
serviceAccountJSON: string | object
}
): TemplateBuilder {
- return this.fromImage(image, {
- registryConfig: {
- type: 'gcp',
- serviceAccountJson: readGCPServiceAccountJSON(
- this.fileContextPath,
- options.serviceAccountJSON
- ),
- },
- })
+ return this.runInNewStackTraceContext(() =>
+ this.fromImage(image, {
+ registryConfig: {
+ type: 'gcp',
+ serviceAccountJson: readGCPServiceAccountJSON(
+ this.fileContextPath,
+ options.serviceAccountJSON
+ ),
+ },
+ })
+ )
}
copy(
@@ -246,13 +260,14 @@ export class TemplateBase
]
this.instructions.push({
- type: 'COPY',
+ type: InstructionType.COPY,
args,
force: item.forceUpload ?? this.forceNextLayer,
forceUpload: item.forceUpload,
})
}
+ this.collectStackTrace()
return this
}
@@ -267,8 +282,7 @@ export class TemplateBase
if (options?.force) {
args.push('-f')
}
- this.runCmd(args.join(' '))
- return this
+ return this.runInNewStackTraceContext(() => this.runCmd(args.join(' ')))
}
rename(
@@ -280,8 +294,7 @@ export class TemplateBase
if (options?.force) {
args.push('-f')
}
- this.runCmd(args.join(' '))
- return this
+ return this.runInNewStackTraceContext(() => this.runCmd(args.join(' ')))
}
makeDir(
@@ -292,14 +305,12 @@ export class TemplateBase
if (options?.mode) {
args.push(`-m ${padOctal(options.mode)}`)
}
- this.runCmd(args.join(' '))
- return this
+ return this.runInNewStackTraceContext(() => this.runCmd(args.join(' ')))
}
makeSymlink(src: string, dest: string): TemplateBuilder {
const args = ['ln', '-s', src, dest]
- this.runCmd(args.join(' '))
- return this
+ return this.runInNewStackTraceContext(() => this.runCmd(args.join(' ')))
}
runCmd(command: string, options?: { user?: string }): TemplateBuilder
@@ -318,28 +329,34 @@ export class TemplateBase
}
this.instructions.push({
- type: 'RUN',
+ type: InstructionType.RUN,
args,
force: this.forceNextLayer,
})
+
+ this.collectStackTrace()
return this
}
setWorkdir(workdir: string): TemplateBuilder {
this.instructions.push({
- type: 'WORKDIR',
+ type: InstructionType.WORKDIR,
args: [workdir],
force: this.forceNextLayer,
})
+
+ this.collectStackTrace()
return this
}
setUser(user: string): TemplateBuilder {
this.instructions.push({
- type: 'USER',
+ type: InstructionType.USER,
args: [user],
force: this.forceNextLayer,
})
+
+ this.collectStackTrace()
return this
}
@@ -355,7 +372,8 @@ export class TemplateBase
} else {
args.push('.')
}
- return this.runCmd(args)
+
+ return this.runInNewStackTraceContext(() => this.runCmd(args))
}
npmInstall(packages?: string | string[], g?: boolean): TemplateBuilder {
@@ -371,20 +389,22 @@ export class TemplateBase
if (g) {
args.push('-g')
}
- return this.runCmd(args)
+
+ return this.runInNewStackTraceContext(() => this.runCmd(args))
}
aptInstall(packages: string | string[]): TemplateBuilder {
const packageList = Array.isArray(packages) ? packages : [packages]
-
- return this.runCmd(
- [
- 'apt-get update',
- `DEBIAN_FRONTEND=noninteractive DEBCONF_NOWARNINGS=yes apt-get install -y --no-install-recommends ${packageList.join(
- ' '
- )}`,
- ],
- { user: 'root' }
+ return this.runInNewStackTraceContext(() =>
+ this.runCmd(
+ [
+ 'apt-get update',
+ `DEBIAN_FRONTEND=noninteractive DEBCONF_NOWARNINGS=yes apt-get install -y --no-install-recommends ${packageList.join(
+ ' '
+ )}`,
+ ],
+ { user: 'root' }
+ )
)
}
@@ -401,8 +421,8 @@ export class TemplateBase
if (options?.depth) {
args.push(`--depth ${options.depth}`)
}
- this.runCmd(args.join(' '))
- return this
+
+ return this.runInNewStackTraceContext(() => this.runCmd(args.join(' ')))
}
setStartCmd(
@@ -417,6 +437,7 @@ export class TemplateBase
this.readyCmd = readyCommand
}
+ this.collectStackTrace()
return this
}
@@ -427,6 +448,7 @@ export class TemplateBase
this.readyCmd = readyCommand
}
+ this.collectStackTrace()
return this
}
@@ -436,10 +458,11 @@ export class TemplateBase
}
this.instructions.push({
- type: 'ENV',
+ type: InstructionType.ENV,
args: Object.entries(envs).flatMap(([key, value]) => [key, value]),
force: this.forceNextLayer,
})
+ this.collectStackTrace()
return this
}
@@ -448,9 +471,36 @@ export class TemplateBase
return this
}
+ private collectStackTrace(stackTracesDepth: number = STACK_TRACE_DEPTH) {
+ if (!this.stackTracesEnabled) {
+ return this
+ }
+
+ this.stackTraces.push(getCallerFrame(stackTracesDepth))
+ return this
+ }
+
+ private disableStackTrace() {
+ this.stackTracesEnabled = false
+ return this
+ }
+
+ private enableStackTrace() {
+ this.stackTracesEnabled = true
+ return this
+ }
+
+ private runInNewStackTraceContext<T>(fn: () => T): T {
+ this.disableStackTrace()
+ const result = fn()
+ this.enableStackTrace()
+ this.collectStackTrace(STACK_TRACE_DEPTH + 1)
+ return result
+ }
+
private async toJSON(): Promise<string> {
return JSON.stringify(
- this.serialize(await this.calculateFilesHashes()),
+ this.serialize(await this.instructionsWithHashes()),
undefined,
2
)
@@ -512,47 +562,62 @@ export class TemplateBase
)
)
- const instructionsWithHashes = await this.calculateFilesHashes()
-
- // Prepare file uploads
- const fileUploads = instructionsWithHashes
- .filter((instruction) => instruction.type === 'COPY')
- .map((instruction) => ({
- src: instruction.args[0],
- dest: instruction.args[1],
- filesHash: instruction.filesHash,
- forceUpload: instruction.forceUpload,
- }))
+ const instructionsWithHashes = await this.instructionsWithHashes()
// Upload files in parallel
- const uploadPromises = fileUploads.map(async (file) => {
- const { present, url } = await getFileUploadLink(client, {
- templateID,
- filesHash: file.filesHash!,
- })
-
- if (
- (file.forceUpload && url != null) ||
- (present === false && url != null)
- ) {
- await uploadFile({
- fileName: file.src,
- fileContextPath: this.fileContextPath,
- url,
- })
- options.onBuildLogs?.(
- new LogEntry(new Date(), 'info', `Uploaded '${file.src}'`)
+ const uploadPromises = instructionsWithHashes.map(
+ async (instruction, index) => {
+ if (instruction.type !== InstructionType.COPY) {
+ return
+ }
+
+ const src = instruction.args.length > 0 ? instruction.args[0] : null
+ const filesHash = instruction.filesHash ?? null
+ if (src === null || filesHash === null) {
+ throw new Error('Source path and files hash are required')
+ }
+
+ const forceUpload = instruction.forceUpload
+ let stackTrace = undefined
+ if (index + 1 >= 0 && index + 1 < this.stackTraces.length) {
+ stackTrace = this.stackTraces[index + 1]
+ }
+
+ const { present, url } = await getFileUploadLink(
+ client,
+ {
+ templateID,
+ filesHash,
+ },
+ stackTrace
)
- } else {
- options.onBuildLogs?.(
- new LogEntry(
- new Date(),
- 'info',
- `Skipping upload of '${file.src}', already cached`
+
+ if (
+ (forceUpload && url != null) ||
+ (present === false && url != null)
+ ) {
+ await uploadFile(
+ {
+ fileName: src,
+ fileContextPath: this.fileContextPath,
+ url,
+ },
+ stackTrace
)
- )
+ options.onBuildLogs?.(
+ new LogEntry(new Date(), 'info', `Uploaded '${src}'`)
+ )
+ } else {
+ options.onBuildLogs?.(
+ new LogEntry(
+ new Date(),
+ 'info',
+ `Skipping upload of '${src}', already cached`
+ )
+ )
+ }
}
- })
+ )
await Promise.all(uploadPromises)
@@ -580,32 +645,46 @@ export class TemplateBase
buildID,
onBuildLogs: options.onBuildLogs,
logsRefreshFrequency: this.logsRefreshFrequency,
+ stackTraces: this.stackTraces,
})
}
// We might no longer need this as we move the logic server-side
- private async calculateFilesHashes(): Promise<Instruction[]> {
- const steps: Instruction[] = []
-
- for (const instruction of this.instructions) {
- if (instruction.type === 'COPY') {
- instruction.filesHash = await calculateFilesHash(
- instruction.args[0],
- instruction.args[1],
- this.fileContextPath,
- [
- ...this.ignoreFilePaths,
- ...(runtime === 'browser'
- ? []
- : readDockerignore(this.fileContextPath)),
- ]
- )
- }
-
- steps.push(instruction)
- }
-
- return steps
+ private async instructionsWithHashes(): Promise<Instruction[]> {
+ return Promise.all(
+ this.instructions.map(async (instruction, index) => {
+ if (instruction.type !== InstructionType.COPY) {
+ return instruction
+ }
+
+ const src = instruction.args.length > 0 ? instruction.args[0] : null
+ const dest = instruction.args.length > 1 ? instruction.args[1] : null
+ if (src === null || dest === null) {
+ throw new Error('Source path and destination path are required')
+ }
+
+ let stackTrace = undefined
+ if (index + 1 >= 0 && index + 1 < this.stackTraces.length) {
+ stackTrace = this.stackTraces[index + 1]
+ }
+
+ return {
+ ...instruction,
+ filesHash: await calculateFilesHash(
+ src,
+ dest,
+ this.fileContextPath,
+ [
+ ...this.ignoreFilePaths,
+ ...(runtime === 'browser'
+ ? []
+ : readDockerignore(this.fileContextPath)),
+ ],
+ stackTrace
+ ),
+ }
+ })
+ )
}
private serialize(steps: Instruction[]): TriggerBuildTemplate {
diff --git a/packages/js-sdk/src/template/types.ts b/packages/js-sdk/src/template/types.ts
index dbcbedd75..02467b549 100644
--- a/packages/js-sdk/src/template/types.ts
+++ b/packages/js-sdk/src/template/types.ts
@@ -1,8 +1,16 @@
import { stripAnsi } from '../utils'
import { ReadyCmd } from './readycmd'
+export enum InstructionType {
+ COPY = 'COPY',
+ ENV = 'ENV',
+ RUN = 'RUN',
+ WORKDIR = 'WORKDIR',
+ USER = 'USER',
+}
+
export type Instruction = {
- type: 'COPY' | 'ENV' | 'RUN' | 'WORKDIR' | 'USER'
+ type: InstructionType
args: string[]
force: boolean
forceUpload?: boolean
@@ -97,6 +105,11 @@ export interface TemplateBuilder {
runCmd(commands: string[], options?: { user?: string }): TemplateBuilder
+ runCmd(
+ commandOrCommands: string | string[],
+ options?: { user?: string }
+ ): TemplateBuilder
+
setWorkdir(workdir: string): TemplateBuilder
setUser(user: string): TemplateBuilder
diff --git a/packages/js-sdk/src/template/utils.ts b/packages/js-sdk/src/template/utils.ts
index 6c6be85e2..8d8649452 100644
--- a/packages/js-sdk/src/template/utils.ts
+++ b/packages/js-sdk/src/template/utils.ts
@@ -2,6 +2,7 @@ import crypto from 'node:crypto'
import fs from 'node:fs'
import path from 'node:path'
import { dynamicGlob, dynamicTar } from '../utils'
+import { BASE_STEP_NAME, FINALIZE_STEP_NAME } from './consts'
export function readDockerignore(contextPath: string): string[] {
const dockerignorePath = path.join(contextPath, '.dockerignore')
@@ -20,7 +21,8 @@ export async function calculateFilesHash(
src: string,
dest: string,
contextPath: string,
- ignorePatterns?: string[]
+ ignorePatterns?: string[],
+ stackTrace?: string
): Promise<string> {
const { globSync } = await dynamicGlob()
const srcPath = path.join(contextPath, src)
@@ -34,7 +36,11 @@ export async function calculateFilesHash(
})
if (files.length === 0) {
- throw new Error(`No files found in ${srcPath}`)
+ const error = new Error(`No files found in ${srcPath}`)
+ if (stackTrace) {
+ error.stack = stackTrace
+ }
+ throw error
}
for (const file of files) {
@@ -45,16 +51,43 @@ export async function calculateFilesHash(
return hash.digest('hex')
}
-export function getCallerDirectory(): string | undefined {
+/**
+ * Get the caller frame
+ * @param depth - The depth of the stack trace
+ * Levels explained: caller (eg. from class TemplateBase.fromImage) > original caller (eg. template file)
+ * @returns The caller frame
+ */
+export function getCallerFrame(depth: number): string | undefined {
const stackTrace = new Error().stack
if (!stackTrace) {
return
}
- const lines = stackTrace.split('\n')
- const caller = lines[4]
+ const lines = stackTrace.split('\n').slice(1) // Skip the this function (getCallerFrame)
+ if (lines.length < depth + 1) {
+ return
+ }
+
+ return lines.slice(depth).join('\n')
+}
+
+/**
+ * Get the caller directory
+ * @returns The caller directory
+ */
+export function getCallerDirectory(depth: number): string | undefined {
+ const caller = getCallerFrame(depth + 1) // +1 depth to skip this function (getCallerDirectory)
+ if (!caller) {
+ return
+ }
+
+ const lines = caller.split('\n')
+ if (lines.length === 0) {
+ return
+ }
+ const firstLine = lines[0]
- const match = caller.match(/at ([^:]+):\d+:\d+/)
+ const match = firstLine.match(/at ([^:]+):\d+:\d+/)
if (match) {
const filePath = match[1]
return path.dirname(filePath)
@@ -98,6 +131,21 @@ export async function tarFileStreamUpload(
}
}
+export function getBuildStepIndex(
+ step: string,
+ stackTracesLength: number
+): number {
+ if (step === BASE_STEP_NAME) {
+ return 0
+ }
+
+ if (step === FINALIZE_STEP_NAME) {
+ return stackTracesLength - 1
+ }
+
+ return Number(step)
+}
+
export function readGCPServiceAccountJSON(
contextPath: string,
pathOrContent: string | object
diff --git a/packages/js-sdk/tests/template/stacktrace.test.ts b/packages/js-sdk/tests/template/stacktrace.test.ts
new file mode 100644
index 000000000..d72b94d7e
--- /dev/null
+++ b/packages/js-sdk/tests/template/stacktrace.test.ts
@@ -0,0 +1,238 @@
+import { assert, test } from 'vitest'
+import { Template, TemplateClass, waitForTimeout } from '../../src'
+import { randomUUID } from 'node:crypto'
+import fs from 'node:fs'
+
+const __fileContent = fs.readFileSync(__filename, 'utf8') // read current file content
+const nonExistentPath = '/nonexistent/path'
+
+function buildTemplate(template: TemplateClass) {
+ return Template.build(template, {
+ alias: randomUUID(),
+ cpuCount: 1,
+ memoryMB: 1024,
+ })
+}
+
+function getStackTraceCallerMethod(
+ fileContent: string,
+ stackTrace: string | undefined
+) {
+ if (!stackTrace) {
+ return null
+ }
+
+ const stackTraceLines = stackTrace.split('\n')
+ if (stackTraceLines.length === 0) {
+ return null
+ }
+ const callerTrace = stackTraceLines[0]
+
+ const [, line, column] = callerTrace.split(':')
+ const lineNumber = parseInt(line)
+ const columnNumber = parseInt(column)
+
+ const lines = fileContent.split('\n')
+ const parsedLine = lines[lineNumber - 1]
+ if (!parsedLine) {
+ return null
+ }
+
+ const match = parsedLine.slice(columnNumber - 1).match(/^(\w+)\s*\(/)
+ if (match) {
+ return match[1]
+ }
+ return null
+}
+
+async function expectToThrowAndCheckTrace(
+ func: (...args: any[]) => Promise<void>,
+ expectedMethod: string
+) {
+ try {
+ await func()
+ assert.fail('Expected Template.build to throw an error')
+ } catch (error) {
+ const callerMethod = getStackTraceCallerMethod(__fileContent, error.stack)
+ if (!callerMethod) {
+ throw error
+ }
+ assert.include(callerMethod, expectedMethod)
+ }
+}
+
+test('traces on fromImage', async () => {
+ const templateFrom = Template()
+ const template = templateFrom.fromImage('e2b.dev/this-image-does-not-exist')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, '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')
+// })
+
+test('traces on fromDockerfile', async () => {
+ const templateFrom = Template()
+ const template = templateFrom.fromDockerfile(
+ 'FROM ubuntu:22.04\nRUN nonexistent'
+ )
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'fromDockerfile')
+})
+
+test('traces on fromRegistry', async () => {
+ const templateFrom = Template()
+ const template = templateFrom.fromRegistry(
+ 'registry.example.com/nonexistent:latest',
+ {
+ username: 'test',
+ password: 'test',
+ }
+ )
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'fromRegistry')
+})
+
+test('traces on fromAWSRegistry', async () => {
+ const templateFrom = Template()
+ const template = templateFrom.fromAWSRegistry(
+ '123456789.dkr.ecr.us-east-1.amazonaws.com/nonexistent:latest',
+ {
+ accessKeyId: 'test',
+ secretAccessKey: 'test',
+ region: 'us-east-1',
+ }
+ )
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'fromAWSRegistry')
+})
+
+test('traces on fromGCPRegistry', async () => {
+ const templateFrom = Template()
+ const template = templateFrom.fromGCPRegistry(
+ 'gcr.io/nonexistent-project/nonexistent:latest',
+ {
+ serviceAccountJSON: { type: 'service_account' },
+ }
+ )
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'fromGCPRegistry')
+})
+
+test('traces on copy', async () => {
+ let template = Template().fromBaseImage()
+ template = template.copy(nonExistentPath, nonExistentPath)
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'copy')
+})
+
+test('traces on remove', async () => {
+ let template = Template().fromBaseImage()
+ template = template.remove(nonExistentPath)
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'remove')
+})
+
+test('traces on rename', async () => {
+ let template = Template().fromBaseImage()
+ template = template.rename(nonExistentPath, '/tmp/dest.txt')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'rename')
+})
+
+test('traces on makeDir', async () => {
+ let template = Template().fromBaseImage()
+ template = template.makeDir('.bashrc')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'makeDir')
+})
+
+test('traces on makeSymlink', async () => {
+ let template = Template().fromBaseImage()
+ template = template.makeSymlink('.bashrc', '.bashrc')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'makeSymlink')
+})
+
+test('traces on runCmd', async () => {
+ let template = Template().fromBaseImage()
+ template = template.runCmd(`./${nonExistentPath}`)
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'runCmd')
+})
+
+test('traces on setWorkdir', async () => {
+ let template = Template().fromBaseImage()
+ template = template.setWorkdir('.bashrc')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'setWorkdir')
+})
+
+test('traces on setUser', async () => {
+ let template = Template().fromBaseImage()
+ template = template.setUser(';')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'setUser')
+})
+
+test('traces on pipInstall', async () => {
+ let template = Template().fromBaseImage()
+ template = template.pipInstall('nonexistent-package')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'pipInstall')
+})
+
+test('traces on npmInstall', async () => {
+ let template = Template().fromBaseImage()
+ template = template.npmInstall('nonexistent-package')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'npmInstall')
+})
+
+test('traces on aptInstall', async () => {
+ let template = Template().fromBaseImage()
+ template = template.aptInstall('nonexistent-package')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'aptInstall')
+})
+
+test('traces on gitClone', async () => {
+ let template = Template().fromBaseImage()
+ template = template.gitClone('https://github.com/nonexistent/repo.git')
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'gitClone')
+})
+
+test('traces on setStartCmd', async () => {
+ let template: any = Template().fromBaseImage()
+ template = template.setStartCmd(
+ `./${nonExistentPath}`,
+ waitForTimeout(10_000)
+ )
+ await expectToThrowAndCheckTrace(async () => {
+ await buildTemplate(template)
+ }, 'setStartCmd')
+})
diff --git a/packages/js-sdk/vitest.workspace.mts b/packages/js-sdk/vitest.workspace.mts
index 4825b2f8f..f74b400d7 100644
--- a/packages/js-sdk/vitest.workspace.mts
+++ b/packages/js-sdk/vitest.workspace.mts
@@ -53,4 +53,12 @@ export default defineWorkspace([
environment: 'node',
},
},
+ {
+ test: {
+ include: ['tests/template/stacktrace.test.ts'],
+ globals: false,
+ testTimeout: 180_000,
+ environment: 'node',
+ },
+ },
])
diff --git a/packages/python-sdk/e2b/api/__init__.py b/packages/python-sdk/e2b/api/__init__.py
index f7c01d4df..713704a37 100644
--- a/packages/python-sdk/e2b/api/__init__.py
+++ b/packages/python-sdk/e2b/api/__init__.py
@@ -1,3 +1,4 @@
+from types import TracebackType
import json
import logging
from typing import Optional
@@ -27,7 +28,9 @@ class SandboxCreateResponse:
def handle_api_exception(
- e: Response, default_exception_class: type[Exception] = SandboxException
+ e: Response,
+ default_exception_class: type[Exception] = SandboxException,
+ stack_trace: Optional[TracebackType] = None,
):
try:
body = json.loads(e.content) if e.content else {}
@@ -47,8 +50,12 @@ def handle_api_exception(
return RateLimitException(message)
if "message" in body:
- return default_exception_class(f"{e.status_code}: {body['message']}")
- return default_exception_class(f"{e.status_code}: {e.content}")
+ return default_exception_class(
+ f"{e.status_code}: {body['message']}"
+ ).with_traceback(stack_trace)
+ return default_exception_class(f"{e.status_code}: {e.content}").with_traceback(
+ stack_trace
+ )
class ApiClient(AuthenticatedClient):
diff --git a/packages/python-sdk/e2b/template/consts.py b/packages/python-sdk/e2b/template/consts.py
new file mode 100644
index 000000000..1ae88cff4
--- /dev/null
+++ b/packages/python-sdk/e2b/template/consts.py
@@ -0,0 +1,9 @@
+FINALIZE_STEP_NAME = "finalize"
+BASE_STEP_NAME = "base"
+
+"""
+Depths explained:
+1. TemplateClass
+2. Caller method (eg. copy(), fromImage(), etc.)
+"""
+STACK_TRACE_DEPTH = 2
diff --git a/packages/python-sdk/e2b/template/main.py b/packages/python-sdk/e2b/template/main.py
index 03bbbe003..0e894b3a7 100644
--- a/packages/python-sdk/e2b/template/main.py
+++ b/packages/python-sdk/e2b/template/main.py
@@ -1,13 +1,16 @@
import json
from typing import Dict, List, Optional, Union
+from types import TracebackType
from httpx import Limits
+from e2b.template.consts import STACK_TRACE_DEPTH
from e2b.template.dockerfile_parser import parse_dockerfile
from e2b.template.types import (
CopyItem,
Instruction,
TemplateType,
RegistryConfig,
+ InstructionType,
)
from e2b.template.utils import (
calculate_files_hash,
@@ -15,6 +18,7 @@ from e2b.template.utils import (
pad_octal,
read_dockerignore,
read_gcp_service_account_json,
+ get_caller_frame,
)
from e2b.template.readycmd import ReadyCmd
@@ -57,12 +61,13 @@ class TemplateBuilder:
]
instruction: Instruction = Instruction(
- type="COPY",
+ type=InstructionType.COPY,
args=args,
force=force_upload or self._template._force_next_layer,
forceUpload=force_upload,
)
self._template._instructions.append(instruction)
+ self._template._collect_stack_trace()
return self
def remove(
@@ -74,16 +79,18 @@ class TemplateBuilder:
if force:
args.append("-f")
- self.run_cmd(" ".join(args))
- return self
+ return self._template._run_in_new_stack_trace_context(
+ lambda: self.run_cmd(" ".join(args))
+ )
def rename(self, src: str, dest: str, force: bool = False) -> "TemplateBuilder":
args = ["mv", src, dest]
if force:
args.append("-f")
- self.run_cmd(" ".join(args))
- return self
+ return self._template._run_in_new_stack_trace_context(
+ lambda: self.run_cmd(" ".join(args))
+ )
def make_dir(
self, paths: Union[str, List[str]], mode: Optional[int] = None
@@ -95,13 +102,15 @@ class TemplateBuilder:
if mode:
args.append(f"-m {pad_octal(mode)}")
- self.run_cmd(" ".join(args))
- return self
+ return self._template._run_in_new_stack_trace_context(
+ lambda: self.run_cmd(" ".join(args))
+ )
def make_symlink(self, src: str, dest: str) -> "TemplateBuilder":
args = ["ln", "-s", src, dest]
- self.run_cmd(" ".join(args))
- return self
+ return self._template._run_in_new_stack_trace_context(
+ lambda: self.run_cmd(" ".join(args))
+ )
def run_cmd(
self, command: Union[str, List[str]], user: Optional[str] = None
@@ -113,32 +122,35 @@ class TemplateBuilder:
args.append(user)
instruction: Instruction = Instruction(
- type="RUN",
+ type=InstructionType.RUN,
args=args,
force=self._template._force_next_layer,
forceUpload=None,
)
self._template._instructions.append(instruction)
+ self._template._collect_stack_trace()
return self
def set_workdir(self, workdir: str) -> "TemplateBuilder":
instruction: Instruction = Instruction(
- type="WORKDIR",
+ type=InstructionType.WORKDIR,
args=[workdir],
force=self._template._force_next_layer,
forceUpload=None,
)
self._template._instructions.append(instruction)
+ self._template._collect_stack_trace()
return self
def set_user(self, user: str) -> "TemplateBuilder":
instruction: Instruction = Instruction(
- type="USER",
+ type=InstructionType.USER,
args=[user],
force=self._template._force_next_layer,
forceUpload=None,
)
self._template._instructions.append(instruction)
+ self._template._collect_stack_trace()
return self
def pip_install(
@@ -153,7 +165,9 @@ class TemplateBuilder:
else:
args.append(".")
- return self.run_cmd(" ".join(args))
+ return self._template._run_in_new_stack_trace_context(
+ lambda: self.run_cmd(" ".join(args))
+ )
def npm_install(
self,
@@ -169,18 +183,22 @@ class TemplateBuilder:
if g:
args.append("-g")
- return self.run_cmd(" ".join(args))
+ return self._template._run_in_new_stack_trace_context(
+ lambda: self.run_cmd(" ".join(args))
+ )
def apt_install(self, packages: Union[str, List[str]]) -> "TemplateBuilder":
if isinstance(packages, str):
packages = [packages]
- return self.run_cmd(
- [
- "apt-get update",
- f"DEBIAN_FRONTEND=noninteractive DEBCONF_NOWARNINGS=yes apt-get install -y --no-install-recommends {' '.join(packages)}",
- ],
- user="root",
+ return self._template._run_in_new_stack_trace_context(
+ lambda: self.run_cmd(
+ [
+ "apt-get update",
+ f"DEBIAN_FRONTEND=noninteractive DEBCONF_NOWARNINGS=yes apt-get install -y --no-install-recommends {' '.join(packages)}",
+ ],
+ user="root",
+ )
)
def git_clone(
@@ -190,25 +208,30 @@ class TemplateBuilder:
branch: Optional[str] = None,
depth: Optional[int] = None,
) -> "TemplateBuilder":
- args = ["git", "clone", url, path]
+ args = ["git", "clone", url]
+ if path:
+ args.append(path)
if branch:
args.append(f"--branch {branch}")
args.append("--single-branch")
if depth:
args.append(f"--depth {depth}")
- return self.run_cmd(" ".join(args))
+ return self._template._run_in_new_stack_trace_context(
+ lambda: self.run_cmd(" ".join(args))
+ )
def set_envs(self, envs: Dict[str, str]) -> "TemplateBuilder":
if len(envs) == 0:
return self
instruction: Instruction = Instruction(
- type="ENV",
+ type=InstructionType.ENV,
args=[item for key, value in envs.items() for item in [key, value]],
force=self._template._force_next_layer,
forceUpload=None,
)
self._template._instructions.append(instruction)
+ self._template._collect_stack_trace()
return self
def skip_cache(self) -> "TemplateBuilder":
@@ -224,6 +247,7 @@ class TemplateBuilder:
ready_cmd = ready_cmd.get_cmd()
self._template._ready_cmd = ready_cmd
+ self._template._collect_stack_trace()
return TemplateFinal(self._template)
def set_ready_cmd(self, ready_cmd: Union[str, ReadyCmd]) -> "TemplateFinal":
@@ -231,6 +255,7 @@ class TemplateBuilder:
ready_cmd = ready_cmd.get_cmd()
self._template._ready_cmd = ready_cmd
+ self._template._collect_stack_trace()
return TemplateFinal(self._template)
@@ -265,34 +290,88 @@ class TemplateBase:
self._instructions: List[Instruction] = []
# If no file_context_path is provided, use the caller's directory
self._file_context_path: str = (
- file_context_path or get_caller_directory() or "."
+ file_context_path or get_caller_directory(STACK_TRACE_DEPTH) or "."
)
self._ignore_file_paths: List[str] = ignore_file_paths or []
+ self._stack_traces: List[TracebackType] = []
+ self._stack_traces_enabled: bool = True
def skip_cache(self) -> "TemplateBase":
"""Skip cache for the next instruction (before from instruction)"""
self._force_next_layer = True
return self
+ def _collect_stack_trace(
+ self, stack_traces_depth: Optional[int] = STACK_TRACE_DEPTH
+ ) -> "TemplateBase":
+ """Collect stack trace if enabled"""
+ if not self._stack_traces_enabled:
+ return self
+
+ stack = get_caller_frame(stack_traces_depth)
+ if stack is None:
+ self._stack_traces.append(None)
+ return self
+
+ # Create a traceback object from the caller frame
+ capture_stack_trace = TracebackType(
+ tb_next=None,
+ tb_frame=stack,
+ tb_lasti=stack.f_lasti,
+ tb_lineno=stack.f_lineno,
+ )
+
+ self._stack_traces.append(capture_stack_trace)
+ return self
+
+ def _disable_stack_trace(self) -> "TemplateBase":
+ """Disable stack trace collection"""
+ self._stack_traces_enabled = False
+ return self
+
+ def _enable_stack_trace(self) -> "TemplateBase":
+ """Enable stack trace collection"""
+ self._stack_traces_enabled = True
+ return self
+
+ def _run_in_new_stack_trace_context(self, fn):
+ """Run a function in a new stack trace context"""
+ self._disable_stack_trace()
+ result = fn()
+ self._enable_stack_trace()
+ self._collect_stack_trace(STACK_TRACE_DEPTH + 1)
+ return result
+
# Built-in image mixins
def from_debian_image(self, variant: str = "slim") -> TemplateBuilder:
- return self.from_image(f"debian:{variant}")
+ return self._run_in_new_stack_trace_context(
+ lambda: self.from_image(f"debian:{variant}")
+ )
def from_ubuntu_image(self, variant: str = "lts") -> TemplateBuilder:
- return self.from_image(f"ubuntu:{variant}")
+ return self._run_in_new_stack_trace_context(
+ lambda: self.from_image(f"ubuntu:{variant}")
+ )
def from_python_image(self, version: str = "3.13") -> TemplateBuilder:
- return self.from_image(f"python:{version}")
+ return self._run_in_new_stack_trace_context(
+ lambda: self.from_image(f"python:{version}")
+ )
def from_node_image(self, variant: str = "lts") -> TemplateBuilder:
- return self.from_image(f"node:{variant}")
+ return self._run_in_new_stack_trace_context(
+ lambda: self.from_image(f"node:{variant}")
+ )
def from_base_image(self) -> TemplateBuilder:
- return self.from_image(self._default_base_image)
+ return self._run_in_new_stack_trace_context(
+ lambda: self.from_image(self._default_base_image)
+ )
def from_image(
self, base_image: str, registry_config: Optional[RegistryConfig] = None
) -> TemplateBuilder:
+ """Private method to set base image without adding stack trace"""
self._base_image = base_image
self._base_template = None
@@ -304,6 +383,7 @@ class TemplateBase:
if self._force_next_layer:
self._force = True
+ self._collect_stack_trace()
return TemplateBuilder(self)
def from_template(self, template: str) -> TemplateBuilder:
@@ -314,6 +394,7 @@ class TemplateBase:
if self._force_next_layer:
self._force = True
+ self._collect_stack_trace()
return TemplateBuilder(self)
def from_dockerfile(self, dockerfile_content_or_path: str) -> TemplateBuilder:
@@ -334,18 +415,21 @@ class TemplateBase:
if self._force_next_layer:
self._force = True
+ self._collect_stack_trace()
return builder
def from_registry(
self, image: str, username: str, password: str
) -> TemplateBuilder:
- return self.from_image(
- image,
- registry_config={
- "type": "registry",
- "username": username,
- "password": password,
- },
+ return self._run_in_new_stack_trace_context(
+ lambda: self.from_image(
+ image,
+ registry_config={
+ "type": "registry",
+ "username": username,
+ "password": password,
+ },
+ )
)
def from_aws_registry(
@@ -355,33 +439,39 @@ class TemplateBase:
secret_access_key: str,
region: str,
) -> TemplateBuilder:
- return self.from_image(
- image,
- registry_config={
- "type": "aws",
- "awsAccessKeyId": access_key_id,
- "awsSecretAccessKey": secret_access_key,
- "awsRegion": region,
- },
+ return self._run_in_new_stack_trace_context(
+ lambda: self.from_image(
+ image,
+ registry_config={
+ "type": "aws",
+ "awsAccessKeyId": access_key_id,
+ "awsSecretAccessKey": secret_access_key,
+ "awsRegion": region,
+ },
+ )
)
def from_gcp_registry(
self, image: str, service_account_json: Union[str, dict]
) -> TemplateBuilder:
- return self.from_image(
- image,
- registry_config={
- "type": "gcp",
- "serviceAccountJson": read_gcp_service_account_json(
- self._file_context_path, service_account_json
- ),
- },
+ return self._run_in_new_stack_trace_context(
+ lambda: self.from_image(
+ image,
+ registry_config={
+ "type": "gcp",
+ "serviceAccountJson": read_gcp_service_account_json(
+ self._file_context_path, service_account_json
+ ),
+ },
+ )
)
@staticmethod
def to_json(template: "TemplateClass") -> str:
return json.dumps(
- template._template._serialize(template._template._calculate_hashes()),
+ template._template._serialize(
+ template._template._instructions_with_hashes()
+ ),
indent=2,
)
@@ -399,35 +489,48 @@ class TemplateBase:
dockerfile = f"FROM {template._template._base_image}\n"
for instruction in template._template._instructions:
- dockerfile += f"{instruction['type']} {' '.join(instruction['args'])}\n"
+ dockerfile += (
+ f"{instruction['type'].value} {' '.join(instruction['args'])}\n"
+ )
if template._template._start_cmd:
dockerfile += f"ENTRYPOINT {template._template._start_cmd}\n"
return dockerfile
- def _calculate_hashes(
+ def _instructions_with_hashes(
self,
) -> List[Instruction]:
steps: List[Instruction] = []
- for instruction in self._instructions:
+ for index, instruction in enumerate(self._instructions):
step: Instruction = Instruction(
- type=instruction["type"],
+ type=instruction["type"].value,
args=instruction["args"],
force=instruction["force"],
forceUpload=instruction.get("forceUpload"),
)
- if instruction["type"] == "COPY":
+ if instruction["type"] == InstructionType.COPY:
+ stack_trace = None
+ if index + 1 < len(self._stack_traces):
+ stack_trace = self._stack_traces[index + 1]
+
+ args = instruction.get("args", [])
+ src = args[0] if len(args) > 0 else None
+ dest = args[1] if len(args) > 1 else None
+ if src is None or dest is None:
+ raise ValueError("Source path and destination path are required")
+
step["filesHash"] = calculate_files_hash(
- instruction["args"][0],
- instruction["args"][1],
+ src,
+ dest,
self._file_context_path,
[
*self._ignore_file_paths,
*read_dockerignore(self._file_context_path),
],
+ stack_trace,
)
steps.append(step)
diff --git a/packages/python-sdk/e2b/template/types.py b/packages/python-sdk/e2b/template/types.py
index 9cbbb9c64..4d787cd3a 100644
--- a/packages/python-sdk/e2b/template/types.py
+++ b/packages/python-sdk/e2b/template/types.py
@@ -3,9 +3,18 @@ from typing_extensions import NotRequired
from dataclasses import dataclass
from datetime import datetime
from typing import Literal
+from enum import Enum
from e2b.template.utils import strip_ansi_escape_codes
+class InstructionType(Enum):
+ COPY = "COPY"
+ ENV = "ENV"
+ RUN = "RUN"
+ WORKDIR = "WORKDIR"
+ USER = "USER"
+
+
class CopyItem(TypedDict):
src: str
dest: str
@@ -15,11 +24,11 @@ class CopyItem(TypedDict):
class Instruction(TypedDict):
- type: str
+ type: InstructionType
args: List[str]
force: bool
- forceUpload: Optional[bool]
- filesHash: NotRequired[str]
+ forceUpload: NotRequired[Optional[bool]]
+ filesHash: NotRequired[Optional[str]]
@dataclass
diff --git a/packages/python-sdk/e2b/template/utils.py b/packages/python-sdk/e2b/template/utils.py
index 8ba2d1de7..486c6c2d0 100644
--- a/packages/python-sdk/e2b/template/utils.py
+++ b/packages/python-sdk/e2b/template/utils.py
@@ -5,8 +5,11 @@ from glob import glob
import fnmatch
import re
import inspect
+from types import TracebackType, FrameType
from typing import List, Optional, Union
+from e2b.template.consts import BASE_STEP_NAME, FINALIZE_STEP_NAME
+
def read_dockerignore(context_path: str) -> List[str]:
dockerignore_path = os.path.join(context_path, ".dockerignore")
@@ -28,6 +31,7 @@ def calculate_files_hash(
dest: str,
context_path: str,
ignore_patterns: Optional[List[str]] = None,
+ stack_trace: Optional[TracebackType] = None,
) -> str:
src_path = os.path.join(context_path, src)
hash_obj = hashlib.sha256()
@@ -46,7 +50,7 @@ def calculate_files_hash(
files.append(file)
if len(files) == 0:
- raise ValueError(f"No files found in {src_path}")
+ raise ValueError(f"No files found in {src_path}").with_traceback(stack_trace)
for file in files:
with open(file, "rb") as f:
@@ -67,17 +71,23 @@ def strip_ansi_escape_codes(text: str) -> str:
return ansi_escape.sub("", text)
-def get_caller_directory() -> Optional[str]:
+def get_caller_frame(depth: int) -> Optional[FrameType]:
+ """Get the caller frame. Skip this function (first frame)."""
+ stack = inspect.stack()[1:]
+ if len(stack) < depth + 1:
+ return None
+ return stack[depth].frame
+
+
+def get_caller_directory(depth: int) -> Optional[str]:
"""Get the directory of the file that called this function."""
try:
# Get the stack trace
- stack = inspect.stack()
- if len(stack) < 3:
+ caller_frame = get_caller_frame(depth)
+ if caller_frame is None:
return None
- # Get the caller frame (skip this function and the immediate caller)
- caller_frame = stack[2]
- caller_file = caller_frame.filename
+ caller_file = caller_frame.f_code.co_filename
# Return the directory of the caller file
return os.path.dirname(os.path.abspath(caller_file))
@@ -89,6 +99,16 @@ def pad_octal(mode: int) -> str:
return f"{mode:04o}"
+def get_build_step_index(step: str, stack_traces_length: int) -> int:
+ if step == BASE_STEP_NAME:
+ return 0
+
+ if step == FINALIZE_STEP_NAME:
+ return stack_traces_length - 1
+
+ return int(step)
+
+
def read_gcp_service_account_json(
context_path: str, path_or_content: Union[str, dict]
) -> str:
diff --git a/packages/python-sdk/e2b/template_async/build_api.py b/packages/python-sdk/e2b/template_async/build_api.py
index a9930fa18..38b4f5cf5 100644
--- a/packages/python-sdk/e2b/template_async/build_api.py
+++ b/packages/python-sdk/e2b/template_async/build_api.py
@@ -3,7 +3,8 @@ import os
from glob import glob
import tarfile
import asyncio
-from typing import Callable, Literal, Optional
+from typing import Callable, Literal, Optional, List
+from types import TracebackType
import httpx
@@ -24,6 +25,7 @@ from e2b.api.client.models import (
)
from e2b.api import handle_api_exception
from e2b.template.exceptions import BuildException, FileUploadException
+from e2b.template.utils import get_build_step_index
async def request_build(
@@ -51,7 +53,10 @@ async def request_build(
async def get_file_upload_link(
- client: AuthenticatedClient, template_id: str, files_hash: str
+ client: AuthenticatedClient,
+ template_id: str,
+ files_hash: str,
+ stack_trace: Optional[TracebackType] = None,
) -> TemplateBuildFileUpload:
res = await get_templates_template_id_files_hash.asyncio_detailed(
template_id=template_id,
@@ -60,30 +65,53 @@ async def get_file_upload_link(
)
if res.status_code >= 300:
- raise handle_api_exception(res, FileUploadException)
+ raise handle_api_exception(res, FileUploadException, stack_trace)
if isinstance(res.parsed, Error):
- raise FileUploadException(f"API error: {res.parsed.message}")
+ raise FileUploadException(f"API error: {res.parsed.message}").with_traceback(
+ stack_trace
+ )
if res.parsed is None:
- raise FileUploadException("Failed to get file upload link")
+ raise FileUploadException("Failed to get file upload link").with_traceback(
+ stack_trace
+ )
return res.parsed
-async def upload_file(file_name: str, context_path: str, url: str):
+async def upload_file(
+ file_name: str,
+ context_path: str,
+ url: str,
+ stack_trace: Optional[TracebackType] = None,
+):
tar_buffer = io.BytesIO()
- with tarfile.open(fileobj=tar_buffer, mode="w:gz") as tar:
- src_path = os.path.join(context_path, file_name)
- files = glob(src_path, recursive=True)
- for file in files:
- arcname = os.path.relpath(file, context_path)
- tar.add(file, arcname=arcname)
+ try:
+ with tarfile.open(fileobj=tar_buffer, mode="w:gz") as tar:
+ src_path = os.path.join(context_path, file_name)
+ files = glob(src_path, recursive=True)
+ for file in files:
+ arcname = os.path.relpath(file, context_path)
+ tar.add(file, arcname=arcname)
+ except Exception as e:
+ raise FileUploadException(f"Failed to create tar file: {e}").with_traceback(
+ stack_trace
+ )
- async with httpx.AsyncClient() as client:
- response = await client.put(url, content=tar_buffer.getvalue())
- response.raise_for_status()
+ try:
+ async with httpx.AsyncClient() as client:
+ response = await client.put(url, content=tar_buffer.getvalue())
+ response.raise_for_status()
+ except httpx.HTTPStatusError as e:
+ raise FileUploadException(f"Failed to upload file: {e}").with_traceback(
+ stack_trace
+ )
+ except Exception as e:
+ raise FileUploadException(f"Failed to upload file: {e}").with_traceback(
+ stack_trace
+ )
async def trigger_build(
@@ -134,6 +162,7 @@ async def wait_for_build_finish(
build_id: str,
on_build_logs: Optional[Callable[[LogEntry], None]] = None,
logs_refresh_frequency: float = 0.2,
+ stack_traces: List[TracebackType] = [],
):
logs_offset = 0
status: Literal["building", "waiting", "ready", "error"] = "building"
@@ -164,7 +193,18 @@ async def wait_for_build_finish(
pass
elif status == "error":
- raise BuildException(build_status.reason or "Build failed")
+ traceback = None
+ if build_status.reason and build_status.reason.step:
+ # Find the corresponding stack trace for the failed step
+ step_index = get_build_step_index(
+ build_status.reason.step, len(stack_traces)
+ )
+ if step_index < len(stack_traces):
+ traceback = stack_traces[step_index]
+
+ raise BuildException(
+ build_status.reason.message if build_status.reason else "Build failed"
+ ).with_traceback(traceback)
# Wait for a short period before checking the status again
await asyncio.sleep(logs_refresh_frequency)
diff --git a/packages/python-sdk/e2b/template_async/main.py b/packages/python-sdk/e2b/template_async/main.py
index 8a02e56f6..551dab0fd 100644
--- a/packages/python-sdk/e2b/template_async/main.py
+++ b/packages/python-sdk/e2b/template_async/main.py
@@ -7,7 +7,7 @@ from datetime import datetime
from e2b.connection_config import ConnectionConfig
from e2b.api import AsyncApiClient
-from e2b.template.types import LogEntry
+from e2b.template.types import LogEntry, InstructionType
from .build_api import (
get_file_upload_link,
request_build,
@@ -73,31 +73,34 @@ class AsyncTemplate(TemplateBase):
)
)
- instructions_with_hashes = template._template._calculate_hashes()
-
- # Prepare file uploads
- file_uploads = [
- {
- "src": instruction["args"][0],
- "dest": instruction["args"][1],
- "filesHash": instruction.get("filesHash"),
- "forceUpload": instruction.get("forceUpload"),
- }
- for instruction in instructions_with_hashes
- if instruction["type"] == "COPY"
- ]
+ instructions_with_hashes = template._template._instructions_with_hashes()
# Upload files
- for file_upload in file_uploads:
+ for index, file_upload in enumerate(instructions_with_hashes):
+ if file_upload["type"] != InstructionType.COPY:
+ continue
+
+ args = file_upload.get("args", [])
+ src = args[0] if len(args) > 0 else None
+ force_upload = file_upload.get("forceUpload")
+ files_hash = file_upload.get("filesHash", None)
+
+ if src is None or files_hash is None:
+ raise ValueError("Source path and files hash are required")
+
+ stack_trace = None
+ if index + 1 < len(template._template._stack_traces):
+ stack_trace = template._template._stack_traces[index + 1]
+
file_info = await get_file_upload_link(
- api_client, template_id, file_upload["filesHash"]
+ api_client, template_id, files_hash, stack_trace
)
- if (file_upload["forceUpload"] and file_info.url) or (
+ if (force_upload and file_info.url) or (
file_info.present is False and file_info.url
):
await upload_file(
- file_upload["src"],
+ src,
template._template._file_context_path,
file_info.url,
)
@@ -106,7 +109,7 @@ class AsyncTemplate(TemplateBase):
LogEntry(
timestamp=datetime.now(),
level="info",
- message=f"Uploaded '{file_upload['src']}'",
+ message=f"Uploaded '{src}'",
)
)
else:
@@ -115,7 +118,7 @@ class AsyncTemplate(TemplateBase):
LogEntry(
timestamp=datetime.now(),
level="info",
- message=f"Skipping upload of '{file_upload['src']}', already cached",
+ message=f"Skipping upload of '{src}', already cached",
)
)
@@ -160,4 +163,5 @@ class AsyncTemplate(TemplateBase):
build_id,
on_build_logs,
logs_refresh_frequency=TemplateBase._logs_refresh_frequency,
+ stack_traces=template._template._stack_traces,
)
diff --git a/packages/python-sdk/e2b/template_sync/build_api.py b/packages/python-sdk/e2b/template_sync/build_api.py
index cec5c08f1..cd8f52882 100644
--- a/packages/python-sdk/e2b/template_sync/build_api.py
+++ b/packages/python-sdk/e2b/template_sync/build_api.py
@@ -3,7 +3,8 @@ import os
from glob import glob
import tarfile
import time
-from typing import Callable, Literal, Optional
+from typing import Callable, Literal, Optional, List
+from types import TracebackType
import httpx
@@ -24,6 +25,7 @@ from e2b.api.client.models import (
)
from e2b.api import handle_api_exception
from e2b.template.exceptions import BuildException, FileUploadException
+from e2b.template.utils import get_build_step_index
def request_build(
@@ -51,7 +53,10 @@ def request_build(
def get_file_upload_link(
- client: AuthenticatedClient, template_id: str, files_hash: str
+ client: AuthenticatedClient,
+ template_id: str,
+ files_hash: str,
+ stack_trace: Optional[TracebackType] = None,
) -> TemplateBuildFileUpload:
res = get_templates_template_id_files_hash.sync_detailed(
template_id=template_id,
@@ -60,30 +65,53 @@ def get_file_upload_link(
)
if res.status_code >= 300:
- raise handle_api_exception(res, FileUploadException)
+ raise handle_api_exception(res, FileUploadException, stack_trace)
if isinstance(res.parsed, Error):
- raise FileUploadException(f"API error: {res.parsed.message}")
+ raise FileUploadException(f"API error: {res.parsed.message}").with_traceback(
+ stack_trace
+ )
if res.parsed is None:
- raise FileUploadException("Failed to get file upload link")
+ raise FileUploadException("Failed to get file upload link").with_traceback(
+ stack_trace
+ )
return res.parsed
-def upload_file(file_name: str, context_path: str, url: str):
+def upload_file(
+ file_name: str,
+ context_path: str,
+ url: str,
+ stack_trace: Optional[TracebackType] = None,
+):
tar_buffer = io.BytesIO()
- with tarfile.open(fileobj=tar_buffer, mode="w:gz") as tar:
- src_path = os.path.join(context_path, file_name)
- files = glob(src_path, recursive=True)
- for file in files:
- arcname = os.path.relpath(file, context_path)
- tar.add(file, arcname=arcname)
-
- with httpx.Client() as client:
- response = client.put(url, content=tar_buffer.getvalue())
- response.raise_for_status()
+ try:
+ with tarfile.open(fileobj=tar_buffer, mode="w:gz") as tar:
+ src_path = os.path.join(context_path, file_name)
+ files = glob(src_path, recursive=True)
+ for file in files:
+ arcname = os.path.relpath(file, context_path)
+ tar.add(file, arcname=arcname)
+ except Exception as e:
+ raise FileUploadException(f"Failed to create tar file: {e}").with_traceback(
+ stack_trace
+ )
+
+ try:
+ with httpx.Client() as client:
+ response = client.put(url, content=tar_buffer.getvalue())
+ response.raise_for_status()
+ except httpx.HTTPStatusError as e:
+ raise FileUploadException(f"Failed to upload file: {e}").with_traceback(
+ stack_trace
+ )
+ except Exception as e:
+ raise FileUploadException(f"Failed to upload file: {e}").with_traceback(
+ stack_trace
+ )
def trigger_build(
@@ -134,6 +162,7 @@ def wait_for_build_finish(
build_id: str,
on_build_logs: Optional[Callable[[LogEntry], None]] = None,
logs_refresh_frequency: float = 0.2,
+ stack_traces: List[TracebackType] = [],
):
logs_offset = 0
status: Literal["building", "waiting", "ready", "error"] = "building"
@@ -162,7 +191,18 @@ def wait_for_build_finish(
pass
elif status == "error":
- raise BuildException(build_status.reason or "Build failed")
+ traceback = None
+ if build_status.reason and build_status.reason.step:
+ # Find the corresponding stack trace for the failed step
+ step_index = get_build_step_index(
+ build_status.reason.step, len(stack_traces)
+ )
+ if step_index < len(stack_traces):
+ traceback = stack_traces[step_index]
+
+ raise BuildException(
+ build_status.reason.message if build_status.reason else "Build failed"
+ ).with_traceback(traceback)
# Wait for a short period before checking the status again
time.sleep(logs_refresh_frequency)
diff --git a/packages/python-sdk/e2b/template_sync/main.py b/packages/python-sdk/e2b/template_sync/main.py
index 7d69d960a..c38988e1d 100644
--- a/packages/python-sdk/e2b/template_sync/main.py
+++ b/packages/python-sdk/e2b/template_sync/main.py
@@ -1,7 +1,7 @@
from typing import Callable, Optional
from e2b.template.main import TemplateBase, TemplateClass
-from e2b.template.types import LogEntry
+from e2b.template.types import LogEntry, InstructionType
import os
from datetime import datetime
@@ -73,31 +73,34 @@ class Template(TemplateBase):
)
)
- instructions_with_hashes = template._template._calculate_hashes()
-
- # Prepare file uploads
- file_uploads = [
- {
- "src": instruction["args"][0],
- "dest": instruction["args"][1],
- "filesHash": instruction.get("filesHash"),
- "forceUpload": instruction.get("forceUpload"),
- }
- for instruction in instructions_with_hashes
- if instruction["type"] == "COPY"
- ]
+ instructions_with_hashes = template._template._instructions_with_hashes()
# Upload files
- for file_upload in file_uploads:
+ for index, file_upload in enumerate(instructions_with_hashes):
+ if file_upload["type"] != InstructionType.COPY:
+ continue
+
+ args = file_upload.get("args", [])
+ src = args[0] if len(args) > 0 else None
+ force_upload = file_upload.get("forceUpload")
+ files_hash = file_upload.get("filesHash", None)
+
+ if src is None or files_hash is None:
+ raise ValueError("Source path and files hash are required")
+
+ stack_trace = None
+ if index + 1 < len(template._template._stack_traces):
+ stack_trace = template._template._stack_traces[index + 1]
+
file_info = get_file_upload_link(
- api_client, template_id, file_upload["filesHash"]
+ api_client, template_id, files_hash, stack_trace
)
- if (file_upload["forceUpload"] and file_info.url) or (
+ if (force_upload and file_info.url) or (
file_info.present is False and file_info.url
):
upload_file(
- file_upload["src"],
+ src,
template._template._file_context_path,
file_info.url,
)
@@ -106,7 +109,7 @@ class Template(TemplateBase):
LogEntry(
timestamp=datetime.now(),
level="info",
- message=f"Uploaded '{file_upload['src']}'",
+ message=f"Uploaded '{src}'",
)
)
else:
@@ -115,7 +118,7 @@ class Template(TemplateBase):
LogEntry(
timestamp=datetime.now(),
level="info",
- message=f"Skipping upload of '{file_upload['src']}', already cached",
+ message=f"Skipping upload of '{src}', already cached",
)
)
@@ -160,4 +163,5 @@ class Template(TemplateBase):
build_id,
on_build_logs,
logs_refresh_frequency=TemplateBase._logs_refresh_frequency,
+ stack_traces=template._template._stack_traces,
)
diff --git a/packages/python-sdk/tests/async/template_async/test_stacktrace.py b/packages/python-sdk/tests/async/template_async/test_stacktrace.py
new file mode 100644
index 000000000..20df08825
--- /dev/null
+++ b/packages/python-sdk/tests/async/template_async/test_stacktrace.py
@@ -0,0 +1,198 @@
+import pytest
+from uuid import uuid4
+import linecache
+
+from e2b import AsyncTemplate, wait_for_timeout, TemplateClass
+
+non_existent_path = "/nonexistent/path"
+
+
+async def build(template: TemplateClass):
+ return await AsyncTemplate.build(
+ template,
+ alias=str(uuid4()),
+ cpu_count=1,
+ memory_mb=1024,
+ )
+
+
+async def _expect_to_throw_and_check_trace(func, expected_method: str):
+ try:
+ await func()
+ assert False, "Expected AsyncTemplate.build to raise an exception"
+ except Exception as e: # noqa: BLE001 - we want to assert on the traceback regardless of type
+ tb = e.__traceback__
+ saw_this_file = False
+ saw_expected_method = False
+ while tb is not None:
+ traceback_file = tb.tb_frame.f_code.co_filename
+ if traceback_file == __file__:
+ saw_this_file = True
+ caller_line = linecache.getline(traceback_file, tb.tb_lineno)
+ if caller_line and f".{expected_method}(" in caller_line:
+ saw_expected_method = True
+ break
+ tb = tb.tb_next
+ assert saw_this_file
+ assert saw_expected_method
+
+
+async def test_traces_on_from_image():
+ template = AsyncTemplate()
+ template = template.from_image("e2b.dev/this-image-does-not-exist")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "from_image")
+
+
+# @pytest.mark.skip_debug()
+# async def test_traces_on_from_template():
+# template = AsyncTemplate().from_template("this-template-does-not-exist")
+# await _expect_to_throw_and_check_trace(lambda: build(template), "from_template")
+
+
+async def test_traces_on_from_dockerfile():
+ template = AsyncTemplate()
+ template = template.from_dockerfile("FROM ubuntu:22.04\nRUN nonexistent")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "from_dockerfile")
+
+
+async def test_traces_on_from_registry():
+ template = AsyncTemplate()
+ template = template.from_registry(
+ "registry.example.com/nonexistent:latest",
+ username="test",
+ password="test",
+ )
+ await _expect_to_throw_and_check_trace(lambda: build(template), "from_registry")
+
+
+async def test_traces_on_from_aws_registry():
+ template = AsyncTemplate()
+ template = template.from_aws_registry(
+ "123456789.dkr.ecr.us-east-1.amazonaws.com/nonexistent:latest",
+ access_key_id="test",
+ secret_access_key="test",
+ region="us-east-1",
+ )
+ await _expect_to_throw_and_check_trace(lambda: build(template), "from_aws_registry")
+
+
+async def test_traces_on_from_gcp_registry():
+ template = AsyncTemplate()
+ template = template.from_gcp_registry(
+ "gcr.io/nonexistent-project/nonexistent:latest",
+ service_account_json={
+ "type": "service_account",
+ },
+ )
+ await _expect_to_throw_and_check_trace(lambda: build(template), "from_gcp_registry")
+
+
+async def test_traces_on_copy():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.copy(non_existent_path, non_existent_path)
+ await _expect_to_throw_and_check_trace(lambda: build(template), "copy")
+
+
+async def test_traces_on_remove():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.remove(non_existent_path)
+ await _expect_to_throw_and_check_trace(lambda: build(template), "remove")
+
+
+async def test_traces_on_rename():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.rename(non_existent_path, "/tmp/dest.txt")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "rename")
+
+
+async def test_traces_on_make_dir():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.make_dir(".bashrc")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "make_dir")
+
+
+async def test_traces_on_make_symlink():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.make_symlink(".bashrc", ".bashrc")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "make_symlink")
+
+
+async def test_traces_on_run_cmd():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.run_cmd(f"cat {non_existent_path}")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "run_cmd")
+
+
+async def test_traces_on_set_workdir():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.set_workdir(".bashrc")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "set_workdir")
+
+
+async def test_traces_on_set_user():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.set_user(";")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "set_user")
+
+
+async def test_traces_on_pip_install():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.pip_install("nonexistent-package")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "pip_install")
+
+
+async def test_traces_on_npm_install():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.npm_install("nonexistent-package")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "npm_install")
+
+
+async def test_traces_on_apt_install():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.apt_install("nonexistent-package")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "apt_install")
+
+
+async def test_traces_on_git_clone():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.git_clone("https://github.com/repo.git")
+ await _expect_to_throw_and_check_trace(lambda: build(template), "git_clone")
+
+
+async def test_traces_on_set_start_cmd():
+ template = AsyncTemplate()
+ template = template.from_base_image()
+ template = template.set_start_cmd(
+ f"./{non_existent_path}", wait_for_timeout(10_000)
+ )
+ await _expect_to_throw_and_check_trace(lambda: build(template), "set_start_cmd")
diff --git a/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py b/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py
new file mode 100644
index 000000000..a9b40cef8
--- /dev/null
+++ b/packages/python-sdk/tests/sync/template_sync/test_stacktrace.py
@@ -0,0 +1,198 @@
+import pytest
+from uuid import uuid4
+import linecache
+
+from e2b import Template, wait_for_timeout, TemplateClass
+
+non_existent_path = "/nonexistent/path"
+
+
+def build(template: TemplateClass):
+ return Template.build(
+ template,
+ alias=str(uuid4()),
+ cpu_count=1,
+ memory_mb=1024,
+ )
+
+
+def _expect_to_throw_and_check_trace(func, expected_method: str):
+ try:
+ func()
+ assert False, "Expected Template.build to raise an exception"
+ except Exception as e: # noqa: BLE001 - we want to assert on the traceback regardless of type
+ tb = e.__traceback__
+ saw_this_file = False
+ saw_expected_method = False
+ while tb is not None:
+ traceback_file = tb.tb_frame.f_code.co_filename
+ if traceback_file == __file__:
+ saw_this_file = True
+ caller_line = linecache.getline(traceback_file, tb.tb_lineno)
+ if caller_line and f".{expected_method}(" in caller_line:
+ saw_expected_method = True
+ break
+ tb = tb.tb_next
+ assert saw_this_file
+ assert saw_expected_method
+
+
+def test_traces_on_from_image():
+ template = Template()
+ template = template.from_image("e2b.dev/this-image-does-not-exist")
+ _expect_to_throw_and_check_trace(lambda: build(template), "from_image")
+
+
+# @pytest.mark.skip_debug()
+# def test_traces_on_from_template():
+# template = Template().from_template("this-template-does-not-exist")
+# _expect_to_throw_and_check_trace(lambda: build(template), "from_template")
+
+
+def test_traces_on_from_dockerfile():
+ template = Template()
+ template = template.from_dockerfile("FROM ubuntu:22.04\nRUN nonexistent")
+ _expect_to_throw_and_check_trace(lambda: build(template), "from_dockerfile")
+
+
+def test_traces_on_from_registry():
+ template = Template()
+ template = template.from_registry(
+ "registry.example.com/nonexistent:latest",
+ username="test",
+ password="test",
+ )
+ _expect_to_throw_and_check_trace(lambda: build(template), "from_registry")
+
+
+def test_traces_on_from_aws_registry():
+ template = Template()
+ template = template.from_aws_registry(
+ "123456789.dkr.ecr.us-east-1.amazonaws.com/nonexistent:latest",
+ access_key_id="test",
+ secret_access_key="test",
+ region="us-east-1",
+ )
+ _expect_to_throw_and_check_trace(lambda: build(template), "from_aws_registry")
+
+
+def test_traces_on_from_gcp_registry():
+ template = Template()
+ template = template.from_gcp_registry(
+ "gcr.io/nonexistent-project/nonexistent:latest",
+ service_account_json={
+ "type": "service_account",
+ },
+ )
+ _expect_to_throw_and_check_trace(lambda: build(template), "from_gcp_registry")
+
+
+def test_traces_on_copy():
+ template = Template()
+ template = template.from_base_image()
+ template = template.copy(non_existent_path, non_existent_path)
+ _expect_to_throw_and_check_trace(lambda: build(template), "copy")
+
+
+def test_traces_on_remove():
+ template = Template()
+ template = template.from_base_image()
+ template = template.remove(non_existent_path)
+ _expect_to_throw_and_check_trace(lambda: build(template), "remove")
+
+
+def test_traces_on_rename():
+ template = Template()
+ template = template.from_base_image()
+ template = template.rename(non_existent_path, "/tmp/dest.txt")
+ _expect_to_throw_and_check_trace(lambda: build(template), "rename")
+
+
+def test_traces_on_make_dir():
+ template = Template()
+ template = template.from_base_image()
+ template = template.make_dir(".bashrc")
+ _expect_to_throw_and_check_trace(lambda: build(template), "make_dir")
+
+
+def test_traces_on_make_symlink():
+ template = Template()
+ template = template.from_base_image()
+ template = template.make_symlink(".bashrc", ".bashrc")
+ _expect_to_throw_and_check_trace(lambda: build(template), "make_symlink")
+
+
+def test_traces_on_run_cmd():
+ template = Template()
+ template = template.from_base_image()
+ template = template.run_cmd(f"cat {non_existent_path}")
+ _expect_to_throw_and_check_trace(lambda: build(template), "run_cmd")
+
+
+def test_traces_on_set_workdir():
+ template = Template()
+ template = template.from_base_image()
+ template = template.set_workdir(".bashrc")
+ _expect_to_throw_and_check_trace(lambda: build(template), "set_workdir")
+
+
+def test_traces_on_set_user():
+ template = Template()
+ template = template.from_base_image()
+ template = template.set_user(";")
+ _expect_to_throw_and_check_trace(lambda: build(template), "set_user")
+
+
+def test_traces_on_pip_install():
+ template = Template()
+ template = template.from_base_image()
+ template = template.pip_install("nonexistent-package")
+ _expect_to_throw_and_check_trace(lambda: build(template), "pip_install")
+
+
+def test_traces_on_npm_install():
+ template = Template()
+ template = template.from_base_image()
+ template = template.npm_install("nonexistent-package")
+ _expect_to_throw_and_check_trace(lambda: build(template), "npm_install")
+
+
+def test_traces_on_apt_install():
+ template = Template()
+ template = template.from_base_image()
+ template = template.apt_install("nonexistent-package")
+ _expect_to_throw_and_check_trace(lambda: build(template), "apt_install")
+
+
+def test_traces_on_git_clone():
+ template = Template()
+ template = template.from_base_image()
+ template = template.git_clone("https://github.com/repo.git")
+ _expect_to_throw_and_check_trace(lambda: build(template), "git_clone")
+
+
+def test_traces_on_set_start_cmd():
+ template = Template()
+ template = template.from_base_image()
+ template = template.set_start_cmd(
+ f"./{non_existent_path}", wait_for_timeout(10_000)
+ )
+ _expect_to_throw_and_check_trace(lambda: build(template), "set_start_cmd")