diff --git a/README.md b/README.md
index eb580a1..0a999ce 100644
--- a/README.md
+++ b/README.md
@@ -297,8 +297,10 @@ declaration as a field.
An enum default is written by name, `Formatting formatting = Formatting.Indented`, which needs the assembly that
declares the enum. A project's come from its restore, `obj/project.assets.json`, and the framework's from the
-installed runtime. A `.dll` or `.nupkg` read on its own finds only what sits next to it: pass the folders that hold
-its dependencies with `-r`, or the value is written as a cast, `(Formatting)1`.
+installed runtime. A `.nupkg` finds its dependencies, and theirs, in the packages folder restore fills
+(`~/.nuget/packages`, or `NUGET_PACKAGES`), at the version restore would pick; nothing is downloaded. A `.dll` read on
+its own finds only what sits next to it: pass the folders that hold its dependencies with `-r`. An enum that is not
+found is written as a cast, `(Formatting)1`.
Check them on every pull request with the `verify-api` action. Checkout and the build are your steps, before it:
diff --git a/src/assembly-differ/ApiFiles/ApiFileCommand.cs b/src/assembly-differ/ApiFiles/ApiFileCommand.cs
index 4330c8b..a08f630 100644
--- a/src/assembly-differ/ApiFiles/ApiFileCommand.cs
+++ b/src/assembly-differ/ApiFiles/ApiFileCommand.cs
@@ -37,7 +37,7 @@ internal sealed class ApiFileCommand
/// Read this project file, whatever kind it is. Repeat it for more than one.
/// Read test projects too.
/// Read projects with IsPackable set to false too.
- /// -r, Look in this directory for the assemblies an API refers to, such as a package's dependencies, so an enum default is written by name. Repeat it for more than one. Projects find their packages without it.
+ /// -r, Look in this directory for the assemblies an API refers to, such as a package's dependencies, so an enum default is written by name. Repeat it for more than one. Projects, and packages already in the packages folder, are found without it.
/// dotnet build -c Release && assembly-differ api -o api
/// assembly-differ api --verify api
/// assembly-differ api bin/Release/net8.0/MyLib.dll
@@ -172,11 +172,16 @@ IReadOnlyList references
if (Nupkg.IsPackage(path))
{
var frameworks = chosen is null ? Nupkg.Frameworks(path) : [chosen];
- return frameworks.SelectMany(
- f => new NupkgAssemblyProvider(path, f)
+ // the package's dependencies, where restore left them
+ return frameworks.SelectMany(f =>
+ {
+ var dependencies = PackageDependencies.Directories(path, f);
+ return new NupkgAssemblyProvider(path, f)
.GetAssemblies(targets)
- .Select(a => new Build(System.IO.Path.GetFileNameWithoutExtension(a.Name), f.GetShortFolderName(), a.FullName, references))
- ).ToList();
+ .Select(
+ a => new Build(System.IO.Path.GetFileNameWithoutExtension(a.Name), f.GetShortFolderName(), a.FullName, [.. references, .. dependencies])
+ );
+ }).ToList();
}
if (Directory.Exists(path))
return new DirectoryAssemblyProvider(path)
diff --git a/src/assembly-differ/ApiFiles/PackageDependencies.cs b/src/assembly-differ/ApiFiles/PackageDependencies.cs
new file mode 100644
index 0000000..170de41
--- /dev/null
+++ b/src/assembly-differ/ApiFiles/PackageDependencies.cs
@@ -0,0 +1,117 @@
+#nullable enable
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using NuGet.Configuration;
+using NuGet.Frameworks;
+using NuGet.Packaging;
+using NuGet.Packaging.Core;
+using NuGet.Versioning;
+
+namespace Differ.ApiFiles;
+
+///
+/// The dependencies of a .nupkg read on its own, found in the packages folder restore fills
+/// (~/.nuget/packages, or NUGET_PACKAGES). Nothing is downloaded: a dependency that is not there is left
+/// out, and an enum from it is written as a cast.
+///
+internal static class PackageDependencies
+{
+ /// The directories holding the assemblies the package's dependencies, and theirs, give a framework to compile against.
+ public static IReadOnlyList Directories(string package, NuGetFramework framework)
+ {
+ var folder = PackagesFolder(Path.GetDirectoryName(Path.GetFullPath(package)));
+ if (folder is null || !Directory.Exists(folder))
+ return [];
+ return Directories(package, framework, folder);
+ }
+
+ internal static IReadOnlyList Directories(string package, NuGetFramework framework, string packagesFolder)
+ {
+ var directories = new List();
+ var seen = new HashSet(StringComparer.OrdinalIgnoreCase);
+ var pending = new Queue();
+ using (var reader = new PackageArchiveReader(package))
+ {
+ seen.Add(reader.GetIdentity().Id);
+ foreach (var dependency in Dependencies(reader, framework))
+ pending.Enqueue(dependency);
+ }
+
+ while (pending.Count > 0)
+ {
+ var dependency = pending.Dequeue();
+ if (!seen.Add(dependency.Id) || Installed(packagesFolder, dependency) is not { } installed)
+ continue;
+ var reader = new PackageFolderReader(installed);
+ foreach (var directory in CompileDirectories(reader, installed, framework))
+ if (!directories.Contains(directory, StringComparer.Ordinal))
+ directories.Add(directory);
+ foreach (var next in Dependencies(reader, framework))
+ pending.Enqueue(next);
+ }
+ return directories;
+ }
+
+ private static string? PackagesFolder(string? directory)
+ {
+ try
+ {
+ return SettingsUtility.GetGlobalPackagesFolder(Settings.LoadDefaultSettings(directory));
+ }
+ catch (NuGetConfigurationException)
+ {
+ return null;
+ }
+ }
+
+ /// The dependencies of the group nearest the framework, as restore picks it.
+ private static IEnumerable Dependencies(PackageReaderBase reader, NuGetFramework framework)
+ {
+ var groups = reader.GetPackageDependencies().ToList();
+ var nearest = new FrameworkReducer().GetNearest(framework, groups.Select(g => g.TargetFramework));
+ return nearest is null ? [] : groups.First(g => g.TargetFramework.Equals(nearest)).Packages;
+ }
+
+ ///
+ /// The version restore would take, the lowest the range allows, when it is in the packages folder; otherwise the
+ /// lowest there that the range allows.
+ ///
+ private static string? Installed(string packagesFolder, PackageDependency dependency)
+ {
+ var directory = Path.Combine(packagesFolder, dependency.Id.ToLowerInvariant());
+ if (!Directory.Exists(directory))
+ return null;
+ var versions = Directory
+ .EnumerateDirectories(directory)
+ .Select(d => NuGetVersion.TryParse(Path.GetFileName(d), out var version) ? (Directory: d, Version: version) : default)
+ .Where(v => v.Version is not null && dependency.VersionRange.Satisfies(v.Version))
+ // a folder restore is still writing, or gave up on, has no .nupkg.metadata yet
+ .Where(v => File.Exists(Path.Combine(v.Directory, ".nupkg.metadata")))
+ .OrderBy(v => v.Version)
+ .ToList();
+ return versions.Count == 0 ? null : versions[0].Directory;
+ }
+
+ /// The folders of the assemblies the package gives the framework to compile against: ref/ when it has one, otherwise lib/.
+ private static IEnumerable CompileDirectories(PackageFolderReader reader, string root, NuGetFramework framework)
+ {
+ foreach (var groups in new[] { reader.GetItems("ref").ToList(), reader.GetLibItems().ToList() })
+ {
+ var nearest = new FrameworkReducer().GetNearest(framework, groups.Select(g => g.TargetFramework));
+ if (nearest is null)
+ continue;
+ var dlls = groups
+ .First(g => g.TargetFramework.Equals(nearest))
+ .Items
+ .Where(i => i.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
+ .Select(i => Path.GetFullPath(Path.GetDirectoryName(Path.Combine(root, i))!))
+ .Distinct(StringComparer.Ordinal)
+ .ToList();
+ if (dlls.Count > 0)
+ return dlls;
+ }
+ return [];
+ }
+}
diff --git a/tests/assembly-differ.Tests/.snapshots/CommandsTests/Help.api.8c1e0300.verified.txt b/tests/assembly-differ.Tests/.snapshots/CommandsTests/Help.api.8c1e0300.verified.txt
index 52ea0b3..3f78f48 100644
--- a/tests/assembly-differ.Tests/.snapshots/CommandsTests/Help.api.8c1e0300.verified.txt
+++ b/tests/assembly-differ.Tests/.snapshots/CommandsTests/Help.api.8c1e0300.verified.txt
@@ -18,7 +18,7 @@ Options:
--project [repeatable] Read this project file, whatever kind it is. Repeat it for more than one. [default: null]
--include-tests Read test projects too.
--include-unpackable Read projects with IsPackable set to false too.
- -r, --reference [repeatable] Look in this directory for the assemblies an API refers to, such as a package's dependencies, so an enum default is written by name. Repeat it for more than one. Projects find their packages without it. [default: null]
+ -r, --reference [repeatable] Look in this directory for the assemblies an API refers to, such as a package's dependencies, so an enum default is written by name. Repeat it for more than one. Projects, and packages already in the packages folder, are found without it. [default: null]
Notes:
diff --git a/tests/assembly-differ.Tests/ApiReferenceTests.cs b/tests/assembly-differ.Tests/ApiReferenceTests.cs
index 9c230bd..41264c1 100644
--- a/tests/assembly-differ.Tests/ApiReferenceTests.cs
+++ b/tests/assembly-differ.Tests/ApiReferenceTests.cs
@@ -62,14 +62,16 @@ public Repository()
private Dictionary Environment => new() { ["NUGET_PACKAGES"] = Packages, ["MSBUILDDISABLENODEREUSE"] = "1" };
- public void Build()
+ public string Package => Path.Combine(Fixture.Root, "out", "Uses.1.0.0.nupkg");
+
+ public void Build() => Dotnet("build", "-c", "Release", "-v", "q");
+
+ /// Packs the project into ; its nuspec says it depends on the package with the enum.
+ public void Pack() => Dotnet("pack", "-c", "Release", "-v", "q", "-o", Path.GetDirectoryName(Package)!);
+
+ private void Dotnet(params string[] args)
{
- var start = new ProcessStartInfo("dotnet", ["build", "-c", "Release", "-v", "q"])
- {
- WorkingDirectory = Directory,
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- };
+ var start = new ProcessStartInfo("dotnet", args) { WorkingDirectory = Directory, RedirectStandardOutput = true, RedirectStandardError = true, };
foreach (var (name, value) in Environment)
start.Environment[name] = value;
using var process = Process.Start(start)!;
@@ -82,6 +84,10 @@ public void Build()
public async Task Run(params string[] args) => await Cli.RunInAsync(Directory, Environment, args);
+ /// Runs with a packages folder of its own instead of the one restore filled.
+ public async Task RunWithPackages(string packages, params string[] args) =>
+ await Cli.RunInAsync(Directory, new Dictionary(Environment) { ["NUGET_PACKAGES"] = packages }, args);
+
public void Dispose() => Fixture.Dispose();
}
@@ -122,4 +128,88 @@ public async Task A_missing_reference_directory_is_an_error()
result.ExitCode.Should().Be(1);
result.Error.Should().Contain("which --reference names");
}
+
+ [Test]
+ public async Task A_package_finds_its_dependencies_in_the_packages_folder()
+ {
+ using var repository = new Repository();
+ repository.Pack();
+ var empty = System.IO.Directory.CreateDirectory(Path.Combine(repository.Fixture.Root, "empty")).FullName;
+
+ var restored = await repository.Run("api", repository.Package);
+ var missing = await repository.RunWithPackages(empty, "api", repository.Package);
+
+ restored.ExitCode.Should().Be(0, restored.Error);
+ restored.Out.Should().Contain("void Run(Mode mode = Mode.Auto)");
+ // nothing is downloaded: a dependency that is not there leaves the value a cast
+ missing.ExitCode.Should().Be(0, missing.Error);
+ missing.Out.Should().Contain("void Run(Mode mode = (Mode)5)");
+ }
+
+ ///
+ /// A package that depends on Middle, which depends on Modes [1.1.0, ). The packages folder holds Modes 1.0.0, below
+ /// the range, 1.1.0 half restored, 1.2.0 and 2.0.0; each declares value 5 under its own name, so the name written
+ /// says which one was read: the lowest the range allows that restore finished, as restore picks.
+ ///
+ [Test]
+ public async Task A_package_takes_the_version_restore_would_through_its_dependencies_dependencies()
+ {
+ using var repository = new Repository();
+ repository.Build();
+ var root = repository.Fixture.Root;
+ var packages = System.IO.Directory.CreateDirectory(Path.Combine(root, "synthetic")).FullName;
+ Installed(packages, "Modes", "1.0.0", root, "Below", finished: true);
+ Installed(packages, "Modes", "1.1.0", root, "Unfinished", finished: false);
+ Installed(packages, "Modes", "1.2.0", root, "Chosen", finished: true);
+ Installed(packages, "Modes", "2.0.0", root, "Higher", finished: true);
+ Installed(packages, "Middle", "1.0.0", root, null, finished: true, dependency: ("Modes", "1.1.0"));
+ var package = Path.Combine(root, "Uses.nupkg");
+ Nupkg(package, "Uses", "1.0.0", ("Middle", "1.0.0"), repository.Assembly);
+
+ var result = await repository.RunWithPackages(packages, "api", package);
+
+ result.ExitCode.Should().Be(0, result.Error);
+ result.Out.Should().Contain("void Run(Mode mode = Mode.Chosen)");
+ }
+
+ private static void Installed(
+ string packages,
+ string id,
+ string version,
+ string root,
+ string? name,
+ bool finished,
+ (string Id, string Version)? dependency = null
+ )
+ {
+ var directory = System.IO.Directory.CreateDirectory(Path.Combine(packages, id.ToLowerInvariant(), version)).FullName;
+ File.WriteAllText(Path.Combine(directory, id.ToLowerInvariant() + ".nuspec"), Nuspec(id, version, dependency));
+ if (finished)
+ File.WriteAllText(Path.Combine(directory, ".nupkg.metadata"), "{}");
+ if (name is null)
+ return;
+ var lib = System.IO.Directory.CreateDirectory(Path.Combine(directory, "lib", "net11.0")).FullName;
+ Fixture.Compile($"namespace Dep {{ public enum Mode {{ Off, On, {name} = 5 }} }}", Path.Combine(lib, "Lib.dll"), referenceAssemblies: true);
+ }
+
+ private static void Nupkg(string path, string id, string version, (string Id, string Version) dependency, string assembly)
+ {
+ using var zip = System.IO.Compression.ZipFile.Open(path, System.IO.Compression.ZipArchiveMode.Create);
+ using (var writer = new StreamWriter(zip.CreateEntry(id + ".nuspec").Open()))
+ writer.Write(Nuspec(id, version, dependency));
+ System.IO.Compression.ZipFileExtensions.CreateEntryFromFile(zip, assembly, "lib/net11.0/" + Path.GetFileName(assembly));
+ }
+
+ private static string Nuspec(string id, string version, (string Id, string Version)? dependency) =>
+ $"""
+
+
+
+ {id}{version}teststests
+
+ {(dependency is { } d ? $"" : "")}
+
+
+
+ """;
}