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
  • Loading branch information
FrozenPandaz authored Jun 28, 2023
1 parent 6588ff9 commit c0508e3
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

1 comment on commit c0508e3

@vercel
Copy link

@vercel vercel bot commented on c0508e3 Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.