From bf0de19d6c5a4040386a1f4b2d3f4eeff257c6fe Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Fri, 27 Feb 2026 09:09:32 -0500 Subject: [PATCH] fix(repo): reset package.json files after local --local false release The local release path (--local false, non-CI) was not resetting the package.json files for angular-rspack, dotnet, and maven after nx release version modified them. This left unstaged changes behind. Extracts a shared resetPackageJsons() function, wraps the release steps in try/finally, and adds a SIGINT handler so files are restored even on error or ctrl+C. --- scripts/nx-release.ts | 120 ++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 58 deletions(-) diff --git a/scripts/nx-release.ts b/scripts/nx-release.ts index 4d9f979a421..3a28311ef00 100644 --- a/scripts/nx-release.ts +++ b/scripts/nx-release.ts @@ -73,6 +73,34 @@ const VALID_AUTHORS_FOR_LATEST = [ }); }; + const packagesToReset = [ + 'packages/angular-rspack', + 'packages/angular-rspack-compiler', + 'packages/dotnet', + 'packages/maven', + ]; + + const packageSnapshots: { [key: string]: string } = {}; + for (const packagePath of packagesToReset) { + const packageJsonPath = join(workspaceRoot, packagePath, 'package.json'); + const packageJson = readFileSync(packageJsonPath, 'utf-8'); + packageSnapshots[packagePath] = packageJson; + } + + const resetPackageJsons = () => { + for (const packagePath of packagesToReset) { + const packageJsonPath = join(workspaceRoot, packagePath, 'package.json'); + writeFileSync(packageJsonPath, packageSnapshots[packagePath]); + } + }; + + // Reset package.json files even on ctrl+C + process.on('SIGINT', () => { + console.log('\nReceived SIGINT, restoring package.json files...'); + resetPackageJsons(); + process.exit(1); + }); + // Intended for creating a github release which triggers the publishing workflow // This runs locally (not in CI) to create the GitHub release that triggers the actual publish workflow if (!options.local && !process.env.GITHUB_ACTIONS) { @@ -84,59 +112,50 @@ const VALID_AUTHORS_FOR_LATEST = [ }); if (isRelativeVersionKeyword(options.version)) { + resetPackageJsons(); throw new Error( 'When creating actual releases, you must use an exact semver version' ); } - runNxReleaseVersion(); + try { + runNxReleaseVersion(); - execSync(`pnpm nx run-many -t add-extra-dependencies --parallel 8`, { - stdio: isVerboseLogging ? [0, 1, 2] : 'ignore', - maxBuffer: LARGE_BUFFER, - windowsHide: false, - }); + execSync(`pnpm nx run-many -t add-extra-dependencies --parallel 8`, { + stdio: isVerboseLogging ? [0, 1, 2] : 'ignore', + maxBuffer: LARGE_BUFFER, + windowsHide: false, + }); - let changelogCommand = `pnpm nx release changelog ${options.version} --interactive workspace`; - if (options.from) { - changelogCommand += ` --from ${options.from}`; - } - if (options.gitRemote) { - changelogCommand += ` --git-remote ${options.gitRemote}`; - } - if (options.dryRun) { - changelogCommand += ' --dry-run'; - } - if (isVerboseLogging) { - changelogCommand += ' --verbose'; - } - console.log(`> ${changelogCommand}`); - execSync(changelogCommand, { - stdio: isVerboseLogging ? [0, 1, 2] : 'ignore', - maxBuffer: LARGE_BUFFER, - windowsHide: false, - }); + let changelogCommand = `pnpm nx release changelog ${options.version} --interactive workspace`; + if (options.from) { + changelogCommand += ` --from ${options.from}`; + } + if (options.gitRemote) { + changelogCommand += ` --git-remote ${options.gitRemote}`; + } + if (options.dryRun) { + changelogCommand += ' --dry-run'; + } + if (isVerboseLogging) { + changelogCommand += ' --verbose'; + } + console.log(`> ${changelogCommand}`); + execSync(changelogCommand, { + stdio: isVerboseLogging ? [0, 1, 2] : 'ignore', + maxBuffer: LARGE_BUFFER, + windowsHide: false, + }); - console.log( - 'Check github: https://github.com/nrwl/nx/actions/workflows/publish.yml' - ); + console.log( + 'Check github: https://github.com/nrwl/nx/actions/workflows/publish.yml' + ); + } finally { + resetPackageJsons(); + } process.exit(0); } - const packagesToReset = [ - 'packages/angular-rspack', - 'packages/angular-rspack-compiler', - 'packages/dotnet', - 'packages/maven', - ]; - - const packageSnapshots: { [key: string]: string } = {}; - for (const packagePath of packagesToReset) { - const packageJsonPath = join(workspaceRoot, packagePath, 'package.json'); - const packageJson = readFileSync(packageJsonPath, 'utf-8'); - packageSnapshots[packagePath] = packageJson; - } - runNxReleaseVersion(); execSync(`pnpm nx run-many -t add-extra-dependencies --parallel 8`, { @@ -228,22 +247,7 @@ const VALID_AUTHORS_FOR_LATEST = [ } // TODO(colum): Remove when we have a better way to handle this - - console.log( - 'Resetting angular-rspack package.json versions to previous versions' - ); - - for (const packagePath of packagesToReset) { - const packageJsonPath = join(workspaceRoot, packagePath, 'package.json'); - writeFileSync(packageJsonPath, packageSnapshots[packagePath]); - } - - execSync( - `npx prettier --write packages/angular-rspack/package.json packages/angular-rspack-compiler/package.json`, - { - cwd: workspaceRoot, - } - ); + resetPackageJsons(); process.exit(0); })();