Repository navigation
error printer: show file/line for JSC parser SyntaxErrors with no stack #36634
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
Open
robobun
wants to merge
4
commits into
main
Choose a base branch
from
farm/21e500e1/fix-5192-syntaxerror-location
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8e400fe
error printer: show file/line for JSC parser SyntaxErrors with no stack
robobun 2001260
[autofix.ci] apply automated fixes
autofix-ci[bot] 53e3fe6
extract Bun__remapParseErrorFrame helper; trim comments
robobun 02618d2
trim comment blocks to single lines (comment-cop)
robobun 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
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,153 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| // https://github.com/oven-sh/bun/issues/5192 | ||
| // | ||
| // When JSC (not Bun's own parser) rejects a module with a SyntaxError, the | ||
| // ErrorInstance carries line/sourceURL on its C++ fields but those were never | ||
| // surfaced because the error has no JS stack at module-parse time. The error | ||
| // printed as a bare "SyntaxError: ..." with no file, line, or code frame. | ||
|
|
||
| async function run(files: Record<string, string>, entry: string) { | ||
| using dir = tempDir("issue-5192", files); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), entry], | ||
| env: { ...bunEnv, NO_COLOR: "1" }, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout, stderr, exitCode }; | ||
| } | ||
|
|
||
| // `with` in strict mode is caught by JSC's parser, not Bun's, so it exercises | ||
| // the JSC ParserError path directly. | ||
| const withInStrict = `"use strict"; | ||
| export const x = 1; | ||
| function foo() { | ||
| with ({ a: 1 }) { | ||
| console.log(a); | ||
| } | ||
| } | ||
| foo(); | ||
| `; | ||
|
|
||
| test.concurrent("JSC parse SyntaxError in the entry module prints file and line", async () => { | ||
| const { stderr, exitCode } = await run({ "entry.mjs": withInStrict }, "entry.mjs"); | ||
| expect(stderr).toContain("SyntaxError: 'with' statements are not valid in strict mode."); | ||
| // File path with a line number must appear somewhere in the output. | ||
| expect(stderr).toMatch(/entry\.mjs:\d+/); | ||
| // A code frame with the `with` line must appear. | ||
| expect(stderr).toMatch(/\bwith\b.*\{ a: 1 \}/); | ||
| expect(exitCode).toBe(1); | ||
| }); | ||
|
|
||
| test.concurrent("JSC parse SyntaxError in an imported ESM module prints file and line", async () => { | ||
| const { stderr, exitCode } = await run( | ||
| { | ||
| "module.mjs": withInStrict, | ||
| "entry.ts": `import "./module.mjs";`, | ||
| }, | ||
| "entry.ts", | ||
| ); | ||
| expect(stderr).toContain("SyntaxError: 'with' statements are not valid in strict mode."); | ||
| expect(stderr).toMatch(/module\.mjs:\d+/); | ||
| expect(stderr).toMatch(/\bwith\b.*\{ a: 1 \}/); | ||
| expect(exitCode).toBe(1); | ||
| }); | ||
|
|
||
| test.concurrent("JSC parse SyntaxError in an imported .ts module prints file and line", async () => { | ||
| const { stderr, exitCode } = await run( | ||
| { | ||
| "module.ts": withInStrict, | ||
| "entry.ts": `import "./module.ts";`, | ||
| }, | ||
| "entry.ts", | ||
| ); | ||
| expect(stderr).toContain("SyntaxError: 'with' statements are not valid in strict mode."); | ||
| expect(stderr).toMatch(/module\.ts:\d+/); | ||
| expect(stderr).toMatch(/\bwith\b.*\{ a: 1 \}/); | ||
| expect(exitCode).toBe(1); | ||
| }); | ||
|
|
||
| test.concurrent("JSC parse SyntaxError: 'delete x' in strict mode prints file and line", async () => { | ||
| const { stderr, exitCode } = await run( | ||
| { | ||
| "module.mjs": `export const x = 1; | ||
| function fn() { | ||
| delete someVar; | ||
| } | ||
| fn(); | ||
| `, | ||
| "entry.mjs": `import "./module.mjs";`, | ||
| }, | ||
| "entry.mjs", | ||
| ); | ||
| expect(stderr).toContain("SyntaxError: Cannot delete unqualified property 'someVar' in strict mode."); | ||
| expect(stderr).toMatch(/module\.mjs:\d+/); | ||
| expect(exitCode).toBe(1); | ||
| }); | ||
|
|
||
| test.concurrent("JSC parse SyntaxError from dynamic import() prints file and line when re-thrown", async () => { | ||
| const { stderr, exitCode } = await run( | ||
| { | ||
| "module.mjs": withInStrict, | ||
| "entry.mjs": `try { | ||
| await import("./module.mjs"); | ||
| } catch (e) { | ||
| console.error(e); | ||
| } | ||
| `, | ||
| }, | ||
| "entry.mjs", | ||
| ); | ||
| expect(stderr).toContain("SyntaxError: 'with' statements are not valid in strict mode."); | ||
| expect(stderr).toMatch(/module\.mjs:\d+/); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("JSC parse SyntaxError in a require()'d CJS module prints the offending line", async () => { | ||
| // require() has a JS stack so the `<parse>` frame comes from the .stack | ||
| // formatter rather than the error printer's synthetic frame; both paths had | ||
| // the same column-0 source-map lookup bug that resolved to the previous line. | ||
| const { stderr, exitCode } = await run( | ||
| { | ||
| "mod.cjs": `"use strict"; | ||
| with ({ a: 1 }) { console.log(a); } | ||
| `, | ||
| "main.cjs": `require("./mod.cjs");`, | ||
| }, | ||
| "main.cjs", | ||
| ); | ||
| expect(stderr).toContain("SyntaxError: 'with' statements are not valid in strict mode."); | ||
| expect(stderr).toMatch(/mod\.cjs:2\b/); | ||
| expect(stderr).toMatch(/2 \|.*\bwith\b/); | ||
| expect(exitCode).toBe(1); | ||
| }); | ||
|
|
||
| test.concurrent( | ||
| "JSC parse SyntaxError reported line points at the offending statement, not the line before", | ||
| async () => { | ||
| // addErrorInfo() discards the parser-error column; a naive source-map lookup | ||
| // at column 0 resolves to bun's own start-of-line mapping for the *previous* | ||
| // source line when the offending statement is indented. | ||
| const { stderr, exitCode } = await run( | ||
| { | ||
| "module.mjs": `export const x = 1; | ||
| function foo() { | ||
| with ({ a: 1 }) { | ||
| console.log(a); | ||
| } | ||
| } | ||
| foo(); | ||
| `, | ||
| }, | ||
| "module.mjs", | ||
| ); | ||
| expect(stderr).toContain("SyntaxError: 'with' statements are not valid in strict mode."); | ||
| expect(stderr).toMatch(/module\.mjs:3\b/); | ||
| expect(stderr).toMatch(/3 \|.*\bwith\b/); | ||
| 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.
Uh oh!
There was an error while loading. Please reload this page.