-
Notifications
You must be signed in to change notification settings - Fork 351
Release: survive a single failed publish, and verify every package reached npm #3967
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pranaygp
wants to merge
2
commits into
main
Choose a base branch
from
pgp/release-publish-resilience
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| diff --git a/dist/changesets-cli.cjs.js b/dist/changesets-cli.cjs.js | ||
| index 0f03e31068a5a7f77ef03ee97684d7f4d5136367..b2bba73c0f10ddbf702fdd2d036277273e28d828 100644 | ||
| --- a/dist/changesets-cli.cjs.js | ||
| +++ b/dist/changesets-cli.cjs.js | ||
| @@ -783,7 +783,7 @@ async function internalPublish(packageJson, opts, twoFactorState) { | ||
| let json = getLastJsonObjectFromString(stderr.toString()) || getLastJsonObjectFromString(stdout.toString()); | ||
| if (json !== null && json !== void 0 && json.error) { | ||
| // The first case is no 2fa provided, the second is when the 2fa is wrong (timeout or wrong words) | ||
| - if ((json.error.code === "EOTP" || json.error.code === "E401" && json.error.detail.includes("--otp=<code>")) && !ciInfo.isCI) { | ||
| + if ((json.error.code === "EOTP" || json.error.code === "E401" && typeof json.error.detail === "string" && json.error.detail.includes("--otp=<code>")) && !ciInfo.isCI) { | ||
| if (twoFactorState.token !== null) { | ||
| // the current otp code must be invalid since it errored | ||
| twoFactorState.token = null; | ||
| @@ -792,7 +792,7 @@ async function internalPublish(packageJson, opts, twoFactorState) { | ||
| twoFactorState.isRequired = Promise.resolve(true); | ||
| return internalPublish(packageJson, opts, twoFactorState); | ||
| } | ||
| - logger.error(`an error occurred while publishing ${packageJson.name}: ${json.error.code}`, json.error.summary, json.error.detail ? "\n" + json.error.detail : ""); | ||
| + logger.error(`an error occurred while publishing ${packageJson.name}: ${json.error.code}`, json.error.summary ?? json.error.message ?? "", json.error.detail ? "\n" + json.error.detail : ""); | ||
| } | ||
| logger.error(stderr.toString() || stdout.toString()); | ||
| return { |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Fails when a publishable package version on this commit is not on npm. | ||
| * | ||
| * `changeset publish` runs one `pnpm publish` per package, concurrently, and | ||
| * reports the release as a whole. When one of those publishes fails the | ||
| * release is left half-shipped: some versions are live, others are not, and | ||
| * `workflow` can point at an `@workflow/core` that does not exist yet. The | ||
| * 5.0.0-beta.48 release shipped 7 of 21 packages on its first attempt, and | ||
| * the failure that stopped it (an E401 whose JSON had no `detail` field) | ||
| * crashed the publish loop before it printed which packages were affected. | ||
| * | ||
| * This script is the check the Release job runs after publishing, whatever | ||
| * the publish step's outcome: every non-private package that changesets does | ||
| * not ignore must have its manifest version in the registry's version list, | ||
| * and the branch's dist-tag (the pre-release tag from `.changeset/pre.json` | ||
| * while in pre mode, `latest` otherwise) must point at it. The invariant | ||
| * holds on every commit of a release branch, not only right after a publish, | ||
| * so a gap keeps failing the job until it is closed. | ||
| * | ||
| * The registry can lag a publish by a few seconds, so missing versions are | ||
| * re-checked a few times before they are reported. | ||
| * | ||
| * Usage: node scripts/check-published.mjs [--tag <dist-tag>] | ||
| */ | ||
|
|
||
| import { existsSync, readdirSync, readFileSync } from 'node:fs'; | ||
| import { dirname, join, resolve } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); | ||
| const REGISTRY = 'https://registry.npmjs.org'; | ||
| const ATTEMPTS = Number(process.env.CHECK_PUBLISHED_ATTEMPTS ?? 6); | ||
| const DELAY_MS = Number(process.env.CHECK_PUBLISHED_DELAY_MS ?? 10_000); | ||
|
|
||
| function readJson(path) { | ||
| return JSON.parse(readFileSync(path, 'utf8')); | ||
| } | ||
|
|
||
| function distTagForBranch() { | ||
| const argIndex = process.argv.indexOf('--tag'); | ||
| if (argIndex !== -1 && process.argv[argIndex + 1]) { | ||
| return process.argv[argIndex + 1]; | ||
| } | ||
| const prePath = join(root, '.changeset', 'pre.json'); | ||
| if (existsSync(prePath)) { | ||
| const pre = readJson(prePath); | ||
| if (pre.mode === 'pre' && pre.tag) return pre.tag; | ||
| } | ||
| return 'latest'; | ||
| } | ||
|
|
||
| function publishablePackages() { | ||
| const { ignore = [] } = readJson(join(root, '.changeset', 'config.json')); | ||
| const ignored = new Set(ignore.filter((name) => !name.includes('*'))); | ||
| const ignoredPatterns = ignore | ||
| .filter((name) => name.includes('*')) | ||
| .map( | ||
| (glob) => new RegExp(`^${glob.split('*').map(escapeRegExp).join('.*')}$`) | ||
| ); | ||
| const packagesDir = join(root, 'packages'); | ||
| const result = []; | ||
| for (const dir of readdirSync(packagesDir)) { | ||
| const manifestPath = join(packagesDir, dir, 'package.json'); | ||
| if (!existsSync(manifestPath)) continue; | ||
| const manifest = readJson(manifestPath); | ||
| if (!manifest.name || !manifest.version || manifest.private) continue; | ||
| if (ignored.has(manifest.name)) continue; | ||
| if (ignoredPatterns.some((re) => re.test(manifest.name))) continue; | ||
| result.push({ name: manifest.name, version: manifest.version }); | ||
| } | ||
| return result.sort((a, b) => a.name.localeCompare(b.name)); | ||
| } | ||
|
|
||
| function escapeRegExp(s) { | ||
| return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
|
|
||
| async function registryState(name) { | ||
| const url = `${REGISTRY}/${name.replace('/', '%2F')}`; | ||
| const response = await fetch(url, { | ||
| headers: { accept: 'application/json', 'cache-control': 'no-cache' }, | ||
| }); | ||
| if (response.status === 404) return { versions: {}, distTags: {} }; | ||
| if (!response.ok) { | ||
| throw new Error( | ||
| `GET ${url} failed: ${response.status} ${response.statusText}` | ||
| ); | ||
| } | ||
| const doc = await response.json(); | ||
| return { versions: doc.versions ?? {}, distTags: doc['dist-tags'] ?? {} }; | ||
| } | ||
|
|
||
| async function check(pkg, tag) { | ||
| const { versions, distTags } = await registryState(pkg.name); | ||
| const visible = Object.hasOwn(versions, pkg.version); | ||
| const tagged = distTags[tag] === pkg.version; | ||
| return { ...pkg, visible, tagged, actualTag: distTags[tag] }; | ||
| } | ||
|
|
||
| const tag = distTagForBranch(); | ||
| const packages = publishablePackages(); | ||
| let results = await Promise.all(packages.map((pkg) => check(pkg, tag))); | ||
|
|
||
| for (let attempt = 1; attempt < ATTEMPTS; attempt++) { | ||
| const pending = results.filter((r) => !(r.visible && r.tagged)); | ||
| if (pending.length === 0) break; | ||
| console.log( | ||
| `${pending.length} package(s) not yet visible with the "${tag}" tag; re-checking in ${DELAY_MS / 1000}s (attempt ${attempt + 1}/${ATTEMPTS})...` | ||
| ); | ||
| await new Promise((resolveDelay) => setTimeout(resolveDelay, DELAY_MS)); | ||
| const rechecked = await Promise.all(pending.map((r) => check(r, tag))); | ||
| results = results.map((r) => rechecked.find((n) => n.name === r.name) ?? r); | ||
| } | ||
|
|
||
| const width = Math.max(...results.map((r) => r.name.length)); | ||
| for (const r of results) { | ||
| const status = r.visible | ||
| ? r.tagged | ||
| ? 'ok' | ||
| : `visible, but "${tag}" is ${r.actualTag ?? 'unset'}` | ||
| : 'NOT ON NPM'; | ||
| console.log(`${r.name.padEnd(width)} ${r.version.padEnd(14)} ${status}`); | ||
| } | ||
|
|
||
| const missing = results.filter((r) => !r.visible); | ||
| const untagged = results.filter((r) => r.visible && !r.tagged); | ||
| if (missing.length === 0 && untagged.length === 0) { | ||
| console.log( | ||
| `\n✓ all ${results.length} publishable packages are on npm under the "${tag}" tag.` | ||
| ); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| console.error(''); | ||
| if (missing.length > 0) { | ||
| console.error( | ||
| `✗ ${missing.length} package version(s) on this commit are not on npm:` | ||
| ); | ||
| for (const r of missing) console.error(` ${r.name}@${r.version}`); | ||
| console.error( | ||
| '\n Read the publish step above for each one. If `pnpm publish` reported the\n' + | ||
| ' version as "previously staged", it is waiting for a maintainer to approve it in\n' + | ||
| " the package's Staged Packages tab on npmjs.com. Otherwise re-run the Release\n" + | ||
| ' workflow (workflow_dispatch); `changeset publish` skips versions already live.' | ||
| ); | ||
| } | ||
| if (untagged.length > 0) { | ||
| console.error( | ||
| `✗ ${untagged.length} package(s) are on npm but the "${tag}" dist-tag does not point at them:` | ||
| ); | ||
| for (const r of untagged) | ||
| console.error( | ||
| ` ${r.name}@${r.version} (tag is ${r.actualTag ?? 'unset'})` | ||
| ); | ||
| } | ||
| process.exit(1); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A transient npm registry error (5xx/429/network/malformed JSON) thrown from
registryState()propagates throughcheck()and rejects the top-levelPromise.all, crashing the check script and marking a fully-successful Release job red.