Skip to content

Commit 1745a95

Browse files
authored
fix(release): compare packed manifests deterministically (#6936)
1 parent fdc76dc commit 1745a95

2 files changed

Lines changed: 56 additions & 11 deletions

File tree

‎scripts/release/verify-published.mjs‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,39 @@ import path from 'node:path';
88
import {fileURLToPath} from 'node:url';
99
import {expandWorkspaceDirs} from '../lib/workspace-globs.mjs';
1010

11-
function sha256(file) {
12-
return crypto
13-
.createHash('sha256')
14-
.update(fs.readFileSync(file))
15-
.digest('hex');
11+
function sha256(contents) {
12+
return crypto.createHash('sha256').update(contents).digest('hex');
13+
}
14+
15+
function canonicalJson(value) {
16+
if (Array.isArray(value)) return value.map(canonicalJson);
17+
if (value && typeof value === 'object') {
18+
return Object.fromEntries(
19+
Object.keys(value)
20+
.sort()
21+
.map(key => [key, canonicalJson(value[key])]),
22+
);
23+
}
24+
return value;
25+
}
26+
27+
function comparableContents(file, relative) {
28+
const contents = fs.readFileSync(file);
29+
if (relative !== 'package.json') return contents;
30+
31+
// pnpm resolves workspace dependencies concurrently, so packed JSON object
32+
// insertion order can vary. Object keys are unordered; values and array order
33+
// remain exact, while every other package file stays byte-for-byte strict.
34+
return Buffer.from(JSON.stringify(canonicalJson(JSON.parse(contents))));
1635
}
1736

1837
function files(root, directory = root, result = new Map()) {
1938
for (const entry of fs.readdirSync(directory, {withFileTypes: true})) {
2039
const absolute = path.join(directory, entry.name);
2140
if (entry.isDirectory()) files(root, absolute, result);
2241
else if (entry.isFile()) {
23-
result.set(
24-
path.relative(root, absolute).split(path.sep).join('/'),
25-
sha256(absolute),
26-
);
42+
const relative = path.relative(root, absolute).split(path.sep).join('/');
43+
result.set(relative, sha256(comparableContents(absolute, relative)));
2744
}
2845
}
2946
return result;

‎scripts/release/verify-published.test.mjs‎

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ it('accepts byte-identical package trees', () => {
2222
'package.json': '{}\n',
2323
'dist/index.js': 'export {};\n',
2424
});
25-
expect(comparePackageTrees(local, published)).toEqual( []);
25+
expect(comparePackageTrees(local, published)).toEqual([]);
2626
});
2727

2828
it('reports missing, unexpected, and changed package bytes', () => {
@@ -36,9 +36,37 @@ it('reports missing, unexpected, and changed package bytes', () => {
3636
'dist/index.js': 'published\n',
3737
'dist/only-published.js': 'published\n',
3838
});
39-
expect(comparePackageTrees(local, published)).toEqual( [
39+
expect(comparePackageTrees(local, published)).toEqual([
4040
'published package differs at dist/index.js',
4141
'published package is missing dist/only-local.js',
4242
'published package has unexpected dist/only-published.js',
4343
]);
4444
});
45+
46+
it('ignores package.json object-key insertion order only', () => {
47+
const local = tree({
48+
'package.json': JSON.stringify({
49+
name: '@astryxdesign/core',
50+
devDependencies: {react: '19.2.7', cli: '0.6.5'},
51+
}),
52+
});
53+
const published = tree({
54+
'package.json': JSON.stringify({
55+
devDependencies: {cli: '0.6.5', react: '19.2.7'},
56+
name: '@astryxdesign/core',
57+
}),
58+
});
59+
expect(comparePackageTrees(local, published)).toEqual([]);
60+
});
61+
62+
it('still rejects package.json value and array-order changes', () => {
63+
const local = tree({
64+
'package.json': JSON.stringify({files: ['dist', 'src'], version: '0.6.5'}),
65+
});
66+
const published = tree({
67+
'package.json': JSON.stringify({files: ['src', 'dist'], version: '0.6.4'}),
68+
});
69+
expect(comparePackageTrees(local, published)).toEqual([
70+
'published package differs at package.json',
71+
]);
72+
});

0 commit comments

Comments
 (0)