-
Notifications
You must be signed in to change notification settings - Fork 5.1k
css: cap & parent-selector expansion when compiling nesting for older targets
#31276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Jarred-Sumner
merged 6 commits into
main
from
farm/cb165a80/css-nesting-expansion-limit
May 24, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d8a83ae
css: cap `&` parent-selector expansion when compiling nesting for old…
robobun 8c57e8d
test: run nested-selector-expansion tests concurrently
robobun 7f64a95
[autofix.ci] apply automated fixes
autofix-ci[bot] 6429c8a
test: assert exit codes in nested-selector-expansion spawn tests
robobun cb5bf4a
ci: retrigger
robobun b20c268
css: give @scope preludes their own nesting expansion budget
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { bunEnv, bunExe, tempDir } from "harness"; | ||
| import path from "node:path"; | ||
|
|
||
| // Regression test for unbounded memory growth when compiling CSS nesting for | ||
| // targets that don't support it (found by CSS fuzzing). | ||
| // | ||
| // When the browser targets lack native nesting support, every `&` in a nested | ||
| // rule's selector is replaced with the parent selector at print time. The | ||
| // parent selector itself may contain `&` referring to the grandparent, so a | ||
| // selector with multiple `&` references per nesting level expands to | ||
| // (references per level)^depth copies of its ancestors. A ~3 KB stylesheet | ||
| // with ~20 nesting levels of `&:is(.bar, &.baz)` makes the printer allocate an | ||
| // effectively unbounded output buffer: `bun build` (whose default browser | ||
| // target predates CSS nesting) and `minifyTest` with explicit targets both | ||
| // spin forever while memory grows. | ||
| // | ||
| // The serializer now budgets the number of `&` substitutions per rule prelude | ||
| // and reports "Maximum nesting expansion exceeded" instead of expanding | ||
| // without bound. Preserving nesting (no targets) and ordinary nested CSS with | ||
| // old targets are unaffected. | ||
|
|
||
| /** Deeply nested rules where each level references the parent twice (`&` appears twice per selector). */ | ||
| function explodingNestedCss(depth: number): string { | ||
| let css = ""; | ||
| for (let i = 0; i < depth; i++) { | ||
| css += "&:is(.bar, &.baz) { color: red; }\n"; | ||
| css += "&:is(.bar, &.baz) { colo\n"; // unclosed block, same shape as the fuzz input | ||
| } | ||
| css += "&:is(.bar, &.baz) { color: red; }\n"; | ||
| css += "}"; | ||
| return css; | ||
| } | ||
|
|
||
| const minifyTestScript = ` | ||
| const { cssInternals } = require("bun:internal-for-testing"); | ||
| const depth = parseInt(process.env.NESTED_CSS_DEPTH, 10); | ||
| const targets = process.env.NESTED_CSS_TARGETS === "1" ? { safari: 13 << 16 } : undefined; | ||
| let css = ""; | ||
| for (let i = 0; i < depth; i++) { | ||
| css += "&:is(.bar, &.baz) { color: red; }\\n"; | ||
| css += "&:is(.bar, &.baz) { colo\\n"; | ||
| } | ||
| css += "&:is(.bar, &.baz) { color: red; }\\n"; | ||
| css += "}"; | ||
| try { | ||
| const out = targets ? cssInternals.minifyTest(css, "", targets) : cssInternals.minifyTest(css, ""); | ||
| console.log("OK " + out.length); | ||
| } catch (err) { | ||
| console.log("ERR " + err.message); | ||
| } | ||
| `; | ||
|
|
||
| async function runMinifyTest(depth: number, withTargets: boolean) { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "-e", minifyTestScript], | ||
| env: { | ||
| ...bunEnv, | ||
| BUN_FEATURE_FLAG_INTERNAL_FOR_TESTING: "1", | ||
| NESTED_CSS_DEPTH: String(depth), | ||
| NESTED_CSS_TARGETS: withTargets ? "1" : "0", | ||
| }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| // Kill switch: before the fix these spins were unbounded. Let the child be | ||
| // killed so a regression fails the assertions below instead of hanging the | ||
| // test runner and exhausting memory. | ||
| timeout: 20_000, | ||
| killSignal: "SIGKILL", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| return { stdout, stderr, exitCode, signalCode: proc.signalCode }; | ||
| } | ||
|
|
||
| test.concurrent( | ||
| "deeply nested `&` selectors error out instead of expanding without bound when compiling nesting", | ||
| async () => { | ||
| const { stdout, stderr, signalCode, exitCode } = await runMinifyTest(24, true); | ||
| expect(stderr).toBe(""); | ||
| expect(signalCode).toBeNull(); // not killed by the kill switch | ||
| expect(stdout).toContain("ERR Maximum nesting expansion exceeded"); | ||
| expect(exitCode).toBe(0); | ||
| }, | ||
| ); | ||
|
|
||
| test.concurrent("deeply nested `&` selectors still minify when nesting is preserved (no targets)", async () => { | ||
| const { stdout, stderr, signalCode, exitCode } = await runMinifyTest(24, false); | ||
| expect(stderr).toBe(""); | ||
| expect(signalCode).toBeNull(); | ||
| // Without targets the nesting is preserved, so the output stays small. | ||
| expect(stdout).toStartWith("OK "); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("ordinary nested CSS still compiles for older targets", async () => { | ||
| await using proc = Bun.spawn({ | ||
| cmd: [ | ||
| bunExe(), | ||
| "-e", | ||
| ` | ||
| const { cssInternals } = require("bun:internal-for-testing"); | ||
| // 8 levels deep, one parent reference per level: well within the budget. | ||
| let css = ".a { color: red; "; | ||
| for (let i = 0; i < 8; i++) css += "&:hover .b" + i + " { color: blue; "; | ||
| css += "}".repeat(9); | ||
| console.log(cssInternals.minifyTest(css, "", { safari: 13 << 16 })); | ||
| `, | ||
| ], | ||
| env: { ...bunEnv, BUN_FEATURE_FLAG_INTERNAL_FOR_TESTING: "1" }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| timeout: 20_000, | ||
| killSignal: "SIGKILL", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| expect(stderr).toBe(""); | ||
| // The innermost rule's `&` chain is fully expanded. | ||
| expect(stdout).toContain(".a:hover .b0:hover .b1:hover .b2:hover .b3:hover .b4:hover .b5:hover .b6:hover .b7"); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
|
|
||
| test.concurrent("bun build does not hang on deeply nested `&` selectors with the default browser target", async () => { | ||
| using dir = tempDir("css-nested-selector-expansion", { | ||
| "explode.css": explodingNestedCss(24), | ||
| }); | ||
| await using proc = Bun.spawn({ | ||
| cmd: [bunExe(), "build", path.join(String(dir), "explode.css"), "--outdir", path.join(String(dir), "out")], | ||
| env: bunEnv, | ||
| cwd: String(dir), | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| // Kill switch: before the fix this build spun forever while allocating. | ||
| timeout: 20_000, | ||
| killSignal: "SIGKILL", | ||
| }); | ||
| const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); | ||
| // The build must terminate on its own (the printer reports an error for the | ||
| // runaway rule) instead of being killed by the 20s kill switch. | ||
| expect(proc.signalCode).toBeNull(); | ||
| expect(exitCode).not.toBeNull(); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.