Skip to content

Commit 7a3c5e9

Browse files
AgentEnderpemsbr
andauthored
fix: use exact project paths (#143)
fix(core): projects w/ similar names should not be picked up as affected by change to one project. Closes #125 Signed-off-by: AgentEnder <craigorycoppola@gmail.com> Co-authored-by: Pedro Rodrigues <4513618+pemsbr@users.noreply.github.com>
1 parent 73d6dd9 commit 7a3c5e9

File tree

4 files changed

+36
-27
lines changed

4 files changed

+36
-27
lines changed

e2e/core-e2e/tests/nx-dotnet.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { join } from 'path';
1717
import { XmlDocument } from 'xmldoc';
1818

1919
import { findProjectFileInPathSync } from '@nx-dotnet/utils';
20-
import { readDependenciesFromNxCache } from '@nx-dotnet/utils/e2e';
20+
import { readDependenciesFromNxDepGraph } from '@nx-dotnet/utils/e2e';
2121
import { execSync } from 'child_process';
2222
import { ensureDirSync } from 'fs-extra';
2323

@@ -65,7 +65,7 @@ describe('nx-dotnet e2e', () => {
6565
'print-affected --target build --base HEAD~1',
6666
);
6767

68-
const deps = await readDependenciesFromNxCache(
68+
const deps = await readDependenciesFromNxDepGraph(
6969
join(__dirname, '../../../', e2eDir),
7070
testApp,
7171
);
@@ -224,8 +224,6 @@ describe('nx-dotnet e2e', () => {
224224

225225
const workspace = readJson<WorkspaceJsonConfiguration>('workspace.json');
226226

227-
console.log('workspace', workspace);
228-
229227
expect(workspace.projects[testApp].targets?.serve).toBeDefined();
230228
expect(workspace.projects[testApp].targets?.build).toBeDefined();
231229
expect(workspace.projects[testApp].targets?.lint).toBeDefined();
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
import { readJson } from 'fs-extra';
1+
import { readJson, remove } from 'fs-extra';
22
import { join } from 'path';
3+
import { execSync } from 'child_process';
34

4-
export async function readDependenciesFromNxCache(
5+
export async function readDependenciesFromNxDepGraph(
56
workspaceRoot: string,
67
project: string,
78
) {
8-
const depGraphCachePath = join(
9-
workspaceRoot,
10-
'node_modules/.cache/nx/nxdeps.json',
11-
);
9+
const depGraphFile = join('tmp', 'dep-graph.json');
10+
execSync(`npx nx dep-graph --file ${depGraphFile}`, {
11+
cwd: workspaceRoot,
12+
stdio: 'inherit',
13+
});
14+
const absolutePath = join(workspaceRoot, depGraphFile);
15+
const { graph } = await readJson(absolutePath);
16+
await remove(absolutePath);
1217

13-
const files: Array<{ file: string; deps: string[] }> = (
14-
await readJson(depGraphCachePath)
15-
).nodes[project].data.files;
16-
return new Set(files.flatMap((x) => x.deps));
18+
const deps: Array<{ source: string; target: string }> =
19+
graph.dependencies[project];
20+
console.log('[E2E]: Found dependencies', deps);
21+
return new Set(deps.map((x) => x.target));
1722
}

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

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
22
getProjects,
3-
normalizePath,
3+
normalizePath as nxNormalizePath,
44
NxJsonProjectConfiguration,
55
ProjectConfiguration,
66
Tree,
@@ -42,11 +42,11 @@ export function getDependantProjectsForNxProject(
4242
const dependantProjects: { [key: string]: ProjectConfiguration } = {};
4343

4444
Object.entries(workspaceConfiguration.projects).forEach(([name, project]) => {
45-
projectRoots[name] = resolve(project.root);
45+
projectRoots[name] = normalizePath(resolve(project.root));
4646
});
4747

4848
const netProjectFilePath = relative(
49-
process.cwd(),
49+
appRootPath,
5050
resolve(
5151
appRootPath,
5252
getProjectFileForNxProjectSync(
@@ -62,16 +62,15 @@ export function getDependantProjectsForNxProject(
6262

6363
xml.childrenNamed('ItemGroup').forEach((itemGroup) =>
6464
itemGroup.childrenNamed('ProjectReference').forEach((x: XmlElement) => {
65-
const includeFilePath = x.attr['Include'].replace(/\\/g, '/');
66-
let workspaceFilePath: string;
67-
if (isAbsolute(includeFilePath)) {
68-
workspaceFilePath = includeFilePath;
69-
} else {
70-
workspaceFilePath = resolve(hostProjectDirectory, includeFilePath);
71-
}
65+
const includeFilePath = normalizePath(x.attr['Include']);
66+
const workspaceFilePath = normalizePath(
67+
isAbsolute(includeFilePath)
68+
? includeFilePath
69+
: resolve(hostProjectDirectory, includeFilePath),
70+
);
7271

7372
Object.entries(projectRoots).forEach(([dependency, path]) => {
74-
if (workspaceFilePath.startsWith(path)) {
73+
if (workspaceFilePath.startsWith(`${path}/`)) {
7574
if (forEachCallback) {
7675
forEachCallback(
7776
{
@@ -118,3 +117,10 @@ export function getProjectFilesForProject(
118117
.filter((x) => x.endsWith('proj'))
119118
.map((x) => `${project.sourceRoot}/${x}`);
120119
}
120+
121+
/**
122+
* Currently @nrwl/devkit[normalizePath] functionality differs a bit based on OS. See
123+
*/
124+
function normalizePath(p: string): string {
125+
return nxNormalizePath(p).split('\\').join('/');
126+
}

smoke/core/tests/nx-dotnet.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ensureDirSync } from 'fs-extra';
55
import { join } from 'path';
66

77
import { rimraf } from '@nx-dotnet/utils';
8-
import { readDependenciesFromNxCache } from '@nx-dotnet/utils/e2e';
8+
import { readDependenciesFromNxDepGraph } from '@nx-dotnet/utils/e2e';
99

1010
const smokeDirectory = 'tmp/smoke-core';
1111
const execSyncOptions: ExecSyncOptions = {
@@ -49,7 +49,7 @@ describe('nx-dotnet smoke', () => {
4949
stdio: 'ignore',
5050
});
5151

52-
const deps = await readDependenciesFromNxCache(
52+
const deps = await readDependenciesFromNxDepGraph(
5353
join(smokeDirectory, 'test'),
5454
testApp,
5555
);

0 commit comments

Comments
 (0)