Skip to content

Commit

Permalink
fix(core): fix workspace validation for implicitDependencies that are… (
Browse files Browse the repository at this point in the history
#17825)

(cherry picked from commit c0508e3)
  • Loading branch information
FrozenPandaz committed Jun 28, 2023
1 parent 0b57d42 commit 09f8a8d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 22 deletions.
23 changes: 12 additions & 11 deletions packages/nx/src/utils/assert-workspace-validity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
57 changes: 46 additions & 11 deletions packages/nx/src/utils/assert-workspace-validity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,26 @@ export function assertWorkspaceValidity(
});
}

const projectsWithNonArrayImplicitDependencies = new Map<string, unknown>();

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];
Expand All @@ -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}`);
}

Expand Down

0 comments on commit 09f8a8d

Please sign in to comment.