Skip to content

Commit

Permalink
fix: solve nondeterminism in rollup2 output order
Browse files Browse the repository at this point in the history
Just sort the chunks once they arrive.
  • Loading branch information
michaelfig committed May 14, 2021
1 parent 2da7f41 commit c72b52d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/SwingSet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
"require": [
"esm"
],
"timeout": "2m"
"timeout": "20m"
},
"prettier": {
"trailingComma": "all",
Expand Down
14 changes: 13 additions & 1 deletion packages/SwingSet/test/test-transcript.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,19 @@ test('transcript-one save', async t => {
const states2 = await buildTrace(c2, storage2);

states1.forEach((s, i) => {
t.deepEqual(s, states2[i]);
// Too expensive! If there is a difference in the 3MB data, AVA will spin
// for a long time trying to compute a "minimal" diff.
// t.deepEqual(s, states2[i]);

// Instead, we just do simple comparison. Leave investigation to the
// experts.
const s2 = states2[i];
const extra = new Set(Object.keys(s2));
for (const k of Object.keys(s)) {
t.assert(s[k] === s2[k], `states[${i}][${k}] differs`);
extra.delete(k);
}
t.deepEqual([...extra.keys()], [], `states2[${i}] has missing keys`);
});
});

Expand Down
17 changes: 15 additions & 2 deletions packages/bundle-source/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default async function bundleSource(
// console.log(output);

// Create a source bundle.
const sourceBundle = {};
const unsortedSourceBundle = {};
let entrypoint;
await Promise.all(
output.map(async chunk => {
Expand All @@ -203,7 +203,7 @@ export default async function bundleSource(
sourceMap: chunk.map,
useLocationUnmap,
});
sourceBundle[fileName] = transformedCode;
unsortedSourceBundle[fileName] = transformedCode;

// console.log(`==== sourceBundle[${fileName}]\n${sourceBundle[fileName]}\n====`);
}),
Expand All @@ -213,6 +213,19 @@ export default async function bundleSource(
throw Error('No entrypoint found in output bundle');
}

const strcmp = (a, b) => {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
};
const sourceBundle = Object.fromEntries(
Object.entries(unsortedSourceBundle).sort(([ka], [kb]) => strcmp(ka, kb)),
);

// 'sourceBundle' is now an object that contains multiple programs, which references
// require() and sets module.exports . This is close, but we need a single
// stringifiable function, so we must wrap it in an outer function that
Expand Down

0 comments on commit c72b52d

Please sign in to comment.