fix(core): throw an error if implicit dependency is a string that is not '*' (#5176)

This commit is contained in:
Jason Jean 2021-03-31 18:08:56 -04:00 committed by GitHub
parent 03d16676ac
commit a300e7613d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 4 deletions

View File

@ -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();
});
});

View File

@ -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[]]) => {