Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/npm_and_yarn/helpers…
Browse files Browse the repository at this point in the history
…/pnpm-dependencies-aed75d57aa
  • Loading branch information
raj-meka authored May 2, 2024
2 parents 963a9f2 + 41313ee commit a43cf5a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, """
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="7.0.1" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
"""),
(directoryBuildPropsPath, """
<Project>
<ItemGroup Condition="'$(ManagePackageVersionsCentrally)' == 'true'">
<GlobalPackageReference Include="System.Text.Json" Version="8.0.3" />
</ItemGroup>
<ItemGroup Condition="'$(ManagePackageVersionsCentrally)' != 'true'">
<PackageReference Include="System.Text.Json" Version="8.0.3" />
</ItemGroup>
</Project>
""")
},
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<string, string[]> getArgs,
TestFile[] initialFiles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void ValidateDependencies(ImmutableArray<Dependency> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,25 @@ public static async Task<ImmutableArray<ProjectDiscoveryResult>> 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)
Expand Down

0 comments on commit a43cf5a

Please sign in to comment.