Skip to content

Commit

Permalink
fix(@angular/cli): error when updating Angular packages across multi-…
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
alan-agius4 committed Nov 24, 2021
1 parent 03da128 commit 745d777
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/angular/cli/commands/update-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -629,11 +629,40 @@ export class UpdateCommand extends Command<UpdateCommandSchema> {
return 1;
}

if (manifest.version === node.package.version) {
if (manifest.version === node.package?.version) {
this.logger.info(`Package '${packageName}' is already up to date.`);
continue;
}

if (node.package && /^@(?:angular|nguniversal)\//.test(node.package.name)) {
const { name, version } = node.package;
const toBeInstalledMajorVersion = +manifest.version.split('.')[0];
const currentMajorVersion = +version.split('.')[0];

if (toBeInstalledMajorVersion - currentMajorVersion > 1) {
// Only allow updating a single version at a time.
if (currentMajorVersion < 6) {
// Before version 6, the major versions were not always sequential.
// Example @angular/core skipped version 3, @angular/cli skipped versions 2-5.
this.logger.error(
`Updating multiple major versions of '${name}' at once is not supported. Please migrate each major version individually.\n` +
`For more information about the update process, see https://update.angular.io/.`,
);
} else {
const nextMajorVersionFromCurrent = currentMajorVersion + 1;

this.logger.error(
`Updating multiple major versions of '${name}' at once is not supported. Please migrate each major version individually.\n` +
`Run 'ng update ${name}@${nextMajorVersionFromCurrent}' in your workspace directory ` +
`to update to latest '${nextMajorVersionFromCurrent}.x' version of '${name}'.\n\n` +
`For more information about the update process, see https://update.angular.io/?v=${currentMajorVersion}.0-${nextMajorVersionFromCurrent}.0`,
);
}

return 1;
}
}

packagesToUpdate.push(requestIdentifier.toString());
}

Expand Down
20 changes: 20 additions & 0 deletions tests/legacy-cli/e2e/tests/update/update-multiple-versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createProjectFromAsset } from '../../utils/assets';
import { ng } from '../../utils/process';
import { expectToFail } from '../../utils/utils';

export default async function () {
await createProjectFromAsset('7.0-project');

const extraArgs = ['--force'];
const { message } = await expectToFail(() =>
ng('update', '@angular/core', ...extraArgs),
);
if (
!message.includes(`Updating multiple major versions of '@angular/core' at once is not supported`)
) {
console.error(message);
throw new Error(
`Expected error message to include "Updating multiple major versions of '@angular/core' at once is not supported" but didn't.`,
);
}
}

0 comments on commit 745d777

Please sign in to comment.