Skip to content

Commit

Permalink
fix(core): read dependencies via dotnet list reference
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Jul 28, 2024
1 parent 80bcb04 commit 071f557
Show file tree
Hide file tree
Showing 14 changed files with 450 additions and 238 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ concurrency:
jobs:
main:
name: Nx Cloud - Main Job
uses: nrwl/ci/.github/workflows/nx-cloud-main.yml@v0.8
uses: nrwl/ci/.github/workflows/nx-cloud-main.yml@v0.15
with:
main-branch-name: 'master'
parallel-commands: |
Expand All @@ -25,6 +25,6 @@ jobs:
agents:
name: Nx Cloud - Agents
uses: nrwl/ci/.github/workflows/nx-cloud-agents.yml@v0.8
uses: nrwl/ci/.github/workflows/nx-cloud-agents.yml@v0.15
with:
number-of-agents: 3
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ concurrency:
jobs:
main:
name: Nx Cloud - Main Job
uses: nrwl/ci/.github/workflows/nx-cloud-main.yml@v0.8
uses: nrwl/ci/.github/workflows/nx-cloud-main.yml@v0.15
with:
main-branch-name: 'master'
parallel-commands: |
Expand All @@ -26,6 +26,6 @@ jobs:
agents:
name: Nx Cloud - Agents
uses: nrwl/ci/.github/workflows/nx-cloud-agents.yml@v0.8
uses: nrwl/ci/.github/workflows/nx-cloud-agents.yml@v0.15
with:
number-of-agents: 3
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ Co-authored-by: Craigory Coppola <craigorycoppola@gmail.com>

### Bug Fixes

- **utils:** getDependantProjectsForNxProject should work on Unix ([96cbc33](https://github.com/nx-dotnet/nx-dotnet/commit/96cbc33ec6b5e9d0492fba4902ee76938230b146)), closes [#43](https://github.com/nx-dotnet/nx-dotnet/issues/43)
- **utils:** forEachDependantProject should work on Unix ([96cbc33](https://github.com/nx-dotnet/nx-dotnet/commit/96cbc33ec6b5e9d0492fba4902ee76938230b146)), closes [#43](https://github.com/nx-dotnet/nx-dotnet/issues/43)

## [0.6.1](https://github.com/nx-dotnet/nx-dotnet/compare/v0.6.0...v0.6.1) (2021-05-19)

Expand Down
3 changes: 2 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"nugetPackages": {
"Swashbuckle.AspNetCore": "6.5.0"
}
}
},
"include": ["demo/**/*"]
}
],
"generators": {
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/graph/create-dependencies.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { workspaceRoot } from '@nx/devkit';
import { resolveReferenceToProject } from './create-dependencies';

describe('createDependencies', () => {
describe('resolveReferenceToProject', () => {
it('should find project in rootMap', () => {
expect(
resolveReferenceToProject(
'../../libs/my-lib/MyLib.csproj',
'apps/my-app/MyApp.csproj',
{
'libs/my-lib': 'my-lib',
'apps/my-app': 'my-app',
},
{
workspaceRoot,
},
),
).toEqual('my-lib');
});
});
});
89 changes: 77 additions & 12 deletions packages/core/src/graph/create-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import {
CreateDependencies,
CreateDependenciesContext,
DependencyType,
NxPluginV1,
ProjectConfiguration,
ProjectGraphBuilder,
RawProjectGraphDependency,
normalizePath,
workspaceRoot,
} from '@nx/devkit';
import { parse } from 'node:path';
import { dirname, parse, relative, resolve } from 'node:path';

import {
getDependenciesFromXmlFile,
NxDotnetConfig,
readConfig,
} from '@nx-dotnet/utils';
import { NxDotnetConfig, readConfig } from '@nx-dotnet/utils';
import { DotNetClient, dotnetFactory } from '@nx-dotnet/dotnet';

// Between Nx versions 16.8 and 17, the signature of `CreateDependencies` changed.
// It used to only consist of the context, but now it also includes the options.
Expand All @@ -29,6 +29,8 @@ type CreateDependenciesCompat<T> = {
): ReturnType<CreateDependencies<T>>;
};

const dotnetClient = new DotNetClient(dotnetFactory());

export const createDependencies: CreateDependenciesCompat<NxDotnetConfig> = (
ctxOrOpts: CreateDependenciesContext | NxDotnetConfig | undefined,
maybeCtx: CreateDependenciesContext | undefined,
Expand All @@ -39,17 +41,35 @@ export const createDependencies: CreateDependenciesCompat<NxDotnetConfig> = (
maybeCtx ?? (ctxOrOpts as CreateDependenciesContext);

let dependencies: RawProjectGraphDependency[] = [];
const rootMap = Object.fromEntries(
Object.entries(ctx.projects).map(([name, project]) => [project.root, name]),
);
const rootMap = createProjectRootMappings(ctx.projects);
for (const source in ctx.filesToProcess.projectFileMap) {
const changed = ctx.filesToProcess.projectFileMap[source];
for (const file of changed) {
const { ext } = parse(file.file);
if (['.csproj', '.fsproj', '.vbproj'].includes(ext)) {
dependencies = dependencies.concat(
getDependenciesFromXmlFile(file.file, source, rootMap),
);
const references = dotnetClient.getProjectReferences(file.file);
const newDeps: RawProjectGraphDependency[] = [];
for (const reference of references) {
const project = resolveReferenceToProject(
normalizePath(reference),
file.file,
rootMap,
ctx,
);
if (project) {
newDeps.push({
source,
target: project,
type: DependencyType.static,
sourceFile: file.file,
});
} else {
console.warn(
`Unable to resolve project for reference ${reference} in ${file.file}`,
);
}
}
dependencies = dependencies.concat(newDeps);
}
}
}
Expand Down Expand Up @@ -78,3 +98,48 @@ export const processProjectGraph: Required<NxPluginV1>['processProjectGraph'] =
}
return builder.getUpdatedProjectGraph();
};

function createProjectRootMappings(
projects: Record<string, ProjectConfiguration>,
) {
const rootMap: Record<string, string> = {};
for (const [, project] of Object.entries(projects)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
rootMap[project.root!] = project.name!;
}
return rootMap;
}

function findProjectForPath(
filePath: string,
rootMap: Record<string, string>,
): string | undefined {
/**
* Project Mappings are in UNIX-style file paths
* Windows may pass Win-style file paths
* Ensure filePath is in UNIX-style
*/
let currentPath = normalizePath(filePath);
for (
;
currentPath !== dirname(currentPath);
currentPath = dirname(currentPath)
) {
const p = rootMap[currentPath];
if (p) {
return p;
}
}
return rootMap[currentPath];
}

export function resolveReferenceToProject(
reference: string,
source: string,
rootMap: Record<string, string>,
context: { workspaceRoot: string },
) {
const resolved = resolve(context.workspaceRoot, dirname(source), reference);
console.log({ reference, source, resolved });
return findProjectForPath(relative(context.workspaceRoot, resolved), rootMap);
}
Loading

0 comments on commit 071f557

Please sign in to comment.