Skip to content

fix(bake): preserve CSS import order in dev server#28119

Open
robobun wants to merge 1 commit intomainfrom
claude/fix-css-import-order-28117
Open

fix(bake): preserve CSS import order in dev server#28119
robobun wants to merge 1 commit intomainfrom
claude/fix-css-import-order-28117

Conversation

@robobun
Copy link
Copy Markdown
Collaborator

@robobun robobun commented Mar 14, 2026

Summary

  • Fix CSS import order being reversed in Bun.serve dev server, causing incorrect cascade behavior
  • The incremental graph built import linked lists by prepending each edge (stack-style), which reversed the order relative to source code. When traceImports traversed these lists to collect CSS files, they appeared in reversed order, making earlier CSS imports override later ones
  • Reverse the linked list after construction in processChunkDependencies so imports are traversed in source order, matching Bun.build behavior

Fixes #28117

Test plan

  • Added test css import order is preserved (#28117) in test/bake/dev/css.test.ts
  • Test imports foo.css (color: red) then bar.css (color: blue) and verifies blue wins the cascade via getComputedStyle
  • Test verifies correct order persists after hard reload
  • Verified test fails with USE_SYSTEM_BUN=1 (receives "red") and passes with debug build (receives "#00f")
  • All 13 existing CSS dev server tests continue to pass

🤖 Generated with Claude Code

The dev server's incremental graph built import linked lists by
prepending each edge (stack-style), which reversed the order relative
to source. This caused CSS files imported from JS to appear in the
wrong cascade order, making earlier imports override later ones.

Reverse the linked list after construction so imports are traversed
in source order, matching Bun.build behavior.

Fixes #28117

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@robobun
Copy link
Copy Markdown
Collaborator Author

robobun commented Mar 14, 2026

Updated 2:16 PM PT - Mar 14th, 2026

❌ Your commit e9e51267 has some failures in Build #39657 (All Failures)


🧪   To try this PR locally:

bunx bun-pr 28119

That installs a local version of the PR into your bun-28119 executable, so you can run:

bun-28119 --bun

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 14, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 27dcb492-6b12-4a3d-8572-aeedc955bed0

📥 Commits

Reviewing files that changed from the base of the PR and between 10bdb48 and e9e5126.

📒 Files selected for processing (2)
  • src/bake/DevServer/IncrementalGraph.zig
  • test/bake/dev/css.test.ts

Walkthrough

Fixes CSS import order preservation in the incremental graph by reversing newly constructed import edge lists to restore source order, ensuring consistent CSS cascade behavior during development.

Changes

Cohort / File(s) Summary
Import edge reversal logic
src/bake/DevServer/IncrementalGraph.zig
Introduces reversal pass for newly constructed import edge lists in both main dependency processing and CSS-specific processing paths. Restores original source order by reversing the new_imports linked list after processing.
CSS import order preservation test
test/bake/dev/css.test.ts
Adds test case validating CSS import order is preserved across reloads. Also hardens extractCssUrl to throw error when backgroundImage lacks url match instead of proceeding with undefined value.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title directly and accurately summarizes the main change: fixing CSS import order preservation in the dev server.
Description check ✅ Passed The description includes a clear summary, root cause analysis, the specific fix, test plan with verification steps, and issue reference.
Linked Issues check ✅ Passed The PR fully addresses issue #28117 by fixing the CSS import order reversal that caused incorrect cascade behavior, with supporting test coverage.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing CSS import order: reversal logic in IncrementalGraph.zig and a test for issue #28117.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

Found 3 issues this PR may fix:

  1. bun-plugin-tailwind: CSS @layer ordering breaks daisyUI v5 component styles #27197 - CSS @layer ordering breaks daisyUI v5 component styles (CSS layer precedence issues caused by incorrect CSS processing order)
  2. Bun's CSS loader incorrectly processes nested @layer rules #25075 - Nested @layer rules processing (CSS layer flattening problems that depend on correct CSS import order)
  3. Inconsistent Bundling and Path Handling Between Development and Production in Bun #18809 - Inconsistent bundling between dev and prod (mentions files need specific load order, dev/prod inconsistency)

If this is helpful, consider adding Fixes #<number> to the PR description to auto-close the issue on merge.

🤖 Generated with Claude Code

Copy link
Copy Markdown
Contributor

@claude claude Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — straightforward linked list reversal to fix CSS cascade ordering, with a solid regression test.

Extended reasoning...

Overview

This PR fixes a CSS import order bug in the Bun dev server. The incremental graph builds import linked lists by prepending each edge (stack-style), which reversed the order relative to source code. The fix adds a standard in-place singly-linked list reversal after construction in processChunkDependencies, so that traceImports traverses CSS files in source order. Two files are changed: IncrementalGraph.zig (the fix) and css.test.ts (a regression test).

Security risks

None. This is a purely internal data structure ordering fix in the dev server bundler graph. No user input handling, authentication, or network-facing code is affected.

Level of scrutiny

Low-to-medium. The change is small (16 lines of Zig + 34 lines of test), self-contained, and uses a well-known algorithm. The old code even had a comment acknowledging the reversal issue but incorrectly claimed ordering did not matter. The defer on new_imports correctly captures the final reversed value since Zig defers evaluate at scope exit. The temporary .none assignment later in the function (to avoid tripping checkEdgeRemoval) is overwritten by the defer, preserving existing behavior.

Other factors

The reversal applies to all import types (both .normal and .css modes), which is harmless — source-order traversal is strictly more correct than reversed order. The test verifies cascade behavior (blue wins over red) both on initial load and after hard reload. The PR description confirms all 13 existing CSS dev server tests continue to pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTMLBundle is inconsistent with the css content of Bun.build

1 participant