From 26aa29ace6c6a1e3e2f52d60b71986e3cfe66a29 Mon Sep 17 00:00:00 2001 From: David Boike Date: Tue, 30 Apr 2024 10:18:37 -0500 Subject: [PATCH 1/2] Failing test for when Directory.Build.Props contains PackageReference or GlobalPackageReference based on condition --- .../EntryPointTests.Discover.cs | 71 +++++++++++++++++++ .../Discover/DiscoveryWorkerTestBase.cs | 2 +- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Cli.Test/EntryPointTests.Discover.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Cli.Test/EntryPointTests.Discover.cs index 2ccd2458e4..048626a287 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Cli.Test/EntryPointTests.Discover.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Cli.Test/EntryPointTests.Discover.cs @@ -214,6 +214,77 @@ await RunAsync(path => }); } + [Fact] + public async Task WithDuplicateDependenciesOfDifferentTypes() + { + var projectPath = "path/to/my.csproj"; + var directoryBuildPropsPath = "path/Directory.Build.props"; + await RunAsync(path => + [ + "discover", + "--repo-root", + path, + "--workspace", + path, + ], + new[] + { + (projectPath, """ + + + net8.0 + + + + + + + """), + (directoryBuildPropsPath, """ + + + + + + + + + """) + }, + expectedResult: new() + { + FilePath = "", + Projects = [ + new() + { + FilePath = projectPath, + TargetFrameworks = ["net8.0"], + ReferencedProjectPaths = [], + ExpectedDependencyCount = 2, + Dependencies = [ + new("Newtonsoft.Json", "7.0.1", DependencyType.PackageReference, TargetFrameworks: ["net8.0"], IsDirect: true), + // $(ManagePackageVersionsCentrally) evaluates false by default, we only get a PackageReference + new("System.Text.Json", "8.0.3", DependencyType.PackageReference, TargetFrameworks: ["net8.0"]) + ], + Properties = [ + new("TargetFramework", "net8.0", "path/to/my.csproj"), + ], + }, + new() + { + FilePath = directoryBuildPropsPath, + ReferencedProjectPaths = [], + ExpectedDependencyCount = 2, + Dependencies = [ + new("System.Text.Json", "8.0.3", DependencyType.PackageReference, IsDirect: true), + new("System.Text.Json", "8.0.3", DependencyType.GlobalPackageReference, IsDirect: true) + ], + Properties = [], + } + ] + }); + } + private static async Task RunAsync( Func getArgs, TestFile[] initialFiles, diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTestBase.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTestBase.cs index 36e5807374..4ddcbbde27 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTestBase.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTestBase.cs @@ -91,7 +91,7 @@ void ValidateDependencies(ImmutableArray expectedDependencies, Immut foreach (var expectedDependency in expectedDependencies) { - var actualDependency = actualDependencies.Single(d => d.Name == expectedDependency.Name); + var actualDependency = actualDependencies.Single(d => d.Name == expectedDependency.Name && d.Type == expectedDependency.Type); Assert.Equal(expectedDependency.Name, actualDependency.Name); Assert.Equal(expectedDependency.Version, actualDependency.Version); Assert.Equal(expectedDependency.Type, actualDependency.Type); From b5dcf58946a2155c8ff63eaf7b6ec388fe9ead9b Mon Sep 17 00:00:00 2001 From: David Boike Date: Tue, 30 Apr 2024 10:19:12 -0500 Subject: [PATCH 2/2] Allow package references to be duplicated if of a different type --- .../Discover/SdkProjectDiscovery.cs | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs index 947966c82c..07c8603d95 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/SdkProjectDiscovery.cs @@ -29,24 +29,25 @@ public static async Task> DiscoverAsync(s // The build file dependencies have the correct DependencyType and the TopLevelDependencies have the evaluated version. // Combine them to have the set of dependencies that are directly referenced from the build file. - var fileDependencies = BuildFile.GetDependencies(buildFile) - .ToDictionary(d => d.Name, StringComparer.OrdinalIgnoreCase); - var sdkDependencies = fileDependencies.Values + var fileDependencies = BuildFile.GetDependencies(buildFile).ToImmutableArray(); + var fileDependencyLookup = fileDependencies + .ToLookup(d => d.Name, StringComparer.OrdinalIgnoreCase); + var sdkDependencies = fileDependencies .Where(d => d.Type == DependencyType.MSBuildSdk) .ToImmutableArray(); var indirectDependencies = topLevelDependencies - .Where(d => !fileDependencies.ContainsKey(d.Name)) + .Where(d => !fileDependencyLookup.Contains(d.Name)) .ToImmutableArray(); var directDependencies = topLevelDependencies - .Where(d => fileDependencies.ContainsKey(d.Name)) - .Select(d => + .Where(d => fileDependencyLookup.Contains(d.Name)) + .SelectMany(d => { - var dependency = fileDependencies[d.Name]; - return d with + var dependencies = fileDependencyLookup[d.Name]; + return dependencies.Select(fileDependency => d with { - Type = dependency.Type, + Type = fileDependency.Type, IsDirect = true - }; + }); }).ToImmutableArray(); if (buildFile.GetFileType() == ProjectBuildFileType.Project)