-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Add CI enforcement to prevent removal of ApiCompatVersion from GA packages #57635
Description
Problem
There is no CI or build-time enforcement preventing a team from removing ApiCompatVersion from their .csproj file. If removed, API compatibility checking silently stops running — CI passes with zero errors, and the PR merges without any warning.
Current behavior
Update-PkgVersion.ps1automatically addsApiCompatVersionwhen a package transitions from GA to its next prerelease version (e.g.,1.0.0→1.1.0-beta.1)- The existing
ValidateRunApiCompattarget (line 16 ofeng/Directory.Build.Common.targets) only checks thatRunApiCompatisn't explicitly set tofalse - However, if
ApiCompatVersionis removed entirely, theMicrosoft.DotNet.ApiCompatNuGet package is never referenced,RunApiCompatis never set (it's empty, notfalse), and the validation check passes silently - The only current defense against removal is PR code review
Why this matters
A team could (intentionally or accidentally) delete ApiCompatVersion from their .csproj, and:
- CI passes ✅
- PR merges ✅
- Breaking API changes ship undetected ❌
Why pre-GA packages don't have ApiCompatVersion
Packages that have never shipped a GA release (e.g., still at 1.0.0-beta.3) don't have ApiCompatVersion because there's no prior GA version to compare against. The ApiCompatRequired property distinguishes "never been GA" (no property) from "was GA but someone removed ApiCompatVersion" (property is true but version is empty).
Related
- Changing the APICompat baseline for a project should be visible to the SDK team #55311 — Centralize ApiCompat baseline management (adds central baselines and blocks
RunApiCompatForSrc=falsebypass) - PR Centralize ApiCompatBaseline files to require review #57600 — Implementation of Changing the APICompat baseline for a project should be visible to the SDK team #55311
The following is the AI proposed solutions, which I don't really like, so this is still a work in progress
AI proposed solutions
Add an ApiCompatRequired property that is automatically set by Update-PkgVersion.ps1 when it first adds ApiCompatVersion to a GA package:
- In
Update-PkgVersion.ps1: When addingApiCompatVersion, also add<ApiCompatRequired>true</ApiCompatRequired>to the.csproj - In
eng/Directory.Build.Common.targets: Add a new validation target:<Target Name="ValidateApiCompatVersionRequired" BeforeTargets="Build" Condition="'$(ValidateRunApiCompat)' == 'true' and '$(ApiCompatRequired)' == 'true' and '$(ApiCompatVersion)' == '' and '$(IncludeBuildOutput)' != 'false'"> <Error Text="Package $(PackageId) requires ApiCompatVersion to be set. Do not remove ApiCompatVersion from GA packages." /> </Target>
- Backfill: Add
<ApiCompatRequired>true</ApiCompatRequired>to all ~292 existing.csprojfiles that currently haveApiCompatVersion
Why not a list file?
A central list (e.g., eng/ApiCompatRequired.txt) was considered but rejected because it requires ongoing maintenance. The property-based approach is fully automatic — Update-PkgVersion.ps1 manages both properties, and no separate list needs to be kept in sync.