From 569c19c4d536cc3e48df2496820ceb1ca753da2f Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Wed, 11 Dec 2024 11:21:27 -0700 Subject: [PATCH] merge SDK and packages.config references in discovery --- .../DiscoveryWorkerTests.PackagesConfig.cs | 56 ++++++++++++++++ .../Discover/DiscoveryWorker.cs | 67 ++++++++++++++++--- 2 files changed, 112 insertions(+), 11 deletions(-) diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.PackagesConfig.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.PackagesConfig.cs index 5dfb287278..1994f06aa2 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.PackagesConfig.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.PackagesConfig.cs @@ -115,5 +115,61 @@ await TestDiscoveryAsync( } ); } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public async Task DiscoveryIsMergedWithPackageReferences(bool useDirectDiscovery) + { + await TestDiscoveryAsync( + experimentsManager: new ExperimentsManager() { UseDirectDiscovery = useDirectDiscovery }, + packages: + [ + MockNuGetPackage.CreateSimplePackage("Package.A", "1.0.0", "net46"), + MockNuGetPackage.CreateSimplePackage("Package.B", "2.0.0", "net46"), + ], + workspacePath: "src", + files: [ + ("src/myproj.csproj", """ + + + net46 + + + + + + + """), + ("unexpected-directory/packages.config", """ + + + + + """), + ], + expectedResult: new() + { + Path = "src", + Projects = [ + new() + { + FilePath = "myproj.csproj", + Properties = [new("TargetFramework", "net46", "src/myproj.csproj")], + TargetFrameworks = ["net46"], + Dependencies = [ + new("Package.A", "1.0.0", DependencyType.PackagesConfig, TargetFrameworks: ["net46"]), + new("Package.B", "2.0.0", DependencyType.PackageReference, IsDirect: true, TargetFrameworks: ["net46"]), + ], + ReferencedProjectPaths = [], + ImportedFiles = [], + AdditionalFiles = [ + "../unexpected-directory/packages.config" + ], + } + ], + } + ); + } } } diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/DiscoveryWorker.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/DiscoveryWorker.cs index 398f04e54f..6cde5e578e 100644 --- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/DiscoveryWorker.cs +++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Discover/DiscoveryWorker.cs @@ -8,6 +8,8 @@ using Microsoft.Build.Evaluation; using Microsoft.Build.Exceptions; +using NuGet.Frameworks; + using NuGetUpdater.Core.Analyze; using NuGetUpdater.Core.Utilities; @@ -364,19 +366,62 @@ private async Task> RunForProjectPathsAsy } } - if (!results.ContainsKey(relativeProjectPath) && - packagesConfigResult is not null && - packagesConfigResult.Dependencies.Length > 0) + if (packagesConfigResult is not null) { - // project contained only packages.config dependencies - results[relativeProjectPath] = new ProjectDiscoveryResult() + // we might have to merge this dependency with some others + if (results.TryGetValue(relativeProjectPath, out var existingProjectDiscovery)) { - FilePath = relativeProjectPath, - Dependencies = packagesConfigResult.Dependencies, - TargetFrameworks = packagesConfigResult.TargetFrameworks, - ImportedFiles = [], // no imported files resolved for packages.config scenarios - AdditionalFiles = packagesConfigResult.AdditionalFiles, - }; + // merge SDK and packages.config results + var mergedDependencies = existingProjectDiscovery.Dependencies.Concat(packagesConfigResult.Dependencies) + .DistinctBy(d => d.Name, StringComparer.OrdinalIgnoreCase) + .OrderBy(d => d.Name) + .ToImmutableArray(); + var mergedTargetFrameworks = existingProjectDiscovery.TargetFrameworks.Concat(packagesConfigResult.TargetFrameworks) + .Select(t => + { + try + { + var tfm = NuGetFramework.Parse(t); + return tfm.GetShortFolderName(); + } + catch + { + return string.Empty; + } + }) + .Where(tfm => !string.IsNullOrEmpty(tfm)) + .Distinct() + .OrderBy(tfm => tfm) + .ToImmutableArray(); + var mergedProperties = existingProjectDiscovery.Properties; // packages.config discovery doesn't produce properties + var mergedImportedFiles = existingProjectDiscovery.ImportedFiles; // packages.config discovery doesn't produce imported files + var mergedAdditionalFiles = existingProjectDiscovery.AdditionalFiles.Concat(packagesConfigResult.AdditionalFiles) + .Distinct(StringComparer.OrdinalIgnoreCase) + .OrderBy(f => f) + .ToImmutableArray(); + var mergedResult = new ProjectDiscoveryResult() + { + FilePath = existingProjectDiscovery.FilePath, + Dependencies = mergedDependencies, + TargetFrameworks = mergedTargetFrameworks, + Properties = mergedProperties, + ImportedFiles = mergedImportedFiles, + AdditionalFiles = mergedAdditionalFiles, + }; + results[relativeProjectPath] = mergedResult; + } + else + { + // add packages.config results + results[relativeProjectPath] = new ProjectDiscoveryResult() + { + FilePath = relativeProjectPath, + Dependencies = packagesConfigResult.Dependencies, + TargetFrameworks = packagesConfigResult.TargetFrameworks, + ImportedFiles = [], // no imported files resolved for packages.config scenarios + AdditionalFiles = packagesConfigResult.AdditionalFiles, + }; + } } } }