From a300e7613d2d5a8467b2ffef816b192aaa2c1b1e Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Wed, 31 Mar 2021 18:08:56 -0400 Subject: [PATCH] fix(core): throw an error if implicit dependency is a string that is not '*' (#5176) --- .../core/assert-workspace-validity.spec.ts | 22 +++++++++++++++++++ .../src/core/assert-workspace-validity.ts | 20 +++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/workspace/src/core/assert-workspace-validity.spec.ts b/packages/workspace/src/core/assert-workspace-validity.spec.ts index 41111d9cfc..8ac4e08e01 100644 --- a/packages/workspace/src/core/assert-workspace-validity.spec.ts +++ b/packages/workspace/src/core/assert-workspace-validity.spec.ts @@ -7,6 +7,9 @@ describe('assertWorkspaceValidity', () => { beforeEach(() => { mockNxJson = { + implicitDependencies: { + 'nx.json': '*', + }, projects: { app1: { tags: [], @@ -126,4 +129,23 @@ describe('assertWorkspaceValidity', () => { }); mockExit.mockRestore(); }); + + it('should throw for a project-level implicit dependency that is a string', () => { + spyOn(output, 'error'); + mockNxJson.implicitDependencies['nx.json'] = 'invalidproj'; + + const mockExit = jest + .spyOn(process, 'exit') + .mockImplementation(((code?: number) => {}) as any); + assertWorkspaceValidity(mockWorkspaceJson, mockNxJson); + + expect(mockExit).toHaveBeenCalledWith(1); + expect(output.error).toHaveBeenCalledWith({ + title: 'Configuration Error', + bodyLines: [ + 'nx.json is not configured properly. "nx.json" is improperly configured to implicitly depend on "invalidproj" but should be an array of project names or "*".', + ], + }); + mockExit.mockRestore(); + }); }); diff --git a/packages/workspace/src/core/assert-workspace-validity.ts b/packages/workspace/src/core/assert-workspace-validity.ts index ec4a90fcc4..3a8449b1ab 100644 --- a/packages/workspace/src/core/assert-workspace-validity.ts +++ b/packages/workspace/src/core/assert-workspace-validity.ts @@ -48,18 +48,30 @@ export function assertWorkspaceValidity(workspaceJson, nxJson: NxJson) { nxJson.implicitDependencies || {} ) .reduce((acc, entry) => { - function recur(value, acc = []) { + function recur(value, acc = [], path: string[]) { if (value === '*') { // do nothing since '*' is calculated and always valid. + } else if (typeof value === 'string') { + // This is invalid because the only valid string is '*' + + output.error({ + title: 'Configuration Error', + bodyLines: [ + `nx.json is not configured properly. "${path.join( + ' > ' + )}" is improperly configured to implicitly depend on "${value}" but should be an array of project names or "*".`, + ], + }); + process.exit(1); } else if (Array.isArray(value)) { acc.push([entry[0], value]); } else { - Object.values(value).forEach((v) => { - recur(v, acc); + Object.entries(value).forEach(([k, v]) => { + recur(v, acc, [...path, k]); }); } } - recur(entry[1], acc); + recur(entry[1], acc, [entry[0]]); return acc; }, []) .reduce((map, [filename, projectNames]: [string, string[]]) => {