Skip to content

Commit

Permalink
fix: default to filename as proj name for target (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
37IulianPopovici authored Nov 1, 2024
1 parent 73e62f8 commit 4863512
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/nuget-parser/parsers/dotnet-core-v2-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ function recursivelyPopulateNodes(
}
}

function getRestoredProjectName(
publishedProjectDeps: PublishedProjectDeps,
runtimeTarget: string,
projectName: string,
) {
return Object.keys(publishedProjectDeps.targets[runtimeTarget]).find((f) =>
f.startsWith(projectName),
);
}

function buildGraph(
projectName: string,
projectAssets: ProjectAssets,
Expand Down Expand Up @@ -134,13 +144,19 @@ function buildGraph(
// What `dotnet` wants to call this project is not always the same as what Snyk wants to call it, and the version
// postfix is not the same as what's defined in `project.assets.json` due to NuGet version normalization, which is
// not applied during publish, only during restore. So we have to rely on the fact that the name is enough.
const restoreProjectName = Object.keys(
publishedProjectDeps.targets[runtimeTarget],
).find((f) => f.startsWith(projectAssets.project.restore.projectName));
const restoreProjectName =
getRestoredProjectName(
publishedProjectDeps,
runtimeTarget,
projectAssets.project.restore.projectName,
) ||
// Last attempt to find the target using the .csproj filename.
// <PackageId> property overrides most of the naming when restoring, but when publishing, the actual filename is used as the target.
getRestoredProjectName(publishedProjectDeps, runtimeTarget, projectName);

if (!restoreProjectName) {
throw new InvalidManifestError(
`no project name containing ${projectAssets.project.restore.projectName} found in ${runtimeTarget} object, cannot continue without it`,
`no project name containing ${projectAssets.project.restore.projectName} or ${projectName} found in ${runtimeTarget} object, cannot continue without it`,
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Company>Snyk</Company>
<PackageId>Snyk.Project.Name</PackageId>
<Authors>Snyk</Authors>
<Description>This is a description</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NUnit" Version="3.13.3" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"depGraph": {
"schemaVersion": "1.3.0",
"pkgManager": {
"name": "nuget"
},
"pkgs": [
{
"id": "[email protected]",
"info": {
"name": "dotnet_8_with_package_id_property",
"version": "1.0.0"
}
},
{
"id": "[email protected]",
"info": {
"name": "NUnit",
"version": "3.13.3"
}
},
{
"id": "[email protected]",
"info": {
"name": "NETStandard.Library",
"version": "2.0.0"
}
},
{
"id": "[email protected]",
"info": {
"name": "Microsoft.NETCore.Platforms",
"version": "1.1.0"
}
}
],
"graph": {
"rootNodeId": "root-node",
"nodes": [
{
"nodeId": "root-node",
"pkgId": "[email protected]",
"deps": [
{
"nodeId": "[email protected]"
}
]
},
{
"nodeId": "[email protected]",
"pkgId": "[email protected]",
"deps": [
{
"nodeId": "[email protected]"
}
]
},
{
"nodeId": "[email protected]",
"pkgId": "[email protected]",
"deps": [
{
"nodeId": "[email protected]"
}
]
},
{
"nodeId": "[email protected]",
"pkgId": "[email protected]",
"deps": []
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System;
class TestFixture {
static public void Main(String[] args)
{
var client = new System.Net.Http.HttpClient();
Console.WriteLine("Hello, World!");
}
}
9 changes: 9 additions & 0 deletions test/parsers/parse-core-v2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ describe('when generating depGraphs and runtime assemblies using the v2 parser',
targetFramework: 'net8.0',
manifestFilePath: 'obj/project.assets.json',
},
{
description: 'parse dotnet 8.0 with PackageId property',
projectPath:
'./test/fixtures/dotnetcore/dotnet_8_with_package_id_property',
projectFile: 'dotnet_8_with_package_id_property.csproj',
targetFramework: 'net8.0',
manifestFilePath: 'obj/project.assets.json',
},
])(
'succeeds given a project file and returns a single dependency graph for single-targetFramework projects: $description',
async ({ projectPath, projectFile, manifestFilePath, targetFramework }) => {
Expand Down Expand Up @@ -159,6 +167,7 @@ describe('when generating depGraphs and runtime assemblies using the v2 parser',
expectedGraph.depGraph,
);
},
1000000,
);

it.each([
Expand Down

0 comments on commit 4863512

Please sign in to comment.