From 0fd3442e472769382331b29cfa925b82125ad484 Mon Sep 17 00:00:00 2001 From: James Henry Date: Mon, 11 Nov 2024 20:32:11 +0400 Subject: [PATCH] fix(core): cross-workspace implicitDependencies should be safely ignored (#28845) --- e2e/nx/src/misc.test.ts | 42 +++++++++++++++++++ .../nx/src/utils/assert-workspace-validity.ts | 5 ++- .../nx/src/utils/find-matching-projects.ts | 3 +- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/e2e/nx/src/misc.test.ts b/e2e/nx/src/misc.test.ts index 7c7076b1e4..34ce9fed33 100644 --- a/e2e/nx/src/misc.test.ts +++ b/e2e/nx/src/misc.test.ts @@ -767,3 +767,45 @@ describe('global installation', () => { }); }); }); + +describe('cross-workspace implicit dependencies', () => { + beforeAll(() => + newProject({ + packages: ['@nx/js'], + }) + ); + + afterAll(() => cleanupProject()); + + it('should successfully build a project graph when cross-workspace implicit dependencies are present', () => { + const npmPackage = uniq('npm-package'); + runCLI(`generate @nx/workspace:npm-package ${npmPackage}`); + + function setImplicitDependencies(deps: string[]) { + updateFile(join(npmPackage, 'package.json'), (content) => { + const json = JSON.parse(content); + json.nx = { + ...json.nx, + implicitDependencies: deps, + }; + return JSON.stringify(json, null, 2); + }); + } + + // First set the implicit dependencies to an intentionally invalid value to prove the command fails during project graph construction + setImplicitDependencies(['this-project-does-not-exist']); + expect( + runCLI(`test ${npmPackage}`, { + silenceError: true, + }) + ).toContain('Failed to process project graph'); + + // Now set the implicit dependencies to a cross-workspace reference to prove that it is valid, despite not being resolvable in the current workspace + setImplicitDependencies(['nx-cloud:another-workspace']); + expect( + runCLI(`test ${npmPackage}`, { + silenceError: true, + }) + ).toContain('Successfully ran target test'); + }); +}); diff --git a/packages/nx/src/utils/assert-workspace-validity.ts b/packages/nx/src/utils/assert-workspace-validity.ts index fe38108080..b9c76b8bd5 100644 --- a/packages/nx/src/utils/assert-workspace-validity.ts +++ b/packages/nx/src/utils/assert-workspace-validity.ts @@ -114,7 +114,10 @@ function detectAndSetInvalidProjectGlobValues( const projectName = implicit.startsWith('!') ? implicit.substring(1) : implicit; - + // Do not error on cross-workspace implicit dependency references + if (projectName.startsWith('nx-cloud:')) { + return false; + } return !( projectConfigurations[projectName] || findMatchingProjects([implicit], projects).length diff --git a/packages/nx/src/utils/find-matching-projects.ts b/packages/nx/src/utils/find-matching-projects.ts index b78e7197a8..b119e653f4 100644 --- a/packages/nx/src/utils/find-matching-projects.ts +++ b/packages/nx/src/utils/find-matching-projects.ts @@ -49,7 +49,8 @@ export function findMatchingProjects( } for (const stringPattern of patterns) { - if (!stringPattern.length) { + // Do not waste time attempting to look up cross-workspace references which will never match + if (!stringPattern.length || stringPattern.startsWith('nx-cloud:')) { continue; }