diff --git a/src/bundler/options.zig b/src/bundler/options.zig index daea5ced59a3..6b32f1ff159e 100644 --- a/src/bundler/options.zig +++ b/src/bundler/options.zig @@ -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_count: usize = if (allow_addons) 1 else 0; + try default_condition_amp.ensureTotalCapacity(defaults.len + 2 + addons_count + conditions.len); + try import_condition_map.ensureTotalCapacity(defaults.len + 2 + addons_count + conditions.len); + try require_condition_map.ensureTotalCapacity(defaults.len + 2 + addons_count + 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..d0bf18b52d82 100644 --- a/test/bundler/bun-build-api.test.ts +++ b/test/bundler/bun-build-api.test.ts @@ -682,6 +682,20 @@ export function testMacro(val: any) { ); }); +test.concurrent("Bun.build does not crash with many conditions", async () => { + const dir = tempDirWithFilesAnon({ + "entry.js": `console.log("hi");`, + }); + for (let n = 0; n <= 16; n++) { + const conditions = Array.from({ length: n }, (_, i) => `cond${i}`); + const result = await Bun.build({ + entrypoints: [join(dir, "entry.js")], + conditions, + }); + expect(result.success).toBe(true); + } +}); + // Since NODE_PATH has to be set, we need to run this test outside the bundler tests. test.concurrent("regression/NODE_PATHBuild api", async () => { const dir = tempDirWithFiles("node-path-build", {