Skip to content

armgit / E2B

Public Imported from GitHub
0

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

Windows: Update context directory regex to match Windows-style file patterns (#1014)

Mish Ushakov470de917ad44
diff --git a/.changeset/every-wolves-brake.md b/.changeset/every-wolves-brake.md
new file mode 100644
index 000000000..ffcb9227b
--- /dev/null
+++ b/.changeset/every-wolves-brake.md
@@ -0,0 +1,5 @@
+---
+'e2b': patch
+---
+
+fixes default context directory for windows paths
diff --git a/packages/js-sdk/src/template/utils.ts b/packages/js-sdk/src/template/utils.ts
index 4ab359465..6bcf5ccd9 100644
--- a/packages/js-sdk/src/template/utils.ts
+++ b/packages/js-sdk/src/template/utils.ts
@@ -182,14 +182,28 @@ export function getCallerFrame(depth: number): string | undefined {
* Matches patterns like:
* - "at <anonymous> (/path/to/file.js:1:1)"
* - "at /path/to/file.js:1:1"
- *
+ * - "at <anonymous> (file:///C:/path/to/file.js:1:1)"
+ * - "at (file:///C:/path/to/file.js:1:1)"
* @param line A line from a stack trace
* @returns The directory of the file, or undefined if not found
*/
export function matchFileDir(line: string): string | undefined {
- const match = line.match(/\/[^:]+/)
+ const match = line.match(
+ /(?:file:\/\/\/)?([A-Za-z]:)?([/\\][^:]+)(?::\d+:\d+)?\)?/
+ )
if (match) {
- const filePath = match[0]
+ // Extract the full matched path
+ let filePath = match[0]
+
+ // Remove file:/// protocol prefix if present
+ filePath = filePath.replace(/^file:\/\/\//, '')
+
+ // Remove trailing closing parenthesis if present
+ filePath = filePath.replace(/\)$/, '')
+
+ // Remove :line:column suffix if present
+ filePath = filePath.replace(/:\d+:\d+$/, '')
+
return path.dirname(filePath)
}
}
diff --git a/packages/js-sdk/tests/template/utils/matchFileDir.test.ts b/packages/js-sdk/tests/template/utils/matchFileDir.test.ts
index e11a9e2b7..b410da911 100644
--- a/packages/js-sdk/tests/template/utils/matchFileDir.test.ts
+++ b/packages/js-sdk/tests/template/utils/matchFileDir.test.ts
@@ -1,6 +1,7 @@
import { expect, test } from 'vitest'
import { matchFileDir } from '../../../src/template/utils'
+// Basic Unix/Linux paths
test('matchFileDir', () => {
expect(matchFileDir('at /path/to/file.js:1:1')).toBe('/path/to')
})
@@ -8,3 +9,70 @@ test('matchFileDir', () => {
test('matchFileDir (anonymous)', () => {
expect(matchFileDir('at <anonymous> (/path/to/file.js:1:1)')).toBe('/path/to')
})
+
+test('matchFileDir Unix path with spaces', () => {
+ expect(matchFileDir('at /home/user/my project/file.js:1:1')).toBe(
+ '/home/user/my project'
+ )
+})
+
+test('matchFileDir Unix path with special characters', () => {
+ expect(matchFileDir('at /path/to-my_dir.test/file.js:1:1')).toBe(
+ '/path/to-my_dir.test'
+ )
+})
+
+test('matchFileDir Unix root directory', () => {
+ expect(matchFileDir('at /file.js:1:1')).toBe('/')
+})
+
+test('matchFileDir Unix path with @symbol (scoped packages)', () => {
+ expect(matchFileDir('at /node_modules/@scope/package/index.js:1:1')).toBe(
+ '/node_modules/@scope/package'
+ )
+})
+
+test('matchFileDir Unix path with brackets (Next.js dynamic routes)', () => {
+ expect(matchFileDir('at /app/routes/[id]/page.js:1:1')).toBe(
+ '/app/routes/[id]'
+ )
+})
+
+// Basic Windows paths (forward slash)
+test('matchFileDir Windows path', () => {
+ expect(matchFileDir('at C:/path/to/file.js:1:1')).toBe('C:/path/to')
+})
+
+test('matchFileDir Windows path (anonymous)', () => {
+ expect(matchFileDir('at <anonymous> (C:/path/to/file.js:1:1)')).toBe(
+ 'C:/path/to'
+ )
+})
+
+test('matchFileDir Windows path with spaces', () => {
+ expect(matchFileDir('at C:/Program Files/App/index.js:1:1')).toBe(
+ 'C:/Program Files/App'
+ )
+})
+
+// File protocol paths
+test('matchFileDir file:// protocol Windows', () => {
+ expect(matchFileDir('at <anonymous> (file:///C:/path/to/file.js:1:1)')).toBe(
+ 'C:/path/to'
+ )
+})
+
+test('matchFileDir file:// protocol Windows (no anonymous)', () => {
+ expect(matchFileDir('at (file:///C:/path/to/file.js:1:1)')).toBe('C:/path/to')
+})
+
+// Paths with parentheses
+test('matchFileDir with parentheses in path', () => {
+ expect(matchFileDir('at /path/to(1)/file.js:1:1')).toBe('/path/to(1)')
+})
+
+test('matchFileDir with parentheses in path (anonymous)', () => {
+ expect(matchFileDir('at <anonymous> (/path/to(1)/file.js:1:1)')).toBe(
+ '/path/to(1)'
+ )
+})