Skip to content

Commit c40eeaa

Browse files
mishushakovdobrac
andauthored
Improve regex for fileContextPath (#939)
<!-- CURSOR_SUMMARY --> > [!NOTE] > Improve stack-trace path parsing for caller directory and add JS/Python tests; publish as patch. > > - **Template utils (JS SDK)**: > - Add `matchFileDir` to robustly extract file directory from stack-trace lines (handles anonymous frames). > - Refactor `getCallerDirectory` to use `matchFileDir` for path parsing. > - **Tests**: > - Add JS tests for `getCallerDirectory` and `matchFileDir`. > - Add Python async/sync tests for `get_caller_directory`. > - **Release**: > - Add changeset marking a patch for `e2b`. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 6bd8aa7. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Jakub Dobry <jakub.dobry8@gmail.com>
1 parent 5b7b3f6 commit c40eeaa

6 files changed

Lines changed: 52 additions & 8 deletions

File tree

.changeset/metal-horses-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'e2b': patch
3+
---
4+
5+
regex path for fileContextPath

packages/js-sdk/src/template/utils.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ export function getCallerFrame(depth: number): string | undefined {
111111
return lines.slice(depth).join('\n')
112112
}
113113

114+
/**
115+
* Matches paths like "at <anonymous> (/path/to/file.js:1:1)" or "at /path/to/file.js:1:1"
116+
* @param line - The line to match
117+
* @returns The directory of the file
118+
*/
119+
export function matchFileDir(line: string): string | undefined {
120+
const match = line.match(/\/[^:]+/)
121+
if (match) {
122+
const filePath = match[0]
123+
return path.dirname(filePath)
124+
}
125+
}
126+
114127
/**
115128
* Get the caller directory
116129
* @returns The caller directory
@@ -125,15 +138,9 @@ export function getCallerDirectory(depth: number): string | undefined {
125138
if (lines.length === 0) {
126139
return
127140
}
128-
const firstLine = lines[0]
129-
130-
const match = firstLine.match(/at ([^:]+):\d+:\d+/)
131-
if (match) {
132-
const filePath = match[1]
133-
return path.dirname(filePath)
134-
}
135141

136-
return
142+
const firstLine = lines[0]
143+
return matchFileDir(firstLine)
137144
}
138145

139146
export function padOctal(mode: number): string {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { expect, test } from 'vitest'
2+
import { getCallerDirectory } from '../../../src/template/utils'
3+
4+
test('getCallerDirectory', () => {
5+
// getCallerDirectory(1) should return the directory of the current file
6+
// __dirname is the directory of the current file
7+
expect(getCallerDirectory(1)).toBe(__dirname)
8+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expect, test } from 'vitest'
2+
import { matchFileDir } from '../../../src/template/utils'
3+
4+
test('matchFileDir', () => {
5+
expect(matchFileDir('at /path/to/file.js:1:1')).toBe('/path/to')
6+
})
7+
8+
test('matchFileDir (anonymous)', () => {
9+
expect(matchFileDir('at <anonymous> (/path/to/file.js:1:1)')).toBe('/path/to')
10+
})
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
from e2b.template.utils import get_caller_directory
3+
import os
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_get_caller_directory():
8+
assert get_caller_directory(1) == os.path.dirname(__file__)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import os
2+
from e2b.template.utils import get_caller_directory
3+
4+
5+
def test_get_caller_directory():
6+
assert get_caller_directory(1) == os.path.dirname(__file__)

0 commit comments

Comments
 (0)