Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(release): compare packed manifests deterministically (#6936)

Cindy Zhang1745a95d4962
diff --git a/scripts/release/verify-published.mjs b/scripts/release/verify-published.mjs
index e7fcdf9265..5e225f5581 100644
--- a/scripts/release/verify-published.mjs
+++ b/scripts/release/verify-published.mjs
@@ -8,11 +8,30 @@ import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {expandWorkspaceDirs} from '../lib/workspace-globs.mjs';
-function sha256(file) {
- return crypto
- .createHash('sha256')
- .update(fs.readFileSync(file))
- .digest('hex');
+function sha256(contents) {
+ return crypto.createHash('sha256').update(contents).digest('hex');
+}
+
+function canonicalJson(value) {
+ if (Array.isArray(value)) return value.map(canonicalJson);
+ if (value && typeof value === 'object') {
+ return Object.fromEntries(
+ Object.keys(value)
+ .sort()
+ .map(key => [key, canonicalJson(value[key])]),
+ );
+ }
+ return value;
+}
+
+function comparableContents(file, relative) {
+ const contents = fs.readFileSync(file);
+ if (relative !== 'package.json') return contents;
+
+ // pnpm resolves workspace dependencies concurrently, so packed JSON object
+ // insertion order can vary. Object keys are unordered; values and array order
+ // remain exact, while every other package file stays byte-for-byte strict.
+ return Buffer.from(JSON.stringify(canonicalJson(JSON.parse(contents))));
}
function files(root, directory = root, result = new Map()) {
@@ -20,10 +39,8 @@ function files(root, directory = root, result = new Map()) {
const absolute = path.join(directory, entry.name);
if (entry.isDirectory()) files(root, absolute, result);
else if (entry.isFile()) {
- result.set(
- path.relative(root, absolute).split(path.sep).join('/'),
- sha256(absolute),
- );
+ const relative = path.relative(root, absolute).split(path.sep).join('/');
+ result.set(relative, sha256(comparableContents(absolute, relative)));
}
}
return result;
diff --git a/scripts/release/verify-published.test.mjs b/scripts/release/verify-published.test.mjs
index 5bc182f027..2d57d1b0f4 100644
--- a/scripts/release/verify-published.test.mjs
+++ b/scripts/release/verify-published.test.mjs
@@ -22,7 +22,7 @@ it('accepts byte-identical package trees', () => {
'package.json': '{}\n',
'dist/index.js': 'export {};\n',
});
- expect(comparePackageTrees(local, published)).toEqual( []);
+ expect(comparePackageTrees(local, published)).toEqual([]);
});
it('reports missing, unexpected, and changed package bytes', () => {
@@ -36,9 +36,37 @@ it('reports missing, unexpected, and changed package bytes', () => {
'dist/index.js': 'published\n',
'dist/only-published.js': 'published\n',
});
- expect(comparePackageTrees(local, published)).toEqual( [
+ expect(comparePackageTrees(local, published)).toEqual([
'published package differs at dist/index.js',
'published package is missing dist/only-local.js',
'published package has unexpected dist/only-published.js',
]);
});
+
+it('ignores package.json object-key insertion order only', () => {
+ const local = tree({
+ 'package.json': JSON.stringify({
+ name: '@astryxdesign/core',
+ devDependencies: {react: '19.2.7', cli: '0.6.5'},
+ }),
+ });
+ const published = tree({
+ 'package.json': JSON.stringify({
+ devDependencies: {cli: '0.6.5', react: '19.2.7'},
+ name: '@astryxdesign/core',
+ }),
+ });
+ expect(comparePackageTrees(local, published)).toEqual([]);
+});
+
+it('still rejects package.json value and array-order changes', () => {
+ const local = tree({
+ 'package.json': JSON.stringify({files: ['dist', 'src'], version: '0.6.5'}),
+ });
+ const published = tree({
+ 'package.json': JSON.stringify({files: ['src', 'dist'], version: '0.6.4'}),
+ });
+ expect(comparePackageTrees(local, published)).toEqual([
+ 'published package differs at package.json',
+ ]);
+});