Skip to content

Commit

Permalink
fix: use exact project paths (#143)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
AgentEnder and pemsbr authored Sep 27, 2021
1 parent 73d6dd9 commit 7a3c5e9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
6 changes: 2 additions & 4 deletions e2e/core-e2e/tests/nx-dotnet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { join } from 'path';
import { XmlDocument } from 'xmldoc';

import { findProjectFileInPathSync } from '@nx-dotnet/utils';
import { readDependenciesFromNxCache } from '@nx-dotnet/utils/e2e';
import { readDependenciesFromNxDepGraph } from '@nx-dotnet/utils/e2e';
import { execSync } from 'child_process';
import { ensureDirSync } from 'fs-extra';

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

const deps = await readDependenciesFromNxCache(
const deps = await readDependenciesFromNxDepGraph(
join(__dirname, '../../../', e2eDir),
testApp,
);
Expand Down Expand Up @@ -224,8 +224,6 @@ describe('nx-dotnet e2e', () => {

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

console.log('workspace', workspace);

expect(workspace.projects[testApp].targets?.serve).toBeDefined();
expect(workspace.projects[testApp].targets?.build).toBeDefined();
expect(workspace.projects[testApp].targets?.lint).toBeDefined();
Expand Down
25 changes: 15 additions & 10 deletions packages/utils/src/lib/utility-functions/e2e/nx.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { readJson } from 'fs-extra';
import { readJson, remove } from 'fs-extra';
import { join } from 'path';
import { execSync } from 'child_process';

export async function readDependenciesFromNxCache(
export async function readDependenciesFromNxDepGraph(
workspaceRoot: string,
project: string,
) {
const depGraphCachePath = join(
workspaceRoot,
'node_modules/.cache/nx/nxdeps.json',
);
const depGraphFile = join('tmp', 'dep-graph.json');
execSync(`npx nx dep-graph --file ${depGraphFile}`, {
cwd: workspaceRoot,
stdio: 'inherit',
});
const absolutePath = join(workspaceRoot, depGraphFile);
const { graph } = await readJson(absolutePath);
await remove(absolutePath);

const files: Array<{ file: string; deps: string[] }> = (
await readJson(depGraphCachePath)
).nodes[project].data.files;
return new Set(files.flatMap((x) => x.deps));
const deps: Array<{ source: string; target: string }> =
graph.dependencies[project];
console.log('[E2E]: Found dependencies', deps);
return new Set(deps.map((x) => x.target));
}
28 changes: 17 additions & 11 deletions packages/utils/src/lib/utility-functions/workspace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
getProjects,
normalizePath,
normalizePath as nxNormalizePath,
NxJsonProjectConfiguration,
ProjectConfiguration,
Tree,
Expand Down Expand Up @@ -42,11 +42,11 @@ export function getDependantProjectsForNxProject(
const dependantProjects: { [key: string]: ProjectConfiguration } = {};

Object.entries(workspaceConfiguration.projects).forEach(([name, project]) => {
projectRoots[name] = resolve(project.root);
projectRoots[name] = normalizePath(resolve(project.root));
});

const netProjectFilePath = relative(
process.cwd(),
appRootPath,
resolve(
appRootPath,
getProjectFileForNxProjectSync(
Expand All @@ -62,16 +62,15 @@ export function getDependantProjectsForNxProject(

xml.childrenNamed('ItemGroup').forEach((itemGroup) =>
itemGroup.childrenNamed('ProjectReference').forEach((x: XmlElement) => {
const includeFilePath = x.attr['Include'].replace(/\\/g, '/');
let workspaceFilePath: string;
if (isAbsolute(includeFilePath)) {
workspaceFilePath = includeFilePath;
} else {
workspaceFilePath = resolve(hostProjectDirectory, includeFilePath);
}
const includeFilePath = normalizePath(x.attr['Include']);
const workspaceFilePath = normalizePath(
isAbsolute(includeFilePath)
? includeFilePath
: resolve(hostProjectDirectory, includeFilePath),
);

Object.entries(projectRoots).forEach(([dependency, path]) => {
if (workspaceFilePath.startsWith(path)) {
if (workspaceFilePath.startsWith(`${path}/`)) {
if (forEachCallback) {
forEachCallback(
{
Expand Down Expand Up @@ -118,3 +117,10 @@ export function getProjectFilesForProject(
.filter((x) => x.endsWith('proj'))
.map((x) => `${project.sourceRoot}/${x}`);
}

/**
* Currently @nrwl/devkit[normalizePath] functionality differs a bit based on OS. See
*/
function normalizePath(p: string): string {
return nxNormalizePath(p).split('\\').join('/');
}
4 changes: 2 additions & 2 deletions smoke/core/tests/nx-dotnet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ensureDirSync } from 'fs-extra';
import { join } from 'path';

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

const smokeDirectory = 'tmp/smoke-core';
const execSyncOptions: ExecSyncOptions = {
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('nx-dotnet smoke', () => {
stdio: 'ignore',
});

const deps = await readDependenciesFromNxCache(
const deps = await readDependenciesFromNxDepGraph(
join(smokeDirectory, 'test'),
testApp,
);
Expand Down

0 comments on commit 7a3c5e9

Please sign in to comment.