Skip to content

Commit 44fcb1a

Browse files
authored
fix(core): catch workspace validity check errors (#23138)
<!-- 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` --> ## Current Behavior <!-- This is the behavior we have today --> Errors thrown by `assertWorkspaceValidity` aren't handled and lead to the nx process' exit ## Expected Behavior <!-- This is the behavior we should expect with the changes in this PR --> Errors are properly caught and handled with the ability to recover a partial project graph like other errors.
1 parent b898dd5 commit 44fcb1a

File tree

3 files changed

+80
-24
lines changed

3 files changed

+80
-24
lines changed

packages/nx/src/project-graph/build-project-graph.ts

+54-22
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ import {
3838
AggregateProjectGraphError,
3939
CreateMetadataError,
4040
isAggregateProjectGraphError,
41+
isWorkspaceValidityError,
4142
ProcessDependenciesError,
4243
ProcessProjectGraphError,
44+
WorkspaceValidityError,
4345
} from './error-types';
4446
import {
4547
ConfigurationSourceMaps,
@@ -96,9 +98,22 @@ export async function buildProjectGraphUsingProjectFileMap(
9698
projects[project.name] = project;
9799
}
98100

101+
const errors: Array<
102+
| CreateMetadataError
103+
| ProcessDependenciesError
104+
| ProcessProjectGraphError
105+
| WorkspaceValidityError
106+
> = [];
107+
99108
const nxJson = readNxJson();
100109
const projectGraphVersion = '6.0';
101-
assertWorkspaceValidity(projects, nxJson);
110+
try {
111+
assertWorkspaceValidity(projects, nxJson);
112+
} catch (e) {
113+
if (isWorkspaceValidityError(e)) {
114+
errors.push(e);
115+
}
116+
}
102117
const packageJsonDeps = readCombinedDeps();
103118
const rootTsConfig = readRootTsConfig();
104119

@@ -125,27 +140,44 @@ export async function buildProjectGraphUsingProjectFileMap(
125140
};
126141
}
127142

128-
const context = createContext(
129-
projects,
130-
nxJson,
131-
externalNodes,
132-
fileMap,
133-
filesToProcess
134-
);
135-
let projectGraph = await buildProjectGraphUsingContext(
136-
externalNodes,
137-
context,
138-
cachedFileData,
139-
projectGraphVersion,
140-
plugins,
141-
sourceMap
142-
);
143-
const projectFileMapCache = createProjectFileMapCache(
144-
nxJson,
145-
packageJsonDeps,
146-
fileMap,
147-
rootTsConfig
148-
);
143+
let projectGraph: ProjectGraph;
144+
let projectFileMapCache: FileMapCache;
145+
try {
146+
const context = createContext(
147+
projects,
148+
nxJson,
149+
externalNodes,
150+
fileMap,
151+
filesToProcess
152+
);
153+
projectGraph = await buildProjectGraphUsingContext(
154+
externalNodes,
155+
context,
156+
cachedFileData,
157+
projectGraphVersion,
158+
plugins,
159+
sourceMap
160+
);
161+
projectFileMapCache = createProjectFileMapCache(
162+
nxJson,
163+
packageJsonDeps,
164+
fileMap,
165+
rootTsConfig
166+
);
167+
} catch (e) {
168+
// we need to include the workspace validity errors in the final error
169+
if (isAggregateProjectGraphError(e)) {
170+
errors.push(...e.errors);
171+
throw new AggregateProjectGraphError(errors, e.partialProjectGraph);
172+
} else {
173+
throw e;
174+
}
175+
}
176+
177+
if (errors.length > 0) {
178+
throw new AggregateProjectGraphError(errors, projectGraph);
179+
}
180+
149181
return {
150182
projectGraph,
151183
projectFileMapCache,

packages/nx/src/project-graph/error-types.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export class ProjectGraphError extends Error {
1515
| ProjectsWithConflictingNamesError
1616
| ProcessDependenciesError
1717
| ProcessProjectGraphError
18+
| WorkspaceValidityError
1819
>;
1920
readonly #partialProjectGraph: ProjectGraph;
2021
readonly #partialSourceMaps: ConfigurationSourceMaps;
@@ -28,6 +29,7 @@ export class ProjectGraphError extends Error {
2829
| ProcessDependenciesError
2930
| ProcessProjectGraphError
3031
| CreateMetadataError
32+
| WorkspaceValidityError
3133
>,
3234
partialProjectGraph: ProjectGraph,
3335
partialSourceMaps: ConfigurationSourceMaps
@@ -219,6 +221,24 @@ export class ProcessDependenciesError extends Error {
219221
this.stack = `${this.message}\n ${cause.stack.split('\n').join('\n ')}`;
220222
}
221223
}
224+
export class WorkspaceValidityError extends Error {
225+
constructor(public message: string) {
226+
message = `Configuration Error\n${message}`;
227+
super(message);
228+
this.name = this.constructor.name;
229+
}
230+
}
231+
232+
export function isWorkspaceValidityError(
233+
e: unknown
234+
): e is WorkspaceValidityError {
235+
return (
236+
e instanceof WorkspaceValidityError ||
237+
(typeof e === 'object' &&
238+
'name' in e &&
239+
e?.name === WorkspaceValidityError.name)
240+
);
241+
}
222242

223243
export class ProcessProjectGraphError extends Error {
224244
constructor(public readonly pluginName: string, { cause }) {
@@ -236,7 +256,10 @@ export class ProcessProjectGraphError extends Error {
236256
export class AggregateProjectGraphError extends Error {
237257
constructor(
238258
public readonly errors: Array<
239-
CreateMetadataError | ProcessDependenciesError | ProcessProjectGraphError
259+
| CreateMetadataError
260+
| ProcessDependenciesError
261+
| ProcessProjectGraphError
262+
| WorkspaceValidityError
240263
>,
241264
public readonly partialProjectGraph: ProjectGraph
242265
) {

packages/nx/src/utils/assert-workspace-validity.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NxJsonConfiguration } from '../config/nx-json';
33
import { findMatchingProjects } from './find-matching-projects';
44
import { output } from './output';
55
import { ProjectGraphProjectNode } from '../config/project-graph';
6+
import { WorkspaceValidityError } from '../devkit-internals';
67

78
export function assertWorkspaceValidity(
89
projects: Record<string, ProjectConfiguration>,
@@ -99,7 +100,7 @@ export function assertWorkspaceValidity(
99100
.join('\n\n');
100101
}
101102

102-
throw new Error(`Configuration Error\n${message}`);
103+
throw new WorkspaceValidityError(message);
103104
}
104105

105106
function detectAndSetInvalidProjectGlobValues(

0 commit comments

Comments
 (0)