From 825c1ef94a0a327d9d3c965adb1a9315ad6749a6 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 12 Aug 2026 13:47:42 +0000 Subject: [PATCH 1/4] bake: keep the import record source index of a CSS file that failed to bundle In the dev server, finish_from_bake_dev_server invalidated the import record pointing at a CSS file that failed to bundle so that the file would not become a CSS chunk. The HTML rewriter and the HMR import conversion identify stylesheet imports through that source index, so the importer (re-bundled alongside the broken stylesheet) was compiled as if it imported a plain asset: HTML routes kept their original tag next to the injected one, and JavaScript importers listed the stylesheet as a module dependency and failed to boot. Both outputs stay cached after the stylesheet itself recovers, since only the stylesheet is rebuilt then. Only skip the CSS chunk and leave the record alone. Nothing in the loop mutates the AST columns anymore, so the raw column pointers are replaced with plain slices. --- src/bundler/bundle_v2.rs | 36 ++++------- test/bake/dev/css.test.ts | 122 +++++++++++++++++++++++++++++++++++++- 2 files changed, 132 insertions(+), 26 deletions(-) diff --git a/src/bundler/bundle_v2.rs b/src/bundler/bundle_v2.rs index a2a31fd5e974..0240ece9a184 100644 --- a/src/bundler/bundle_v2.rs +++ b/src/bundler/bundle_v2.rs @@ -5291,33 +5291,21 @@ pub mod bv2_impl { let asts = self.graph.ast.slice(); let css_asts = asts.items_css(); - // SoA columns are physically disjoint slabs but rustc cannot - // see that through `&Slice`. Route the two columns we mutate (`parts`, - // `import_records`) through `split_raw()` (root-provenance `*mut [T]`, - // no `&mut` intermediate) so the per-index `&mut` does not conflict - // with the `&asts` reads (`css`, `target`). Mirrors the pattern at - // `find_reachable_files` (~L1457). The slab does not resize for the - // duration of this loop and no other `&mut` to these columns exists. - let ast_raw = asts.split_raw(); - let parts_col: *mut bun_ast::PartList = ast_raw.parts.cast::(); - let import_records_col: *mut import_record::List = - ast_raw.import_records.cast::(); + let parts = asts.items_parts(); + let all_import_records = asts.items_import_records(); let input_files = self.graph.input_files.slice(); let loaders = input_files.items_loader(); let sources = input_files.items_source(); for index in 1..self.graph.ast.len() { - // SAFETY: `index < ast.len()`; see note above for column aliasing. - let part_list = unsafe { &mut *parts_col.add(index) }; - // SAFETY: `index < ast.len()`; see note above for column aliasing. - let import_records = unsafe { &mut *import_records_col.add(index) }; + let import_records = &all_import_records[index]; let maybe_css = &css_asts[index]; let target = asts.items_target()[index]; // Dev Server proceeds even with failed files. // These files are filtered out via the lack of any parts. // // Actual empty files will contain a part exporting an empty object. - if part_list.len() != 0 { + if parts[index].len() != 0 { if maybe_css.is_some() { // CSS has restrictions on what files can be imported. // This means the file can become an error after @@ -5372,21 +5360,19 @@ pub mod bv2_impl { } // Discover all CSS roots. - for record in import_records.as_mut_slice() { + for record in import_records.as_slice() { if !record.source_index.is_valid() { continue; } if loaders[record.source_index.get() as usize] != Loader::Css { continue; } - // SAFETY: `source_index < ast.len()` (validated above); read - // via the raw column ptr so we don't reborrow `asts.parts()` - // while `import_records` (a sibling column) is held `&mut`. - if unsafe { - (*parts_col.add(record.source_index.get() as usize)).len() - } == 0 - { - record.source_index = Index::INVALID; + // A CSS file that failed to bundle has no parts and must + // not become a CSS chunk. The record keeps its source + // index: the HTML rewriter and the HMR import conversion + // use it to recognize the import as a stylesheet, and the + // importer's output stays cached after the CSS recovers. + if parts[record.source_index.get() as usize].len() == 0 { continue; } diff --git a/test/bake/dev/css.test.ts b/test/bake/dev/css.test.ts index 18d665708976..5e94d41c5c9b 100644 --- a/test/bake/dev/css.test.ts +++ b/test/bake/dev/css.test.ts @@ -1,7 +1,7 @@ // CSS tests concern bundling bugs with CSS files import { expect } from "bun:test"; import assert from "node:assert"; -import { devTest, emptyHtmlFile, imageFixtures } from "../bake-harness"; +import { type Dev, devTest, emptyHtmlFile, imageFixtures } from "../bake-harness"; devTest("css file with syntax error does not kill old styles", { files: { @@ -691,6 +691,126 @@ devTest("css import before create project relative", { await dev.fetch("/").expect.toContain("HELLO"); }, }); +devTest("html route strips its link tag when the stylesheet is bundled alongside a failing version of itself", { + // When a stylesheet fails, the route that links it is re-bundled together + // with the broken stylesheet (on first load, or when the route is requested + // while it is broken). The HTML generated by that bundle is what gets served + // once the stylesheet recovers, since only the stylesheet is rebuilt then. + files: { + "index.html": emptyHtmlFile({ + styles: ["styles.css"], + body: `
hello
`, + }), + "styles.css": ` + .a { color: red; }} + `, + }, + async test(dev) { + expect((await dev.fetch("/")).status).toBe(500); + await dev.write( + "styles.css", + ` + .a { color: blue; } + `, + ); + const recovered = await fetchRouteHtml(dev); + const links = recovered.match(/]*>/g); + expect(links).toEqual([expect.stringMatching(injectedStylesheetLink)]); + await dev.fetch(links![0].match(injectedStylesheetLink)![1]).expect.toMatch(/color:\s*#00f/); + + // Syntax error, then recovery. + await dev.write( + "styles.css", + ` + .a { color: blue; }} + `, + ); + expect((await dev.fetch("/")).status).toBe(500); + await dev.write( + "styles.css", + ` + .a { color: green; } + `, + ); + expect(await fetchRouteHtml(dev)).toBe(recovered); + + // A stylesheet that parses but fails import resolution, then recovery. + await dev.write( + "styles.css", + ` + .a { background-image: url(./missing.png); } + `, + ); + expect((await dev.fetch("/")).status).toBe(500); + await dev.write( + "styles.css", + ` + .a { color: yellow; } + `, + ); + expect(await fetchRouteHtml(dev)).toBe(recovered); + }, +}); +devTest("script importing a stylesheet still boots after the stylesheet fails and recovers", { + // Same as above for a stylesheet imported from JavaScript: the importer is + // re-bundled alongside the broken stylesheet, and the module it produced + // then must not list the stylesheet as a JavaScript dependency. + files: { + "index.html": emptyHtmlFile({ + scripts: ["index.ts"], + body: `
hello
`, + }), + "index.ts": ` + import "./styles.css"; + `, + "styles.css": ` + .a { color: red; }} + `, + }, + async test(dev) { + expect((await dev.fetch("/")).status).toBe(500); + await dev.write( + "styles.css", + ` + .a { color: blue; } + `, + ); + { + await using c = await dev.client("/"); + await c.style(".a").color.expect.toBe("#00f"); + } + + await dev.write( + "styles.css", + ` + .a { color: blue; }} + `, + ); + expect((await dev.fetch("/")).status).toBe(500); + await dev.write( + "styles.css", + ` + .a { color: green; } + `, + ); + { + await using c = await dev.client("/"); + await c.style(".a").color.expect.toBe("green"); + } + }, +}); + +const injectedStylesheetLink = /^$/; + +/** + * Serves "/" and masks the route script's URL, which embeds a generation + * number that changes every time the route is re-bundled. + */ +async function fetchRouteHtml(dev: Dev): Promise { + const res = await dev.fetch("/"); + expect(res.status).toBe(200); + return (await res.text()).replace(/src="\/_bun\/client\/[^"]*"/, 'src=""'); +} function extractCssUrl(backgroundImage: string): string { const url = backgroundImage.match(/url\((['"])(.*?)\1\)/); From 8ca4fe306d550ffc6a03dc013c527be7a61a5d73 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 12 Aug 2026 17:38:24 +0000 Subject: [PATCH 2/4] test: also cover editing the importer while its stylesheet is broken Editing the HTML file or the script that imports the broken stylesheet rebundles it together with the stylesheet as well, so each test now exercises that trigger in addition to first load and a request while broken. --- test/bake/dev/css.test.ts | 40 +++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/test/bake/dev/css.test.ts b/test/bake/dev/css.test.ts index 5e94d41c5c9b..64f5511e52a0 100644 --- a/test/bake/dev/css.test.ts +++ b/test/bake/dev/css.test.ts @@ -692,10 +692,11 @@ devTest("css import before create project relative", { }, }); devTest("html route strips its link tag when the stylesheet is bundled alongside a failing version of itself", { - // When a stylesheet fails, the route that links it is re-bundled together - // with the broken stylesheet (on first load, or when the route is requested - // while it is broken). The HTML generated by that bundle is what gets served - // once the stylesheet recovers, since only the stylesheet is rebuilt then. + // The route that links a broken stylesheet gets bundled together with it on + // first load, when the route is requested while it is broken, and when the + // HTML file itself is edited while it is broken. The HTML generated by that + // bundle is what gets served once the stylesheet recovers, since only the + // stylesheet is rebuilt then. files: { "index.html": emptyHtmlFile({ styles: ["styles.css"], @@ -718,7 +719,7 @@ devTest("html route strips its link tag when the stylesheet is bundled alongside expect(links).toEqual([expect.stringMatching(injectedStylesheetLink)]); await dev.fetch(links![0].match(injectedStylesheetLink)![1]).expect.toMatch(/color:\s*#00f/); - // Syntax error, then recovery. + // Syntax error, route requested while broken, then recovery. await dev.write( "styles.css", ` @@ -734,14 +735,15 @@ devTest("html route strips its link tag when the stylesheet is bundled alongside ); expect(await fetchRouteHtml(dev)).toBe(recovered); - // A stylesheet that parses but fails import resolution, then recovery. + // A stylesheet that parses but fails import resolution, HTML file edited + // while broken, then recovery. await dev.write( "styles.css", ` .a { background-image: url(./missing.png); } `, ); - expect((await dev.fetch("/")).status).toBe(500); + await dev.writeNoChanges("index.html"); await dev.write( "styles.css", ` @@ -752,9 +754,9 @@ devTest("html route strips its link tag when the stylesheet is bundled alongside }, }); devTest("script importing a stylesheet still boots after the stylesheet fails and recovers", { - // Same as above for a stylesheet imported from JavaScript: the importer is - // re-bundled alongside the broken stylesheet, and the module it produced - // then must not list the stylesheet as a JavaScript dependency. + // Same three situations as above for a stylesheet imported from JavaScript: + // the module generated for the importer while the stylesheet is broken must + // not list the stylesheet as a JavaScript dependency. files: { "index.html": emptyHtmlFile({ scripts: ["index.ts"], @@ -797,6 +799,24 @@ devTest("script importing a stylesheet still boots after the stylesheet fails an await using c = await dev.client("/"); await c.style(".a").color.expect.toBe("green"); } + + await dev.write( + "styles.css", + ` + .a { background-image: url(./missing.png); } + `, + ); + await dev.writeNoChanges("index.ts"); + await dev.write( + "styles.css", + ` + .a { color: yellow; } + `, + ); + { + await using c = await dev.client("/"); + await c.style(".a").color.expect.toBe("#ff0"); + } }, }); From d403aead357b5a8956e0f30a63c85b05b996df82 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 12 Aug 2026 17:44:58 +0000 Subject: [PATCH 3/4] bundler: shorten the comment on failed CSS roots --- src/bundler/bundle_v2.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/bundler/bundle_v2.rs b/src/bundler/bundle_v2.rs index 0240ece9a184..c53c57c20b7a 100644 --- a/src/bundler/bundle_v2.rs +++ b/src/bundler/bundle_v2.rs @@ -5367,11 +5367,8 @@ pub mod bv2_impl { if loaders[record.source_index.get() as usize] != Loader::Css { continue; } - // A CSS file that failed to bundle has no parts and must - // not become a CSS chunk. The record keeps its source - // index: the HTML rewriter and the HMR import conversion - // use it to recognize the import as a stylesheet, and the - // importer's output stays cached after the CSS recovers. + // No chunk for failed CSS, but keep the record's source index: + // the HTML rewriter and HMR import conversion key on it. if parts[record.source_index.get() as usize].len() == 0 { continue; } From 4b2b14c223fe2b9e0358945d9f0360888ab1f448 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Wed, 12 Aug 2026 17:49:15 +0000 Subject: [PATCH 4/4] bundler: drop the comment on failed CSS roots --- src/bundler/bundle_v2.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/bundler/bundle_v2.rs b/src/bundler/bundle_v2.rs index c53c57c20b7a..84a9dbebef77 100644 --- a/src/bundler/bundle_v2.rs +++ b/src/bundler/bundle_v2.rs @@ -5367,8 +5367,6 @@ pub mod bv2_impl { if loaders[record.source_index.get() as usize] != Loader::Css { continue; } - // No chunk for failed CSS, but keep the record's source index: - // the HTML rewriter and HMR import conversion key on it. if parts[record.source_index.get() as usize].len() == 0 { continue; }