Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dotnet watch falls over frequently with "An item with the same key has already been added" #45455

Closed
1 task done
paulguz-datapa opened this issue Nov 18, 2024 · 12 comments
Closed
1 task done
Assignees
Labels
Area-Watch needs-info untriaged Request triage from a team member
Milestone

Comments

@paulguz-datapa
Copy link

paulguz-datapa commented Nov 18, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Since upgrading our ASP.Net Core projects to .Net 9 (but leaving the others alone for now), we are experiencing the following error when running dotnet watch:

dotnet watch ❌ System.ArgumentException: An item with the same key has already been added. Key: 
C:\xxx\yyy\zzz\aaa\Language.csproj
   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 System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Microsoft.DotNet.Watcher.Tools.IncrementalMSBuildWorkspace.UpdateProjectConeAsync(String rootProjectPath, CancellationToken cancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.Program.RunAsync()
dotnet watch ❌ An unexpected error occurred

Language.csproj is a an assembly containing resource files.

Dotnet watch falls over at this point. It happens very frequently, making Hot Reload impossible.

Expected Behavior

Dotnet watch runs without failure

Steps To Reproduce

No response

Exceptions (if any)

System.ArgumentException: An item with the same key has already been added

.NET Version

9.0

Anything else?

This did not happen in .Net 8

@paulguz-datapa
Copy link
Author

I also see this warning in the Command Prompt:

dotnet watch ⚠ msbuild: [Warning] Found project reference without a matching metadata reference: C:\xxx\yyy\zzz\aaa\Language.csproj

dotnet watch ⏳ Waiting for a file to change before restarting dotnet...

I don't know if the warning is related to the error in the subject of this Issue, or if the "waiting for file to change" problem is related to the warning (but it only appears after the warning occurs)

@jofford
Copy link

jofford commented Nov 18, 2024

Also noticing this when pressing Ctrl+R to reload in the CLI. I am running an api and Blazor site simultaneously via 'dotnet watch' but wasn't a problem in .NET 8

@paulguz-datapa
Copy link
Author

This may not be restricted to dotnet watch in the command line. I also get Hot Reload build failures when running via Visual Studio.

@paulguz-datapa
Copy link
Author

Like @jofford, we are also have a Blazor (Wasm) project and an ASP.Net Core API project (which references the Blazor project)

@eldo-xy
Copy link

eldo-xy commented Nov 19, 2024

Also having this annoying issue, doing random stuff with the projects causes the watch to just crash

@paul-green
Copy link

paul-green commented Nov 21, 2024

Me too;

  • Run dotnet watch
  • Make a change to .razor file
  • terminal says : File deleted: .(path to razor file)
  • The change isn't reflected in the UI
  • A few seconds later; Waiting for a file to change before restarting dotnet...
  • I make another change, save it and get : System.ArgumentException: An item with the same key has already been added. Key: 5529

Then dotnet watch crashes out. This is worse than previous version which was also really flakey (sigh)

@timheuer
Copy link
Member

@tmat should this move to dotnet/sdk?

@dbondar-dev
Copy link

Possible temporary solution is using previous dotnet version. It can be done by adding global.json file in your projects folder with previous working dotnet version:

{ "sdk": { "version": "8.0.0" } }

(https://learn.microsoft.com/en-us/dotnet/core/versions/selection#the-sdk-uses-the-latest-installed-version)

@timheuer timheuer transferred this issue from dotnet/aspnetcore Dec 13, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Watch untriaged Request triage from a team member labels Dec 13, 2024
@tmat
Copy link
Member

tmat commented Dec 18, 2024

Anyone has a project they can share where this issue reproduces?

@tmat tmat self-assigned this Dec 18, 2024
@limefrogyank
Copy link

limefrogyank commented Dec 18, 2024 via email

@jofford
Copy link

jofford commented Dec 18, 2024

Attaching a small sample project that has the error. It seems to be caused by a class library which targets multiple frameworks.BlazorWasmApp.zip

Steps to reproduce:

  1. dotnet watch the BlazorWasmApp.csproj
  2. Change the html in Pages\Home.razor and click save

Here's what showed up when I ran it with dotnet watch --verbose

dotnet watch ⏳ Waiting for a file to change before restarting dotnet...
dotnet watch ⌚ File change: Add 'C:\..\BlazorWasmApp\BlazorWasmApp\BlazorWasmApp.Client\Pages\lak2f2kh.may~' (written 14:39:57.4387364 after 14:39:45.1684471).
dotnet watch ⌚ Ignoring file change during build: Update 'C:\..\BlazorWasmApp\BlazorWasmApp\BlazorWasmApp.Client\Pages\lak2f2kh.may~' (created 00:00:00.0000000 and written 00:00:00.0000000 before 14:39:45.1684471).
dotnet watch ⌚ Ignoring file change during build: Add 'C:\..\BlazorWasmApp\BlazorWasmApp\BlazorWasmApp.Client\Pages\Home.razor~RF375d9c5.TMP' (created 00:00:00.0000000 and written 00:00:00.0000000 before 14:39:45.1684471).
dotnet watch ⌚ File 'C:\..\BlazorWasmApp\BlazorWasmApp\BlazorWasmApp.Client\Pages\Home.razor~RF375d9c5.TMP' deleted after 14:39:45.1684471.
dotnet watch ⌚ File 'C:\..\BlazorWasmApp\BlazorWasmApp\BlazorWasmApp.Client\Pages\Home.razor' deleted after 14:39:45.1684471.
dotnet watch ⌚ Ignoring file change during build: Add 'C:\..\BlazorWasmApp\BlazorWasmApp\BlazorWasmApp.Client\Pages\Home.razor~RF375d9c5.TMP' (created 00:00:00.0000000 and written 00:00:00.0000000 before 14:39:45.1684471).
dotnet watch ⌚ File 'C:\..\BlazorWasmApp\BlazorWasmApp\BlazorWasmApp.Client\Pages\lak2f2kh.may~' deleted after 14:39:45.1684471.
dotnet watch ⌚ File change: Add 'C:\..\BlazorWasmApp\BlazorWasmApp\BlazorWasmApp.Client\Pages\Home.razor' (written 14:39:57.4387364 after 14:39:45.1684471).
dotnet watch ⌚ File 'C:\..\BlazorWasmApp\BlazorWasmApp\BlazorWasmApp.Client\Pages\Home.razor~RF375d9c5.TMP' deleted after 14:39:45.1684471.
dotnet watch ❌ System.ArgumentException: An item with the same key has already been added. Key: C:\..\BlazorWasmApp\SharedLib\SharedLib.csproj
   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 System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)
   at Microsoft.DotNet.Watcher.Tools.IncrementalMSBuildWorkspace.UpdateProjectConeAsync(String rootProjectPath, CancellationToken cancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.HotReloadDotNetWatcher.WatchAsync(CancellationToken shutdownCancellationToken)
   at Microsoft.DotNet.Watcher.Program.RunAsync()
dotnet watch ❌ An unexpected error occurred

@tmat
Copy link
Member

tmat commented Dec 20, 2024

Fixed by #45602

@tmat tmat closed this as completed Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Watch needs-info untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

8 participants