Skip to content

Fix GenerateDepsFile "An item with the same key has already been added" when project name matches a dependency reference assembly name#53450

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-generate-deps-file-error
Draft

Fix GenerateDepsFile "An item with the same key has already been added" when project name matches a dependency reference assembly name#53450
Copilot wants to merge 2 commits intomainfrom
copilot/fix-generate-deps-file-error

Conversation

Copy link
Contributor

Copilot AI commented Mar 13, 2026

https://developercommunity.visualstudio.com/t/Error-when-building-a-project-in-Microso/11045675

SDK 11.x enabled TrimDepsJsonLibrariesWithoutAssets=true by default, which introduced a runtimeLibraries.ToDictionary() call in DependencyContextBuilder.Build(). Any pre-existing duplicate library name in runtimeLibraries now causes an ArgumentException crash instead of silently producing a malformed deps.json.

A prior fix (f1c92d6ca) addressed collisions between the project library name and NuGet package names (in _usedLibraryNames at construction time) and _directReferences (pre-registered by GetProjectDependencies()). However, _dependencyReferences — assemblies from ReferenceDependencyPaths — are not processed by GetProjectDependencies(). If the project name matches one of these assembly names and neither appears as a NuGet package, both libraries receive the same name from GetUniqueLibraryName, crashing ToDictionary.

Changes

  • DependencyContextBuilder.GetProjectRuntimeLibrary(): After computing the unique project library name via GetUniqueLibraryName, add it to _usedLibraryNames. This ensures that when the reference loop subsequently calls GetReferenceLibraryName() for a _dependencyReference with the same base name, the name is detected as taken and a disambiguated name (e.g., MyApp.Reference) is returned instead.

  • GivenADependencyContextBuilder: Added regression test ItHandlesProjectNameMatchingDirectReferenceAssemblyName using WithDependencyReferences (not WithDirectReferences) to exercise the specific code path that was unguarded — verifying no exception is thrown and the runtime library names are unique.

Copilot AI and others added 2 commits March 13, 2026 19:13
…pendency reference assembly name

When TrimDepsJsonLibrariesWithoutAssets=true (now default in SDK 11.x),
DependencyContextBuilder.Build() calls runtimeLibraries.ToDictionary()
which crashes if two libraries share the same name.

The existing fix (f1c92d6) used GetUniqueLibraryName() for the project
library name, which handles conflicts with NuGet package names and with
_directReferences (since GetProjectDependencies() pre-registers them).

However, _dependencyReferences (from ReferenceDependencyPaths) are NOT
processed by GetProjectDependencies(). If the project name matches a
dependency reference assembly name (and neither is a NuGet package),
both get the same unique name, causing the ToDictionary crash.

Fix: After computing the unique project library name, add it to
_usedLibraryNames so that subsequent GetReferenceLibraryName() calls
for dependency references with the same name produce a disambiguated name.

Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com>
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.

2 participants