Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ast/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1494,7 +1494,7 @@ pub const Parser = struct {

var state: PragmaState = .{};

while (cursor < self.lexer.end) : (cursor += 1) {
while (cursor < end) : (cursor += 1) {
switch (contents[cursor]) {
'\n' => break,
'@' => {
Expand Down
29 changes: 29 additions & 0 deletions test/bundler/bundler_banner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,33 @@ module.exports = 1;`,
`);
},
});

itBundled("banner/SourceHashbangWithBytecodeAndCJSTargetBun", {
banner: "// Copyright 2024 Example Corp",
format: "cjs",
target: "bun",
bytecode: true,
outdir: "/out",
minifyWhitespace: true,
backend: "api",
files: {
"/a.js": `#!/usr/bin/env bun
module.exports = 1;
console.log("bun!");`,
},
onAfterBundle(api) {
const content = api.readFile("/out/a.js");
// Shebang from source should come first, then @bun pragma
expect(content).toMatchInlineSnapshot(`
"#!/usr/bin/env bun
// @bun @bytecode @bun-cjs
(function(exports, require, module, __filename, __dirname) {// Copyright 2024 Example Corp
module.exports=1;console.log("bun!")})
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
"
`);
},
Comment thread
kjanat marked this conversation as resolved.
Resolved
run: {
stdout: "bun!\n",
},
});
Comment on lines +207 to +234

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Consider adding explicit order verification.

The test includes a comment (line 222) emphasizing that "Shebang from source should come first, then @Bun pragma," which suggests order is critical for this bug fix. Consider adding explicit order checks similar to the BannerWithBytecodeAndCJSTargetBun test (lines 173-179) to make the ordering requirement more explicit and provide clearer failure messages:

// After the snapshot assertion
const shebangIndex = content.indexOf("#!/usr/bin/env bun");
const pragmaIndex = content.indexOf("// @bun @bytecode @bun-cjs");
const wrapperIndex = content.indexOf("(function(exports, require, module, __filename, __dirname) {");
const bannerIndex = content.indexOf("// Copyright 2024 Example Corp");

expect(shebangIndex).toBe(0);
expect(pragmaIndex).toBeGreaterThan(shebangIndex);
expect(wrapperIndex).toBeGreaterThan(pragmaIndex);
expect(bannerIndex).toBeGreaterThan(wrapperIndex);

This would provide more specific failure messages if the ordering regresses and be consistent with similar tests in the file.

🤖 Prompt for AI Agents
In @test/bundler/bundler_banner.test.ts around lines 207 - 234, In the itBundled
test "banner/SourceHashbangWithBytecodeAndCJSTargetBun" add explicit order
assertions after the snapshot by computing indices from the content (e.g.
shebangIndex = content.indexOf("#!/usr/bin/env bun"), pragmaIndex =
content.indexOf("// @bun @bytecode @bun-cjs"), wrapperIndex =
content.indexOf("(function(exports, require, module, __filename, __dirname) {"),
bannerIndex = content.indexOf("// Copyright 2024 Example Corp")) and assert
shebangIndex === 0, pragmaIndex > shebangIndex, wrapperIndex > pragmaIndex, and
bannerIndex > wrapperIndex to mirror the ordering checks used in
BannerWithBytecodeAndCJSTargetBun and give clearer failure messages.

});