diff --git a/docs/beta-release.md b/docs/beta-release.md index 1f7f6c253e7..3feb61b66c7 100644 --- a/docs/beta-release.md +++ b/docs/beta-release.md @@ -38,7 +38,8 @@ For each module, the script: - If a beta tag exists and its base version >= latest, use the beta tag as base - If the beta tag's base version < latest, start a new prerelease from latest - If no beta tag exists, create one from latest -3. Computes the next version via `semver.inc(prevTag, 'prerelease', preid)` +3. Computes a candidate via `semver.inc(prevTag, 'prerelease', preid)` +4. Checks the exact package/version endpoint and keeps incrementing until the candidate is not already published Example: `8.2.1-beta.1009` → `8.2.1-beta.1010` diff --git a/scripts/prepare-release.ts b/scripts/prepare-release.ts index 20352f2ce23..bfc7f6fb5fa 100644 --- a/scripts/prepare-release.ts +++ b/scripts/prepare-release.ts @@ -11,7 +11,7 @@ * Usage: npx tsx scripts/prepare-release.ts [preid] --scope [scope] --root-dir [dir] */ import assert from 'node:assert'; -import { readFileSync, writeFileSync } from 'fs'; +import { existsSync, readFileSync, writeFileSync } from 'fs'; import path from 'path'; import { inc, lt } from 'semver'; import yargs from 'yargs'; @@ -23,6 +23,8 @@ import { getLernaModules, changeScopeInFile, setDependencyVersion, + findFirstUnpublishedVersion, + getDistTagsCache, DistTags, LernaModule, } from './prepareRelease'; @@ -94,12 +96,13 @@ function writeModulePackageJson(module: LernaModule, json: any): void { * @param {LernaModule[]} allModules - All modules for dependency updates * @returns {String|undefined} - The new version if set, undefined otherwise */ -function incrementVersionsForModuleLocation( +async function incrementVersionsForModuleLocation( preid: string, module: LernaModule, tags: DistTags | undefined, allModules: LernaModule[], -): string | undefined { + probeRegistry: boolean, +): Promise { const json = readModulePackageJson(module); let prevTag: string | undefined = undefined; @@ -111,17 +114,22 @@ function incrementVersionsForModuleLocation( prevTag = lt(version[0], latest[0]) ? `${tags.latest}-${preid}` : tags[preid]; - } else { + } else if (tags.latest) { prevTag = `${tags.latest}-${preid}`; } } - if (prevTag) { - const next = inc(prevTag, 'prerelease', undefined, preid); + if (prevTag || (tags && Object.keys(tags).length === 0)) { + const initialCandidate = prevTag + ? inc(prevTag, 'prerelease', undefined, preid) + : json.version; assert( - typeof next === 'string', + typeof initialCandidate === 'string', `Failed to increment version for ${json.name} prevTag=${prevTag}`, ); + const next = probeRegistry + ? await findFirstUnpublishedVersion(json.name, initialCandidate, preid) + : initialCandidate; console.log(`Setting next version for ${json.name} to ${next}`); json.version = next; writeModulePackageJson(module, json); @@ -159,22 +167,25 @@ async function incrementVersions( preid: string, lernaModules: LernaModule[], ): Promise { + const cachePath = getDistTagsCache(); + const probeRegistry = !(cachePath && existsSync(cachePath)); const distTags = await getDistTagsForModules(lernaModules); for (const m of lernaModules) { try { - incrementVersionsForModuleLocation( + await incrementVersionsForModuleLocation( preid, m, distTags.get(m), lernaModules, + probeRegistry, ); } catch (e) { - // it's not necessarily a blocking error. Let lerna try and publish anyways - console.warn( + console.error( `Couldn't set next version for ${m.name} at ${m.location}`, e, ); + throw e; } } } diff --git a/scripts/prepareRelease/distTags.ts b/scripts/prepareRelease/distTags.ts index 46c4dad9a97..25f0d9c752c 100644 --- a/scripts/prepareRelease/distTags.ts +++ b/scripts/prepareRelease/distTags.ts @@ -1,5 +1,6 @@ import { readFileSync, writeFileSync, existsSync } from 'fs'; import path from 'path'; +import { inc } from 'semver'; import { LernaModule } from './getLernaModules'; export type DistTags = Record; @@ -11,12 +12,56 @@ export async function getDistTags(packageName: string): Promise { console.log(`Fetching dist tags for ${packageName}`); const url = `https://registry.npmjs.org/-/package/${packageName}/dist-tags`; const response = await fetch(url); + if (response.status === 404) { + return {}; + } if (!response.ok) { throw new Error(`Failed ${url}: ${response.status} ${response.statusText} ${await response.text()}`); } return response.json(); } +export type PackageVersionExists = ( + packageName: string, + version: string, +) => Promise; + +export const packageVersionExists: PackageVersionExists = async ( + packageName, + version, +) => { + const url = `https://registry.npmjs.org/${encodeURIComponent(packageName)}/${encodeURIComponent(version)}`; + const response = await fetch(url); + if (response.status === 404 || response.status === 410) { + return false; + } + if (!response.ok) { + throw new Error( + `Failed ${url}: ${response.status} ${response.statusText} ${await response.text()}`, + ); + } + return true; +}; + +export async function findFirstUnpublishedVersion( + packageName: string, + initialCandidate: string, + preid: string, + versionExists: PackageVersionExists = packageVersionExists, +): Promise { + let candidate = initialCandidate; + while (await versionExists(packageName, candidate)) { + const next = inc(candidate, 'prerelease', undefined, preid); + if (!next) { + throw new Error( + `Failed to increment occupied version ${packageName}@${candidate}`, + ); + } + candidate = next; + } + return candidate; +} + // Add this function to read from cache export function getDistTagsCache(): string | undefined { return process.env.BITGO_PREPARE_RELEASE_CACHE_DIST_TAGS; @@ -45,12 +90,7 @@ export async function getDistTagsForModuleNames(moduleNames: string[]): Promise< console.warn(`Skipping ${moduleName} as it's not published to npm`); return []; } - try { - return [[moduleName, await getDistTags(moduleName)]]; - } catch (e) { - console.warn(`Failed to fetch dist tags for ${moduleName}`, e); - return []; - } + return [[moduleName, await getDistTags(moduleName)]]; }) ) ).flat() diff --git a/scripts/tests/prepareRelease/version-allocation.test.js b/scripts/tests/prepareRelease/version-allocation.test.js new file mode 100644 index 00000000000..8bd277131ae --- /dev/null +++ b/scripts/tests/prepareRelease/version-allocation.test.js @@ -0,0 +1,77 @@ +require('tsx/cjs'); + +const assert = require('node:assert/strict'); +const { + findFirstUnpublishedVersion, + getDistTags, + getDistTagsForModuleNames, +} = require('../../prepareRelease/distTags.ts'); + +describe('prerelease version allocation', function () { + it('keeps an unoccupied candidate', async function () { + const version = await findFirstUnpublishedVersion( + '@bitgo-beta/sdk-core', + '1.2.3-beta.8', + 'beta', + async () => false, + ); + + assert.equal(version, '1.2.3-beta.8'); + }); + + it('skips consecutive occupied candidates', async function () { + const occupied = new Set(['1.2.3-beta.8', '1.2.3-beta.9']); + const version = await findFirstUnpublishedVersion( + '@bitgo-beta/sdk-core', + '1.2.3-beta.8', + 'beta', + async (_name, candidate) => occupied.has(candidate), + ); + + assert.equal(version, '1.2.3-beta.10'); + }); + + it('fails instead of guessing when the registry probe fails', async function () { + await assert.rejects( + findFirstUnpublishedVersion( + '@bitgo-beta/sdk-core', + '1.2.3-beta.8', + 'beta', + async () => { + throw new Error('registry unavailable'); + }, + ), + /registry unavailable/, + ); + }); + + it('treats a missing dist-tag document as a new package', async function () { + const originalFetch = global.fetch; + global.fetch = async () => + new Response('', { status: 404, statusText: 'Not Found' }); + + try { + assert.deepEqual(await getDistTags('@bitgo-beta/new-package'), {}); + } finally { + global.fetch = originalFetch; + } + }); + + it('fails instead of dropping a module when dist-tag lookup fails', async function () { + const originalFetch = global.fetch; + global.fetch = async () => + new Response('unavailable', { + status: 503, + statusText: 'Service Unavailable', + }); + + try { + await assert.rejects( + getDistTagsForModuleNames(['@bitgo-beta/sdk-core']), + /503 Service Unavailable/, + ); + } finally { + global.fetch = originalFetch; + } + }); +}); diff --git a/scripts/verify-release.ts b/scripts/verify-release.ts index 4b74d9274f9..60a3f39d777 100644 --- a/scripts/verify-release.ts +++ b/scripts/verify-release.ts @@ -101,6 +101,7 @@ async function verify(preid?: string) { if (!(await verifyPackage(dir, preid))) { console.error('Failed to verify outstanding packages.'); if (!RECOVERY_MODE) { + process.exitCode = 1; return; } // keep bumping/retrying the rest of the packages instead of aborting on the first stuck one