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): errors thrown when creating projects should prevent running targets #22807

Merged
Merged
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
53 changes: 20 additions & 33 deletions packages/nx/src/project-graph/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ export async function runCreateNodesInParallel(
): Promise<CreateNodesResultWithContext[]> {
performance.mark(`${plugin.name}:createNodes - start`);

const promises: Array<
CreateNodesResultWithContext | Promise<CreateNodesResultWithContext>
> = configFiles.map((file) => {
const errors: CreateNodesError[] = [];
const results: CreateNodesResultWithContext[] = [];

const promises: Array<Promise<void>> = configFiles.map((file) => {
performance.mark(`${plugin.name}:createNodes:${file} - start`);
// Result is either static or a promise, using Promise.resolve lets us
// handle both cases with same logic
Expand All @@ -68,11 +69,14 @@ export async function runCreateNodesInParallel(
return value
.catch((e) => {
performance.mark(`${plugin.name}:createNodes:${file} - end`);
return new CreateNodesError({
error: e,
pluginName: plugin.name,
file,
});
errors.push(
new CreateNodesError({
error: e,
pluginName: plugin.name,
file,
})
);
return null;
})
.then((r) => {
performance.mark(`${plugin.name}:createNodes:${file} - end`);
Expand All @@ -82,42 +86,25 @@ export async function runCreateNodesInParallel(
`${plugin.name}:createNodes:${file} - end`
);

return { ...r, pluginName: plugin.name, file };
// Existing behavior is to ignore null results of
// createNodes function.
if (r) {
results.push(r);
}
});
});
const results = await Promise.all(promises).then((results) => {

await Promise.all(promises).then(() => {
performance.mark(`${plugin.name}:createNodes - end`);
performance.measure(
`${plugin.name}:createNodes`,
`${plugin.name}:createNodes - start`,
`${plugin.name}:createNodes - end`
);
return results;
});

const [errors, successful] = partition<
CreateNodesError,
CreateNodesResultWithContext
>(results, (r): r is CreateNodesError => r instanceof CreateNodesError);

if (errors.length > 0) {
throw new AggregateCreateNodesError(plugin.name, errors, successful);
throw new AggregateCreateNodesError(plugin.name, errors, results);
}
return results;
}

function partition<T, T2 = T>(
arr: Array<T | T2>,
test: (item: T | T2) => item is T
): [T[], T2[]] {
const pass: T[] = [];
const fail: T2[] = [];
for (const item of arr) {
if (test(item)) {
pass.push(item);
} else {
fail.push(item as any as T2);
}
}
return [pass, fail];
}