fix(core): ensure nx init implementation for angular correctly resolves @angular/core (#31430)

## Current Behavior

When running `nx init` in an Angular CLI repo located in a directory
that has an ancestor directory with an Nx installation, it silently
exits when trying to determine the compatibility of the Angular version.
The migration is not performed, and no feedback is given to the user.
This happens because the current implementation will determine that
ancestor directory (outside the workspace root) as the workspace root
(when starting the migration, there's no `nx` in the repo).

## Expected Behavior

Running `nx init` should work correctly, and proper feedback should be
printed to the user if it can't determine the compatibility of the
Angular version. It should correctly resolve the `@angular/core` package
from the Angular CLI workspace root.

## Related Issue(s)

Fixes #31291
This commit is contained in:
Leosvel Pérez Espinosa 2025-06-03 12:55:29 +02:00 committed by GitHub
parent b97ee099c8
commit cb930d006a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,7 @@ import { execSync } from 'child_process';
import { join } from 'path'; import { join } from 'path';
import { gte, major } from 'semver'; import { gte, major } from 'semver';
import { readJsonFile, writeJsonFile } from '../../../../utils/fileutils'; import { readJsonFile, writeJsonFile } from '../../../../utils/fileutils';
import { getNxRequirePaths } from '../../../../utils/installation-directory';
import { sortObjectByKeys } from '../../../../utils/object-sort'; import { sortObjectByKeys } from '../../../../utils/object-sort';
import { output } from '../../../../utils/output'; import { output } from '../../../../utils/output';
import { readModulePackageJson } from '../../../../utils/package-json'; import { readModulePackageJson } from '../../../../utils/package-json';
@ -11,9 +12,9 @@ import {
resolvePackageVersionUsingInstallation, resolvePackageVersionUsingInstallation,
resolvePackageVersionUsingRegistry, resolvePackageVersionUsingRegistry,
} from '../../../../utils/package-manager'; } from '../../../../utils/package-manager';
import { connectExistingRepoToNxCloudPrompt } from '../../../connect/connect-to-nx-cloud';
import { initCloud } from '../utils'; import { initCloud } from '../utils';
import type { Options } from './types'; import type { Options } from './types';
import { connectExistingRepoToNxCloudPrompt } from '../../../connect/connect-to-nx-cloud';
// map of Angular major versions to Nx versions to use for legacy `nx init` migrations, // map of Angular major versions to Nx versions to use for legacy `nx init` migrations,
// key is major Angular version and value is Nx version to use // key is major Angular version and value is Nx version to use
@ -35,9 +36,25 @@ export async function getLegacyMigrationFunctionIfApplicable(
repoRoot: string, repoRoot: string,
options: Options options: Options
): Promise<() => Promise<void> | null> { ): Promise<() => Promise<void> | null> {
const angularVersion = let majorAngularVersion: number;
readModulePackageJson('@angular/core').packageJson.version; try {
const majorAngularVersion = major(angularVersion); const angularVersion = readModulePackageJson('@angular/core', [
repoRoot,
...getNxRequirePaths(),
]).packageJson.version;
majorAngularVersion = major(angularVersion);
} catch {
output.error({
title: 'Could not determine the existing Angular version',
bodyLines: [
'Please ensure "@angular/core" is installed in the workspace and you are running this command from the root of the workspace.',
],
});
// errors are caught in the initHandler and not logged to the user, so we
// log it first and then throw to ensure it is logged to the user
throw new Error('Could not determine the existing Angular version');
}
if (majorAngularVersion >= minMajorAngularVersionSupported) { if (majorAngularVersion >= minMajorAngularVersionSupported) {
// non-legacy // non-legacy
return null; return null;