Skip to content

Commit

Permalink
fix(core): fix plugin error indexes (#29936)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

The errors were misaligned to the plugin index they pertained to.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

The errors are not misaligned to the plugin index they pertain to.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
FrozenPandaz authored and jaysoo committed Feb 12, 2025
1 parent 02253cc commit bc80cc5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
7 changes: 6 additions & 1 deletion packages/nx/src/project-graph/error-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@ export function formatAggregateCreateNodesError(
const errorBodyLines = [
`${
error.errors.length > 1 ? `${error.errors.length} errors` : 'An error'
} occurred while processing files for the ${pluginName} plugin.`,
} occurred while processing files for the ${pluginName} plugin${
error.pluginIndex
? ` (Defined at nx.json#plugins[${error.pluginIndex}])`
: ''
}`,
`.`,
];
const errorStackLines = [];

Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/project-graph/plugins/get-plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async function loadSpecifiedNxPlugins(
const cleanupFunctions: Array<() => void> = [];
const ret = [
await Promise.all(
plugins.map(async (plugin) => {
plugins.map(async (plugin, index) => {
const pluginPath = typeof plugin === 'string' ? plugin : plugin.plugin;
performance.mark(`Load Nx Plugin: ${pluginPath} - start`);

Expand All @@ -170,6 +170,7 @@ async function loadSpecifiedNxPlugins(

cleanupFunctions.push(cleanup);
const res = await loadedPluginPromise;
res.index = index;
performance.mark(`Load Nx Plugin: ${pluginPath} - end`);
performance.measure(
`Load Nx Plugin: ${pluginPath}`,
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/project-graph/plugins/loaded-nx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { isIsolationEnabled } from './isolation/enabled';
import { isDaemonEnabled } from '../../daemon/client/client';

export class LoadedNxPlugin {
index?: number;
readonly name: string;
readonly createNodes?: [
filePattern: string,
Expand Down
36 changes: 22 additions & 14 deletions packages/nx/src/project-graph/utils/project-configuration-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@ import { performance } from 'perf_hooks';

import { LoadedNxPlugin } from '../plugins/loaded-nx-plugin';
import {
MergeNodesError,
ProjectConfigurationsError,
ProjectsWithNoNameError,
MultipleProjectsWithSameNameError,
AggregateCreateNodesError,
formatAggregateCreateNodesError,
isAggregateCreateNodesError,
isMultipleProjectsWithSameNameError,
isProjectsWithNoNameError,
ProjectWithNoNameError,
ProjectWithExistingNameError,
isProjectWithExistingNameError,
isProjectWithNoNameError,
isAggregateCreateNodesError,
AggregateCreateNodesError,
formatAggregateCreateNodesError,
MergeNodesError,
MultipleProjectsWithSameNameError,
ProjectConfigurationsError,
ProjectsWithNoNameError,
ProjectWithExistingNameError,
ProjectWithNoNameError,
} from '../error-types';
import { CreateNodesResult } from '../plugins/public-api';
import { isGlobPattern } from '../../utils/globs';
Expand Down Expand Up @@ -377,7 +377,13 @@ export async function createProjectConfigurationsWithPlugins(
// We iterate over plugins first - this ensures that plugins specified first take precedence.
for (const [
index,
{ createNodes: createNodesTuple, include, exclude, name: pluginName },
{
index: pluginIndex,
createNodes: createNodesTuple,
include,
exclude,
name: pluginName,
},
] of plugins.entries()) {
const [pattern, createNodes] = createNodesTuple ?? [];

Expand All @@ -403,8 +409,10 @@ export async function createProjectConfigurationsWithPlugins(
e
: // This represents a single plugin erroring out with a hard error.
new AggregateCreateNodesError([[null, e]], []);
if (pluginIndex) {
error.pluginIndex = pluginIndex;
}
formatAggregateCreateNodesError(error, pluginName);
error.pluginIndex = index;
// This represents a single plugin erroring out with a hard error.
errors.push(error);
// The plugin didn't return partial results, so we return an empty array.
Expand Down Expand Up @@ -458,7 +466,7 @@ function mergeCreateNodesResults(
plugin: string,
file: string,
result: CreateNodesResult,
index?: number
pluginIndex?: number
])[][],
nxJsonConfiguration: NxJsonConfiguration,
errors: (
Expand All @@ -477,7 +485,7 @@ function mergeCreateNodesResults(
> = {};

for (const result of results.flat()) {
const [pluginName, file, nodes, index] = result;
const [pluginName, file, nodes, pluginIndex] = result;

const { projects: projectNodes, externalNodes: pluginExternalNodes } =
nodes;
Expand Down Expand Up @@ -506,7 +514,7 @@ function mergeCreateNodesResults(
file,
pluginName,
error,
pluginIndex: index,
pluginIndex,
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function retrieveProjectConfigurations(
workspaceRoot,
nxJson,
pluginConfigFiles,
plugins
pluginsWithCreateNodes
);
}

Expand Down

0 comments on commit bc80cc5

Please sign in to comment.