From 48635120e8005b9636c6d9a6b947dcdf4782858b Mon Sep 17 00:00:00 2001 From: 37IulianPopovici <136596702+37IulianPopovici@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:21:39 +0200 Subject: [PATCH] fix: default to filename as proj name for target (#227) --- .../parsers/dotnet-core-v2-parser.ts | 24 +++++- .../dotnet_8_with_package_id_property.csproj | 13 ++++ .../expected_depgraph.json | 75 +++++++++++++++++++ .../program.cs | 8 ++ test/parsers/parse-core-v2.spec.ts | 9 +++ 5 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 test/fixtures/dotnetcore/dotnet_8_with_package_id_property/dotnet_8_with_package_id_property.csproj create mode 100644 test/fixtures/dotnetcore/dotnet_8_with_package_id_property/expected_depgraph.json create mode 100644 test/fixtures/dotnetcore/dotnet_8_with_package_id_property/program.cs diff --git a/lib/nuget-parser/parsers/dotnet-core-v2-parser.ts b/lib/nuget-parser/parsers/dotnet-core-v2-parser.ts index 0abe384..b7e2c24 100644 --- a/lib/nuget-parser/parsers/dotnet-core-v2-parser.ts +++ b/lib/nuget-parser/parsers/dotnet-core-v2-parser.ts @@ -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, @@ -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. + // 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`, ); } diff --git a/test/fixtures/dotnetcore/dotnet_8_with_package_id_property/dotnet_8_with_package_id_property.csproj b/test/fixtures/dotnetcore/dotnet_8_with_package_id_property/dotnet_8_with_package_id_property.csproj new file mode 100644 index 0000000..b2a3ddb --- /dev/null +++ b/test/fixtures/dotnetcore/dotnet_8_with_package_id_property/dotnet_8_with_package_id_property.csproj @@ -0,0 +1,13 @@ + + + net8.0 + Snyk + Snyk.Project.Name + Snyk + This is a description + + + + + + diff --git a/test/fixtures/dotnetcore/dotnet_8_with_package_id_property/expected_depgraph.json b/test/fixtures/dotnetcore/dotnet_8_with_package_id_property/expected_depgraph.json new file mode 100644 index 0000000..bcfcc99 --- /dev/null +++ b/test/fixtures/dotnetcore/dotnet_8_with_package_id_property/expected_depgraph.json @@ -0,0 +1,75 @@ +{ + "depGraph": { + "schemaVersion": "1.3.0", + "pkgManager": { + "name": "nuget" + }, + "pkgs": [ + { + "id": "dotnet_8_with_package_id_property@1.0.0", + "info": { + "name": "dotnet_8_with_package_id_property", + "version": "1.0.0" + } + }, + { + "id": "NUnit@3.13.3", + "info": { + "name": "NUnit", + "version": "3.13.3" + } + }, + { + "id": "NETStandard.Library@2.0.0", + "info": { + "name": "NETStandard.Library", + "version": "2.0.0" + } + }, + { + "id": "Microsoft.NETCore.Platforms@1.1.0", + "info": { + "name": "Microsoft.NETCore.Platforms", + "version": "1.1.0" + } + } + ], + "graph": { + "rootNodeId": "root-node", + "nodes": [ + { + "nodeId": "root-node", + "pkgId": "dotnet_8_with_package_id_property@1.0.0", + "deps": [ + { + "nodeId": "NUnit@3.13.3" + } + ] + }, + { + "nodeId": "NUnit@3.13.3", + "pkgId": "NUnit@3.13.3", + "deps": [ + { + "nodeId": "NETStandard.Library@2.0.0" + } + ] + }, + { + "nodeId": "NETStandard.Library@2.0.0", + "pkgId": "NETStandard.Library@2.0.0", + "deps": [ + { + "nodeId": "Microsoft.NETCore.Platforms@1.1.0" + } + ] + }, + { + "nodeId": "Microsoft.NETCore.Platforms@1.1.0", + "pkgId": "Microsoft.NETCore.Platforms@1.1.0", + "deps": [] + } + ] + } + } +} diff --git a/test/fixtures/dotnetcore/dotnet_8_with_package_id_property/program.cs b/test/fixtures/dotnetcore/dotnet_8_with_package_id_property/program.cs new file mode 100644 index 0000000..0be1a5e --- /dev/null +++ b/test/fixtures/dotnetcore/dotnet_8_with_package_id_property/program.cs @@ -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!"); + } +} diff --git a/test/parsers/parse-core-v2.spec.ts b/test/parsers/parse-core-v2.spec.ts index 377b2c9..6924ff4 100644 --- a/test/parsers/parse-core-v2.spec.ts +++ b/test/parsers/parse-core-v2.spec.ts @@ -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 }) => { @@ -159,6 +167,7 @@ describe('when generating depGraphs and runtime assemblies using the v2 parser', expectedGraph.depGraph, ); }, + 1000000, ); it.each([