Skip to content

[release/11.0.2xx] Fix dotnet watch crash after switching target frameworks - #57088

Draft
jonathanpeppers wants to merge 2 commits into
release/11.0.2xxfrom
jonathanpeppers-android-hot-reload-crash
Draft

jonathanpeppers wants to merge 2 commits into
release/11.0.2xxfrom
jonathanpeppers-android-hot-reload-crash

Conversation

@jonathanpeppers

@jonathanpeppers jonathanpeppers commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

A MAUI project can target both Windows and Android. Running it on Windows, then Android, then starting dotnet watch for Android can make the first C# edit crash with a NullReferenceException in Roslyn's DefinitionMap.GetPreviousMethodHandle.

Watch prepares compiler inputs for the selected target, but also loaded the other targets into Roslyn. If the earlier Windows build still matched the source, Roslyn tried to update Windows without complete compiler inputs, even though only Android was running.

This change passes the project instances from completed design-time builds to Roslyn, instead of every target in the project graph. It does not depend on C# compiler items, so VB and F# projects are kept too. Referenced projects targeting other frameworks are still included.

The regression test reproduces the crash without MAUI or a device: build another target first, start watch for the selected target, then edit a method. The control case skips the earlier build. All 18 focused tests pass, including source edits in VB and F#, dependencies, and project changes.

Related to dotnet/roslyn#79898. This fixes the reproduced cross-target case; I have not confirmed whether it also fixes the empty-update case in that issue.

Full stack trace from the MAUI reproduction
dotnet watch ❌ An unexpected error occurred: System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.CodeAnalysis.Emit.DefinitionMap.GetPreviousMethodHandle(IMethodSymbolInternal oldProperty, IMethodSymbolInternal& peMethod)
   at Microsoft.CodeAnalysis.Emit.DefinitionMap.GetDeletedSynthesizedMethods(IMethodSymbolInternal oldMethod, ImmutableArray`1 currentLambdas)
   at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.<CreateDeletedMemberDefs>g__addDeletedClosureMethods|87_2(IMethodSymbolInternal oldMethod, ImmutableArray`1 currentLambdas, ImmutableArray`1 orderedLambdaRuntimeRudeEdits, <>c__DisplayClass87_0&, <>c__DisplayClass87_1&)
   at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.<CreateDeletedMemberDefs>g__getDeletedMemberDefs|87_1(ITypeDefinition typeDef, <>c__DisplayClass87_0&)
   at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.<CreateDeletedMemberDefs>g__recurse|87_0(ITypeDefinition typeDef, <>c__DisplayClass87_0&)
   at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.CreateDeletedMemberDefs(EmitContext context, SymbolChanges changes)
   at Microsoft.CodeAnalysis.Emit.CommonPEModuleBuilder.CreateDeletedMemberDefinitions(DiagnosticBag diagnosticBag)
   at Microsoft.CodeAnalysis.CSharp.MethodCompiler.CompileMethodBodies(CSharpCompilation compilation, PEModuleBuilder moduleBeingBuiltOpt, Boolean emittingPdb, Boolean hasDeclarationErrors, Boolean emitMethodBodies, BindingDiagnosticBag diagnostics, Predicate`1 filterOpt, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.CSharpCompilation.CompileMethods(CommonPEModuleBuilder moduleBuilder, Boolean emittingPdb, DiagnosticBag diagnostics, Predicate`1 filterOpt, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Compilation.Compile(CommonPEModuleBuilder moduleBuilder, Boolean emittingPdb, DiagnosticBag diagnostics, Predicate`1 filterOpt, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.Emit.EmitHelpers.EmitDifference(CSharpCompilation compilation, EmitBaseline baseline, IEnumerable`1 edits, Func`2 isAddedSymbol, Stream metadataStream, Stream ilStream, Stream pdbStream, EmitDifferenceOptions options, CompilationTestData testData, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Compilation.EmitDifference(EmitBaseline baseline, IEnumerable`1 edits, Func`2 isAddedSymbol, Stream metadataStream, Stream ilStream, Stream pdbStream, EmitDifferenceOptions options, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.EditAndContinue.EditSession.EmitSolutionUpdateAsync(Solution solution, ActiveStatementSpanProvider solutionActiveStatementSpanProvider, UpdateId updateId, ImmutableDictionary`2 runningProjects, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.EditAndContinue.DebuggingSession.EmitSolutionUpdateAsync(Solution solution, ImmutableDictionary`2 runningProjects, ActiveStatementSpanProvider activeStatementSpanProvider, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.ExternalAccess.HotReload.Api.HotReloadService.GetUpdatesAsync(Solution solution, ImmutableDictionary`2 runningProjects, CancellationToken cancellationToken)
   at Microsoft.DotNet.Watch.ProjectUpdatesBuilder.AddManagedCodeUpdatesAsync(Func`3 restartPrompt, Boolean autoRestart, CancellationToken cancellationToken)
   at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watch.Program.RunAsync()
   at Microsoft.DotNet.Watch.Program.RunAsync()

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers
jonathanpeppers requested review from a team and tmat as code owners November 12, 2026 15:30
Copilot AI balanced review requested due to automatic review settings November 12, 2026 15:30
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The filter incorrectly excludes F# and VB projects without CscCommandLineArgs, removing workspace documents needed to trigger rebuilds or restarts and potentially leaving apps running stale code after source edits.

1 open issue
What changed in this PR

This PR addresses a dotnet watch Hot Reload crash after switching target frameworks by filtering the project instances loaded into Roslyn based on available compiler inputs.

Changes:

  • Excludes project instances without C# compiler arguments from the workspace.
  • Adds regression coverage for edits with and without a previously built inactive target.
  • Updates reference-project coverage to include a different target framework and design-time evaluation.
File Description
test/​dotnet-watch.Tests/​HotReload/​CompilationHandlerTests.cs Adds cross-target regression cases and updates reference-project setup.
src/​Dotnet.Watch/​Watch/​HotReload/​ManagedCodeWorkspace.cs Filters workspace project instances by compiler-argument availability.

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers

Copy link
Copy Markdown
Member Author

This might be fixed by:

I think we should let the fix flow to VMR and see if the test added here passes.

@jonathanpeppers
jonathanpeppers marked this pull request as draft November 13, 2026 15:58

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants