Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): nx migrate should determine angular devkit migration from cli or schematics #20282 #27634

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 5 additions & 46 deletions packages/nx/src/command-line/migrate/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1699,70 +1699,29 @@ function getImplementationPath(
return { path: implPath, fnSymbol };
}

// TODO (v17): This should just become something like:
// ```
// return !collection.generators[name] && collection.schematics[name]
// ```
// TODO (v21): Remove CLI determination of Angular Migration
function isAngularMigration(
collection: MigrationsJson,
collectionPath: string,
name: string
) {
const entry = collection.generators?.[name] || collection.schematics?.[name];

// In the future we will determine this based on the location of the entry in the collection.
// If the entry is under `schematics`, it will be assumed to be an angular cli migration.
// If the entry is under `generators`, it will be assumed to be an nx migration.
// For now, we will continue to obey the cli property, if it exists.
// If it doesn't exist, we will check if the implementation references @angular/devkit.
const shouldBeNx = !!collection.generators?.[name];
const shouldBeNg = !!collection.schematics?.[name];
let useAngularDevkitToRunMigration = false;

const { path: implementationPath } = getImplementationPath(
collection,
collectionPath,
name
);
const implStringContents = readFileSync(implementationPath, 'utf-8');
// TODO (v17): Remove this check and the cli property access - it is only here for backwards compatibility.
if (
['@angular/material', '@angular/cdk'].includes(collection.name) ||
[
"import('@angular-devkit",
'import("@angular-devkit',
"require('@angular-devkit",
'require("@angular-devkit',
"from '@angular-devkit",
'from "@angular-devkit',
].some((s) => implStringContents.includes(s))
) {
useAngularDevkitToRunMigration = true;
}

if (useAngularDevkitToRunMigration && shouldBeNx) {
if (entry.cli && entry.cli !== 'nx' && collection.generators?.[name]) {
output.warn({
title: `The migration '${collection.name}:${name}' appears to be an Angular CLI migration, but is located in the 'generators' section of migrations.json.`,
bodyLines: [
'In Nx 17, migrations inside `generators` will be treated as Angular Devkit migrations.',
"Please open an issue on the plugin's repository if you believe this is an error.",
],
});
}

if (!useAngularDevkitToRunMigration && entry.cli === 'nx' && shouldBeNg) {
output.warn({
title: `The migration '${collection.name}:${name}' appears to be an Nx migration, but is located in the 'schematics' section of migrations.json.`,
bodyLines: [
'In Nx 17, migrations inside `generators` will be treated as nx devkit migrations.',
'In Nx 21, migrations inside `generators` will be treated as Nx Devkit migrations and therefore may not run correctly if they are using Angular Devkit.',
'If the migration should be run with Angular Devkit, please place the migration inside `schematics` instead.',
"Please open an issue on the plugin's repository if you believe this is an error.",
],
});
}

// Currently, if the cli property exists we listen to it. If its nx, its not an ng cli migration.
// If the property is not set, we will fall back to our intuition.
return entry.cli ? entry.cli !== 'nx' : useAngularDevkitToRunMigration;
return entry.cli ? entry.cli !== 'nx' : !shouldBeNx && shouldBeNg;
}

const getNgCompatLayer = (() => {
Expand Down