From 8891bdc3ab5fcc35306b66365c1e367f93139029 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Fri, 7 Jun 2024 14:14:22 -0400 Subject: [PATCH] fix(core): ensure tuple passed to aggregate create nodes error (#26456) --- packages/nx/src/project-graph/error-types.ts | 15 +++++++++++++++ .../nx/src/project-graph/plugins/internal-api.ts | 2 +- .../utils/project-configuration-utils.ts | 11 ++++++++--- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/nx/src/project-graph/error-types.ts b/packages/nx/src/project-graph/error-types.ts index 9cda2cf0230b9..795e1c3d0cfbd 100644 --- a/packages/nx/src/project-graph/error-types.ts +++ b/packages/nx/src/project-graph/error-types.ts @@ -226,6 +226,21 @@ export class AggregateCreateNodesError extends Error { ) { super('Failed to create nodes'); this.name = this.constructor.name; + if ( + // Errors should be an array + !Array.isArray(errors) || + !errors.every( + // Where every element is a tuple + (errorTuple) => + Array.isArray(errorTuple) && + // That has a length of 2 + errorTuple.length === 2 + ) + ) { + throw new Error( + 'AggregateCreateNodesError must be constructed with an array of tuples where the first element is a filename or undefined and the second element is the underlying error.' + ); + } } } diff --git a/packages/nx/src/project-graph/plugins/internal-api.ts b/packages/nx/src/project-graph/plugins/internal-api.ts index 5d07df6ced9c1..230f27f381a18 100644 --- a/packages/nx/src/project-graph/plugins/internal-api.ts +++ b/packages/nx/src/project-graph/plugins/internal-api.ts @@ -101,7 +101,7 @@ export class LoadedNxPlugin { throw e; } // The underlying plugin errored out. We can't know any partial results. - throw new AggregateCreateNodesError([null, e], []); + throw new AggregateCreateNodesError([[null, e]], []); } finally { performance.mark(`${plugin.name}:createNodes - end`); performance.measure( diff --git a/packages/nx/src/project-graph/utils/project-configuration-utils.ts b/packages/nx/src/project-graph/utils/project-configuration-utils.ts index 84abcac8503e5..ded545b3a22a4 100644 --- a/packages/nx/src/project-graph/utils/project-configuration-utils.ts +++ b/packages/nx/src/project-graph/utils/project-configuration-utils.ts @@ -386,9 +386,14 @@ export async function createProjectConfigurations( : // This represents a single plugin erroring out with a hard error. new AggregateCreateNodesError([[null, e]], []); - errorBodyLines.push( - ...error.errors.map(([file, e]) => ` - ${file}: ${e.message}`) - ); + const innerErrors = error.errors; + for (const [file, e] of innerErrors) { + if (file) { + errorBodyLines.push(` - ${file}: ${e.message}`); + } else { + errorBodyLines.push(` - ${e.message}`); + } + } error.message = errorBodyLines.join('\n');