Skip to content

[dotnet-watch] avoid PlatformNotSupportedException on various platforms#52351

Merged
jonathanpeppers merged 1 commit intomainfrom
dev/peppers/dotnet-watch-android-pnse
Jan 7, 2026
Merged

[dotnet-watch] avoid PlatformNotSupportedException on various platforms#52351
jonathanpeppers merged 1 commit intomainfrom
dev/peppers/dotnet-watch-android-pnse

Conversation

@jonathanpeppers
Copy link
Member

In Microsoft.Extensions.DotNetDeltaApplier.dll, which is used by dotnet-watch.

Running StartupHook.Initialize() on Android results in:

01-07 14:12:27.328  7304  7304 I DOTNET  : System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
01-07 14:12:27.328  7304  7304 I DOTNET  :  ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
01-07 14:12:27.328  7304  7304 I DOTNET  :    at System.ConsolePal.set_ForegroundColor(ConsoleColor value)
01-07 14:12:27.328  7304  7304 I DOTNET  :    at System.Console.set_ForegroundColor(ConsoleColor value)
01-07 14:12:27.328  7304  7304 I DOTNET  :    at StartupHook.Log(String message)
01-07 14:12:27.328  7304  7304 I DOTNET  :    at StartupHook.Initialize()

If you review the System.Console APIs, you can see they are decorated with:

[UnsupportedOSPlatform("android")]
[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]

Checking for these platforms makes the exception go away, and we get a bit further now:

01-07 14:39:37.433  8293  8293 I DOTNET  : [HotReload] Environment variable DOTNET_WATCH_HOTRELOAD_NAMEDPIPE_NAME has no value

To be investigated next...

…orms

In `Microsoft.Extensions.DotNetDeltaApplier.dll`, which is used by
`dotnet-watch`.

Running `StartupHook.Initialize()` on Android results in:

    01-07 14:12:27.328  7304  7304 I DOTNET  : System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
    01-07 14:12:27.328  7304  7304 I DOTNET  :  ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
    01-07 14:12:27.328  7304  7304 I DOTNET  :    at System.ConsolePal.set_ForegroundColor(ConsoleColor value)
    01-07 14:12:27.328  7304  7304 I DOTNET  :    at System.Console.set_ForegroundColor(ConsoleColor value)
    01-07 14:12:27.328  7304  7304 I DOTNET  :    at StartupHook.Log(String message)
    01-07 14:12:27.328  7304  7304 I DOTNET  :    at StartupHook.Initialize()

If you review the System.Console APIs, you can see they are decorated with:

    [UnsupportedOSPlatform("android")]
    [UnsupportedOSPlatform("browser")]
    [UnsupportedOSPlatform("ios")]
    [UnsupportedOSPlatform("tvos")]

* https://github.com/dotnet/runtime/blob/242f7b23752599f22157268de41fee91cb97ef6c/src/libraries/System.Console/src/System/Console.cs#L334-L351

Checking for these platforms makes the exception go away, and we get a
bit further now:

    01-07 14:39:37.433  8293  8293 I DOTNET  : [HotReload] Environment variable DOTNET_WATCH_HOTRELOAD_NAMEDPIPE_NAME has no value

To be investigated next...
Copy link
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 fixes a PlatformNotSupportedException that occurs when using dotnet-watch on Android, iOS, tvOS, and browser platforms. The exception was thrown because Console.ForegroundColor and Console.ResetColor are not supported on these platforms. The fix adds platform checks to conditionally execute console color operations only on supported platforms.

Key Changes

  • Added a static readonly field s_supportsConsoleColor that checks if the current platform supports console color operations
  • Wrapped calls to Console.ForegroundColor and Console.ResetColor in conditional blocks that check s_supportsConsoleColor

@jonathanpeppers jonathanpeppers enabled auto-merge (squash) January 7, 2026 22:04
@jonathanpeppers jonathanpeppers merged commit 5c9f0f9 into main Jan 7, 2026
32 of 33 checks passed
@jonathanpeppers jonathanpeppers deleted the dev/peppers/dotnet-watch-android-pnse branch January 7, 2026 22:05
@jonathanpeppers
Copy link
Member Author

/backport to release/10.0.2xx

@github-actions
Copy link
Contributor

github-actions bot commented Jan 7, 2026

Started backporting to release/10.0.2xx (link to workflow run)

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.

4 participants