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

Open
Copilot wants to merge 3 commits intomainfrom
copilot/fix-generate-deps-file-error
Open

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 3 commits intomainfrom
copilot/fix-generate-deps-file-error

Conversation

Copy link
Copy Markdown
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>
@marcpopMSFT marcpopMSFT requested a review from dsplaisted March 16, 2026 23:56
@marcpopMSFT marcpopMSFT marked this pull request as ready for review April 1, 2026 22:52
Copilot AI review requested due to automatic review settings April 1, 2026 22:52
@marcpopMSFT
Copy link
Copy Markdown
Member

I couldn't get a repro project even with copilot's help to repro the original issue. The original issue appears to be a duplicate name from the main project and a reference project. Looking at the code, the fix looks correct to me based on the explanation and at least one customer hit it. I think the risk is low so marking for review.

Copy link
Copy Markdown
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

Fixes a crash in GenerateDepsFile/DependencyContextBuilder.Build() where runtimeLibraries.ToDictionary(...) throws ArgumentException when the main project name collides with a dependency reference assembly name (from ReferenceDependencyPaths).

Changes:

  • Ensure the computed project runtime library name is registered in _usedLibraryNames so subsequent reference libraries are disambiguated.
  • Add a regression unit test covering the project-name vs dependency-reference collision path via WithDependencyReferences.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Tasks/Microsoft.NET.Build.Tasks/DependencyContextBuilder.cs Adds the project library name to _usedLibraryNames to avoid duplicate runtime library keys during ToDictionary.
src/Tasks/Microsoft.NET.Build.Tasks.UnitTests/GivenADependencyContextBuilder.cs Adds a regression test to validate unique runtime library names when a dependency reference matches the project name.

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.

3 participants