Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

PackageBat

Dependabot tells you a new version exists. PackageBat tells you what it breaks in your code.


PackageBat compares a dependency's public API between the version you have and the version a PR bumps it to, then cross-checks that against the APIs your code actually calls. Instead of a changelog to read, you get:

❌ This bump changes 12 APIs you use, in 2 files. (11 breaking, 1 fixed by recompiling)

API Change Where you use it
❌ Command.AddOption(Option) Removed src/TodoCli/Program.cs:12
src/TodoCli/Program.cs:17
❌ System.CommandLine.Handler Type removed (you use SetHandler) src/TodoCli/Program.cs:13
src/TodoCli/Program.cs:18
❌ new Argument<T>(string, string) Signature changed. Now: new Argument<T>(string) src/TodoCli/Program.cs:6
🔁 new Option<T>(string, string) Signature changed, but existing calls still compile (new optional or params parameters)… src/TodoCli/Program.cs:7
…

(Real output for a small app bumping System.CommandLine 2.0.0-beta4 → 2.0.0. The lines it flags are exactly the lines the compiler rejects after the bump. In a PR comment each location is a link.)

Library authors have API-compat tools. PackageBat is the consumer side.

Use it as a GitHub Action

Add .github/workflows/packagebat.yml (also in examples/packagebat.yml):

name: Dependency blast radius
on:
  pull_request:
    paths: ['**/*.csproj', '**/*.fsproj', '**/*.vbproj', '**/*.props', '**/*.targets', '**/packages.config']

permissions:
  contents: read
  pull-requests: write   # to post the report (works for Dependabot PRs too)

jobs:
  blast-radius:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: Moonwalker1307/PackageBat@v0
        with:
          fail-on: breaking   # or 'none' to only comment

On every PR that changes a package version (Dependabot, Renovate or by hand) it:

  1. finds the version changes by reading your project files before and after the PR (PackageReference, central Directory.Packages.props, packages.config, and versions set through MSBuild properties such as Version="$(FooVersion)"; see Versions in MSBuild properties);
  2. builds your code as it was before the bump, which still compiles;
  3. downloads both package versions and compares their public APIs;
  4. scans your compiled assemblies for every reference into the package and maps each one to a file and line through the PDB;
  5. follows the dependencies the bump changes too, and checks what the new versions load by name at run time;
  6. posts one PR comment (updated on each push) and writes the job summary; optionally fails the check.
Input Default
project (auto) Solution or project to build. By default the one at the root, else the only .sln, else every project.
build-args Extra dotnet build arguments.
dotnet-version 8.0.x SDKs to install (one per line). Add yours if your code targets something newer.
fail-on none breaking, binary or warning: fail when used changes reach this level.
threshold 0 Only fail when more than this many used changes reach fail-on.
fail-on-license-change false true: fail when a bumped package, or a dependency it updates, declares a different licence.
coverage Opt-in. Cobertura coverage file(s) from your tests, one path or glob per line. See Test coverage.
comment true Post the report as a PR comment.
base-build Path to a checkout of the base commit that an earlier step already built. PackageBat analyses it instead of building again. See Cost in CI.
packagebat-version Install this PackageBat version from nuget.org instead of compiling it from the action's source.
base-ref PR base What to compare against.

Outputs: breaking, used-changes, license-changes (packages whose licence changed), untested (with coverage: affected call sites your tests never run), report (Markdown path), report-json.

Pin a version

Point uses: at a release tag, not @main. @v0 follows the latest 0.x release. @v0.2.0 (or a full commit SHA) never moves, which is the safer choice if you want to review every PackageBat update yourself, for example through Dependabot's github-actions updates. @main is whatever was merged last.

Cost in CI

A run that finds no package version change stops after reading your project files, and the paths filter above keeps the workflow from starting at all on PRs that don't touch them. When there is a bump, the expensive part is building your code at the base commit, a second build next to your normal CI. Two inputs cut that down:

  • base-build: if an earlier step in the same job already built the base commit (the coverage step below does), pass its folder and PackageBat analyses that build instead of building again.
  • packagebat-version: install the published PackageBat tool from nuget.org instead of compiling it from source. Use the version that matches the tag you pin, e.g. packagebat-version: 0.2.0 with @v0.2.0.

project limits the build to one solution or project if the default picks up more than the code that uses your packages.

Pull requests from forks

On pull_request events from a fork, GitHub gives the workflow a read-only token, so PackageBat can't post the comment. It logs a warning and the report is still in the job summary, and fail-on still fails the check. Dependabot and Renovate PRs in your own repository are not forks and get the comment. If you accept PRs from forks and want the comment there too, set comment: false, upload the file named by the report output as an artifact, and post it from a separate workflow_run workflow, which runs with a write token. Don't switch this workflow to pull_request_target instead: that runs with write access and your secrets while PackageBat reads files from the PR.

Test coverage (opt-in)

Give PackageBat a Cobertura coverage file and the report also says whether your tests run each affected line: a summary such as "2 of 3 affected call sites are never run by your tests", a Run by tests column, and (untested) after each location no test reaches. It only reads the file; producing it is up to you, in a step before PackageBat.

Run the tests on the PR's base commit. That is the code PackageBat analyses, so the line numbers match, and it still compiles even when the bump breaks the build:

      - name: Test coverage before the bump
        env:
          BASE: ${{ github.event.pull_request.base.sha }}
        run: |
          git worktree add "$RUNNER_TEMP/base" "$(git merge-base "$BASE" HEAD)"
          dotnet test "$RUNNER_TEMP/base" --collect "XPlat Code Coverage" --results-directory "$RUNNER_TEMP/coverage"
      - uses: Moonwalker1307/PackageBat@v0
        with:
          coverage: ${{ runner.temp }}/coverage/**/coverage.cobertura.xml
          base-build: ${{ runner.temp }}/base   # dotnet test already built it, so PackageBat doesn't build again

XPlat Code Coverage comes from the coverlet.collector package, which dotnet new xunit/nunit/mstest projects already reference. Any Cobertura file works (ReportGenerator, coverage.py, istanbul...), and several files are added up. Paths are matched on their end, so coverage made in another checkout or on another machine still lines up.

Mocked in tests. When your test projects' build output is part of --consumer, PackageBat also notices tests that replace a changed API's type with a test double: a mocking library's generic argument (new Mock<IFoo>(), Substitute.For<IFoo>(), A.Fake<IFoo>(), from Moq, NSubstitute, FakeItEasy, JustMock or Rhino Mocks) or a class in a test assembly that implements the interface. The Run by tests cell then adds "🎭 tests mock IFoo" with where, and the summary warns that a passing test there may have run the double instead of the package. Static members and constructors can't be mocked, so they never get this. Coverage adds up all tests, so it means "some test mocks this type", not "this line only ran against a mock".

A covered line is one that ran during the tests. That doesn't prove a test checks what the line does, so treat ✅ as "something would probably have noticed" and ❌ as "nothing will".

Use it from the command line

dotnet tool install --global PackageBat

# one package, against your built code
packagebat analyze --package System.CommandLine --from 2.0.0-beta4.22272.1 --to 2.0.0 --consumer src

# whatever changed between two git revisions
packagebat analyze --base origin/main --consumer . --markdown report.md --json report.json --fail-on breaking

Add --coverage coverage.cobertura.xml (repeatable) to analyze to include test coverage.

--consumer takes built assemblies or folders to search (anything with a .pdb next to it, obj/ skipped). Run packagebat help for all options. Exit codes: 0 ok, 1 error, 2 over the --fail-on threshold (or a licence change with --fail-on-license-change).

What it detects

Severity Change Hits you when you…
❌ Breaking Type removed or moved (with where it went) use the type at all
❌ Breaking Member removed call it, read it, or override it
❌ Breaking Signature changed (parameter, return or property type, static ↔ instance) call it
❌ Breaking New member on an interface / new abstract member implement or derive from it
❌ Breaking Class sealed or its constructors hidden derive from it
❌ Breaking [Obsolete(..., error: true)] added use it
🔁 Recompile Only optional or params parameters added call it (old binaries break; source still compiles, so check the new parameters mean what you expect)
⚠️ Warning [Obsolete] added use it
⚠️ Warning New overload that takes the same number of arguments as a method or constructor you call (including a new extension method of the same name in the same static class) call it: your compiled code still binds to the old member, but the source may now be ambiguous (Send(null) with a new Send(Uri)) or pick the new overload. PackageBat can't tell which calls are affected, so it lists every call to recompile and look at
⚠️ Warning New version loads an assembly by name at run time (Type.GetType("Ns.Type, Some.Assembly"), Assembly.Load) that nothing in your dependency graph provides depend on the package at all: the build passes and the app fails when that code runs, so the report names the package to add
⚠️ Warning Not every project moves to the new version: other project files (or a VersionOverride) in the repository still set the package to another version after the change build those projects into the same app: only one version loads at run time, so code built against the other one can fail. The report lists each file and its version
⚠️ Warning Licence changed: a different SPDX expression (MIT → BUSL-1.1), a switch to or from a licence file, a licence file whose text changed, or the package newly requiring licence acceptance depend on the package at all, including through a dependency it updates. Add --fail-on-license-change to fail the check on it

Changes that don't touch your code are listed in a collapsed section, so you can still see the whole picture.

Dependencies that change with a bump. A package's new version often needs newer versions of its own dependencies, which then change even though no project file mentions them (Refit.HttpClientFactory 8 → 16 also takes Refit from 8 to 16). PackageBat reads both versions' .nuspec, takes the version you had from your build's obj/project.assets.json, and analyses each dependency that changes like any other bump, marked via the package that brought it in. The ones your code doesn't reference are listed on one line each.

Versions in MSBuild properties. <PackageReference Include="Foo" Version="$(FooVersion)" /> (or a central PackageVersion that does the same) is resolved the way the project sees it: a property set in the project file itself, else in the nearest .props/.targets in its folder or a parent folder (Directory.Build.props, Directory.Packages.props), else in any other .props/.targets in the repository when every definition there agrees (a shared versions file such as eng/Versions.props). Properties built from other properties ($(FooMajor).4.0) are followed. A PR that only edits the property counts as a bump of every package that uses it, reported against the project file that references the package. PackageBat doesn't run MSBuild, so a property set under a build-dependent Condition (anything but the '$(Foo)' == '' default pattern), set on the command line, or computed with a property function is not known, and versions that use it are skipped as before.

Why compiled code? The compiler has already resolved overloads, extension methods, generics and implicit conversions, so PackageBat sees exactly which member each line binds to, with no guessing from source text. The flip side is that it sees the binding against the old version; see What a green report does not tell you.

What a green report does not tell you

PackageBat compares the two versions' metadata and reads how your already compiled code binds to the old one. A report with nothing in it means none of the API changes it can see touch the APIs your code binds to. It does not mean the upgrade is safe. It can't see:

  • Behaviour changes behind an unchanged signature: different results, new exceptions, changed defaults. Your tests are the check for those, and the coverage column shows which affected lines they don't reach.
  • Source that binds differently after the bump. The compiler resolved overloads, extension methods and conversions against the old version. A new overload is flagged as a warning (see above), but a new extension method in another class or namespace, a better implicit conversion, or a new generic overload with fewer parameters can change what a line binds to without any warning.
  • Changed generic constraints (where T : class becoming where T : IDisposable) and nullable annotation changes. Neither is part of the API PackageBat extracts yet. A tightened constraint can stop code from compiling, and a changed nullable annotation gives new warnings, which fail builds with warnings as errors.
  • APIs your code reaches by name: your own reflection (Type.GetType, GetMethod("...")), DI container registration or scanning by convention, serializers and config binding. There is no compile-time reference to follow, so these usages are invisible. (What the new package loads by name is checked, see the table above.)
  • Dependency changes forced by something other than the bumped packages' own minimum versions, and overrides of members on generic base classes where the signature uses the base's type parameters.

Treat a clean report as "nothing obviously breaks at compile time", and still build and test the PR.

Other languages

Only the Ecosystems/ folder knows about .NET. Everything else (the diff, matching usages to changes, the report, the Action's comment) works on a small language-neutral JSON format:

  • API surface: packagebat api --package X --version 1.2.3 → types, members with a join key, parameters, deprecation.
  • Usages: packagebat usages --package X --version 1.2.3 --consumer bin/ → { typeName, memberKey, kind, location: { file, line } }.
  • Analyze: packagebat analyze --old-api old.json --new-api new.json --usages usages.json runs on JSON alone.

So adding TypeScript, Java or Python means writing an extractor and a usage scanner (in any language) that emit those two files, or implementing IEcosystem in-process. The rules for the join key are at the top of ApiSurface.cs.

Layout

src/PackageBat/
  Model/        language-neutral API, usage and report types (the JSON format)
  Analysis/     ApiDiff (old vs new API), UsageMatcher (your usages vs the diff), LineCoverage (Cobertura)
  Report/       Markdown for PR comments
  Ecosystems/   IEcosystem + DotNet/ (NuGet download, metadata API extractor, IL+PDB usage scanner, bump detector)
action.yml, action/run.sh   the GitHub Action
tests/PackageBat.Tests/   end-to-end tests that compile two library versions and a consumer in memory
tests/PackageBat.Fuzz/    fuzz and property tests (see below)

Fuzzing

tests/PackageBat.Fuzz uses CsCheck to throw corrupted package and consumer assemblies, corrupted PDBs, corrupted JSON (through packagebat analyze) and junk project files at PackageBat, and checks properties of the diff and matcher on random API surfaces. In normal CI each target runs a few hundred inputs; the Fuzz workflow runs every night for longer and can be started by hand.

FUZZ_SECONDS=60 dotnet test tests/PackageBat.Fuzz           # one minute per target
FUZZ_SEED=<seed> dotnet test tests/PackageBat.Fuzz --filter <test>   # replay a failure

Inputs that crash a target are saved under fuzz-findings/ (the workflow uploads them as an artifact).

Releasing

Create a GitHub release with a new tag like v0.3.0 on a commit in main. Two workflows then run on the tag:

  • publish.yml publishes that version of the tool to nuget.org.
  • major-tag.yml moves v0 to the same commit, so @v0 users get the release. Prereleases (v0.3.0-beta.1) and tags older than the newest 0.x release leave v0 alone. To point v0 at an existing release, run the workflow by hand from the Actions tab with that tag.

Moving v0 does not publish anything: publish.yml only reacts to full v*.*.* tags.

License

MIT

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages