Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
15 changes: 10 additions & 5 deletions src/assembly-differ/ApiFiles/ApiFileCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal sealed class ApiFileCommand
/// <param name="project">Read this project file, whatever kind it is. Repeat it for more than one.</param>
/// <param name="includeTests">Read test projects too.</param>
/// <param name="includeUnpackable">Read projects with IsPackable set to false too.</param>
/// <param name="reference">-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.</param>
/// <param name="reference">-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.</param>
/// <example>dotnet build -c Release &amp;&amp; assembly-differ api -o api</example>
/// <example>assembly-differ api --verify api</example>
/// <example>assembly-differ api bin/Release/net8.0/MyLib.dll</example>
Expand Down Expand Up @@ -172,11 +172,16 @@ IReadOnlyList<string> 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)
Expand Down
117 changes: 117 additions & 0 deletions src/assembly-differ/ApiFiles/PackageDependencies.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// The dependencies of a <c>.nupkg</c> read on its own, found in the packages folder restore fills
/// (<c>~/.nuget/packages</c>, or <c>NUGET_PACKAGES</c>). Nothing is downloaded: a dependency that is not there is left
/// out, and an enum from it is written as a cast.
/// </summary>
internal static class PackageDependencies
{
/// <summary>The directories holding the assemblies the package's dependencies, and theirs, give a framework to compile against.</summary>
public static IReadOnlyList<string> 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<string> Directories(string package, NuGetFramework framework, string packagesFolder)
{
var directories = new List<string>();
var seen = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var pending = new Queue<PackageDependency>();
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;
}
}

/// <summary>The dependencies of the group nearest the framework, as restore picks it.</summary>
private static IEnumerable<PackageDependency> 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;
}

/// <summary>
/// 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.
/// </summary>
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;
}

/// <summary>The folders of the assemblies the package gives the framework to compile against: ref/ when it has one, otherwise lib/.</summary>
private static IEnumerable<string> 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 [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Options:
--project <values> [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 <values> [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 <values> [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:

Expand Down
104 changes: 97 additions & 7 deletions tests/assembly-differ.Tests/ApiReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,16 @@ public Repository()

private Dictionary<string, string> 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");

/// <summary>Packs the project into <see cref="Package"/>; its nuspec says it depends on the package with the enum.</summary>
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)!;
Expand All @@ -82,6 +84,10 @@ public void Build()

public async Task<CliResult> Run(params string[] args) => await Cli.RunInAsync(Directory, Environment, args);

/// <summary>Runs with a packages folder of its own instead of the one restore filled.</summary>
public async Task<CliResult> RunWithPackages(string packages, params string[] args) =>
await Cli.RunInAsync(Directory, new Dictionary<string, string>(Environment) { ["NUGET_PACKAGES"] = packages }, args);

public void Dispose() => Fixture.Dispose();
}

Expand Down Expand Up @@ -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)");
}

/// <summary>
/// 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.
/// </summary>
[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) =>
$"""
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2013/05/nuspec.xsd">
<metadata>
<id>{id}</id><version>{version}</version><authors>tests</authors><description>tests</description>
<dependencies>
<group targetFramework="net11.0">{(dependency is { } d ? $"<dependency id=\"{d.Id}\" version=\"{d.Version}\" />" : "")}</group>
</dependencies>
</metadata>
</package>
""";
}
Loading