-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Fix panic when parsing declare global with type annotations #24491
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
claude/fix-declare-global-scope-crash
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
|
|
||
| test("declare global with type annotation should not crash", async () => { | ||
| using dir = tempDir("declare-global-test", { | ||
| "test.ts": ` | ||
| declare global { | ||
| A: 'a'; | ||
| } | ||
|
|
||
| () => {}; | ||
|
|
||
| console.log("SUCCESS"); | ||
| `, | ||
| }); | ||
|
|
||
| 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(stderr).not.toContain("panic"); | ||
| expect(stderr).not.toContain("Scope mismatch"); | ||
| expect(stdout).toContain("SUCCESS"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test("declare global with multiple type annotations and nested arrow functions", async () => { | ||
| using dir = tempDir("declare-global-multi", { | ||
| "test.ts": ` | ||
| declare global { | ||
| TIMER: NodeJS.Timeout; | ||
| FOO: string; | ||
| BAR: number; | ||
| BAZ: () => void; | ||
| } | ||
|
|
||
| // Test nested arrow functions to ensure scope handling is correct | ||
| const fn = () => { | ||
| const nested = () => { | ||
| const deeplyNested = () => "nested"; | ||
| return deeplyNested(); | ||
| }; | ||
| return nested(); | ||
| }; | ||
|
|
||
| console.log(fn()); | ||
| console.log("SUCCESS"); | ||
| `, | ||
| }); | ||
|
|
||
| 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(stderr).not.toContain("panic"); | ||
| expect(stderr).not.toContain("Scope mismatch"); | ||
| expect(stdout).toContain("SUCCESS"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
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.
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.
🧹 Nitpick | 🔵 Trivial
Consider simplifying process cleanup for these short-lived scripts.
Both tests create short-lived processes that execute a single console.log and exit naturally. While
await usingis valid and provides explicit cleanup, the more idiomatic pattern for such scripts isconst proc = spawn(...); await proc.exited;, which is significantly more common in the test suite (based on patterns in test/js/bun/spawn/spawn.test.ts).Also applies to: 57-57
🤖 Prompt for AI Agents
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.
Please never suggest this again.
await usingis better.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.
@Jarred-Sumner, understood! I apologize for the incorrect suggestion. I've noted that
await usingis the preferred pattern for spawned processes in Bun tests and will not suggest replacing it with theconst proc / await proc.exitedpattern in the future.Would you like me to remove the conflicting learning (ID: cac5565e-8acc-466c-b450-3c32b247331b) from my knowledge base? It currently suggests that
await usingshould only be used for long-running processes.✏️ Learnings added
🧠 Learnings used