You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bundler emits license comments in a non-deterministic order across sibling @imports
#1412
When a bundled stylesheet @imports several files that each contain a /*! … */ license comment, the order of the hoisted license comments changes between runs with identical input. The rules themselves always come out in the correct order; only the license comments are affected.
This breaks reproducible builds. Tools that name output by content hash (Vite, for example) produce a different file name for the same sources on different runs or machines. In our case, a CSS file built in CI and the copy built into a Docker image got different hashes, and the page referenced a file that had never been uploaded.
Reproduction
lightningcss 1.33.0. Reproduced on macOS arm64 (Node 26.8.1) and Linux aarch64 (Node 24.21.0, node:24.21.0-bookworm-slim), both with 10 CPUs.
// repro.mjsimport{bundle}from'lightningcss';import{mkdirSync,writeFileSync}from'node:fs';import{join}from'node:path';constdir=join(import.meta.dirname,'css');mkdirSync(dir,{recursive: true});constnames='ABCDEFGHIJ'.split('');for(constnofnames){writeFileSync(join(dir,`${n}.css`),`/*! License ${n} */\n.${n.toLowerCase()} { color: red }\n`);}writeFileSync(join(dir,'entry.css'),names.map((n)=>`@import "./${n}.css";`).join('\n'));constorders=newSet();for(leti=0;i<300;i++){constcode=bundle({filename: join(dir,'entry.css')}).code.toString();orders.add([...code.matchAll(/License(\w)/g)].map((m)=>m[1]).join(''));}console.log(`${orders.size} distinct license comment orders in 300 runs`,[...orders].slice(0,5));
The source map's sources array has the same problem. Bundling the same entry with sourceMap: true gave a different sources order on almost every run, with both bundle() and bundleAsync() and on both platforms (299–300 distinct orders in 300 runs, e.g. entry,F,G,J,E,B,H,I,C,A,D).
With two imports it's less frequent but still common: bundle() gave BA in 10 of 200 runs, and bundleAsync() with an async read resolver in 31 of 200.
Cause
In src/bundler.rs:
Each stylesheet's dependencies are loaded in parallel (par_iter_mut(), L476). load_file assigns each file the next index in self.stylesheets when it takes the lock (L416), so sibling imports get indexes in whatever order the threads finish.
Rules are concatenated in a computed order (self.order() / self.inline(), L306–L310), so the rule output is stable.
license_comments are collected by iterating self.stylesheets in index order (L330), so they follow the order the threads happened to finish. sources (L312) and source_map_urls (L322) are collected the same way, which explains the source-map sources order above.
test_license_comments only has a single import, so it can't catch this.
Possible fix
Collect license_comments, sources and source_map_urls in a deterministic order, independent of thread scheduling. A depth-first walk of the import graph in source order would keep the order test_license_comments already expects (the importing file's comment first, then its imports' comments).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Description
When a bundled stylesheet
@imports several files that each contain a/*! … */license comment, the order of the hoisted license comments changes between runs with identical input. The rules themselves always come out in the correct order; only the license comments are affected.This breaks reproducible builds. Tools that name output by content hash (Vite, for example) produce a different file name for the same sources on different runs or machines. In our case, a CSS file built in CI and the copy built into a Docker image got different hashes, and the page referenced a file that had never been uploaded.
Reproduction
lightningcss 1.33.0. Reproduced on macOS arm64 (Node 26.8.1) and Linux aarch64 (Node 24.21.0,
node:24.21.0-bookworm-slim), both with 10 CPUs.Expected:
ABCDEFGHIJevery time.The source map's
sourcesarray has the same problem. Bundling the same entry withsourceMap: truegave a differentsourcesorder on almost every run, with bothbundle()andbundleAsync()and on both platforms (299–300 distinct orders in 300 runs, e.g.entry,F,G,J,E,B,H,I,C,A,D).With two imports it's less frequent but still common:
bundle()gaveBAin 10 of 200 runs, andbundleAsync()with an asyncreadresolver in 31 of 200.Cause
In
src/bundler.rs:par_iter_mut(), L476).load_fileassigns each file the next index inself.stylesheetswhen it takes the lock (L416), so sibling imports get indexes in whatever order the threads finish.self.order()/self.inline(), L306–L310), so the rule output is stable.license_commentsare collected by iteratingself.stylesheetsin index order (L330), so they follow the order the threads happened to finish.sources(L312) andsource_map_urls(L322) are collected the same way, which explains the source-mapsourcesorder above.test_license_commentsonly has a single import, so it can't catch this.Possible fix
Collect
license_comments,sourcesandsource_map_urlsin a deterministic order, independent of thread scheduling. A depth-first walk of the import graph in source order would keep the ordertest_license_commentsalready expects (the importing file's comment first, then its imports' comments).🤖 Generated with Claude Code
All reactions