Skip to content

Commit 30295b3

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 ce1ec04 commit 30295b3

File tree

2 files changed

+63
-23
lines changed

2 files changed

+63
-23
lines changed

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

+28-23
Original file line numberDiff line numberDiff line change
@@ -612,35 +612,40 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
612612
return 1;
613613
}
614614

615-
if (node.package?.name === '@angular/cli') {
616-
// Migrations for non LTS versions of Angular CLI are no longer included in @schematics/angular v12.
615+
if (manifest.version === node.package?.version) {
616+
this.logger.info(`Package '${packageName}' is already up to date.`);
617+
continue;
618+
}
619+
620+
if (node.package && /^@(?:angular|nguniversal)\//.test(node.package.name)) {
621+
const { name, version } = node.package;
617622
const toBeInstalledMajorVersion = +manifest.version.split('.')[0];
618-
const currentMajorVersion = +node.package.version.split('.')[0];
619-
if (currentMajorVersion < 9 && toBeInstalledMajorVersion >= 12) {
620-
const updateVersions: Record<number, number> = {
621-
1: 6,
622-
6: 7,
623-
7: 8,
624-
8: 9,
625-
};
626-
627-
const updateTo = updateVersions[currentMajorVersion];
628-
this.logger.error(
629-
'Updating multiple major versions at once is not recommended. ' +
630-
`Run 'ng update @angular/cli@${updateTo}' in your workspace directory ` +
631-
`to update to latest '${updateTo}.x' version of '@angular/cli'.\n\n` +
632-
'For more information about the update process, see https://update.angular.io/.',
633-
);
623+
const currentMajorVersion = +version.split('.')[0];
624+
625+
if (toBeInstalledMajorVersion - currentMajorVersion > 1) {
626+
// Only allow updating a single version at a time.
627+
if (currentMajorVersion < 6) {
628+
// Before version 6, the major versions were not always sequential.
629+
// Example @angular/core skipped version 3, @angular/cli skipped versions 2-5.
630+
this.logger.error(
631+
`Updating multiple major versions of '${name}' at once is not supported. Please migrate each major version individually.\n` +
632+
`For more information about the update process, see https://update.angular.io/.`,
633+
);
634+
} else {
635+
const nextMajorVersionFromCurrent = currentMajorVersion + 1;
636+
637+
this.logger.error(
638+
`Updating multiple major versions of '${name}' at once is not supported. Please migrate each major version individually.\n` +
639+
`Run 'ng update ${name}@${nextMajorVersionFromCurrent}' in your workspace directory ` +
640+
`to update to latest '${nextMajorVersionFromCurrent}.x' version of '${name}'.\n\n` +
641+
`For more information about the update process, see https://update.angular.io/?v=${currentMajorVersion}.0-${nextMajorVersionFromCurrent}.0`,
642+
);
643+
}
634644

635645
return 1;
636646
}
637647
}
638648

639-
if (manifest.version === node.package?.version) {
640-
this.logger.info(`Package '${packageName}' is already up to date.`);
641-
continue;
642-
}
643-
644649
packagesToUpdate.push(requestIdentifier.toString());
645650
}
646651

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { createProjectFromAsset } from '../../utils/assets';
2+
import { installWorkspacePackages, setRegistry } from '../../utils/packages';
3+
import { ng } from '../../utils/process';
4+
import { isPrereleaseCli } from '../../utils/project';
5+
import { expectToFail } from '../../utils/utils';
6+
7+
export default async function () {
8+
try {
9+
await createProjectFromAsset('8.0-project', true, true);
10+
await setRegistry(false);
11+
await installWorkspacePackages();
12+
13+
await setRegistry(true);
14+
const extraArgs = ['--force'];
15+
if (isPrereleaseCli()) {
16+
extraArgs.push('--next');
17+
}
18+
19+
const { message } = await expectToFail(() =>
20+
ng('update', '@angular/cli', '--force', ...extraArgs),
21+
);
22+
if (
23+
!message.includes(
24+
`Updating multiple major versions of '@angular/cli' at once is not supported`,
25+
)
26+
) {
27+
console.error(message);
28+
throw new Error(
29+
`Expected error message to include "Updating multiple major versions of '@angular/cli' at once is not supported" but didn't.`,
30+
);
31+
}
32+
} finally {
33+
await setRegistry(true);
34+
}
35+
}

0 commit comments

Comments
 (0)