Annotate all DllImport/LibraryImport/UnmanagedCallersOnly with [SupportedOSPlatform]
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Description
## Summary
Annotate platform-specific `DllImport` / `LibraryImport` / `UnmanagedCallersOnly` declarations with `[SupportedOSPlatform]` / `[UnsupportedOSPlatform]`.
## Motivation
The wasm CoreCLR `ManagedToNativeGenerator` (`src/tasks/WasmAppBuilder/coreclr/PInvokeCollector.cs`) already **skips** P/Invokes and callbacks that are unsupported on the target OS, evaluating `SupportedOSPlatform` / `UnsupportedOSPlatform` at method → declaring-type → assembly scope against the target (`browser` / `wasi`).
However, most low-level `Interop.*` declarations are **not** annotated — e.g. `Interop.Kernel32.GetCurrentProcess` lives in the cross-platform `System.Private.CoreLib` with no platform attribute. As a result they flow into the scan and produce `WASM0066` noise on browser/wasi (`kernel32`, `ntdll`, `advapi32`, …), and more generally they are invisible to platform-compatibility tooling.
Annotating these declarations would let the existing wasm platform-skip eliminate the noise **at the source**, and would improve CA1416 platform-compatibility analysis across the board.
## The hard part — callers
> Annotating the interop leaves is **not** enough; the callers must be solved too.
Adding `[SupportedOSPlatform("windows")]` (etc.) to the low-level interop will surface CA1416 on every caller. Many callers are already guarded (`if (OperatingSystem.IsWindows())`) and will be fine, but some are not, and cross-cutting annotation requires walking and annotating/guarding those call chains. This is the bulk of the work and must be scoped in — it cannot be done by annotating leaves alone.
## Scope
- `src/libraries/Common/src/Interop/**` and the per-assembly interop partials.
- Coordinate with the platform-compatibility analyzer (CA1416) expectations.
- Enables (and is complemented by) the wasm relink-gating work in the companion issue.
> [!NOTE]
> This issue was drafted with GitHub Copilot.
Contributor guide
Assessment
This issue has not been assessed yet.