Skip to content

Commit a560aa9

Browse files
committed
fix(@angular/cli): revert package.json changes if installation tasks fail during update
When running the update command, if package manager installation fails (e.g., due to peer dependency conflicts or network issues), the project's package.json is left in an updated state but node_modules is missing or inconsistent. To prevent leaving the workspace in a broken state, backup the original package.json before applying the update plan, and restore it from the backup if the installation tasks throw an error.
1 parent b0b3a47 commit a560aa9

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

  • packages/angular/cli/src/commands/update

packages/angular/cli/src/commands/update/cli.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,14 @@ export default class UpdateCommandModule extends CommandModule<UpdateCommandArgs
541541
return 1;
542542
}
543543

544+
const packageJsonPath = path.join(this.context.root, 'package.json');
545+
let originalPackageJsonContent: string | undefined;
546+
try {
547+
originalPackageJsonContent = await fs.readFile(packageJsonPath, 'utf8');
548+
} catch {
549+
// Ignore backup errors.
550+
}
551+
544552
try {
545553
await applyUpdatePlan(this.context.root, plan, logger);
546554
} catch (error) {
@@ -596,6 +604,16 @@ export default class UpdateCommandModule extends CommandModule<UpdateCommandArgs
596604
Module._pathCache = Object.create(null);
597605
}
598606
} catch (e) {
607+
if (originalPackageJsonContent !== undefined) {
608+
try {
609+
await fs.writeFile(packageJsonPath, originalPackageJsonContent, 'utf8');
610+
logger.info('Restored package.json to its original state.');
611+
} catch (restoreError) {
612+
assertIsError(restoreError);
613+
logger.error(`Failed to restore package.json: ${restoreError.message}`);
614+
}
615+
}
616+
599617
if (e instanceof CommandError) {
600618
return 1;
601619
}

0 commit comments

Comments
 (0)