Skip to content

Commit 87f906a

Browse files
committed
PR comments
1 parent ee824dd commit 87f906a

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -314,21 +314,11 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
314314
* libraryCandidatesForRemoval if it isn't already there
315315
* Repeat 3 until libraryCandidatesForRemoval is empty
316316
*/
317-
var libraries = runtimeLibraries.ToDictionary(lib => lib.Library.Name, lib => lib);
318-
foreach (var reference in runtimeLibraries)
319-
{
320-
foreach (var dependency in reference.Library.Dependencies)
321-
{
322-
if (libraries.TryGetValue(dependency.Name, out var dep))
323-
{
324-
dep.Dependents.Add(reference.Library.Name);
325-
}
326-
}
327-
}
328317

329318
// Rather than adding the main project's dependencies, we added references to them, i.e., Foo.Reference.dll
330319
// instead of Foo.dll. This adds Foo.dll as a reference directly so another reference can be removed if it
331320
// isn't necessary because of a direct reference from the main project.
321+
var referenceNameToRealName = new Dictionary<string, string>();
332322
if (_includeMainProjectInDepsFile)
333323
{
334324
var mainProjectReferences = _directReferences;
@@ -348,10 +338,20 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
348338
{
349339
foreach (var directReference in mainProjectReferences)
350340
{
351-
if (libraries.TryGetValue(directReference.Name, out var dep))
352-
{
353-
dep.Dependents.Add(_mainProjectInfo.Name);
354-
}
341+
referenceNameToRealName[GetReferenceLibraryName(directReference)] = directReference.Name;
342+
}
343+
}
344+
}
345+
346+
var libraries = runtimeLibraries.ToDictionary(lib => lib.Library.Name, lib => lib);
347+
foreach (var reference in runtimeLibraries)
348+
{
349+
foreach (var dependency in reference.Library.Dependencies)
350+
{
351+
var name = referenceNameToRealName.TryGetValue(dependency.Name, out var realName) ? realName : dependency.Name;
352+
if (libraries.TryGetValue(name, out var dep))
353+
{
354+
dep.Dependents.Add(reference.Library.Name);
355355
}
356356
}
357357
}
@@ -370,7 +370,7 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
370370
libraries.Remove(lib.Library.Name);
371371
foreach (var dependency in lib.Library.Dependencies)
372372
{
373-
if (libraries.TryGetValue(dependency.Name, out ModifiableRuntimeLibrary? value))
373+
if (libraries.TryGetValue(dependency.Name, out ModifiableRuntimeLibrary value))
374374
{
375375
value.Dependents.Remove(lib.Library.Name);
376376
}
@@ -479,6 +479,8 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
479479
.Where(expansion => expansion.Contains(_runtimeIdentifier))
480480
.Select(expansion => new RuntimeFallbacks(expansion.First(), expansion.Skip(1))); // ExpandRuntime return runtime itself as first item.
481481

482+
var libraryNames = runtimeLibraries.Select(lib => lib.Library.Name).Concat(compilationLibraries.Select(lib => lib.Name)).ToHashSet();
483+
482484
return new DependencyContext(
483485
targetInfo,
484486
_compilationOptions ?? CompilationOptions.Default,
@@ -491,9 +493,7 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
491493
library.Library.RuntimeAssemblyGroups,
492494
library.Library.NativeLibraryGroups,
493495
library.Library.ResourceAssemblies,
494-
library.Library.Dependencies.Where(
495-
dependency => runtimeLibraries.Any(lib => lib.Library.Name.Equals(dependency.Name)) ||
496-
compilationLibraries.Any(lib => lib.Name.Equals(dependency.Name))).ToList(),
496+
library.Library.Dependencies.Where(dependency => libraryNames.Contains(dependency.Name)).ToList(),
497497
library.Library.Serviceable,
498498
library.Library.Path,
499499
library.Library.HashPath,

test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildADesktopLibrary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public void PackageWithoutAssets_ShouldNotShowUpInDepsJson()
149149
var buildCommand = new BuildCommand(testAsset);
150150
buildCommand.Execute().Should().Pass();
151151

152-
using (var depsJsonFileStream = File.OpenRead(Path.Combine(buildCommand.GetOutputDirectory("net9.0").FullName, "PackageWithoutAssets_ShouldNotShowUpInDepsJson.deps.json")))
152+
using (var depsJsonFileStream = File.OpenRead(Path.Combine(buildCommand.GetOutputDirectory(ToolsetInfo.CurrentTargetFramework).FullName, "PackageWithoutAssets_ShouldNotShowUpInDepsJson.deps.json")))
153153
{
154154
var dependencyContext = new DependencyContextJsonReader().Read(depsJsonFileStream);
155155
dependencyContext.RuntimeLibraries.Any(l => l.Name.Equals("Nerdbank.GitVersioning")).Should().BeFalse();

0 commit comments

Comments
 (0)