Skip to content

Add banned API analyzer to guard the reference assembly surface#2447

Merged
Sergio0694 merged 2 commits into
staging/winrt-runtime-ref-assemblyfrom
user/sergiopedri/banned-api-analyzer
Jun 17, 2026
Merged

Add banned API analyzer to guard the reference assembly surface#2447
Sergio0694 merged 2 commits into
staging/winrt-runtime-ref-assemblyfrom
user/sergiopedri/banned-api-analyzer

Conversation

@Sergio0694

@Sergio0694 Sergio0694 commented Jun 17, 2026

Copy link
Copy Markdown
Member

Summary

Adds the Microsoft.CodeAnalysis.BannedApiAnalyzers analyzer to the reference assembly build of WinRT.Runtime, to enforce that no implementation-only types can leak into the public API surface. Also marks several implementation-only types that had been missed, so they are correctly stripped from the reference assembly.

Motivation

WinRT.Runtime ships a custom reference assembly that strips out all private implementation detail types. Implementation-only types are marked with [WindowsRuntimeImplementationOnlyMember] and live in source files that opt out of the reference assembly build via #define WINDOWS_RUNTIME_IMPLEMENTATION_ONLY_FILE. Until now this relied entirely on manual discipline: if a type was not correctly marked or excluded, it would silently leak into the public reference assembly, leaking abstract members to downstream consumers and breaking reference projections.

The banned API analyzer turns this into a compile-time guarantee. The marker attribute itself is added to the banned symbols list, so any implementation-only type that survives into the reference assembly build is immediately flagged. The diagnostic (RS0030) is promoted to an error for the reference assembly build, so a leak fails the build even during local prototyping, catching the problem as early as possible.

While enabling the analyzer, it surfaced a few implementation-only types that had been missed; those are now correctly excluded.

Changes

  • src/Directory.Packages.props: add the Microsoft.CodeAnalysis.BannedApiAnalyzers package version.
  • WinRT.Runtime.csproj: reference the banned API analyzer and include BannedSymbols.txt (both only for the reference assembly build), promote RS0030 to an error in that build, and add Windows.UI.Xaml.Interop\** to the excluded implementation-only folders. The two configuration PropertyGroups are also reorganized under clearer Settings for the implementation/reference assembly headers.
  • BannedSymbols.txt: new file banning WindowsRuntime.WindowsRuntimeImplementationOnlyMemberAttribute in the reference assembly.
  • WindowsRuntimeActivationArgsReference.cs, WindowsRuntimeActivationFactoryCallback.cs, WindowsRuntimeActivationTypes.cs, TrustLevel.cs: mark these previously-missed implementation-only sources with #define WINDOWS_RUNTIME_IMPLEMENTATION_ONLY_FILE so they are excluded from the reference assembly.

@Sergio0694 Sergio0694 added validation Validation or investigation tasks CsWinRT 3.0 labels Jun 17, 2026
@Sergio0694 Sergio0694 requested a review from manodasanW June 17, 2026 06:39
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/ref-assembly-setup branch from 8d17cee to a27b766 Compare June 17, 2026 16:17
Base automatically changed from user/sergiopedri/ref-assembly-setup to staging/winrt-runtime-ref-assembly June 17, 2026 18:58
Add Microsoft.CodeAnalysis.BannedApiAnalyzers to central package versions and include it as a private analyzer when building the reference assembly. Add a BannedSymbols.txt listing the WindowsRuntimeImplementationOnlyMemberAttribute to prevent implementation-only types from being exposed in the published ref assembly. Rearrange and annotate the WinRT.Runtime.csproj property groups for implementation vs reference assembly builds, set RS0030 as an error to fail on leaked private APIs, and wire BannedSymbols.txt as an AdditionalFiles input for the analyzer.
Add #define WINDOWS_RUNTIME_IMPLEMENTATION_ONLY_FILE to several implementation-only sources (Activation/WindowsRuntimeActivationArgsReference.cs, WindowsRuntimeActivationFactoryCallback.cs, WindowsRuntimeActivationTypes.cs, Windows.Foundation/TrustLevel.cs) so they can be excluded from reference-assembly builds. Tidy WinRT.Runtime.csproj Compile Remove entries (self-closing tags / spacing) and add exclusion for Windows.UI.Xaml.Interop to ensure implementation-only folders are removed from reference builds.
@Sergio0694 Sergio0694 force-pushed the user/sergiopedri/banned-api-analyzer branch from 378d716 to 4237af9 Compare June 17, 2026 19:00
@Sergio0694 Sergio0694 marked this pull request as ready for review June 17, 2026 19:02
@Sergio0694 Sergio0694 merged commit cadd7eb into staging/winrt-runtime-ref-assembly Jun 17, 2026
11 checks passed
@Sergio0694 Sergio0694 deleted the user/sergiopedri/banned-api-analyzer branch June 17, 2026 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CsWinRT 3.0 validation Validation or investigation tasks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants