Skip to content

dotnet new logs ArgumentException when old SDKs are installed because of old NUnit templates #54160

@svick

Description

@svick

Describe the bug

A clear and concise description of what the bug is.

When I ran dotnet new console, I got the following as part of the output:

Error: Failed to load template from "C:\Program Files\dotnet\templates\7.0.20\microsoft.dotnet.test.projecttemplates.7.0.1.1.0-rc.23559.1.nupkg(
/content/NUnit-CSharp/.template.config/template.json)".                                                                                         
Details: System.ArgumentException: An item with the same key has already been added. Key: defaultName (Parameter 'key')                         
   at System.Collections.ThrowHelper.ThrowDuplicateKey[TKey](TKey key)                                                                          
   at System.Collections.Generic.OrderedDictionary`2.TryInsert(Int32 index, TKey key, TValue value, InsertionBehavior behavior, Int32& keyIndex)
   at System.Collections.Generic.OrderedDictionary`2.Add(TKey key, TValue value)                                                                
   at System.Text.Json.Nodes.JsonObject.InitializeDictionary()                                                                                  
   at System.Text.Json.Nodes.JsonObject.TryGetPropertyValue(String propertyName, JsonNode& jsonNode, Int32& index)                              
   at System.Text.Json.Nodes.JsonObject.TryGetPropertyValue(String propertyName, JsonNode& jsonNode)                                            
   at Microsoft.TemplateEngine.JExtensions.GetPropertyCaseInsensitive(JsonObject obj, String key)                                               
   at Microsoft.TemplateEngine.JExtensions.Get[T](JsonNode token, String key)                                                                   
   at Microsoft.TemplateEngine.JExtensions.ArrayAsStrings(JsonNode token, String propertyName)                                                  
   at Microsoft.TemplateEngine.Orchestrator.RunnableProjects.DirectoryBasedTemplate.MergeAdditionalConfiguration(JsonObject primarySource,      
IFileSystemInfo primarySourceConfig)                                                                                                            
   at Microsoft.TemplateEngine.Orchestrator.RunnableProjects.DirectoryBasedTemplate..ctor(IEngineEnvironmentSettings settings, IGenerator       
generator, IFile templateFile, String baselineName)                                                                                             
   at Microsoft.TemplateEngine.Orchestrator.RunnableProjects.RunnableProjectGenerator.GetTemplatesFromMountPointInternalAsync(IMountPoint       
source, CancellationToken cancellationToken)
The template "Console App" was created successfully.      

This is caused by an issue with the template that has been fixed a long time ago (dotnet/test-templates#105), but it still happens on .Net 11 if you have the .Net 7 SDK installed.

I believe this would have been fixed by dotnet/templating#10048, but that was never merged.

To Reproduce

  1. Have .Net 7 and .Net 11 SDKs installed.
  2. On .Net 11 SDK, run dotnet new console.

cc: @marcpopMSFT

Metadata

Metadata

Labels

untriagedRequest triage from a team member

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions