Skip to content

Add [UnsupportedOSPlatform("wasi")] to HangDump / Console / Process surface#8564

Merged
Evangelink merged 1 commit into
mainfrom
dev/amauryleve/wasi-unsupported-annotations
May 25, 2026
Merged

Add [UnsupportedOSPlatform("wasi")] to HangDump / Console / Process surface#8564
Evangelink merged 1 commit into
mainfrom
dev/amauryleve/wasi-unsupported-annotations

Conversation

@Evangelink
Copy link
Copy Markdown
Member

Closes #8557. Related: #2196, #5366.

MTP projects already advertise wasi support via <SupportedPlatform Include="wasi" />, so CA1416 now requires call sites and exposed members to be annotated for wasi the same way they are for browser/ios/tvos. This PR closes the annotation gap on the parts of MTP that structurally cannot work on wasi-wasm today (single-instance wasm, no PID/process model, no PosixSignalRegistration, no tty buffer/window APIs).

Why these surfaces?

  • Console.CancelKeyPress is implemented via PosixSignalRegistration which is unavailable on wasi (this is the original #5366 crash).
  • Buffer/window/foreground-color console APIs require a tty which wasi-wasm doesn't expose.
  • System.Diagnostics.Process has no analog on wasi: one wasm instance per program, no PID, no /proc, no Process.Start.
  • HangDump is structurally inapplicable on wasi: no watchdog process, no MiniDumpWriteDump, no threading.

Changes

  • Console abstraction (IConsole / SystemConsole): mirror existing browser annotations on CancelKeyPress, BufferHeight/Width, WindowHeight/Width, and SetForegroundColor/GetForegroundColor.
  • Process abstraction (SystemProcessHandler): GetCurrentProcess, GetProcessById, Start.
  • HangDump extension: AddHangDumpProvider entry point (plus matching PNSE runtime guard), TestingPlatformBuilderHook.AddExtensions, HangDumpProcessLifetimeHandler (class + TakeDumpOfTreeAsync), HangDumpActivityIndicator, and the IProcessExtensions helpers (GetProcessTreeAsync, GetParentPidWindows/Linux/MacOsAsync, IsChildCandidate).
  • TestApplication.WaitForDebuggerToAttach: add wasi to the attribute and add a sibling runtime guard at both call sites (env-var driven and command-line option driven). New localized resource WaitDebuggerAttachNotSupportedInWasiErrorMessage.
  • Terminal helpers (AnsiTerminal/SimpleTerminalBase Width/Height and NativeMethods.QueryIsScreenAndTryEnableAnsiColorCodes): include wasi in the existing browser/android/ios/tvos guards so the call sites stop CA1416-warning once the underlying APIs are flagged.

CTRLPlusCCancellationTokenSource, NonAnsiTerminal, HotReloadHandler and OutputDeviceManager already had OperatingSystem.IsWasi() guards (plus matching [SupportedOSPlatformGuard("wasi")]) and continue to compile cleanly.

Out of scope

  • IConsole.Clear() (no browser annotation today; Console.Clear() may work on wasi via ANSI escapes — separate concern).
  • Normalizing HangDumpActivityIndicator's browser-only annotation up to the full browser/ios/tvos/wasi quartet (kept minimal here).
  • IProcessHandler interface remains attribute-free (only the concrete SystemProcessHandler impl carries the annotations).

Validation

  • .\build.cmd -c Debug succeeds with no CA1416 warnings.
  • No public API change: [UnsupportedOSPlatform] is not part of the PublicAPI.txt format.
  • All 13 localized XLF files regenerated via UpdateXlf.

…urface

Fixes #8557. Related: #2196, #5366.

MTP projects already advertise wasi support via <SupportedPlatform
Include="wasi" />, so CA1416 now requires call sites and exposed members
to be annotated for wasi the same way they are for browser/ios/tvos.

This PR closes the annotation gap on the parts of MTP that structurally
cannot work on wasi-wasm today (single-instance wasm, no PID/process
model, no PosixSignalRegistration, no tty buffer/window APIs):

- Console abstraction (IConsole / SystemConsole): mirror the existing
  `browser` annotations on CancelKeyPress, BufferHeight/Width,
  WindowHeight/Width, and SetForegroundColor/GetForegroundColor.
- Process abstraction (SystemProcessHandler): GetCurrentProcess,
  GetProcessById, Start.
- HangDump extension: AddHangDumpProvider entry point (plus matching
  PNSE runtime guard), TestingPlatformBuilderHook.AddExtensions,
  HangDumpProcessLifetimeHandler (class + TakeDumpOfTreeAsync),
  HangDumpActivityIndicator, and the IProcessExtensions helpers.
- TestApplication.WaitForDebuggerToAttach: add `wasi` to the
  attribute and add a sibling runtime guard at both call sites
  (env-var driven and command-line option driven). New localized
  resource string WaitDebuggerAttachNotSupportedInWasiErrorMessage.
- Terminal helpers (AnsiTerminal/SimpleTerminalBase Width/Height and
  NativeMethods.QueryIsScreenAndTryEnableAnsiColorCodes): include
  wasi in the existing `browser/android/ios/tvos` guards so the
  call sites stop CA1416-warning once the underlying APIs are
  flagged.

CTRLPlusCCancellationTokenSource, NonAnsiTerminal, HotReloadHandler
and OutputDeviceManager already had `OperatingSystem.IsWasi()`
guards (plus matching `[SupportedOSPlatformGuard("wasi")]`), so
they continue to compile cleanly.

No public API change: `[UnsupportedOSPlatform]` is not part of the
PublicAPI.txt format.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 25, 2026 13:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates Microsoft.Testing.Platform (MTP) and the HangDump extension to explicitly mark APIs that are structurally unavailable on wasi-wasm using [UnsupportedOSPlatform("wasi")], and adds corresponding runtime guards where those APIs are conditionally invoked. This prevents future CA1416 platform analyzer warnings from leaking to consumers and avoids runtime failures when running on WASI.

Changes:

  • Add [UnsupportedOSPlatform("wasi")] to Console/Process abstractions and HangDump public entry points and helpers.
  • Add WASI runtime guards for “wait for debugger attach” and HangDump extension activation, including a new localized error message for debugger attach on WASI.
  • Extend terminal sizing / ANSI probing guards to avoid unsupported console APIs on WASI.
Show a summary per file
File Description
src/Platform/Microsoft.Testing.Platform/Builder/TestApplication.cs Adds WASI guard + annotation for debugger-attach wait path and uses new localized resource string.
src/Platform/Microsoft.Testing.Platform/Helpers/System/IConsole.cs Extends console abstraction members with [UnsupportedOSPlatform("wasi")] to propagate platform limitations.
src/Platform/Microsoft.Testing.Platform/Helpers/System/SystemConsole.cs Mirrors WASI unsupported annotations on concrete console implementation members.
src/Platform/Microsoft.Testing.Platform/Helpers/System/SystemProcessHandler.cs Marks Process APIs as unsupported on WASI to match platform reality and avoid CA1416 at call sites.
src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/AnsiTerminal.cs Treats WASI like other non-terminal platforms for width/height to avoid unsupported console window APIs.
src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/SimpleTerminalBase.cs Treats WASI like other non-terminal platforms for width/height to avoid unsupported console window APIs.
src/Platform/Microsoft.Testing.Platform/OutputDevice/Terminal/NativeMethods.cs Avoids buffer-size probing on WASI by extending the existing platform guard condition.
src/Platform/Microsoft.Testing.Platform/Resources/PlatformResources.resx Adds localized resource key for “wait debugger attach not supported on wasi”.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.cs.xlf Regenerates localization file to include the new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.de.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.es.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.fr.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.it.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ja.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ko.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pl.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.pt-BR.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.ru.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.tr.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hans.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Platform/Resources/xlf/PlatformResources.zh-Hant.xlf Localization regen for new WASI debugger-attach resource string.
src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpExtensions.cs Marks HangDump entry point as unsupported on WASI and adds a runtime guard for WASI usage.
src/Platform/Microsoft.Testing.Extensions.HangDump/TestingPlatformBuilderHook.cs Marks MSBuild hook entry point as unsupported on WASI.
src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpProcessLifetimeHandler.cs Marks HangDump process lifetime handler and dump-taking path as unsupported on WASI.
src/Platform/Microsoft.Testing.Extensions.HangDump/HangDumpActivityIndicator.cs Marks activity indicator as unsupported on WASI alongside existing browser restriction.
src/Platform/Microsoft.Testing.Extensions.HangDump/Helpers/IProcessExtensions.cs Marks process-tree helpers as unsupported on WASI to match Process model limitations.

Copilot's findings

  • Files reviewed: 26/26 changed files
  • Comments generated: 0

@Evangelink
Copy link
Copy Markdown
Member Author

Localization & Resources — ISSUE

Problem: Hardcoded user-facing strings in HangDumpExtensions.cs and MSBuildExtensions.cs violate localization policy.

Concrete Failing Scenarios

1. HangDumpExtensions.cs (lines 33, 38, 43)

Current code:

throw new PlatformNotSupportedException("Hang dump extension is not available on browser");
throw new PlatformNotSupportedException("Hang dump extension is not available on ios nor tvos");
throw new PlatformNotSupportedException("Hang dump extension is not available on wasi");

Issue: These are hardcoded English strings, but:

  • The HangDump extension has a resource file at src/Platform/Microsoft.Testing.Extensions.HangDump/Resources/ExtensionResources.resx
  • Similar error messages in the same PR (e.g., PlatformResources.WaitDebuggerAttachNotSupportedInBrowserErrorMessage, PlatformResources.WaitDebuggerAttachNotSupportedInWasiErrorMessage) correctly use resource strings
  • The Retry extension follows the correct pattern: ExtensionResources.RetryExtensionNotSupportedOnBrowserErrorMessage

Expected: These three strings should be added to ExtensionResources.resx and referenced via the resource accessor.

2. MSBuildExtensions.cs (line 29)

Current code:

throw new PlatformNotSupportedException("MSBuild extension is not supported in browser environments.");

Issue: Hardcoded English string, but:

  • The MSBuild extension has a resource file at src/Platform/Microsoft.Testing.Extensions.MSBuild/Resources/ExtensionResources.resx
  • Should follow the same pattern as other extensions

Expected: Add to ExtensionResources.resx and reference via the resource accessor.

Evidence of Pattern Consistency

TestApplication.cs (this PR): Uses PlatformResources.WaitDebuggerAttachNotSupportedInWasiErrorMessage
RetryExtensions.cs: Uses ExtensionResources.RetryExtensionNotSupportedOnBrowserErrorMessage
OutputDeviceManager.cs: Uses PlatformResources.BrowserPlatformNotSupportedOnOlderFrameworks
HangDumpExtensions.cs: Hardcoded strings (3 instances)
MSBuildExtensions.cs: Hardcoded string (1 instance)

Why This Matters

These PlatformNotSupportedException messages are user-facing: they appear when developers call AddHangDumpProvider() or AddMSBuildExtensions() on an unsupported platform. They will be displayed in test output, CI logs, and error messages, making them candidates for localization like all other user-facing exception messages in the platform.


Note: The new WaitDebuggerAttachNotSupportedInWasiErrorMessage resource string is correctly implemented with proper XLF generation — this issue is pre-existing and out of scope for PR #8564, but should be tracked for consistency.

Generated by Expert Code Review (on open) for issue #8564 · ● 3.8M ·

@Evangelink Evangelink merged commit 8635764 into main May 25, 2026
26 of 28 checks passed
@Evangelink Evangelink deleted the dev/amauryleve/wasi-unsupported-annotations branch May 25, 2026 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add [UnsupportedOSPlatform("wasi")] to HangDump / Console / Process surface

2 participants