Skip to content

Compare rich models and classify who each change breaks - #96

Merged
Mpdreamz merged 1 commit into
mainfrom
api-compare
Nov 14, 2026
Merged

Mpdreamz merged 1 commit into
mainfrom
api-compare

Conversation

@Mpdreamz

Copy link
Copy Markdown
Contributor

Step 3 of the public API files: the comparison on the rich model, with the semver policy we agreed. Nothing in the CLI uses it yet. That comes in the next PR, along with api, --split, --verify and the verify-api action.

What it reports

ApiComparison.Compare(old, new, options) gives a list of changes. Each one has:

  • a path, such as Lib › Widget › Draw(Canvas), and what changed (return type, parameter 1 name, base type);
  • the old and new C#;
  • who it breaks: callers, implementers or inheritors;
  • whether it breaks compiled code, source, or both;
  • its own version level, and a note when the reason isn't obvious.

Suggest(baseline) gives the overall bump.

Some examples, from the scenario snapshots:

A › M()  removed: public void M() → -  [Major, breaks Callers Binary, Source]
I › N()  added: - → void N()  [Major, breaks Implementers Binary, Source]
I › N()  added: - → void N()  [Minor, breaks Implementers Binary, Source, does not count] (I is not an extension point (declared))
I › interfaces  changed: - → IBase  [Major, breaks Implementers Binary, Source]
Widget › base type  changed: Shape → Mid  [Minor] (Shape is still a base)
Widget › Draw()  removed: public void Draw() → -  [Patch] (still inherited from Shape)
A › M(int)  removed: public void M(int a) → -  [Major, breaks Callers Binary] (an overload with more optional parameters replaces it; callers compile, but compiled callers fail)
A › M(int) › parameter 1 name  changed: count → total  [Major, breaks Callers Source] (callers that pass it by name)
A › Max › constant  changed: 1 → 2  [Major, breaks Callers Binary] (compiled code keeps the old value)
A › M() › return type  changed: string → string?  [Minor] (nullability)

The policy

  • Callers: a change that breaks callers always asks for Major.
  • Implementers and inheritors: a break that only reaches them asks for Major only when the type is an extension point. Otherwise it's listed, and the bump comes from what's left. A type isn't an extension point when:
    • nobody outside can implement or derive from it (sealed, static, or no public or protected constructor). Then it isn't a break at all;
    • it's declared not to be one, with an attribute named InternalImplementationOnlyAttribute;
    • it's configured not to be one, through ApiComparisonOptions.NotExtensionPoints (for example Lib.Internal.*). Then it's marked "does not count".
  • Below 1.0: a break asks for Minor.
  • Removed members: a member that was [Obsolete] is noted "deprecated, now removed". A member moved to a base class in the same assembly, or an override that was removed, is still found through the base, so it only asks for Patch.

Checked against

  • Scenarios: 35 old and new libraries, one per rule, each snapshotted.
  • Round trip: a framework assembly compared with its own parsed .api file has no changes.
  • Mutations: each of these made a scenario fail: ignoring the declared extension point, ignoring base-class insertion, and treating an optional-parameter overload as a full break.
  • Real releases: I compared seven pairs of Proc releases by hand, against what the current engine says. They agree on every pair but one. For 0.20.0 → 0.21.0 the current engine says Patch, because of compiler-attribute and reference churn the new model leaves out.

The JustAssembly model moves to the Nullean.PublicApi.Diff.JustAssembly namespace, so its internal types no longer clash with the rich model's. Reports are unchanged: all 617 tool tests pass.

🤖 Generated with Claude Code

ApiComparison compares two ApiAssembly models, from assemblies or .api files, and gives each change its path, what
changed, the old and new C#, who it breaks (callers, implementers, inheritors), whether it breaks compiled code,
source or both, and the version change it calls for. A break that only reaches implementers or inheritors does not
count when nobody outside can implement or derive from the type, or when the type is declared
(InternalImplementationOnlyAttribute) or configured not to be an extension point. Below 1.0 a break suggests a minor
version.

Members moved to a base class or overrides that were removed are still found and only ask for a patch; a member
replaced by an overload with more optional parameters breaks compiled callers only; a removed [Obsolete] member is
noted as deprecated, then removed.

The JustAssembly model moves to its own namespace, so its internal types no longer clash with the rich model's.

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 4e47537 into main Nov 14, 2026
6 of 7 checks passed
@Mpdreamz
Mpdreamz deleted the api-compare branch November 14, 2026 19:12
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]>
Mpdreamz added a commit that referenced this pull request Nov 14, 2026
The CsCheck mirror property failed on #103's CI with seed
`7EiLdrYJA8Z9`. That failure is a bug on main since #96, not a
regression in #103.

A parameter's changes were labelled with its new name. So a parameter
that was renamed and also made `params` read `parameter rest` in one
direction and `parameter p0` in the other. The rename itself was already
reported by position (`parameter 1 name`), and now every other part of a
parameter is too:

```
-A › M(int) › parameter a default  changed: 1 → 2
+A › M(int) › parameter 1 default  changed: 1 → 2
```

Checks: the seed passes with the fix. `mise run ci` passes: 81 library
tests and 630 tool tests.

🤖 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