From 6f4da3c458b3ffa0a33190fbab883a01747b5a84 Mon Sep 17 00:00:00 2001 From: Zahin Mohammad Date: Fri, 23 Oct 2026 14:32:27 -0400 Subject: [PATCH 1/2] fix(scripts): skip occupied beta versions TICKET: WCN-3802 --- docs/beta-release.md | 3 +- scripts/prepare-release.ts | 20 ++++++--- scripts/prepareRelease/distTags.ts | 42 +++++++++++++++++ .../prepareRelease/version-allocation.test.js | 45 +++++++++++++++++++ scripts/verify-release.ts | 1 + 5 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 scripts/tests/prepareRelease/version-allocation.test.js 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..796629d63e8 100644 --- a/scripts/prepare-release.ts +++ b/scripts/prepare-release.ts @@ -23,6 +23,7 @@ import { getLernaModules, changeScopeInFile, setDependencyVersion, + findFirstUnpublishedVersion, DistTags, LernaModule, } from './prepareRelease'; @@ -94,12 +95,12 @@ 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 { +): Promise { const json = readModulePackageJson(module); let prevTag: string | undefined = undefined; @@ -117,11 +118,16 @@ function incrementVersionsForModuleLocation( } if (prevTag) { - const next = inc(prevTag, 'prerelease', undefined, preid); + const initialCandidate = inc(prevTag, 'prerelease', undefined, preid); assert( - typeof next === 'string', + typeof initialCandidate === 'string', `Failed to increment version for ${json.name} prevTag=${prevTag}`, ); + const next = await findFirstUnpublishedVersion( + json.name, + initialCandidate, + preid, + ); console.log(`Setting next version for ${json.name} to ${next}`); json.version = next; writeModulePackageJson(module, json); @@ -163,18 +169,18 @@ async function incrementVersions( for (const m of lernaModules) { try { - incrementVersionsForModuleLocation( + await incrementVersionsForModuleLocation( preid, m, distTags.get(m), lernaModules, ); } 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..61d4ad279f8 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; @@ -17,6 +18,47 @@ export async function getDistTags(packageName: string): Promise { 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; diff --git a/scripts/tests/prepareRelease/version-allocation.test.js b/scripts/tests/prepareRelease/version-allocation.test.js new file mode 100644 index 00000000000..3709410e29e --- /dev/null +++ b/scripts/tests/prepareRelease/version-allocation.test.js @@ -0,0 +1,45 @@ +require('tsx/cjs'); + +const assert = require('node:assert/strict'); +const { + findFirstUnpublishedVersion, +} = 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/, + ); + }); +}); 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 From ad730f5fca414b60acc56c3ca9988f1759192e05 Mon Sep 17 00:00:00 2001 From: Zahin Mohammad Date: Fri, 23 Oct 2026 15:07:35 -0400 Subject: [PATCH 2/2] fix(scripts): keep beta preparation fail-closed TICKET: WCN-3802 --- scripts/prepare-release.ts | 23 +++++++------ scripts/prepareRelease/distTags.ts | 10 +++--- .../prepareRelease/version-allocation.test.js | 32 +++++++++++++++++++ 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/scripts/prepare-release.ts b/scripts/prepare-release.ts index 796629d63e8..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'; @@ -24,6 +24,7 @@ import { changeScopeInFile, setDependencyVersion, findFirstUnpublishedVersion, + getDistTagsCache, DistTags, LernaModule, } from './prepareRelease'; @@ -100,6 +101,7 @@ async function incrementVersionsForModuleLocation( module: LernaModule, tags: DistTags | undefined, allModules: LernaModule[], + probeRegistry: boolean, ): Promise { const json = readModulePackageJson(module); @@ -112,22 +114,22 @@ async 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 initialCandidate = 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 initialCandidate === 'string', `Failed to increment version for ${json.name} prevTag=${prevTag}`, ); - const next = await findFirstUnpublishedVersion( - json.name, - initialCandidate, - preid, - ); + 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); @@ -165,6 +167,8 @@ 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) { @@ -174,6 +178,7 @@ async function incrementVersions( m, distTags.get(m), lernaModules, + probeRegistry, ); } catch (e) { console.error( diff --git a/scripts/prepareRelease/distTags.ts b/scripts/prepareRelease/distTags.ts index 61d4ad279f8..25f0d9c752c 100644 --- a/scripts/prepareRelease/distTags.ts +++ b/scripts/prepareRelease/distTags.ts @@ -12,6 +12,9 @@ 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()}`); } @@ -87,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 index 3709410e29e..8bd277131ae 100644 --- a/scripts/tests/prepareRelease/version-allocation.test.js +++ b/scripts/tests/prepareRelease/version-allocation.test.js @@ -3,6 +3,8 @@ require('tsx/cjs'); const assert = require('node:assert/strict'); const { findFirstUnpublishedVersion, + getDistTags, + getDistTagsForModuleNames, } = require('../../prepareRelease/distTags.ts'); describe('prerelease version allocation', function () { @@ -42,4 +44,34 @@ describe('prerelease version allocation', function () { /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; + } + }); });