Skip to content

Commit 745d777

Browse files
committed
fix(@angular/cli): error when updating Angular packages across multi-major migrations
With this change we show an error message when users try to update `@angular/` and `@nguniversal/` packages across multiple major versions. (cherry picked from commit d60d374)
1 parent 03da128 commit 745d777

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/angular/cli/commands/update-impl.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,11 +629,40 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
629629
return 1;
630630
}
631631

632-
if (manifest.version === node.package.version) {
632+
if (manifest.version === node.package?.version) {
633633
this.logger.info(`Package '${packageName}' is already up to date.`);
634634
continue;
635635
}
636636

637+
if (node.package && /^@(?:angular|nguniversal)\//.test(node.package.name)) {
638+
const { name, version } = node.package;
639+
const toBeInstalledMajorVersion = +manifest.version.split('.')[0];
640+
const currentMajorVersion = +version.split('.')[0];
641+
642+
if (toBeInstalledMajorVersion - currentMajorVersion > 1) {
643+
// Only allow updating a single version at a time.
644+
if (currentMajorVersion < 6) {
645+
// Before version 6, the major versions were not always sequential.
646+
// Example @angular/core skipped version 3, @angular/cli skipped versions 2-5.
647+
this.logger.error(
648+
`Updating multiple major versions of '${name}' at once is not supported. Please migrate each major version individually.\n` +
649+
`For more information about the update process, see https://update.angular.io/.`,
650+
);
651+
} else {
652+
const nextMajorVersionFromCurrent = currentMajorVersion + 1;
653+
654+
this.logger.error(
655+
`Updating multiple major versions of '${name}' at once is not supported. Please migrate each major version individually.\n` +
656+
`Run 'ng update ${name}@${nextMajorVersionFromCurrent}' in your workspace directory ` +
657+
`to update to latest '${nextMajorVersionFromCurrent}.x' version of '${name}'.\n\n` +
658+
`For more information about the update process, see https://update.angular.io/?v=${currentMajorVersion}.0-${nextMajorVersionFromCurrent}.0`,
659+
);
660+
}
661+
662+
return 1;
663+
}
664+
}
665+
637666
packagesToUpdate.push(requestIdentifier.toString());
638667
}
639668

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createProjectFromAsset } from '../../utils/assets';
2+
import { ng } from '../../utils/process';
3+
import { expectToFail } from '../../utils/utils';
4+
5+
export default async function () {
6+
await createProjectFromAsset('7.0-project');
7+
8+
const extraArgs = ['--force'];
9+
const { message } = await expectToFail(() =>
10+
ng('update', '@angular/core', ...extraArgs),
11+
);
12+
if (
13+
!message.includes(`Updating multiple major versions of '@angular/core' at once is not supported`)
14+
) {
15+
console.error(message);
16+
throw new Error(
17+
`Expected error message to include "Updating multiple major versions of '@angular/core' at once is not supported" but didn't.`,
18+
);
19+
}
20+
}

0 commit comments

Comments
 (0)