runtime: Corrupted environment block causes GetEnvironmentVariables to throw ArgumentException: Item has already been added.

From @livarcocc on June 12, 2018 21:11

From @shaunluttin on June 11, 2018 23:30

Steps to reproduce

Install .NET Core SDK 2.1.300

Open a PowerShell session and run this:

$scope = [System.EnvironmentVariableTarget]::User;
[System.Environment]::SetEnvironmentVariable("TMP", "Foobarbaz", $scope); 
[System.Environment]::SetEnvironmentVariable("TEMP", "Foobarbaz", $scope);

Open another PowerShell session and run dotnet new.

Expected behavior

The CLI will show the standard dotnet new output.

Actual behavior

PS C:\dev> dotnet new                                                                                                                                                                                      
System.ArgumentException: Item has already been added. Key in dictionary: 'Foobarbaz'  Key being added: 'Foobarbaz'                                                                                        
   at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)                                                                                                                          
   at System.Environment.ToHashtable(IEnumerable`1 pairs)                                                                                                                                                  
   at System.Environment.GetEnvironmentVariables()                                                                                                                                                         
   at Microsoft.TemplateEngine.Utils.EngineEnvironmentSettings.DefaultEnvironment..ctor()                                                                                                                  
   at Microsoft.TemplateEngine.Utils.EngineEnvironmentSettings..ctor(ITemplateEngineHost host, Func`2 settingsLoaderFactory, String hiveLocation)                                                          
   at Microsoft.TemplateEngine.Cli.New3Command..ctor(String commandName, ITemplateEngineHost host, ITelemetryLogger telemetryLogger, Action`2 onFirstRun, INewCommandInput commandInput, String hivePath)  
   at Microsoft.TemplateEngine.Cli.New3Command.ActualRun(String commandName, ITemplateEngineHost host, ITelemetryLogger telemetryLogger, Action`2 onFirstRun, String[] args, String hivePath)              
   at Microsoft.TemplateEngine.Cli.New3Command.Run(String commandName, ITemplateEngineHost host, ITelemetryLogger telemetryLogger, Action`2 onFirstRun, String[] args, String hivePath)                    
   at Microsoft.TemplateEngine.Cli.New3Command.Run(String commandName, ITemplateEngineHost host, ITelemetryLogger telemetryLogger, Action`2 onFirstRun, String[] args)                                     
   at Microsoft.DotNet.Tools.New.NewCommandShim.Run(String[] args)                                                                                                                                         
   at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)                                                                                                                  
   at Microsoft.DotNet.Cli.Program.Main(String[] args)                                                                                                                                                     

Environment data

dotnet --info output:

.NET Core SDK (reflecting any global.json):                                                  
 Version:   2.1.300                                                                          
 Commit:    adab45bf0c                                                                       
                                                                                             
Runtime Environment:                                                                         
 OS Name:     Windows                                                                        
 OS Version:  10.0.16299                                                                     
 OS Platform: Windows                                                                        
 RID:         win10-x64                                                                      
 Base Path:   C:\Program Files\dotnet\sdk\2.1.300\                                           
                                                                                             
Host (useful for support):                                                                   
  Version: 2.1.0                                                                             
  Commit:  caa7b7e2ba                                                                        
                                                                                             
.NET Core SDKs installed:                                                                    
  2.1.4 [C:\Program Files\dotnet\sdk]                                                        
  2.1.300 [C:\Program Files\dotnet\sdk]                                                      
                                                                                             
.NET Core runtimes installed:                                                                
  Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]   
  Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]   
  Microsoft.NETCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]         
                                                                                             
To install additional .NET Core runtimes or SDKs:                                            
  https://aka.ms/dotnet-download                                                             

Other Notes

The actual situation is that we had the following two environmental variables:

TMP                            C:\Users\bigfo\AppData\Local\Temp
TEMP                           C:\Users\bigfo\AppData\Local\Temp

Our fix was to remove the TMP variable.

[System.Environment]::SetEnvironmentVariable("TMP", "", [System.EnvironmentVariableTarget]::User)

Copied from original issue: dotnet/cli#9461

Copied from original issue: dotnet/templating#1563

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 38 (23 by maintainers)

Most upvoted comments

@maryamariyan when you have cycles, could you please take this.

@Etrimus I sent you a collaborator invite, if you accept, I can assign this issue. Note this will initially subscribe you to everything happening in the repo, which you will probably want to switch off to avoid the mails.

In the reproing state, could you dump the process environment block? In Windbg, attach to the powershell proces, and do !peb. See whether you see Foobarbaz123456789 listed twice as an env var name.