Skip to content

Commit fa1bd9d

Browse files
committed
First step in fixing deps.json
1 parent a8ae853 commit fa1bd9d

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

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

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
265265
{
266266
CalculateExcludedLibraries();
267267

268-
List<RuntimeLibrary> runtimeLibraries = new();
268+
List<ModifiableRuntimeLibrary> runtimeLibraries = new();
269269

270270
if (_includeMainProjectInDepsFile)
271271
{
@@ -292,23 +292,34 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
292292

293293
foreach (var directReference in directAndDependencyReferences)
294294
{
295-
var runtimeLibrary = new RuntimeLibrary(
295+
var runtimeLibrary = new ModifiableRuntimeLibrary(new RuntimeLibrary(
296296
type: "reference",
297297
name: GetReferenceLibraryName(directReference),
298298
version: directReference.Version,
299299
hash: string.Empty,
300-
runtimeAssemblyGroups: new[] { new RuntimeAssetGroup(string.Empty, new[] { CreateRuntimeFile(directReference.FileName, directReference.FullPath) }) },
301-
nativeLibraryGroups: new RuntimeAssetGroup[] { },
300+
runtimeAssemblyGroups: [new RuntimeAssetGroup(string.Empty, [CreateRuntimeFile(directReference.FileName, directReference.FullPath)])],
301+
nativeLibraryGroups: [],
302302
resourceAssemblies: CreateResourceAssemblies(directReference.ResourceAssemblies),
303-
dependencies: Enumerable.Empty<Dependency>(),
303+
dependencies: [],
304304
path: null,
305305
hashPath: null,
306306
runtimeStoreManifestName: null,
307-
serviceable: false);
307+
serviceable: false), []);
308308

309309
runtimeLibraries.Add(runtimeLibrary);
310310
}
311311

312+
/*
313+
* We now need to modify runtimeLibraries to eliminate those that don't have any runtime assets. We follow the following steps:
314+
* 0. Construct a reverse dependencies list: all runtimeLibraries that depend on this one
315+
* 1. If runtimeAssemblyGroups, nativeLibraryGroups, dependencies, and resourceAssemblies are all empty, remove this runtimeLibrary as well as any dependencies on it.
316+
* 2. Add all runtimeLibraries to a list of to-be-processed libraries called libraryCandidatesForRemoval
317+
* 3. libraryCandidatesForRemoval.Pop() --> if there are no runtimeAssemblyGroups, nativeLibraryGroups, or resourceAssemblies, and either dependencies is empty or all
318+
* dependencies have something else that depends on them, remove it (and from libraryCandidatesForRemoval), adding everything that depends on this to
319+
* libraryCandidatesForRemoval if it isn't already there
320+
* Repeat 3 until libraryCandidatesForRemoval is empty
321+
*/
322+
312323
List<CompilationLibrary> compilationLibraries = new();
313324
if (IncludeCompilationLibraries)
314325
{
@@ -393,7 +404,6 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
393404
//
394405
// Otherwise, it is the set of all runtimes compatible with (inheriting)
395406
// the target runtime-identifier.
396-
397407
var runtimeFallbackGraph =
398408
(_runtimeGraph == null || _runtimeIdentifier == null) ?
399409
new RuntimeFallbacks[] { } :
@@ -406,11 +416,11 @@ public DependencyContext Build(string[] userRuntimeAssemblies = null)
406416
targetInfo,
407417
_compilationOptions ?? CompilationOptions.Default,
408418
compilationLibraries,
409-
runtimeLibraries,
419+
runtimeLibraries.Select(library => library.Library),
410420
runtimeFallbackGraph);
411421
}
412422

413-
private RuntimeLibrary GetProjectRuntimeLibrary()
423+
private ModifiableRuntimeLibrary GetProjectRuntimeLibrary()
414424
{
415425
RuntimeAssetGroup[] runtimeAssemblyGroups = new[] { new RuntimeAssetGroup(string.Empty, _mainProjectInfo.OutputName) };
416426

@@ -425,7 +435,7 @@ private RuntimeLibrary GetProjectRuntimeLibrary()
425435
}
426436
}
427437

428-
return new RuntimeLibrary(
438+
return new ModifiableRuntimeLibrary(new RuntimeLibrary(
429439
type: "project",
430440
name: _mainProjectInfo.Name,
431441
version: _mainProjectInfo.Version,
@@ -437,7 +447,7 @@ private RuntimeLibrary GetProjectRuntimeLibrary()
437447
path: null,
438448
hashPath: null,
439449
runtimeStoreManifestName: GetRuntimeStoreManifestName(_mainProjectInfo.Name, _mainProjectInfo.Version),
440-
serviceable: false);
450+
serviceable: false), dependencies.ToHashSet());
441451
}
442452

443453
private List<Dependency> GetProjectDependencies()
@@ -484,11 +494,11 @@ private List<Dependency> GetProjectDependencies()
484494
return dependencies;
485495
}
486496

487-
private IEnumerable<RuntimeLibrary> GetRuntimePackLibraries()
497+
private IEnumerable<ModifiableRuntimeLibrary> GetRuntimePackLibraries()
488498
{
489499
if (_runtimePackAssets == null)
490500
{
491-
return Enumerable.Empty<RuntimeLibrary>();
501+
return [];
492502
}
493503
return _runtimePackAssets.Select(runtimePack =>
494504
{
@@ -500,20 +510,20 @@ private IEnumerable<RuntimeLibrary> GetRuntimePackLibraries()
500510
runtimePack.Value.Where(asset => asset.AssetType == AssetType.Native)
501511
.Select(asset => CreateRuntimeFile(asset.DestinationSubPath, asset.SourcePath)));
502512

503-
return new RuntimeLibrary(
513+
return new ModifiableRuntimeLibrary(new RuntimeLibrary(
504514
type: "runtimepack",
505515
name: runtimePack.Key,
506516
version: runtimePack.Value.First().PackageVersion,
507517
hash: string.Empty,
508-
runtimeAssemblyGroups: new[] { runtimeAssemblyGroup },
509-
nativeLibraryGroups: new[] { nativeLibraryGroup },
510-
resourceAssemblies: Enumerable.Empty<ResourceAssembly>(),
511-
dependencies: Enumerable.Empty<Dependency>(),
512-
serviceable: false);
518+
runtimeAssemblyGroups: [runtimeAssemblyGroup],
519+
nativeLibraryGroups: [nativeLibraryGroup],
520+
resourceAssemblies: [],
521+
dependencies: [],
522+
serviceable: false), []);
513523
});
514524
}
515525

516-
private RuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, string[] userRuntimeAssemblies)
526+
private ModifiableRuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, string[] userRuntimeAssemblies)
517527
{
518528
GetCommonLibraryProperties(library,
519529
out string hash,
@@ -589,7 +599,7 @@ private RuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, string[] use
589599

590600
}
591601

592-
var runtimeLibrary = new RuntimeLibrary(
602+
var runtimeLibrary = new ModifiableRuntimeLibrary(new RuntimeLibrary(
593603
type: library.Type,
594604
name: library.Name,
595605
version: library.Version.ToString(),
@@ -601,7 +611,7 @@ private RuntimeLibrary GetRuntimeLibrary(DependencyLibrary library, string[] use
601611
path: path,
602612
hashPath: hashPath,
603613
runtimeStoreManifestName: GetRuntimeStoreManifestName(library.Name, library.Version.ToString()),
604-
serviceable: serviceable);
614+
serviceable: serviceable), libraryDependencies);
605615

606616
return runtimeLibrary;
607617
}
@@ -923,5 +933,17 @@ private struct LibraryDependency
923933
public string Name { get; set; }
924934
public NuGetVersion MinVersion { get; set; }
925935
}
936+
937+
private class ModifiableRuntimeLibrary
938+
{
939+
public RuntimeLibrary Library { get; set; }
940+
public HashSet<Dependency> Dependencies { get; set; }
941+
942+
public ModifiableRuntimeLibrary(RuntimeLibrary library, HashSet<Dependency> dependencies)
943+
{
944+
this.Library = library;
945+
this.Dependencies = dependencies;
946+
}
947+
}
926948
}
927949
}

0 commit comments

Comments
 (0)