Skip to content

Commit 83524f6

Browse files
clydindgp1130
authored andcommitted
fix(@angular/cli): allow ng add to find prerelease versions when CLI 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)
1 parent 1a58436 commit 83524f6

File tree

1 file changed

+8
-2
lines changed
  • packages/angular/cli/src/commands/add

1 file changed

+8
-2
lines changed

packages/angular/cli/src/commands/add/cli.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
import { askConfirmation } from '../../utilities/prompt';
3535
import { Spinner } from '../../utilities/spinner';
3636
import { isTTY } from '../../utilities/tty';
37+
import { VERSION } from '../../utilities/version';
3738

3839
interface AddCommandArgs extends SchematicsCommandArgs {
3940
collection: string;
@@ -178,11 +179,15 @@ export class AddCommandModule
178179
);
179180
} else if (!latestManifest || (await this.hasMismatchedPeer(latestManifest))) {
180181
// 'latest' is invalid so search for most recent matching package
182+
183+
// Allow prelease versions if the CLI itself is a prerelease
184+
const allowPrereleases = prerelease(VERSION.full);
185+
181186
const versionExclusions = packageVersionExclusions[packageMetadata.name];
182187
const versionManifests = Object.values(packageMetadata.versions).filter(
183188
(value: PackageManifest) => {
184189
// Prerelease versions are not stable and should not be considered by default
185-
if (prerelease(value.version)) {
190+
if (!allowPrereleases && prerelease(value.version)) {
186191
return false;
187192
}
188193
// Deprecated versions should not be used or considered
@@ -198,7 +203,8 @@ export class AddCommandModule
198203
},
199204
);
200205

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

203209
let newIdentifier;
204210
for (const versionManifest of versionManifests) {

0 commit comments

Comments
 (0)