From 0c7a84d53737b084023f5c4bec464890671e68ae Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 11 May 2026 20:02:54 +0000 Subject: [PATCH] Fix capacity calculation in ESMConditions.init The expression 'if (allow_addons) 1 else 0 + conditions.len' parses as 'if (allow_addons) 1 else (0 + conditions.len)', so when allow_addons is true (the default), conditions.len is dropped from the reserved capacity, causing putAssumeCapacity to overflow when enough conditions are passed to Bun.build(). --- src/bundler/options.zig | 6 +++--- test/bundler/bun-build-api.test.ts | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/bundler/options.zig b/src/bundler/options.zig index daea5ced59a3..b11d54112ee3 100644 --- a/src/bundler/options.zig +++ b/src/bundler/options.zig @@ -1087,9 +1087,9 @@ 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); + try default_condition_amp.ensureTotalCapacity(defaults.len + 2 + @intFromBool(allow_addons) + conditions.len); + try import_condition_map.ensureTotalCapacity(defaults.len + 2 + @intFromBool(allow_addons) + conditions.len); + try require_condition_map.ensureTotalCapacity(defaults.len + 2 + @intFromBool(allow_addons) + conditions.len); try style_condition_map.ensureTotalCapacity(defaults.len + 2 + conditions.len); import_condition_map.putAssumeCapacity("import", {}); diff --git a/test/bundler/bun-build-api.test.ts b/test/bundler/bun-build-api.test.ts index c1112743f02c..2187d789b95d 100644 --- a/test/bundler/bun-build-api.test.ts +++ b/test/bundler/bun-build-api.test.ts @@ -649,6 +649,17 @@ describe("Bun.build", () => { expect(await html?.text()).toContain(""); }, ); + + test.concurrent("many conditions does not crash", async () => { + const dir = tempDirWithFiles("bun-build-api-many-conditions", { + "index.ts": "export const a = 1;", + }); + const result = await Bun.build({ + entrypoints: [join(dir, "index.ts")], + conditions: ["c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10"], + }); + expect(result.success).toBe(true); + }); }); test.concurrent("macro with nested object", async () => {