Skip to content

Commit

Permalink
fix(@angular/cli): allow ng add to find prerelease versions when CL…
Browse files Browse the repository at this point in the history
…I is prerelease

When the CLI is a prerelease version, the `ng add` command will now consider the
use of prerelease versions of requested packages. Without this behavior, attempting
to install a package without a version specifier (e.g., `ng add @angular/material`)
will install an older stable version of the requested package instead of the expected
prerelease version compatible with the prerelease Angular project.

(cherry picked from commit 56cb767)
  • Loading branch information
clydin authored and dgp1130 committed Oct 25, 2022
1 parent 1a58436 commit 83524f6
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/angular/cli/src/commands/add/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import { askConfirmation } from '../../utilities/prompt';
import { Spinner } from '../../utilities/spinner';
import { isTTY } from '../../utilities/tty';
import { VERSION } from '../../utilities/version';

interface AddCommandArgs extends SchematicsCommandArgs {
collection: string;
Expand Down Expand Up @@ -178,11 +179,15 @@ export class AddCommandModule
);
} else if (!latestManifest || (await this.hasMismatchedPeer(latestManifest))) {
// 'latest' is invalid so search for most recent matching package

// Allow prelease versions if the CLI itself is a prerelease
const allowPrereleases = prerelease(VERSION.full);

const versionExclusions = packageVersionExclusions[packageMetadata.name];
const versionManifests = Object.values(packageMetadata.versions).filter(
(value: PackageManifest) => {
// Prerelease versions are not stable and should not be considered by default
if (prerelease(value.version)) {
if (!allowPrereleases && prerelease(value.version)) {
return false;
}
// Deprecated versions should not be used or considered
Expand All @@ -198,7 +203,8 @@ export class AddCommandModule
},
);

versionManifests.sort((a, b) => compare(a.version, b.version, true));
// Sort in reverse SemVer order so that the newest compatible version is chosen
versionManifests.sort((a, b) => compare(b.version, a.version, true));

let newIdentifier;
for (const versionManifest of versionManifests) {
Expand Down

0 comments on commit 83524f6

Please sign in to comment.