dotnet watch crashes
Describe the bug
I use 'dotnet watch' for .NET 9 Blazor project in Server Side Rendering mode. Very often when I change html on razor pages or components, it crashes with 'Index was outside the bounds of the array' exception. It happens not often but mostly when I do significant changes in my markup, move or add blocks of the code.
To Reproduce
There is no stable steps to reproduce. The attached below project is a very cut version of my project where I usually get the errors.
Steps:
- Go to
DocumentReview.razorfile. - Select any duplicatable markup block, for example <TemplatedClumn> from line 161 to line 209, or from line 258 to line 282
- Start to duplicate the code block and press ctrl+s
- Wait for a short amount of time, usually from 1 to 2 seconds
- Repeat duplication and saving
- Ctrl+Z + Ctrl+S
- Repeat several times
- Usually one of the three error below happens
Exceptions (if any)
Exception 1:
dotnet watch ❌ [Gea.Idp (net9.0)] Exited with error code -1
dotnet watch ❌ System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.EmitParametersFromOriginalMetadata(IMethodDefinition methodDef, MethodDefinitionHandle handle)
at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.CreateIndicesForMethod(IMethodDefinition methodDef, SymbolChange methodChange)
at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.CreateIndicesForNonTypeMembers(ITypeDefinition typeDef)
at Microsoft.Cci.MetadataWriter.<CreateIndicesForModule>g__createIndices|102_0(ITypeDefinition typeDef, Queue`1 typesToIndex)
at Microsoft.Cci.MetadataWriter.CreateIndicesForModule()
at Microsoft.Cci.MetadataWriter.CreateIndices()
at Microsoft.Cci.MetadataWriter.BuildMetadataAndIL(PdbWriter nativePdbWriterOpt, BlobBuilder ilBuilder, PooledBlobBuilder& mappedFieldDataBuilder, PooledBlobBuilder& managedResourceDataBuilder, Blob& mvidFixup, Blob& mvidStringFixup)
at Microsoft.Cci.MetadataWriter.WriteMetadataAndIL(PdbWriter nativePdbWriterOpt, Stream metadataStream, Stream ilStream, Stream portablePdbStreamOpt, MetadataSizes& metadataSizes)
at Microsoft.CodeAnalysis.Compilation.SerializeToDeltaStreams(CommonPEModuleBuilder moduleBeingBuilt, DefinitionMap definitionMap, SymbolChanges changes, Stream metadataStream, Stream ilStream, Stream pdbStream, ArrayBuilder`1 updatedMethods, ArrayBuilder`1 changedTypes, DiagnosticBag diagnostics, Func`2 testSymWriterFactory, String pdbFilePath, 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, CompilationTestData testData, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Compilation.EmitDifference(EmitBaseline baseline, IEnumerable`1 edits, Func`2 isAddedSymbol, Stream metadataStream, Stream ilStream, Stream pdbStream, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.EditAndContinue.EditSession.EmitSolutionUpdateAsync(Solution solution, ActiveStatementSpanProvider solutionActiveStatementSpanProvider, UpdateId updateId, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.EditAndContinue.DebuggingSession.EmitSolutionUpdateAsync(Solution solution, IImmutableSet`1 runningProjects, ActiveStatementSpanProvider activeStatementSpanProvider, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.ExternalAccess.Watch.Api.WatchHotReloadService.GetUpdatesAsync(Solution solution, IImmutableSet`1 runningProjects, CancellationToken cancellationToken)
at Microsoft.DotNet.Watch.CompilationHandler.HandleManagedCodeChangesAsync(Func`3 restartPrompt, CancellationToken cancellationToken)
at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
at Microsoft.DotNet.Watch.Program.RunAsync()
dotnet watch ❌ An unexpected error occurred
Exception2:
System.ArgumentException: An item with the same key has already been added. Key: 4170
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.GetDelta(Compilation compilation, Guid encId, MetadataSizes metadataSizes)
at Microsoft.CodeAnalysis.Compilation.SerializeToDeltaStreams(CommonPEModuleBuilder moduleBeingBuilt, DefinitionMap definitionMap, SymbolChanges changes, Stream metadataStream, Stream ilStream, Stream pdbStream, ArrayBuilder`1 updatedMethods, ArrayBuilder`1 changedTypes, DiagnosticBag diagnostics, Func`2 testSymWriterFactory, String pdbFilePath, 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, CompilationTestData testData, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Compilation.EmitDifference(EmitBaseline baseline, IEnumerable`1 edits, Func`2 isAddedSymbol, Stream metadataStream, Stream ilStream, Stream pdbStream, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.EditAndContinue.EditSession.EmitSolutionUpdateAsync(Solution solution, ActiveStatementSpanProvider solutionActiveStatementSpanProvider, UpdateId updateId, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.EditAndContinue.DebuggingSession.EmitSolutionUpdateAsync(Solution solution, IImmutableSet`1 runningProjects, ActiveStatementSpanProvider activeStatementSpanProvider, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.ExternalAccess.Watch.Api.WatchHotReloadService.GetUpdatesAsync(Solution solution, IImmutableSet`1 runningProjects, CancellationToken cancellationToken)
at Microsoft.DotNet.Watch.CompilationHandler.HandleManagedCodeChangesAsync(Func`3 restartPrompt, CancellationToken cancellationToken)
at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
at Microsoft.DotNet.Watch.Program.RunAsync()
Further technical details
.NET SDK: Version: 9.0.301 Commit: a596cd22e2 Workload version: 9.0.300-manifests.07067f5b MSBuild version: 17.14.5+edd3bbf37
Runtime Environment: OS Name: Windows OS Version: 10.0.26100 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\9.0.301\
.NET workloads installed: [aspire] Installation Source: VS 17.14.36203.30 Manifest Version: 8.2.2/8.0.100 Manifest Path: C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.aspire\8.2.2\WorkloadManifest.json Install Type: Msi
Configured to use loose manifests when installing new manifests.
Host: Version: 10.0.0-preview.5.25277.114 Architecture: x64 Commit: ddf39a1b46
.NET SDKs installed: 6.0.428 [C:\Program Files\dotnet\sdk] 8.0.411 [C:\Program Files\dotnet\sdk] 9.0.301 [C:\Program Files\dotnet\sdk] 10.0.100-preview.5.25277.114 [C:\Program Files\dotnet\sdk]
.NET runtimes installed: Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 10.0.0-preview.5.25277.114 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 10.0.0-preview.5.25277.114 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 9.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 10.0.0-preview.5.25277.114 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Other architectures found: x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]
Environment variables: Not set
global.json file: P:\global.json
Thanks for report. Could you please provide repro steps?
Alternatively, please set Microsoft_CodeAnalysis_EditAndContinue_LogDir env variable to a directory path, run dotnet-watch --verbose and share the content of the directory and the console output.
One more exception
dotnet watch ❌ [BlazorApp1 (net9.0)] Exited with error code -1
dotnet watch ❌ System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.CodeAnalysis.Emit.DefinitionMap.GetPreviousMethodHandle(IMethodSymbolInternal oldMethod, IMethodSymbolInternal& peMethod)
at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.<CreateDeletedMethodsDefs>g__getDeletedMethodDefs|83_1(ITypeDefinition typeDef, <>c__DisplayClass83_0&)
at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.<CreateDeletedMethodsDefs>g__recurse|83_0(ITypeDefinition typeDef, <>c__DisplayClass83_0&)
at Microsoft.CodeAnalysis.Emit.DeltaMetadataWriter.CreateDeletedMethodsDefs(EmitContext context, SymbolChanges changes)
at Microsoft.CodeAnalysis.Compilation.SerializeToDeltaStreams(CommonPEModuleBuilder moduleBeingBuilt, DefinitionMap definitionMap, SymbolChanges changes, Stream metadataStream, Stream ilStream, Stream pdbStream, ArrayBuilder`1 updatedMethods, ArrayBuilder`1 changedTypes, DiagnosticBag diagnostics, Func`2 testSymWriterFactory, String pdbFilePath, 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, CompilationTestData testData, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Compilation.EmitDifference(EmitBaseline baseline, IEnumerable`1 edits, Func`2 isAddedSymbol, Stream metadataStream, Stream ilStream, Stream pdbStream, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.EditAndContinue.EditSession.EmitSolutionUpdateAsync(Solution solution, ActiveStatementSpanProvider solutionActiveStatementSpanProvider, UpdateId updateId, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.EditAndContinue.DebuggingSession.EmitSolutionUpdateAsync(Solution solution, IImmutableSet`1 runningProjects, ActiveStatementSpanProvider activeStatementSpanProvider, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.ExternalAccess.Watch.Api.WatchHotReloadService.GetUpdatesAsync(Solution solution, IImmutableSet`1 runningProjects, CancellationToken cancellationToken)
at Microsoft.DotNet.Watch.CompilationHandler.HandleManagedCodeChangesAsync(Func`3 restartPrompt, CancellationToken cancellationToken)
at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
at Microsoft.DotNet.Watch.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
at Microsoft.DotNet.Watch.Program.RunAsync()
dotnet watch ❌ An unexpected error occurred
@tmat I updated the initial description with steps I was able to use to reproduce the Object reference not set to an instance of an object and An item with the same key has already been added. Key: XXXX exceptions. Please pay attention that the way of reproduction is completely unstable and it might work.
@AndreiShenets Thanks for the detailed steps. I was able to get it working and try duplicating the code segments as you described.
Have you tried on the latest Preview of .NET 10? It seems to be working
@tmat if you mean switching the whole project to .net 10 then no I haven't. It is not so easy to move the whole project to .net 10 yet.
But I allowed preview versions of .net tool chain with global.json file. dotnet watch --verbose in this case says that it uses .net 10 preview for running the command if I understand the log correctly.
I was able to get An item with the same key has already been added. Key: XXXX exception.