An open source design system that's fully customizable and agent ready
TypeScript
fix(release): publish stable packages dependencies first, so a failed dependency always holds back its dependents (#9657)
Cindy Zhang6fca41d16185
diff --git a/.github/scripts/release-publish-workflow.test.mjs b/.github/scripts/release-publish-workflow.test.mjsindex 945d1e6413..b4a814a558 100644--- a/.github/scripts/release-publish-workflow.test.mjs+++ b/.github/scripts/release-publish-workflow.test.mjs@@ -41,7 +41,11 @@ function writeExecutable(root, name, contents) {* unknown, so nothing is skipped) and no waits between attempts.*/function withPublisher(root) {- for (const file of ['publish-package.mjs', 'registry.mjs']) {+ for (const file of [+ 'publish-order.mjs',+ 'publish-package.mjs',+ 'registry.mjs',+ ]) {const target = path.join(root, 'scripts/npm', file);fs.mkdirSync(path.dirname(target), {recursive: true});fs.copyFileSync(path.join(ROOT, 'scripts/npm', file), target);@@ -192,6 +196,12 @@ function runStablePublish({failingPackage = '', failOnce = ''} = {}) {name: '@astryxdesign/theme-neutral',version: '0.7.7',});+ // Listed before Core and depends on it: the publish order moves it after.+ writeJson(root, 'packages/cli/package.json', {+ name: '@astryxdesign/cli',+ version: '0.7.7',+ dependencies: {'@astryxdesign/core': '0.7.7'},+ });// Publishes after core in the loop and peers on it.writeJson(root, 'packages/themes/stone/package.json', {name: '@astryxdesign/theme-stone',@@ -473,12 +483,34 @@ describe('stable and canary publication authority', () => {'Not publishing @astryxdesign/[email protected] because @astryxdesign/core failed',);expect(result.stdout).toMatch(- /Stable publish failed for: @astryxdesign\/core @astryxdesign\/theme-stone\./u,+ /Stable publish failed for: @astryxdesign\/core @astryxdesign\/cli @astryxdesign\/theme-stone\./u,);expect(result.published).not.toContain('publish ./packages/themes/stone');expect(result.published).toContain('publish ./packages/themes/neutral');});+ it('holds back a package listed before its failed dependency', () => {+ const result = runStablePublish({failingPackage: 'packages/core'});++ expect(result.stdout).toContain(+ 'Not publishing @astryxdesign/[email protected] because @astryxdesign/core failed',+ );+ expect(result.published).not.toContain('publish ./packages/cli');+ });++ it('publishes every dependency before the packages that use it', () => {+ const result = runStablePublish();+ const order = result.published+ .split('\n')+ .filter(Boolean)+ .map(line => line.split(' ')[1]);++ expect(order.indexOf('./packages/core')).toBeGreaterThanOrEqual(0);+ expect(order.indexOf('./packages/core')).toBeLessThan(+ order.indexOf('./packages/cli'),+ );+ });+it('retries a stable package whose first signature is rejected', () => {// Both publish; the step then reaches registry verification, which this// scratch root does not carry, so only the publish calls are asserted.diff --git a/.github/workflows/release.yml b/.github/workflows/release.ymlindex 14c0c29a95..6b40c9ba9b 100644--- a/.github/workflows/release.yml+++ b/.github/workflows/release.yml@@ -181,7 +181,10 @@ jobs:set -eupublished=0FAILED=""- for pkg_dir in packages/* packages/themes/*; do+ # Dependencies first, so the hold-back below sees every failed+ # dependency before the packages that use it.+ ORDER=$(node scripts/npm/publish-order.mjs packages/* packages/themes/*)+ for pkg_dir in $ORDER; do[ -f "$pkg_dir/package.json" ] || continue# Stable `latest` never includes canaryOnly packages (e.g. lab):# skip them explicitly so even an accidental `private: false` candiff --git a/scripts/npm/publish-order.mjs b/scripts/npm/publish-order.mjsnew file mode 100644index 0000000000..d2294a43b9--- /dev/null+++ b/scripts/npm/publish-order.mjs@@ -0,0 +1,75 @@+#!/usr/bin/env node+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file Order package directories so that each package comes after the+ * @astryxdesign packages it depends on or peers on.+ * @input Package directories as arguments; a directory without a+ * package.json is left out.+ * @output The directories, one per line, dependencies first: each step takes+ * the earliest listed package whose dependencies are already placed. Exit 1+ * on a cycle.+ * @position The stable publish step in .github/workflows/release.yml walks+ * this order. That step holds back a package whose dependency failed to+ * publish in the same run, which works only when every dependency is+ * published first.+ */++import fs from 'node:fs';+import path from 'node:path';+import {pathToFileURL} from 'node:url';++/**+ * Orders `packages` (`{dir, name, dependsOn}`, where `dependsOn` lists+ * package names) dependencies first: each step takes the earliest listed+ * package whose dependencies are already placed. Names outside `packages` are+ * ignored. Throws on a cycle.+ */+export function publishOrder(packages) {+ const byName = new Map(packages.map(pkg => [pkg.name, pkg]));+ const ordered = [];+ const placed = new Set();+ while (ordered.length < packages.length) {+ const next = packages.find(+ pkg =>+ !placed.has(pkg.name) &&+ pkg.dependsOn.every(name => !byName.has(name) || placed.has(name)),+ );+ if (!next) {+ const cycle = packages+ .filter(pkg => !placed.has(pkg.name))+ .map(pkg => pkg.name);+ throw new Error(`dependency cycle among: ${cycle.join(', ')}`);+ }+ ordered.push(next);+ placed.add(next.name);+ }+ return ordered;+}++function readPackage(dir) {+ const file = path.join(dir, 'package.json');+ if (!fs.existsSync(file)) return null;+ const manifest = JSON.parse(fs.readFileSync(file, 'utf8'));+ return {+ dir,+ name: manifest.name,+ dependsOn: Object.keys({+ ...manifest.dependencies,+ ...manifest.peerDependencies,+ }).filter(name => name.startsWith('@astryxdesign/')),+ };+}++if (+ process.argv[1] &&+ import.meta.url === pathToFileURL(process.argv[1]).href+) {+ try {+ const packages = process.argv.slice(2).map(readPackage).filter(Boolean);+ for (const pkg of publishOrder(packages)) console.log(pkg.dir);+ } catch (error) {+ console.error(error.message);+ process.exit(1);+ }+}diff --git a/scripts/npm/publish-order.test.mjs b/scripts/npm/publish-order.test.mjsnew file mode 100644index 0000000000..679a3157e7--- /dev/null+++ b/scripts/npm/publish-order.test.mjs@@ -0,0 +1,66 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file The stable publish order: dependencies before the packages that use+ * them (scripts/npm/publish-order.mjs).+ */++import {spawnSync} from 'node:child_process';+import path from 'node:path';+import {describe, expect, it} from 'vitest';++import {publishOrder} from './publish-order.mjs';++const ROOT = path.resolve(import.meta.dirname, '../..');+const pkg = (dir, name, dependsOn = []) => ({dir, name, dependsOn});+const dirs = packages => packages.map(entry => entry.dir);++describe('publishOrder', () => {+ it('puts a package after the packages it depends on, wherever they were listed', () => {+ const order = publishOrder([+ pkg('cli', 'cli', ['core', 'theme']),+ pkg('core', 'core'),+ pkg('theme', 'theme', ['core']),+ ]);+ expect(dirs(order)).toEqual(['core', 'theme', 'cli']);+ });++ it('keeps the given order between packages that do not depend on each other', () => {+ const order = publishOrder([+ pkg('b', 'b'),+ pkg('a', 'a'),+ pkg('c', 'c', ['a']),+ ]);+ expect(dirs(order)).toEqual(['b', 'a', 'c']);+ });++ it('ignores dependencies outside the listed packages', () => {+ const order = publishOrder([pkg('theme', 'theme', ['react', 'lab'])]);+ expect(dirs(order)).toEqual(['theme']);+ });++ it('refuses a dependency cycle instead of guessing an order', () => {+ expect(() =>+ publishOrder([pkg('a', 'a', ['b']), pkg('b', 'b', ['a'])]),+ ).toThrow('dependency cycle among: a, b');+ });++ it("orders this repository's packages so the CLI comes after Core and its theme", () => {+ const result = spawnSync(+ process.execPath,+ [+ 'scripts/npm/publish-order.mjs',+ 'packages/cli',+ 'packages/core',+ 'packages/themes/neutral',+ ],+ {cwd: ROOT, encoding: 'utf8'},+ );+ expect(result.status).toBe(0);+ expect(result.stdout.trim().split('\n')).toEqual([+ 'packages/core',+ 'packages/themes/neutral',+ 'packages/cli',+ ]);+ });+});