Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
7 changes: 4 additions & 3 deletions src/bundler/options.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,10 @@ pub const ESMConditions = struct {
var require_condition_map = ConditionsMap.init(allocator);
var style_condition_map = ConditionsMap.init(allocator);

try default_condition_amp.ensureTotalCapacity(defaults.len + 2 + if (allow_addons) 1 else 0 + conditions.len);
try import_condition_map.ensureTotalCapacity(defaults.len + 2 + if (allow_addons) 1 else 0 + conditions.len);
try require_condition_map.ensureTotalCapacity(defaults.len + 2 + if (allow_addons) 1 else 0 + conditions.len);
const addons_extra: usize = if (allow_addons) 1 else 0;
try default_condition_amp.ensureTotalCapacity(defaults.len + 2 + addons_extra + conditions.len);
try import_condition_map.ensureTotalCapacity(defaults.len + 2 + addons_extra + conditions.len);
try require_condition_map.ensureTotalCapacity(defaults.len + 2 + addons_extra + conditions.len);
try style_condition_map.ensureTotalCapacity(defaults.len + 2 + conditions.len);

import_condition_map.putAssumeCapacity("import", {});
Expand Down
13 changes: 13 additions & 0 deletions test/bundler/bun-build-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,19 @@ describe("Bun.build", () => {
expect(await bundle.outputs[0].text()).toBe("var o=/*@__PURE__*/console.log(1);export{o as OUT};\n");
});

test.concurrent("many user conditions does not crash", async () => {
const fixture = tempDirWithFiles("build-many-conditions", {
"entry.ts": `export const x = 1;`,
});

const bundle = await Bun.build({
entrypoints: [join(fixture, "entry.ts")],
target: "bun",
conditions: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"],
});
expect(bundle.success).toBe(true);
});

test.concurrent(
"you can write onLoad and onResolve plugins using the 'html' loader, and it includes script and link tags as bundled entrypoints",
async () => {
Expand Down
19 changes: 19 additions & 0 deletions test/js/bun/resolve/import-custom-condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,22 @@ it("custom condition when don't match condition should resolves to default", asy

expect(exitCode).toBe(1);
});

// https://github.com/oven-sh/bun/issues/30619
it("many custom conditions resolve correctly", async () => {
for (const n of [4, 5, 6, 8, 12, 20]) {
const flags = [];
for (let i = 1; i < n; i++) flags.push(`--conditions=c${i}`);
flags.push("--conditions=first");
const { exitCode, stdout, stderr } = Bun.spawnSync({
cmd: [bunExe(), ...flags, `${dir}/test.js`],
env: bunEnv,
cwd: import.meta.dir,
});
expect({ n, exitCode, stdout: stdout.toString("utf8"), stderr: stderr.toString("utf8") }).toMatchObject({
n,
exitCode: 0,
stdout: "1\n",
});
}
});
Comment on lines +172 to +188

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 | ⚡ Quick win

Use parameterized test cases instead of a manual loop.

This should be split into parameterized cases so each n reports independently and can run concurrently across the matrix.

♻️ Suggested rewrite
-it("many custom conditions resolve correctly", async () => {
-  for (const n of [4, 5, 6, 8, 12, 20]) {
-    const flags = [];
-    for (let i = 1; i < n; i++) flags.push(`--conditions=c${i}`);
-    flags.push("--conditions=first");
-    const { exitCode, stdout, stderr } = Bun.spawnSync({
-      cmd: [bunExe(), ...flags, `${dir}/test.js`],
-      env: bunEnv,
-      cwd: import.meta.dir,
-    });
-    expect({ n, exitCode, stdout: stdout.toString("utf8"), stderr: stderr.toString("utf8") }).toMatchObject({
-      n,
-      exitCode: 0,
-      stdout: "1\n",
-    });
-  }
-});
+it.concurrent.each([4, 5, 6, 8, 12, 20])("many custom conditions resolve correctly (n=%i)", async n => {
+  const flags = [];
+  for (let i = 1; i < n; i++) flags.push(`--conditions=c${i}`);
+  flags.push("--conditions=first");
+
+  const { exitCode, stdout, stderr } = Bun.spawnSync({
+    cmd: [bunExe(), ...flags, `${dir}/test.js`],
+    env: bunEnv,
+    cwd: import.meta.dir,
+  });
+
+  expect({ n, exitCode, stdout: stdout.toString("utf8"), stderr: stderr.toString("utf8") }).toMatchObject({
+    n,
+    exitCode: 0,
+    stdout: "1\n",
+  });
+});

Based on learnings, in test/js/bun/**/*.test.ts, prefer test.each()/test.concurrent.each() over manual loops when each parameter value maps to a single assertion.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it("many custom conditions resolve correctly", async () => {
for (const n of [4, 5, 6, 8, 12, 20]) {
const flags = [];
for (let i = 1; i < n; i++) flags.push(`--conditions=c${i}`);
flags.push("--conditions=first");
const { exitCode, stdout, stderr } = Bun.spawnSync({
cmd: [bunExe(), ...flags, `${dir}/test.js`],
env: bunEnv,
cwd: import.meta.dir,
});
expect({ n, exitCode, stdout: stdout.toString("utf8"), stderr: stderr.toString("utf8") }).toMatchObject({
n,
exitCode: 0,
stdout: "1\n",
});
}
});
it.concurrent.each([4, 5, 6, 8, 12, 20])("many custom conditions resolve correctly (n=%i)", async n => {
const flags = [];
for (let i = 1; i < n; i++) flags.push(`--conditions=c${i}`);
flags.push("--conditions=first");
const { exitCode, stdout, stderr } = Bun.spawnSync({
cmd: [bunExe(), ...flags, `${dir}/test.js`],
env: bunEnv,
cwd: import.meta.dir,
});
expect({ n, exitCode, stdout: stdout.toString("utf8"), stderr: stderr.toString("utf8") }).toMatchObject({
n,
exitCode: 0,
stdout: "1\n",
});
});
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/js/bun/resolve/import-custom-condition.test.ts` around lines 172 - 188,
Replace the manual for-loop inside the "many custom conditions resolve
correctly" test with a parameterized test so each `n` is its own case: convert
the current it(...) block in import-custom-condition.test.ts to use
test.concurrent.each([4,5,6,8,12,20]) (or test.each if concurrency is undesired)
and move the body that builds `flags`, calls `Bun.spawnSync({...})`, and asserts
the result into the per-case callback; keep the same `flags` construction logic,
same `Bun.spawnSync` call and the expect comparing `{ n, exitCode, stdout,
stderr }` to the expected object, but run it once per `n` so failures report
independently.

Loading