Skip to content

Commit 3870b55

Browse files
committed
fix(core): check-module-boundaries should work if single quotes not consumed by shell
1 parent decf365 commit 3870b55

File tree

4 files changed

+47
-62
lines changed

4 files changed

+47
-62
lines changed

packages/core/src/generators/init/templates/root/Directory.Build.targets__tmpl__

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
<NodeModulesRelativePath>$([MSBuild]::MakeRelative($(MSBuildProjectDirectory), $(RepoRoot)))</NodeModulesRelativePath>
99
</PropertyGroup>
1010
<Target Name="CheckNxModuleBoundaries" BeforeTargets="Build">
11-
<Exec Command="node $(NodeModulesRelativePath)/<%= checkModuleBoundariesScriptPath %> --project-root '$(MSBuildProjectDirRelativePath)'"/>
11+
<Exec Command="node $(NodeModulesRelativePath)/<%= checkModuleBoundariesScriptPath %> --project-root &quot;$(MSBuildProjectDirRelativePath)&quot;"/>
1212
</Target>
1313
</Project>

packages/core/src/tasks/check-module-boundaries.spec.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,10 @@ describe('enforce-module-boundaries', () => {
7878
it('should exit early if no tags on project', async () => {
7979
const spy = jest.spyOn(checkModule, 'loadModuleBoundaries');
8080
const results = await checkModuleBoundariesForProject('a', {
81-
version: 2,
82-
projects: {
83-
a: {
84-
tags: [],
85-
targets: {},
86-
root: '',
87-
},
81+
a: {
82+
tags: [],
83+
targets: {},
84+
root: '',
8885
},
8986
});
9087
expect(spy).not.toHaveBeenCalled();

packages/core/src/tasks/check-module-boundaries.ts

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import {
2-
NxJsonConfiguration,
32
ProjectConfiguration,
4-
readJsonFile,
3+
ProjectsConfigurations,
54
Tree,
6-
WorkspaceJsonConfiguration,
75
workspaceRoot,
6+
Workspaces,
87
} from '@nrwl/devkit';
9-
import { Workspaces } from '@nrwl/tao/src/shared/workspace';
108

119
import { ESLint } from 'eslint';
12-
import { join, relative } from 'path';
10+
import { relative } from 'path';
1311

1412
import {
1513
getDependantProjectsForNxProject,
@@ -19,10 +17,10 @@ import {
1917

2018
export async function checkModuleBoundariesForProject(
2119
project: string,
22-
workspace: WorkspaceJsonConfiguration,
20+
projects: Record<string, ProjectConfiguration>,
2321
): Promise<string[]> {
24-
const projectRoot = workspace.projects[project].root;
25-
const tags = workspace.projects[project].tags ?? [];
22+
const projectRoot = projects[project].root;
23+
const tags = projects[project].tags ?? [];
2624
if (!tags.length) {
2725
return [];
2826
}
@@ -38,7 +36,7 @@ export async function checkModuleBoundariesForProject(
3836
const violations: string[] = [];
3937
getDependantProjectsForNxProject(
4038
project,
41-
workspace,
39+
{ version: 2, projects },
4240
(configuration, name, implicit) => {
4341
if (implicit) return;
4442
const dependencyTags = configuration?.tags ?? [];
@@ -86,57 +84,47 @@ export async function loadModuleBoundaries(
8684
}
8785
}
8886

87+
function findProjectGivenRoot(
88+
root: string,
89+
projects: ProjectsConfigurations['projects'],
90+
): string {
91+
// Note that this returns the first matching project and would succeed for multiple (cs|fs...)proj under an nx project path,
92+
// but getProjectFileForNxProject explicitly throws if it's not exactly one.
93+
const normalizedRoot = root.replace(/^["'](.+(?=["']$))["']$/, '$1');
94+
const [projectName] =
95+
Object.entries(projects).find(([, projectConfig]) => {
96+
const relativePath = relative(projectConfig.root, normalizedRoot);
97+
return relativePath?.startsWith('..') === false;
98+
}) || [];
99+
100+
if (projectName) {
101+
return projectName;
102+
} else {
103+
console.error(
104+
`Failed to find nx workspace project associated with dotnet project directory: ${root}`,
105+
);
106+
process.exit(1);
107+
}
108+
}
109+
89110
async function main() {
90111
const parser = await import('yargs-parser');
91112
const { project, projectRoot } = parser(process.argv.slice(2), {
92113
alias: {
93114
project: 'p',
94115
},
95-
});
116+
string: ['project', 'projectRoot'],
117+
}) as { project?: string; projectRoot?: string };
96118
const workspace = new Workspaces(workspaceRoot);
97-
const workspaceJson: WorkspaceJsonConfiguration =
119+
const { projects }: ProjectsConfigurations =
98120
workspace.readWorkspaceConfiguration();
99121

100-
// Nx v12 support
101-
const nxJson: NxJsonConfiguration & Record<string, ProjectConfiguration> =
102-
readJsonFile(join(workspaceRoot, 'nx.json'));
103-
if (nxJson.projects) {
104-
Object.entries(nxJson.projects).forEach(([name, config]) => {
105-
const existingTags = workspaceJson.projects[name]?.tags ?? [];
106-
workspaceJson.projects[name].tags = [
107-
...existingTags,
108-
...(config.tags ?? []),
109-
];
110-
});
111-
}
112-
// End Nx v12 support
113-
114-
let nxProject = project;
115122
// Find the associated nx project for the msbuild project directory.
116-
if (!project && projectRoot) {
117-
// Note that this returns the first matching project and would succeed for multiple (cs|fs...)proj under an nx project path,
118-
// but getProjectFileForNxProject explicitly throws if it's not exactly one.
119-
const [projectName] =
120-
Object.entries(workspaceJson.projects).find(([, projectConfig]) => {
121-
const relativePath = relative(projectConfig.root, projectRoot);
122-
return relativePath?.startsWith('..') === false;
123-
}) || [];
124-
125-
if (projectName) {
126-
nxProject = projectName;
127-
} else {
128-
console.error(
129-
`Failed to find nx workspace project associated with dotnet project directory: ${projectRoot}`,
130-
);
131-
process.exit(1);
132-
}
133-
}
123+
const nxProject: string =
124+
project ?? findProjectGivenRoot(projectRoot as string, projects);
134125

135126
console.log(`Checking module boundaries for ${nxProject}`);
136-
const violations = await checkModuleBoundariesForProject(
137-
nxProject,
138-
workspaceJson,
139-
);
127+
const violations = await checkModuleBoundariesForProject(nxProject, projects);
140128
if (violations.length) {
141129
violations.forEach((error) => {
142130
console.error(error);

packages/utils/src/lib/utility-functions/workspace.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import {
22
getProjects,
33
normalizePath as nxNormalizePath,
44
ProjectConfiguration,
5+
ProjectsConfigurations,
56
Tree,
6-
WorkspaceJsonConfiguration,
77
workspaceRoot,
88
} from '@nrwl/devkit';
99

@@ -27,7 +27,7 @@ export function getProjectFileForNxProjectSync(project: ProjectConfiguration) {
2727

2828
export function getDependantProjectsForNxProject(
2929
targetProject: string,
30-
workspaceConfiguration: WorkspaceJsonConfiguration,
30+
projectsConfiguration: ProjectsConfigurations,
3131
forEachCallback?: (
3232
project: ProjectConfiguration & { projectFile: string },
3333
projectName: string,
@@ -39,14 +39,14 @@ export function getDependantProjectsForNxProject(
3939
const projectRoots: { [key: string]: string } = {};
4040
const dependantProjects: { [key: string]: ProjectConfiguration } = {};
4141

42-
Object.entries(workspaceConfiguration.projects).forEach(([name, project]) => {
42+
Object.entries(projectsConfiguration.projects).forEach(([name, project]) => {
4343
projectRoots[name] = normalizePath(resolve(project.root));
4444
});
4545

4646
const absoluteNetProjectFilePath = resolve(
4747
workspaceRoot,
4848
getProjectFileForNxProjectSync(
49-
workspaceConfiguration.projects[targetProject],
49+
projectsConfiguration.projects[targetProject],
5050
),
5151
);
5252
const netProjectFilePath = relative(
@@ -74,15 +74,15 @@ export function getDependantProjectsForNxProject(
7474
if (forEachCallback) {
7575
forEachCallback(
7676
{
77-
...workspaceConfiguration.projects[dependency],
77+
...projectsConfiguration.projects[dependency],
7878
projectFile: workspaceFilePath,
7979
},
8080
dependency,
8181
implicit,
8282
);
8383
}
8484
dependantProjects[dependency] =
85-
workspaceConfiguration.projects[dependency];
85+
projectsConfiguration.projects[dependency];
8686
}
8787
});
8888
}),

0 commit comments

Comments
 (0)