From fa06dca901350c44747731c8198174a87f4f54ac Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Tue, 25 Jul 2023 10:55:33 -0500 Subject: [PATCH] fix(core): report should not include non-plugin packages as local plugins --- .../nx/src/command-line/generate/generate.ts | 4 +--- packages/nx/src/command-line/list/list.ts | 5 ++++- packages/nx/src/command-line/report/report.ts | 8 ++++--- .../nx/src/utils/plugins/local-plugins.ts | 21 ++++++++++++++----- .../src/utils/plugins/plugin-capabilities.ts | 7 +++++-- 5 files changed, 31 insertions(+), 14 deletions(-) diff --git a/packages/nx/src/command-line/generate/generate.ts b/packages/nx/src/command-line/generate/generate.ts index 57b4c901f3830f..bacb95bd5de2cd 100644 --- a/packages/nx/src/command-line/generate/generate.ts +++ b/packages/nx/src/command-line/generate/generate.ts @@ -51,11 +51,10 @@ export function printChanges(fileChanges: FileChange[]) { async function promptForCollection( generatorName: string, - ws: Workspaces, interactive: boolean, projectsConfiguration: ProjectsConfigurations ): Promise { - const localPlugins = await getLocalWorkspacePlugins(projectsConfiguration); + const localPlugins = await getLocalWorkspacePlugins(projectsConfiguration, readNxJson()); const installedCollections = Array.from( new Set(findInstalledPlugins().map((x) => x.name)) @@ -221,7 +220,6 @@ async function convertToGenerateOptions( } else if (!defaultCollectionName) { const generatorString = await promptForCollection( generatorDescriptor, - ws, interactive, projectsConfiguration ); diff --git a/packages/nx/src/command-line/list/list.ts b/packages/nx/src/command-line/list/list.ts index adfa98200a9abf..09a459c83b9a3e 100644 --- a/packages/nx/src/command-line/list/list.ts +++ b/packages/nx/src/command-line/list/list.ts @@ -15,6 +15,7 @@ import { createProjectGraphAsync, readProjectsConfigurationFromProjectGraph, } from '../../project-graph/project-graph'; +import { readNxJson } from '../../config/nx-json'; export interface ListArgs { /** The name of an installed plugin to query */ @@ -33,11 +34,13 @@ export async function listHandler(args: ListArgs): Promise { if (args.plugin) { await listPluginCapabilities(args.plugin); } else { + const nxJson = readNxJson(); const corePlugins = fetchCorePlugins(); const projectGraph = await createProjectGraphAsync({ exitOnError: true }); const localPlugins = await getLocalWorkspacePlugins( - readProjectsConfigurationFromProjectGraph(projectGraph) + readProjectsConfigurationFromProjectGraph(projectGraph), + nxJson ); const installedPlugins = await getInstalledPluginsAndCapabilities( workspaceRoot diff --git a/packages/nx/src/command-line/report/report.ts b/packages/nx/src/command-line/report/report.ts index 2bce04b90714c0..f6378cc1c38746 100644 --- a/packages/nx/src/command-line/report/report.ts +++ b/packages/nx/src/command-line/report/report.ts @@ -20,6 +20,7 @@ import { import { gt, valid } from 'semver'; import { findInstalledPlugins } from '../../utils/plugins/installed-plugins'; import { getNxRequirePaths } from '../../utils/installation-directory'; +import { NxJsonConfiguration, readNxJson } from '../../config/nx-json'; const nxPackageJson = readJsonFile( join(__dirname, '../../../package.json') @@ -148,7 +149,7 @@ export async function getReportData(): Promise { const pm = detectPackageManager(); const pmVersion = getPackageManagerVersion(pm); - const localPlugins = await findLocalPlugins(); + const localPlugins = await findLocalPlugins(readNxJson()); const communityPlugins = findInstalledCommunityPlugins(); let projectGraphError: Error | null = null; @@ -185,11 +186,12 @@ export async function getReportData(): Promise { }; } -async function findLocalPlugins() { +async function findLocalPlugins(nxJson: NxJsonConfiguration) { try { const projectGraph = await createProjectGraphAsync({ exitOnError: true }); const localPlugins = await getLocalWorkspacePlugins( - readProjectsConfigurationFromProjectGraph(projectGraph) + readProjectsConfigurationFromProjectGraph(projectGraph), + nxJson ); return Array.from(localPlugins.keys()); } catch { diff --git a/packages/nx/src/utils/plugins/local-plugins.ts b/packages/nx/src/utils/plugins/local-plugins.ts index 43ed122ac97957..4ea842b884a37b 100644 --- a/packages/nx/src/utils/plugins/local-plugins.ts +++ b/packages/nx/src/utils/plugins/local-plugins.ts @@ -9,23 +9,29 @@ import { join } from 'path'; import { workspaceRoot } from '../workspace-root'; import { existsSync } from 'fs'; import { getPluginCapabilities } from './plugin-capabilities'; +import { NxJsonConfiguration, readNxJson } from '../../config/nx-json'; export async function getLocalWorkspacePlugins( - projectsConfiguration: ProjectsConfigurations + projectsConfiguration: ProjectsConfigurations, + nxJson: NxJsonConfiguration ): Promise> { const plugins: Map = new Map(); for (const project of Object.values(projectsConfiguration.projects)) { const packageJsonPath = join(workspaceRoot, project.root, 'package.json'); if (existsSync(packageJsonPath)) { const packageJson: PackageJson = readJsonFile(packageJsonPath); + const includeRuntimeCapabilities = nxJson?.plugins?.some((p) => + p.startsWith(packageJson.name) + ); const capabilities = await getPluginCapabilities( workspaceRoot, - packageJson.name + packageJson.name, + includeRuntimeCapabilities ); if ( capabilities && - (capabilities.executors || - capabilities.generators || + (Object.keys(capabilities.executors ?? {}).length || + Object.keys(capabilities.generators ?? {}).length || capabilities.projectGraphExtension || capabilities.projectInference) ) { @@ -36,7 +42,6 @@ export async function getLocalWorkspacePlugins( } } } - return plugins; } @@ -53,6 +58,12 @@ export function listLocalWorkspacePlugins( if (hasElements(p.generators)) { capabilities.push('generators'); } + if (p.projectGraphExtension) { + capabilities.push('graph-extension'); + } + if (p.projectInference) { + capabilities.push('project-inference'); + } bodyLines.push(`${chalk.bold(p.name)} (${capabilities.join()})`); } diff --git a/packages/nx/src/utils/plugins/plugin-capabilities.ts b/packages/nx/src/utils/plugins/plugin-capabilities.ts index dd2d0642436b17..e420f7760f7dcc 100644 --- a/packages/nx/src/utils/plugins/plugin-capabilities.ts +++ b/packages/nx/src/utils/plugins/plugin-capabilities.ts @@ -33,14 +33,17 @@ function tryGetCollection( export async function getPluginCapabilities( workspaceRoot: string, - pluginName: string + pluginName: string, + includeRuntimeCapabilities = false ): Promise { try { const { json: packageJson, path: packageJsonPath } = readPluginPackageJson( pluginName, getNxRequirePaths(workspaceRoot) ); - const pluginModule = await tryGetModule(packageJson, workspaceRoot); + const pluginModule = includeRuntimeCapabilities + ? await tryGetModule(packageJson, workspaceRoot) + : ({} as Record); return { name: pluginName, generators: {