From 78441368328b0fe60aa46f69bb36ee26af97c8ae Mon Sep 17 00:00:00 2001 From: Andrea Bueide Date: Fri, 9 Oct 2026 10:14:46 -0500 Subject: [PATCH] fix(core): stamp libraryInfo.version with the version being released Since #1141 stopped committing version bumps, packages/core/package.json on master stayed at 2.21.4, and the build that runs before semantic-release baked that into info.ts/info.js. Every release since 2.22.0 reports context.library.version 2.21.4. Add a local semantic-release prepare plugin for core that runs after @semantic-release/npm's prepare, rebuilds with SEGMENT_LIBRARY_VERSION set to nextRelease.version, and refuses to publish if src/info.ts or lib/*/info.js don't carry that version. constants-generator.js reads the env var and falls back to package.json for local builds. Co-Authored-By: Claude Opus 5.5 (1M context) --- .eslintignore | 3 ++- devbox.lock | 2 +- packages/core/constants-generator.js | 13 +++++----- packages/core/release-stamp-version.js | 33 ++++++++++++++++++++++++++ packages/core/release.config.js | 8 +++++++ 5 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 packages/core/release-stamp-version.js diff --git a/.eslintignore b/.eslintignore index 4a3581756..9190755ae 100644 --- a/.eslintignore +++ b/.eslintignore @@ -9,4 +9,5 @@ examples/ **/*.config.js **/*.config.base.js .eslintrc.js -constants-generator.js \ No newline at end of file +constants-generator.js +release-stamp-version.js \ No newline at end of file diff --git a/devbox.lock b/devbox.lock index 63e5a592d..8fd623830 100644 --- a/devbox.lock +++ b/devbox.lock @@ -199,7 +199,7 @@ }, "nodejs@22": { "last_modified": "2026-03-27T11:17:38Z", - "plugin_version": "0.0.2", + "plugin_version": "0.0.4", "resolved": "github:NixOS/nixpkgs/832efc09b4caf6b4569fbf9dc01bec3082a00611#nodejs_22", "source": "devbox-search", "version": "22.22.2", diff --git a/packages/core/constants-generator.js b/packages/core/constants-generator.js index 314ac7f50..53baef5c0 100644 --- a/packages/core/constants-generator.js +++ b/packages/core/constants-generator.js @@ -1,17 +1,16 @@ const fs = require('fs'); const packageJson = require('./package.json'); +// The release passes the computed version here because package.json on master is not bumped (see release-stamp-version.js). +const version = process.env.SEGMENT_LIBRARY_VERSION || packageJson.version; + const body = ` export const libraryInfo = { name: '${packageJson.name}', - version: '${packageJson.version}', + version: '${version}', } `; -fs.writeFile('./src/info.ts', body, function (err) { - if (err) { - return console.log(err); - } +fs.writeFileSync('./src/info.ts', body); - console.log('Configuration file has generated'); -}); +console.log(`Configuration file has generated (version ${version})`); diff --git a/packages/core/release-stamp-version.js b/packages/core/release-stamp-version.js new file mode 100644 index 000000000..f44edc222 --- /dev/null +++ b/packages/core/release-stamp-version.js @@ -0,0 +1,33 @@ +const { execFileSync } = require('child_process'); +const fs = require('fs'); +const path = require('path'); + +const STAMPED_FILES = [ + 'src/info.ts', + 'lib/commonjs/info.js', + 'lib/module/info.js', +]; + +// Local semantic-release plugin: rebuilds core with the version semantic-release just computed, so the published lib/ reports it. +async function prepare(_pluginConfig, { cwd, nextRelease, logger }) { + const { version } = nextRelease; + logger.log(`Stamping ${version} into libraryInfo and rebuilding`); + + execFileSync('yarn', ['build'], { + cwd, + stdio: 'inherit', + env: { ...process.env, SEGMENT_LIBRARY_VERSION: version }, + }); + + const quoted = new RegExp(`['"]${version.replace(/[.+]/g, '\\$&')}['"]`); + for (const file of STAMPED_FILES) { + const contents = fs.readFileSync(path.join(cwd, file), 'utf8'); + if (!quoted.test(contents)) { + throw new Error( + `${file} is not stamped with ${version}; refusing to publish` + ); + } + } +} + +module.exports = { prepare }; diff --git a/packages/core/release.config.js b/packages/core/release.config.js index 94ad927e6..432361c70 100644 --- a/packages/core/release.config.js +++ b/packages/core/release.config.js @@ -1,3 +1,11 @@ +const { plugins } = require('../../release.config.js'); + module.exports = { extends: ['../../release.config.js'], + // Runs right after @semantic-release/npm's prepare (version bump) and before its publish. + plugins: plugins.flatMap((plugin) => + [].concat(plugin)[0] === '@semantic-release/npm' + ? [plugin, './release-stamp-version.js'] + : [plugin] + ), };