From d833eaa657f3a9d10675e1deda177e77c8c12963 Mon Sep 17 00:00:00 2001 From: William Lee Date: Fri, 15 Dec 2017 15:04:59 -0800 Subject: [PATCH] Fix windows keep adding tools path to env:PATH (#8248) Environment.GetEnvironmentVariable(PathName) means Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.Process) However, I have added to .User. So the detection of path existence failed. And it ends up adding the path again and again --- src/dotnet/ShellShim/EnvironmentPathFactory.cs | 3 +-- src/dotnet/ShellShim/WindowsEnvironmentPath.cs | 10 ++++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/dotnet/ShellShim/EnvironmentPathFactory.cs b/src/dotnet/ShellShim/EnvironmentPathFactory.cs index a777bd78a..55daa11f6 100644 --- a/src/dotnet/ShellShim/EnvironmentPathFactory.cs +++ b/src/dotnet/ShellShim/EnvironmentPathFactory.cs @@ -33,8 +33,7 @@ namespace Microsoft.DotNet.ShellShim { environmentPath = new WindowsEnvironmentPath( cliFolderPathCalculator.ExecutablePackagesPath, - Reporter.Output, - environmentProvider); + Reporter.Output); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && hasSuperUserAccess) { diff --git a/src/dotnet/ShellShim/WindowsEnvironmentPath.cs b/src/dotnet/ShellShim/WindowsEnvironmentPath.cs index eee4861bd..f6058079c 100644 --- a/src/dotnet/ShellShim/WindowsEnvironmentPath.cs +++ b/src/dotnet/ShellShim/WindowsEnvironmentPath.cs @@ -11,18 +11,14 @@ namespace Microsoft.DotNet.ShellShim internal class WindowsEnvironmentPath : IEnvironmentPath { private readonly IReporter _reporter; - private readonly IEnvironmentProvider _environmentProvider; private const string PathName = "PATH"; private readonly string _packageExecutablePath; public WindowsEnvironmentPath( - string packageExecutablePath, IReporter reporter, - IEnvironmentProvider environmentProvider) + string packageExecutablePath, IReporter reporter) { _packageExecutablePath = packageExecutablePath ?? throw new ArgumentNullException(nameof(packageExecutablePath)); - _environmentProvider - = environmentProvider ?? throw new ArgumentNullException(nameof(environmentProvider)); _reporter = reporter ?? throw new ArgumentNullException(nameof(reporter)); } @@ -44,7 +40,9 @@ namespace Microsoft.DotNet.ShellShim private bool PackageExecutablePathExists() { - return _environmentProvider.GetEnvironmentVariable(PathName).Split(';').Contains(_packageExecutablePath); + return Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.User).Split(';').Contains(_packageExecutablePath) + || Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.Machine).Split(';').Contains(_packageExecutablePath) + || Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.Process).Split(';').Contains(_packageExecutablePath); } public void PrintAddPathInstructionIfPathDoesNotExist()