Skip to content

Commit

Permalink
merge SDK and packages.config references in discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed Dec 27, 2024
1 parent c1d2edc commit 569c19c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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", """
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Include="..\unexpected-directory\packages.config" />
<PackageReference Include="Package.B" Version="2.0.0" />
</ItemGroup>
</Project>
"""),
("unexpected-directory/packages.config", """
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Package.A" version="1.0.0" targetFramework="net46" />
</packages>
"""),
],
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"
],
}
],
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.Build.Evaluation;
using Microsoft.Build.Exceptions;

using NuGet.Frameworks;

using NuGetUpdater.Core.Analyze;
using NuGetUpdater.Core.Utilities;

Expand Down Expand Up @@ -364,19 +366,62 @@ private async Task<ImmutableArray<ProjectDiscoveryResult>> 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,
};
}
}
}
}
Expand Down

0 comments on commit 569c19c

Please sign in to comment.