Skip to content

Commit

Permalink
Fix NRE (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikmav authored Sep 19, 2023
1 parent e69e879 commit 711da2f
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/Tasks/CollectDeclaredReferencesTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,32 @@ private Dictionary<string, PackageInfo> GetPackageInfos()
var packageFolders = lockFile.PackageFolders.Select(item => item.Path).ToList();

var nugetFramework = NuGetFramework.ParseComponents(TargetFrameworkMoniker, TargetPlatformMoniker);
var nugetTarget = lockFile.GetTarget(nugetFramework, RuntimeIdentifier);
var nugetLibraries = nugetTarget.Libraries
.Where(nugetLibrary => nugetLibrary.Type.Equals("Package", StringComparison.OrdinalIgnoreCase))
.ToList();
LockFileTarget? nugetTarget = lockFile.GetTarget(nugetFramework, RuntimeIdentifier);

List<LockFileTargetLibrary> nugetLibraries;
if (nugetTarget?.Libraries is not null)
{
nugetLibraries = nugetTarget.Libraries
.Where(nugetLibrary => string.Equals(nugetLibrary.Type, "Package", StringComparison.OrdinalIgnoreCase))
.ToList();
}
else
{
nugetLibraries = new List<LockFileTargetLibrary>();
}

// Compute the hierarchy of packages.
// Keys are packages and values are packages which depend on that package.
var nugetDependants = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
var nugetDependents = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
foreach (LockFileTargetLibrary nugetLibrary in nugetLibraries)
{
var packageId = nugetLibrary.Name;
foreach (var dependency in nugetLibrary.Dependencies)
{
if (!nugetDependants.TryGetValue(dependency.Id, out var parents))
if (!nugetDependents.TryGetValue(dependency.Id, out var parents))
{
parents = new List<string>();
nugetDependants.Add(dependency.Id, parents);
nugetDependents.Add(dependency.Id, parents);
}

parents.Add(packageId);
Expand Down Expand Up @@ -258,7 +267,7 @@ private Dictionary<string, PackageInfo> GetPackageInfos()
if (nugetLibraryAssemblies.Count > 0 || buildFiles.Count > 0)
{
// Walk up to add assets to all packages which directly or indirectly depend on this one.
var seenDependants = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var seenDependents = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var queue = new Queue<string>();
queue.Enqueue(nugetLibrary.Name);
while (queue.Count > 0)
Expand All @@ -274,14 +283,14 @@ private Dictionary<string, PackageInfo> GetPackageInfos()
packageInfoBuilder.AddCompileTimeAssemblies(nugetLibraryAssemblies);
packageInfoBuilder.AddBuildFiles(buildFiles);

// Recurse though dependants
if (nugetDependants.TryGetValue(packageId, out var dependants))
// Recurse though dependents
if (nugetDependents.TryGetValue(packageId, out var dependents))
{
foreach (var dependant in dependants)
foreach (var dependent in dependents)
{
if (seenDependants.Add(dependant))
if (seenDependents.Add(dependent))
{
queue.Enqueue(dependant);
queue.Enqueue(dependent);
}
}
}
Expand Down

0 comments on commit 711da2f

Please sign in to comment.