From 53f93b93bb354b96587d7c47a0487ebef3162662 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Thu, 19 Oct 2023 07:18:28 +0000 Subject: [PATCH] fix(@schematics/angular): noop workspace config migration when already executed Prior to this change there was a missing check that causes the migration to remove valid config when it was executed multiple times or on configs that do not contain the deprecated option. Closes #26063 --- .../update-17/update-workspace-config.ts | 9 ++++---- .../update-17/update-workspace-config_spec.ts | 23 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/schematics/angular/migrations/update-17/update-workspace-config.ts b/packages/schematics/angular/migrations/update-17/update-workspace-config.ts index 802c487a5381..755fa34342db 100644 --- a/packages/schematics/angular/migrations/update-17/update-workspace-config.ts +++ b/packages/schematics/angular/migrations/update-17/update-workspace-config.ts @@ -6,8 +6,7 @@ * found in the LICENSE file at https://angular.io/license */ -import { Rule, chain } from '@angular-devkit/schematics'; -import { removePackageJsonDependency } from '../../utility/dependencies'; +import { Rule } from '@angular-devkit/schematics'; import { allTargetOptions, updateWorkspace } from '../../utility/workspace'; import { Builders, ProjectType } from '../../utility/workspace-models'; @@ -22,8 +21,10 @@ export default function (): Rule { for (const [, target] of project.targets) { if (target.builder === Builders.ExtractI18n || target.builder === Builders.DevServer) { for (const [, options] of allTargetOptions(target, false)) { - options['buildTarget'] = options['browserTarget']; - delete options['browserTarget']; + if (options['browserTarget'] !== undefined) { + options['buildTarget'] = options['browserTarget']; + delete options['browserTarget']; + } } } } diff --git a/packages/schematics/angular/migrations/update-17/update-workspace-config_spec.ts b/packages/schematics/angular/migrations/update-17/update-workspace-config_spec.ts index 51067cee49d6..3f51d5109dcf 100644 --- a/packages/schematics/angular/migrations/update-17/update-workspace-config_spec.ts +++ b/packages/schematics/angular/migrations/update-17/update-workspace-config_spec.ts @@ -109,4 +109,27 @@ describe(`Migration to update 'angular.json'.`, () => { expect(browserTarget).toBe('app:build'); expect(buildTarget).toBeUndefined(); }); + + it(`should not remove 'buildTarget' when migration is ran multiple times`, async () => { + const runMigrationAndExpects = async (testTree: UnitTestTree) => { + const newTree = await schematicRunner.runSchematic(schematicName, {}, testTree); + const { + projects: { app }, + } = JSON.parse(newTree.readContent('/angular.json')); + + const { browserTarget, buildTarget } = app.architect['serve'].options; + expect(browserTarget).toBeUndefined(); + expect(buildTarget).toBe('app:build:development'); + + const { browserTarget: browserTargetProd, buildTarget: buildTargetProd } = + app.architect['serve'].configurations['production']; + expect(browserTargetProd).toBeUndefined(); + expect(buildTargetProd).toBe('app:build:production'); + + return newTree; + }; + + const newTree = await runMigrationAndExpects(tree); + await runMigrationAndExpects(newTree); + }); });