Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit d833eaa

Browse files
author
William Lee
authored
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
1 parent a86e59a commit d833eaa

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

src/dotnet/ShellShim/EnvironmentPathFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public static IEnvironmentPath CreateEnvironmentPath(
3333
{
3434
environmentPath = new WindowsEnvironmentPath(
3535
cliFolderPathCalculator.ExecutablePackagesPath,
36-
Reporter.Output,
37-
environmentProvider);
36+
Reporter.Output);
3837
}
3938
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && hasSuperUserAccess)
4039
{

src/dotnet/ShellShim/WindowsEnvironmentPath.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@ namespace Microsoft.DotNet.ShellShim
1111
internal class WindowsEnvironmentPath : IEnvironmentPath
1212
{
1313
private readonly IReporter _reporter;
14-
private readonly IEnvironmentProvider _environmentProvider;
1514
private const string PathName = "PATH";
1615
private readonly string _packageExecutablePath;
1716

1817
public WindowsEnvironmentPath(
19-
string packageExecutablePath, IReporter reporter,
20-
IEnvironmentProvider environmentProvider)
18+
string packageExecutablePath, IReporter reporter)
2119
{
2220
_packageExecutablePath
2321
= packageExecutablePath ?? throw new ArgumentNullException(nameof(packageExecutablePath));
24-
_environmentProvider
25-
= environmentProvider ?? throw new ArgumentNullException(nameof(environmentProvider));
2622
_reporter
2723
= reporter ?? throw new ArgumentNullException(nameof(reporter));
2824
}
@@ -44,7 +40,9 @@ public void AddPackageExecutablePathToUserPath()
4440

4541
private bool PackageExecutablePathExists()
4642
{
47-
return _environmentProvider.GetEnvironmentVariable(PathName).Split(';').Contains(_packageExecutablePath);
43+
return Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.User).Split(';').Contains(_packageExecutablePath)
44+
|| Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.Machine).Split(';').Contains(_packageExecutablePath)
45+
|| Environment.GetEnvironmentVariable(PathName, EnvironmentVariableTarget.Process).Split(';').Contains(_packageExecutablePath);
4846
}
4947

5048
public void PrintAddPathInstructionIfPathDoesNotExist()

0 commit comments

Comments
 (0)