From 64b2153d80786abb4762da35840ac0e7645ae7c4 Mon Sep 17 00:00:00 2001 From: FrozenPandaz Date: Tue, 27 Jun 2023 17:22:38 -0400 Subject: [PATCH] fix(core): fix workspace validation for implicitDependencies that are not arrays --- .../utils/assert-workspace-validity.spec.ts | 23 ++++---- .../nx/src/utils/assert-workspace-validity.ts | 57 +++++++++++++++---- 2 files changed, 58 insertions(+), 22 deletions(-) diff --git a/packages/nx/src/utils/assert-workspace-validity.spec.ts b/packages/nx/src/utils/assert-workspace-validity.spec.ts index 90a76c7a1971c..be5fc66cbba3b 100644 --- a/packages/nx/src/utils/assert-workspace-validity.spec.ts +++ b/packages/nx/src/utils/assert-workspace-validity.spec.ts @@ -30,17 +30,18 @@ describe('assertWorkspaceValidity', () => { it('should throw for an invalid project-level implicit dependency', () => { mockWorkspaceJson.projects.app2.implicitDependencies = ['invalidproj']; - - try { - assertWorkspaceValidity(mockWorkspaceJson, {}); - fail('should not reach'); - } catch (e) { - expect(e.message).toContain( - 'The following implicitDependencies point to non-existent project(s)' - ); - expect(e.message).toContain('invalidproj'); - expect(e.message).toContain('invalidproj'); - } + mockWorkspaceJson.projects.lib1.implicitDependencies = '*'; + + expect(() => assertWorkspaceValidity(mockWorkspaceJson, {})) + .toThrowErrorMatchingInlineSnapshot(` + "Configuration Error + The following implicitDependencies should be an array of strings: + lib1.implicitDependencies: "*" + + The following implicitDependencies point to non-existent project(s): + app2 + invalidproj" + `); }); it('should throw for an invalid project-level implicit dependency with glob', () => { diff --git a/packages/nx/src/utils/assert-workspace-validity.ts b/packages/nx/src/utils/assert-workspace-validity.ts index fa74b79711104..03035c80c48fb 100644 --- a/packages/nx/src/utils/assert-workspace-validity.ts +++ b/packages/nx/src/utils/assert-workspace-validity.ts @@ -38,10 +38,26 @@ export function assertWorkspaceValidity( }); } + const projectsWithNonArrayImplicitDependencies = new Map(); + projectNames .filter((projectName) => { const project = projects[projectName]; - return !!project.implicitDependencies; + + // Report if for whatever reason, a project is configured to use implicitDependencies but it is not an array + if ( + !!project.implicitDependencies && + !Array.isArray(project.implicitDependencies) + ) { + projectsWithNonArrayImplicitDependencies.set( + projectName, + project.implicitDependencies + ); + } + return ( + !!project.implicitDependencies && + Array.isArray(project.implicitDependencies) + ); }) .reduce((map, projectName) => { const project = projects[projectName]; @@ -55,19 +71,38 @@ export function assertWorkspaceValidity( return map; }, invalidImplicitDependencies); - if (invalidImplicitDependencies.size === 0) { + if ( + projectsWithNonArrayImplicitDependencies.size === 0 && + invalidImplicitDependencies.size === 0 + ) { + // No issues return; } - let message = `The following implicitDependencies point to non-existent project(s):\n`; - message += [...invalidImplicitDependencies.keys()] - .map((key) => { - const projectNames = invalidImplicitDependencies.get(key); - return ` ${key}\n${projectNames - .map((projectName) => ` ${projectName}`) - .join('\n')}`; - }) - .join('\n\n'); + let message = ''; + + if (projectsWithNonArrayImplicitDependencies.size > 0) { + message += `The following implicitDependencies should be an array of strings:\n`; + projectsWithNonArrayImplicitDependencies.forEach( + (implicitDependencies, projectName) => { + message += ` ${projectName}.implicitDependencies: "${implicitDependencies}"\n`; + } + ); + message += '\n'; + } + + if (invalidImplicitDependencies.size > 0) { + message += `The following implicitDependencies point to non-existent project(s):\n`; + message += [...invalidImplicitDependencies.keys()] + .map((key) => { + const projectNames = invalidImplicitDependencies.get(key); + return ` ${key}\n${projectNames + .map((projectName) => ` ${projectName}`) + .join('\n')}`; + }) + .join('\n\n'); + } + throw new Error(`Configuration Error\n${message}`); }