Files
copilotkit__copilotkit/scripts/release/prerelease.ts
Benjamin Taylor 710b5ad783 fix(release): report literal cross-scope pins, which scope=all cannot fix
Adversarial pass on the previous commit. The warning only inspected
`workspace:` ranges, so it missed the OTHER way a cross-scope pin goes stale: a
literal version range naming a package in another scope. `bumpPackages` rewrites
literal ranges for in-scope packages only, so such a pin survives every bump —
`scope=all` publishes the canary and the artifact still resolves the dependency's
last stable release, silently, which is the exact failure this warning exists to
surface.

`findCrossScopeWorkspaceDeps` becomes `findCrossScopePins`, reporting both shapes
tagged with a `reason`, and the literal case carries its own remedy (convert to
`workspace:`) instead of the useless "re-run with scope=all".

No such pin exists in the tree today — every cross-scope edge is `workspace:` —
so this closes a latent hole rather than a live one, in the one place a future
refactor would reintroduce it.

Also documents the multi-scope partial-failure mode in prerelease.ts: the
cross-scope graph has cycles, so no publish order avoids a package shipping
before the same-run version it pins, and npm's no-republish rule means a failed
run must be retried under a new suffix.
2026-07-24 16:08:15 -05:00

169 lines
5.8 KiB
TypeScript

/**
* Publish a prerelease to npm (publish-only, no build/test/bump).
*
* Version bumping is handled by bump-prerelease.ts in the secrets-free CI
* build job. Build and test also run there. This script receives pre-built,
* correctly-versioned artifacts and only performs the npm publish step.
*
* Always publishes with the "canary" dist-tag.
*
* Multi-scope caveat (scope=all): packages publish scope by scope, and the
* cross-scope dependency graph has cycles (runtime -> channels-intelligence,
* channels-core -> core), so NO order avoids publishing a package before the
* same-run version it pins. A run that dies partway therefore leaves published
* canaries pinning versions that never shipped — uninstallable until the rest
* lands. There is no resume: npm rejects republishing a version, so retry with a
* NEW suffix and abandon the half-published id.
*
* Usage: tsx scripts/release/prerelease.ts --scope <scope from release.config.json | all> [--dry-run]
*/
import { spawnSync } from "child_process";
import { getCurrentVersion, getPackagesForScope } from "./lib/versions.js";
import type { PublishablePackage } from "./lib/versions.js";
import { ALL_SCOPES, ROOT, loadConfig, resolveScopes } from "./lib/config.js";
import type { ReleaseScope } from "./lib/config.js";
import { emitGithubOutputs } from "./lib/github-output.js";
function run(cmd: string, args: string[], opts?: { cwd?: string }) {
const result = spawnSync(cmd, args, {
cwd: opts?.cwd ?? ROOT,
stdio: "inherit",
encoding: "utf8",
});
if (result.status !== 0) {
throw new Error(`Command failed: ${cmd} ${args.join(" ")}`);
}
return result;
}
// Valid scopes come from release.config.json — the single source of truth.
const VALID_SCOPES = Object.keys(loadConfig().scopes);
function main() {
const argv = process.argv.slice(2);
const dryRun = argv.includes("--dry-run");
const scopeIdx = argv.indexOf("--scope");
const selector = scopeIdx !== -1 ? argv[scopeIdx + 1] : null;
const usage = `Usage: prerelease.ts --scope <${[...VALID_SCOPES, ALL_SCOPES].join("|")}> [--dry-run]`;
if (!selector) {
console.error(usage);
process.exit(1);
}
let scopes: ReleaseScope[];
try {
scopes = resolveScopes(selector);
} catch (error) {
console.error(error instanceof Error ? error.message : error);
console.error(usage);
process.exit(1);
}
const config = loadConfig();
const distTag = config.prereleaseTag;
// Read the versions from package.json — already bumped by bump-prerelease.ts
// in the CI build job.
const scopeVersions = scopes.map((scope) => {
const version = getCurrentVersion(scope);
if (!version) {
console.error(
`Scope "${scope}" version source has no version field; refusing to publish.`,
);
process.exit(1);
}
return { scope, version };
});
// Union of every scope's packages, in per-scope publish order. Deduplicated by
// name: a package enrolled in two scopes must be published once, not twice
// (the second publish would fail on an already-taken version).
const packages: PublishablePackage[] = [];
const seen = new Set<string>();
for (const scope of scopes) {
for (const p of getPackagesForScope(scope)) {
if (seen.has(p.name)) continue;
seen.add(p.name);
packages.push(p);
}
}
if (packages.length === 0) {
console.error(
`No packages found for scope "${selector}" — refusing to emit a version for a publish that did nothing.`,
);
process.exit(1);
}
// `version` stays single-valued for the workflow's emitted-version guard and
// the stable-shaped summary; `versions` carries every scope for a multi-scope
// canary, where no single version describes the publish.
const publishVersion = scopeVersions[0].version;
const publishVersions = scopeVersions
.map(({ scope, version }) => `${scope}@${version}`)
.join(" ");
console.log(`Scope: ${selector} -> ${scopes.join(", ")}`);
console.log(`Publishing versions: ${publishVersions}`);
console.log(`Dist tag: ${distTag}`);
if (dryRun) {
console.log("\n[DRY RUN] Would publish these packages:");
for (const p of packages) {
console.log(` ${p.name}@${p.pkg.version}`);
}
// Emitting in dry-run is safe — the publish workflow gates both the
// publish step and the verify guard on `inputs.dry-run != true`, so this
// only serves local/e2e verification of the output contract.
emitGithubOutputs({
version: publishVersion,
versions: publishVersions,
scope: selector,
});
console.log("\n[DRY RUN] Exiting.");
return;
}
// NOTE: Version bumping is handled by bump-prerelease.ts in the CI build
// job (no secrets). Build and test also run there.
// The publish job receives pre-built artifacts via download-artifact.
// We intentionally do NOT rebuild/retest here to keep NPM_TOKEN out
// of the build process tree.
// Publish each package via pnpm pack + npx npm@11 (OIDC-aware)
console.log("\nPublishing packages...");
for (const p of packages) {
console.log(
` Publishing ${p.name}@${p.pkg.version} with tag ${distTag}...`,
);
run("pnpm", ["pack"], { cwd: p.dir });
const tarball = `${p.name.replace("@", "").replace("/", "-")}-${p.pkg.version}.tgz`;
run(
"npx",
[
"--yes",
"npm@11.15.0",
"publish",
tarball,
"--tag",
distTag,
"--access",
"public",
],
{ cwd: p.dir },
);
}
// The workflow's "Verify publish step emitted version" guard and the
// prerelease summary read these from steps.publish.outputs.
emitGithubOutputs({
version: publishVersion,
versions: publishVersions,
scope: selector,
});
console.log(`\nPrerelease published: ${publishVersions} (tag: ${distTag})`);
}
main();