Skip to content

Commit b0d96c4

Browse files
committed
fix: use map module order for checkSplit order-constraint filter
orderConstraints filters out constraint pairs where the binding's statement index exceeds the reader's, which correctly reflects source order when analyzing the original unsplit file. checkSplit instead analyzes the joined text from joinClientRuntime, where modules are concatenated in chain evaluation order. When the chain inverts two modules relative to the original source, the binding appears after the reader in the joined text, the filter removes the pair, and the order violation goes undetected. Fix by computing a sourceOrder mapping from the map's module order (which mirrors the original single-file layout) and passing it to orderConstraints. Within each module the joined text preserves the original statement order; the map's module rank provides the correct inter-module ordering. When the map is unavailable (e.g., test fixtures), the filter falls back to the joined text order.
1 parent fdaf44b commit b0d96c4

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

‎scripts/split-runtime.mjs‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ function reachIndex(model) {
666666
* bundle with the read and never folds. `inline`: a primitive `const` above a
667667
* later read, unless Rolldown inlines it anywhere.
668668
*/
669-
export function orderConstraints(model, map) {
669+
export function orderConstraints(model, map, { sourceOrder } = {}) {
670670
const writers = writerIndex(model);
671671
const reach = reachIndex(model);
672672
// What every rendering bundle keeps: the intersection of what each root
@@ -684,7 +684,9 @@ export function orderConstraints(model, map) {
684684
for (const [name, r] of reader.refs) {
685685
if (!r.read) continue;
686686
const binding = model.unitByName.get(name);
687-
if (binding.order > reader.order) continue;
687+
const bPos = sourceOrder?.get(binding.id) ?? binding.order;
688+
const rPos = sourceOrder?.get(reader.id) ?? reader.order;
689+
if (bPos > rPos) continue;
688690
if (isMutable(binding)) {
689691
const kept = reach(reader);
690692
const written = (writers.get(binding.id) ?? []).some(
@@ -813,6 +815,23 @@ export function checkSplit(src = RUNTIME, rootEntries = ROOT_ENTRIES) {
813815
const moduleOf = new Map(
814816
model.units.map((u) => [u.id, moduleAt(model.sf.statements[u.statements[0]].getStart())]),
815817
);
818+
// The joined text orders modules by evaluation chain, but the source-order
819+
// filter in orderConstraints must reflect the original single-file layout.
820+
// Use the map's module order (which mirrors the unsplit source) when available;
821+
// fall back to the joined text order when the map does not cover the modules.
822+
let sourceOrder;
823+
try {
824+
const map = readMap();
825+
const rank = new Map(map.modules.map((m, i) => [m.name, i]));
826+
if (model.units.every((u) => rank.has(moduleOf.get(u.id)))) {
827+
const stride = model.sf.statements.length;
828+
sourceOrder = new Map(
829+
model.units.map((u) => [u.id, rank.get(moduleOf.get(u.id)) * stride + u.order]),
830+
);
831+
}
832+
} catch {
833+
/* map unavailable; model order is the fallback */
834+
}
816835
const violations = moduleRules(model, moduleOf);
817836
const position = new Map(chain.map((m, i) => [m, i]));
818837
const offChain = new Set(OFF_CHAIN);
@@ -825,7 +844,7 @@ export function checkSplit(src = RUNTIME, rootEntries = ROOT_ENTRIES) {
825844
offChain.add(m);
826845
}
827846
}
828-
for (const c of orderConstraints(model, { rootEntries })) {
847+
for (const c of orderConstraints(model, { rootEntries }, { sourceOrder })) {
829848
const [a, b] = [moduleOf.get(c.before.id), moduleOf.get(c.after.id)];
830849
if (a === b || !position.has(a) || !position.has(b)) continue;
831850
if (position.get(a) > position.get(b))

0 commit comments

Comments
 (0)