From 77ab6f88247ce00f2cbe1c6986af33ced531710e Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Mon, 19 Jun 2023 12:59:27 -0400 Subject: [PATCH] fix(core): prune project graph after affected nodes are found to avoid errors --- e2e/nx-run/src/affected-graph.test.ts | 4 ++-- packages/nx/src/command-line/graph/graph.ts | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/e2e/nx-run/src/affected-graph.test.ts b/e2e/nx-run/src/affected-graph.test.ts index 88e83a2d5dce0..93f2349f95b90 100644 --- a/e2e/nx-run/src/affected-graph.test.ts +++ b/e2e/nx-run/src/affected-graph.test.ts @@ -511,8 +511,8 @@ describe('Nx Affected and Graph Tests', () => { expect(model).toHaveProperty('tasks'); }); - it('affected:graph should include affected projects in environment file', () => { - runCLI(`affected:graph --file=project-graph.html`); + it('should include affected projects in environment file', () => { + runCLI(`graph --affected --file=project-graph.html`); const environmentJs = readFile('static/environment.js'); const affectedProjects = environmentJs diff --git a/packages/nx/src/command-line/graph/graph.ts b/packages/nx/src/command-line/graph/graph.ts index bcb18298f5ffc..183ffa8d5d5c2 100644 --- a/packages/nx/src/command-line/graph/graph.ts +++ b/packages/nx/src/command-line/graph/graph.ts @@ -215,11 +215,12 @@ export async function generateGraph( ? args.targets[0] : args.targets; - let graph = pruneExternalNodes( - await createProjectGraphAsync({ exitOnError: true }) - ); + const rawGraph = await createProjectGraphAsync({ exitOnError: true }); + let prunedGraph = pruneExternalNodes(rawGraph); - const projects = Object.values(graph.nodes) as ProjectGraphProjectNode[]; + const projects = Object.values( + prunedGraph.nodes + ) as ProjectGraphProjectNode[]; projects.sort((a, b) => { return a.name.localeCompare(b.name); }); @@ -243,7 +244,7 @@ export async function generateGraph( { printWarnings: true }, readNxJson() ).nxArgs, - graph + rawGraph ) ).map((n) => n.name); } @@ -271,14 +272,18 @@ export async function generateGraph( 'utf-8' ); - graph = filterGraph(graph, args.focus || null, args.exclude || []); + prunedGraph = filterGraph( + prunedGraph, + args.focus || null, + args.exclude || [] + ); if (args.file) { // stdout is a magical constant that doesn't actually write a file if (args.file === 'stdout') { console.log( JSON.stringify( - createJsonOutput(graph, args.projects, args.targets), + createJsonOutput(prunedGraph, args.projects, args.targets), null, 2 ) @@ -334,7 +339,7 @@ export async function generateGraph( } else if (ext === '.json') { ensureDirSync(dirname(fullFilePath)); - const json = createJsonOutput(graph, args.projects, args.targets); + const json = createJsonOutput(prunedGraph, args.projects, args.targets); json.affectedProjects = affectedProjects; json.criticalPath = affectedProjects;