Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge SDK and packages.config references in discovery #11099

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -0,0 +1,65 @@
using NuGetUpdater.Core.Utilities;

using Xunit;

namespace NuGetUpdater.Core.Test.Utilities;

public class ProjectHelperTests : TestBase
{
[Theory]
[MemberData(nameof(AdditionalFile))]
public async Task GetAdditionalFilesFromProject(string projectPath, (string Name, string Content)[] files, string[] expectedAdditionalFiles)
{
using var tempDirectory = await TemporaryDirectory.CreateWithContentsAsync(files);
var fullProjectPath = Path.Join(tempDirectory.DirectoryPath, projectPath);

var actualAdditionalFiles = ProjectHelper.GetAllAdditionalFilesFromProject(fullProjectPath, ProjectHelper.PathFormat.Relative);
AssertEx.Equal(expectedAdditionalFiles, actualAdditionalFiles);
}

public static IEnumerable<object[]> AdditionalFile()
{
// no additional files
yield return
[
// project path
"src/project.csproj",
// files
new[]
{
("src/project.csproj", """
<Project>
</Project>
""")
},
// expected additional files
Array.Empty<string>()
];

// files with relative paths
yield return
[
// project path
"src/project.csproj",
// files
new[]
{
("src/project.csproj", """
<Project>
<ItemGroup>
<None Include="..\unexpected-path\packages.config" />
</ItemGroup>
</Project>
"""),
("unexpected-path/packages.config", """
<packages></packages>
""")
},
// expected additional files
new[]
{
"../unexpected-path/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 @@ -374,19 +376,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
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ private static string MakePathAppropriateFormat(string fullProjectPath, string f
var itemPath = projectRootElement.Items
.Where(i => i.ElementName.Equals("None", StringComparison.OrdinalIgnoreCase) ||
i.ElementName.Equals("Content", StringComparison.OrdinalIgnoreCase))
.Where(i => Path.GetFileName(i.Include).Equals(itemFileName, StringComparison.OrdinalIgnoreCase))
.Select(i => Path.GetFullPath(Path.Combine(projectDirectory, i.Include)))
.Where(i => !string.IsNullOrEmpty(i.Include))
.Select(i => Path.GetFullPath(Path.Combine(projectDirectory, i.Include.NormalizePathToUnix())))
.Where(p => Path.GetFileName(p).Equals(itemFileName, StringComparison.OrdinalIgnoreCase))
.Where(File.Exists)
.FirstOrDefault()
?.NormalizePathToUnix();
.FirstOrDefault();
return itemPath;
}

Expand Down
Loading