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): report should not include non-plugin packages as local plugins #18306

Merged
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions packages/nx/src/command-line/generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ export function printChanges(fileChanges: FileChange[]) {

async function promptForCollection(
generatorName: string,
ws: Workspaces,
interactive: boolean,
projectsConfiguration: ProjectsConfigurations
): Promise<string> {
const localPlugins = await getLocalWorkspacePlugins(projectsConfiguration);
const localPlugins = await getLocalWorkspacePlugins(
projectsConfiguration,
readNxJson()
);

const installedCollections = Array.from(
new Set(findInstalledPlugins().map((x) => x.name))
Expand Down Expand Up @@ -221,7 +223,6 @@ async function convertToGenerateOptions(
} else if (!defaultCollectionName) {
const generatorString = await promptForCollection(
generatorDescriptor,
ws,
interactive,
projectsConfiguration
);
Expand Down
5 changes: 4 additions & 1 deletion packages/nx/src/command-line/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -33,11 +34,13 @@ export async function listHandler(args: ListArgs): Promise<void> {
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
Expand Down
8 changes: 5 additions & 3 deletions packages/nx/src/command-line/report/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof import('../../../package.json')>(
join(__dirname, '../../../package.json')
Expand Down Expand Up @@ -148,7 +149,7 @@ export async function getReportData(): Promise<ReportData> {
const pm = detectPackageManager();
const pmVersion = getPackageManagerVersion(pm);

const localPlugins = await findLocalPlugins();
const localPlugins = await findLocalPlugins(readNxJson());
const communityPlugins = findInstalledCommunityPlugins();

let projectGraphError: Error | null = null;
Expand Down Expand Up @@ -185,11 +186,12 @@ export async function getReportData(): Promise<ReportData> {
};
}

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 {
Expand Down
21 changes: 16 additions & 5 deletions packages/nx/src/utils/plugins/local-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<string, PluginCapabilities>> {
const plugins: Map<string, PluginCapabilities> = 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)
) {
Expand All @@ -36,7 +42,6 @@ export async function getLocalWorkspacePlugins(
}
}
}

return plugins;
}

Expand All @@ -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()})`);
}

Expand Down
7 changes: 5 additions & 2 deletions packages/nx/src/utils/plugins/plugin-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ function tryGetCollection<T extends object>(

export async function getPluginCapabilities(
workspaceRoot: string,
pluginName: string
pluginName: string,
includeRuntimeCapabilities = false
): Promise<PluginCapabilities | null> {
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<string, unknown>);
return {
name: pluginName,
generators: {
Expand Down