Skip to content

Read the public API into a rich model and write it as a .api file - #94

Merged
Mpdreamz merged 1 commit into
mainfrom
rich-reader
Nov 14, 2026
Merged

Mpdreamz merged 1 commit into
mainfrom
rich-reader

Conversation

@Mpdreamz

Copy link
Copy Markdown
Contributor

The reader and writer behind the .api files, step 1 of the plan agreed for public API files. It needs no CLI changes yet: the api command, --split and --verify come later, as does the comparison on this model.

What Nullean.PublicApi reads now

ApiReader.Read returns what a C# declaration says:

  • base types and interfaces;
  • type parameters with their variance and constraints, including notnull, unmanaged and allows ref struct;
  • nullability, tuple element names and dynamic;
  • ref, out, in, ref readonly, params and scoped;
  • default and constant values, required and init;
  • the attributes that are about the API (the compiler's bookkeeping attributes are dropped).

It also records whether code outside the assembly can derive from or implement a type, which the semver classification will use.

ApiWriter.Write writes the tree, one declaration per line:

assembly Lib
using System: EventArgs, EventHandler<>, IComparable<>, IDisposable, ObsoleteAttribute

namespace Lib.Drawing
  public interface IShape : IDisposable
    double Area { get; }
    static abstract IShape Create()
    void Draw(Canvas canvas)
    void Scale(double factor) { … }

  public sealed class Widget : Shape, IComparable<Widget>
    public Widget(string name, int size = 1)
    public override double Area { get; }
    public required string? Name { get; init; }
    public sealed override void Draw(Canvas canvas)

The header names the types from other assemblies, namespace by namespace, so the lines can use short names. A name two namespaces share is written in full.

The JustAssembly model

The model the existing reports are built on moves into Nullean.PublicApi.Diff as an internal detail, with only its visibility and namespace changed. Neither library has been released, so nothing breaks, and the tool's reports are unchanged: all 617 tests pass.

Tests

  • WriterTests snapshots the .api text of C# samples covering types, signatures and name clashes.
  • RoslynOracleTests compares every visible signature, base list and member of six assemblies (CoreLib, System.Collections.Immutable, System.Text.Json, System.Linq, System.Net.Http, System.Memory) and the samples against Roslyn's reading of the same metadata. It covers types and their nullability, tuple names, ref kinds, modifiers, accessibility, constraints, params, optional and scoped. It found these bugs, all fixed:
    • generic value types do take a nullability byte;
    • a modreq(In) wraps ref readonly property returns;
    • ValueTuple\8` is only a tuple when its eighth argument is one;
    • internal interfaces were showing on public types;
    • a uint DecimalConstant overflowed;
    • notnull next to allows ref struct was missed.
  • Mutations of four decoding rules each made the oracle fail: generic value type nullability, ref readonly, virtual detection and protected visibility.

🤖 Generated with Claude Code

Nullean.PublicApi now reads what a C# declaration says: base types and interfaces, type parameters with variance and
constraints, nullability, tuple element names, dynamic, ref kinds, params, scoped, default and constant values,
required and init, and the attributes that are about the API. ApiWriter writes it as an indented tree of namespaces,
types and members, one declaration per line, with a header that names the types from other assemblies so the
declarations can use short names.

The JustAssembly model the existing reports are built on moves into Nullean.PublicApi.Diff as an internal detail;
neither library has been released, and the reports are unchanged.

RoslynOracleTests checks every visible signature of CoreLib and five other framework assemblies against Roslyn's
reading of the same metadata.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Mpdreamz Mpdreamz added the feature A new capability (minor bump) label Nov 14, 2026
@Mpdreamz
Mpdreamz merged commit 146a978 into main Nov 14, 2026
6 of 7 checks passed
@Mpdreamz
Mpdreamz deleted the rich-reader branch November 14, 2026 18:31
Mpdreamz added a commit that referenced this pull request Nov 14, 2026
Step 4 of the public API files: the CLI and the action, on top of the
reader, parser and comparison from #94 to #96.

## `assembly-differ api`

```sh
dotnet build -c Release && assembly-differ api -o api   # api/MyLib.api for every library in the solution
assembly-differ api --verify api                        # exit 5 when the build no longer matches them
assembly-differ api MyLib.1.2.0.nupkg                   # print a package's API
assembly-differ api bin/Release/net8.0 -t MyLib -f json # every part of each declaration as a field
```

- **Sources:** a `.dll`, a directory, a `.nupkg`, or nothing. With
nothing, it reads the solution's libraries for every target framework,
skipping test and unpackable projects the way `projects` does. It
doesn't build.
- **Frameworks:** an assembly gets one file when all its frameworks have
the same API, and one per framework otherwise, such as
`MyLib.net8.0.api`.
- **Splitting:** `--split namespace` or `--split top-level-namespace`
writes `MyLib/<namespace>.api`. Each file's header names the types it
uses from the other files, so every file parses on its own.
- **Writing:** `-o` removes `.api` files the API no longer has. It only
touches files that start with `assembly `.
- **Verifying:** `--verify` lists each change by path, `file:line`, who
it breaks and how, and the bump, then prints the command that updates
the files:

```
[api] Lib: 2 changes since the committed files, suggested version change: Major
  {fixture}/api/Lib.api:7  Lib › Widget › Name(int, bool)  added public string Name(int index, bool upper)  [minor]
  {fixture}/api/Lib.api:11  Lib › Widget › Name(int)  removed public string Name(int index)  [major: breaks callers, compiled code and source]
[api] {fixture}/api does not match the build. Suggested version change: Major
[api] Update the files with: assembly-differ api {fixture}/new -o {fixture}/api
```

## `nullean/assembly-differ/verify-api@v1`

A composite action that installs the tool and runs `api --verify`.
Checkout and the build are the caller's own steps, as the README
documents.
- **Permissions:** it needs only `contents: read`.
- **Comment:** with `comment: true` and `pull-requests: write`, it keeps
one comment on the pull request through `gh api`, holding the report and
the update command. When the files match again it updates that comment,
and it never posts one for a pull request that always matched.
- **Outputs:** `matches` and `suggested-change`.

## Library

- `ApiWriter.Write(assembly, split)` returns the chunks.
- `ApiParser.Parse(files)` reads them back into one API.
- `ApiDocument.LocationOf(node)` gives each declaration's file and line.
- `ApiChange.OldScope` locates an addition in the old file.

## Tests

- **CLI** (`ApiFileTests`, `ProjectsTests`): print, JSON, write then
verify, five kinds of change under `--verify`, a split
write/verify/rewrite cycle, a missing directory, solution mode and the
help text.
- **Library:** CoreLib and System.Text.Json split both ways parse back
to the same model, field by field.
- **Mutation:** a `--verify` that never reports a match makes two tests
fail.
- **The action:** I ran its shell steps locally, with the built tool and
a stub `gh`. The outputs, exit code 5 and the comment body came out
right. It has **not** run on GitHub. It installs the released tool,
which doesn't have `api` yet, so it can only work after the next
release.

## Not in this PR

`.api` files as a baseline for `files`, `release` and `pull-request`.
Those commands report through the JustAssembly-compatible engine, which
compares assemblies. Taking `.api` files means moving them to the new
comparison, which is the deliberate switch we agreed on: behind a flag
first, the default in 2.0.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature A new capability (minor bump)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant