-
Notifications
You must be signed in to change notification settings - Fork 5k
error: don't double source-map rethrown frames after reading .stack #36726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+135
−8
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/15859 | ||
| // | ||
| // Reading `error.stack` before rethrowing caused the uncaught-exception printer | ||
| // to show wrong line numbers for non-top frames (double source-mapped) and to | ||
| // drop frames without a function name. | ||
|
|
||
| const fixture = `import * as i1 from "util"; | ||
| import * as i2 from "util"; | ||
| import * as i3 from "util"; | ||
| function err() { | ||
| throw new Error() | ||
| }; | ||
| function f1(){ | ||
| err() | ||
| } | ||
| function f2(){ | ||
|
|
||
| } | ||
| try { | ||
| f1(); | ||
| } catch (error: any) { | ||
| let x = error.stack | ||
| throw error | ||
| } | ||
| `; | ||
|
|
||
| test("uncaught exception frames are not double source-mapped after reading error.stack", async () => { | ||
| using dir = tempDir("issue-15859", { | ||
| "test.ts": fixture, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "test.ts"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stdout).toBe(""); | ||
| // f1's body calls err() on line 8. Before the fix this printed line 13 | ||
| // (source-mapped twice). | ||
| expect(stderr).toContain("at f1 "); | ||
| expect(stderr).toMatch(/at f1 \(.*test\.ts:8:5\)/); | ||
| expect(stderr).not.toMatch(/at f1 \(.*test\.ts:13:/); | ||
| // err() throws on line 5; this frame was already correct (top frame). | ||
| expect(stderr).toMatch(/at err \(.*test\.ts:5:/); | ||
| expect(exitCode).toBe(1); | ||
| }); | ||
|
|
||
| test("uncaught exception printer keeps the anonymous top-level frame after reading error.stack", async () => { | ||
| using dir = tempDir("issue-15859-anon", { | ||
| "test.ts": fixture, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "test.ts"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| expect(stdout).toBe(""); | ||
| // The try block calls f1() on line 14 from module scope (no function name). | ||
| // Before the fix this frame was dropped entirely because the .stack string | ||
| // parser could not handle "at <url>:<line>:<col>" frames. | ||
| expect(stderr).toMatch(/at .*test\.ts:14:5/); | ||
| expect(exitCode).toBe(1); | ||
| }); | ||
|
|
||
| test("uncaught exception frames match error.stack after reading it", async () => { | ||
| using dir = tempDir("issue-15859-match", { | ||
| "test.ts": `import * as i1 from "util"; | ||
| import * as i2 from "util"; | ||
| import * as i3 from "util"; | ||
| function err() { | ||
| throw new Error() | ||
| }; | ||
| function f1(){ | ||
| err() | ||
| } | ||
| function f2(){ | ||
|
|
||
| } | ||
| try { | ||
| f1(); | ||
| } catch (error: any) { | ||
| console.log(error.stack) | ||
| throw error | ||
| } | ||
| `, | ||
| }); | ||
|
|
||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "test.ts"], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
|
|
||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
|
|
||
| const extract = (s: string) => [...s.matchAll(/test\.ts:(\d+):(\d+)/g)].map(m => `${m[1]}:${m[2]}`); | ||
|
|
||
| const stackPositions = extract(stdout); | ||
| const printedPositions = extract(stderr); | ||
|
|
||
| expect(stackPositions.length).toBeGreaterThanOrEqual(3); | ||
| expect(printedPositions).toEqual(stackPositions); | ||
| expect(exitCode).toBe(1); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code