diff --git a/src/bundler/options.zig b/src/bundler/options.zig index daea5ced59a3..ae72e3509ff1 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 + @as(usize, if (allow_addons) 1 else 0) + conditions.len); + try import_condition_map.ensureTotalCapacity(defaults.len + 2 + @as(usize, if (allow_addons) 1 else 0) + conditions.len); + try require_condition_map.ensureTotalCapacity(defaults.len + 2 + @as(usize, if (allow_addons) 1 else 0) + 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..caec8732964c 100644 --- a/test/bundler/bun-build-api.test.ts +++ b/test/bundler/bun-build-api.test.ts @@ -649,6 +649,21 @@ describe("Bun.build", () => { expect(await html?.text()).toContain(""); }, ); + + test.concurrent("many custom conditions does not crash", async () => { + // The capacity reserved for the ESM conditions map was miscomputed when + // node-addons were enabled (the default), so enough custom conditions + // would overflow the reserved capacity and assert/corrupt. + const dir = tempDirWithFiles("bun-build-conditions", { + "entry.ts": `export const x = 1;`, + }); + const build = await Bun.build({ + entrypoints: [join(dir, "entry.ts")], + conditions: ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"], + }); + expect(build.success).toBe(true); + expect(build.outputs).toHaveLength(1); + }); }); test.concurrent("macro with nested object", async () => {