From 2d50f35d83c0d4cc7c63de6de01268ed916e3107 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Fri, 19 Aug 2022 17:58:40 -0400 Subject: [PATCH] fix(core): Add throw of error if a dependency is not in the graph (#11652) Co-authored-by: Andreas Richter <708186+richtera@users.noreply.github.com> --- .../utilities/buildable-libs-utils.spec.ts | 31 +++++++++++++++++++ .../src/utilities/buildable-libs-utils.ts | 22 ++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/packages/workspace/src/utilities/buildable-libs-utils.spec.ts b/packages/workspace/src/utilities/buildable-libs-utils.spec.ts index b4e80afc9a..e3c30cfd39 100644 --- a/packages/workspace/src/utilities/buildable-libs-utils.spec.ts +++ b/packages/workspace/src/utilities/buildable-libs-utils.spec.ts @@ -84,3 +84,34 @@ describe('calculateProjectDependencies', () => { }); }); }); + +describe('missingDependencies', () => { + it('should throw an error if dependency is missing', async () => { + const graph: ProjectGraph = { + nodes: { + example: { + type: 'lib', + name: 'example', + data: { + files: [], + root: '/root/example', + }, + }, + }, + externalNodes: {}, + dependencies: { + example: [ + { + source: 'example', + target: 'npm:formik', + type: DependencyType.static, + }, + ], + }, + }; + + expect(() => + calculateProjectDependencies(graph, 'root', 'example', 'build', undefined) + ).toThrow(); + }); +}); diff --git a/packages/workspace/src/utilities/buildable-libs-utils.ts b/packages/workspace/src/utilities/buildable-libs-utils.ts index 9b62221341..231f50e2ae 100644 --- a/packages/workspace/src/utilities/buildable-libs-utils.ts +++ b/packages/workspace/src/utilities/buildable-libs-utils.ts @@ -44,7 +44,27 @@ export function calculateProjectDependencies( // gather the library dependencies const nonBuildableDependencies = []; const topLevelDependencies: DependentBuildableProjectNode[] = []; - const dependencies = collectDependencies(projectName, projGraph, [], shallow) + const collectedDeps = collectDependencies( + projectName, + projGraph, + [], + shallow + ); + const missing = collectedDeps.reduce( + (missing: string[] | undefined, { name: dep }) => { + const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep]; + if (!depNode) { + missing = missing || []; + missing.push(dep); + } + return missing; + }, + null + ); + if (missing) { + throw new Error(`Unable to find ${missing.join(', ')} in project graph.`); + } + const dependencies = collectedDeps .map(({ name: dep, isTopLevel }) => { let project: DependentBuildableProjectNode = null; const depNode = projGraph.nodes[dep] || projGraph.externalNodes[dep];