Skip to content

Commit cc1ac1e

Browse files
committed
src: dump snapshot source with node:generate_default_snapshot_source
This helps diffing snapshots when the reproducibility gets broken.
1 parent dcb9573 commit cc1ac1e

File tree

3 files changed

+20
-35
lines changed

3 files changed

+20
-35
lines changed

src/node.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,20 @@ ExitCode GenerateAndWriteSnapshotData(const SnapshotData** snapshot_data_ptr,
13741374
DCHECK(snapshot_config.builder_script_path.has_value());
13751375
const std::string& builder_script =
13761376
snapshot_config.builder_script_path.value();
1377+
1378+
// For the special builder node:generate_default_snapshot_source, generate
1379+
// the snapshot as C++ source and write it to snapshot.cc (for testing).
1380+
if (builder_script == "node:generate_default_snapshot_source") {
1381+
// Reset to empty to generate from scratch.
1382+
snapshot_config.builder_script_path = {};
1383+
exit_code = node::SnapshotBuilder::GenerateAsSource("snapshot.cc",
1384+
args_maybe_patched,
1385+
result->exec_args(),
1386+
snapshot_config,
1387+
true /* use_array_literals */);
1388+
return exit_code;
1389+
}
1390+
13771391
// node:embedded_snapshot_main indicates that we are using the
13781392
// embedded snapshot and we are not supposed to clean it up.
13791393
if (builder_script == "node:embedded_snapshot_main") {

test/fixtures/exports.cjs

Whitespace-only changes.

test/parallel/test-snapshot-reproducible.js

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ const tmpdir = require('../common/tmpdir');
66
const fs = require('fs');
77
const assert = require('assert');
88

9-
// When the test fails this helper can be modified to write outputs
10-
// differently and aid debugging.
11-
function log(line) {
12-
console.log(line);
13-
}
14-
159
function generateSnapshot() {
1610
tmpdir.refresh();
1711

@@ -21,7 +15,7 @@ function generateSnapshot() {
2115
'--random_seed=42',
2216
'--predictable',
2317
'--build-snapshot',
24-
'node:generate_default_snapshot',
18+
'node:generate_default_snapshot_source',
2519
],
2620
{
2721
env: { ...process.env, NODE_DEBUG_NATIVE: 'SNAPSHOT_SERDES' },
@@ -38,33 +32,10 @@ function generateSnapshot() {
3832
},
3933
}
4034
);
41-
const blobPath = tmpdir.resolve('snapshot.blob');
42-
return fs.readFileSync(blobPath);
35+
const outputPath = tmpdir.resolve('snapshot.cc');
36+
return fs.readFileSync(outputPath, 'utf-8').split('\n');
4337
}
4438

45-
const buf1 = generateSnapshot();
46-
const buf2 = generateSnapshot();
47-
48-
const diff = [];
49-
let offset = 0;
50-
const step = 16;
51-
do {
52-
const length = Math.min(buf1.length - offset, step);
53-
const slice1 = buf1.slice(offset, offset + length).toString('hex');
54-
const slice2 = buf2.slice(offset, offset + length).toString('hex');
55-
if (slice1 !== slice2) {
56-
diff.push({ offset: '0x' + (offset).toString(16), slice1, slice2 });
57-
}
58-
offset += length;
59-
} while (offset < buf1.length);
60-
61-
assert.strictEqual(offset, buf1.length);
62-
if (offset < buf2.length) {
63-
const length = Math.min(buf2.length - offset, step);
64-
const slice2 = buf2.slice(offset, offset + length).toString('hex');
65-
diff.push({ offset, slice1: '', slice2 });
66-
offset += length;
67-
} while (offset < buf2.length);
68-
69-
assert.deepStrictEqual(diff, []);
70-
assert.strictEqual(buf1.length, buf2.length);
39+
const source1 = generateSnapshot();
40+
const source2 = generateSnapshot();
41+
assert.deepStrictEqual(source1, source2);

0 commit comments

Comments
 (0)