Skip to content

Commit 7d3013e

Browse files
authored
Remove manual home variable lookup (#46349)
1 parent bb9c0fd commit 7d3013e

File tree

13 files changed

+25
-45
lines changed

13 files changed

+25
-45
lines changed

src/Cli/Microsoft.DotNet.Configurer/CliFolderPathCalculator.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ public static string WindowsNonExpandedToolsShimPath
4242

4343
public static string ToolsResolverCachePath => Path.Combine(DotnetUserProfileFolderPath, ToolsResolverCacheFolderName);
4444

45-
public static string PlatformHomeVariableName => CliFolderPathCalculatorCore.PlatformHomeVariableName;
46-
4745
public static string DotnetHomePath
4846
{
4947
get

src/Cli/Microsoft.TemplateEngine.Cli/Commands/CliPathInfo.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public CliPathInfo(
2424
throw new ArgumentException($"{nameof(host.Version)} of {nameof(host)} cannot be null or whitespace.", nameof(host));
2525
}
2626

27-
UserProfileDir = GetUserProfileDir(environment);
27+
UserProfileDir = CliFolderPathCalculator.DotnetHomePath;
2828
GlobalSettingsDir = GetGlobalSettingsDir(settingsLocation);
2929
HostSettingsDir = Path.Combine(GlobalSettingsDir, host.HostIdentifier);
3030
HostVersionSettingsDir = Path.Combine(GlobalSettingsDir, host.HostIdentifier, host.Version);
@@ -38,13 +38,6 @@ public CliPathInfo(
3838

3939
public string HostVersionSettingsDir { get; }
4040

41-
private static string GetUserProfileDir(IEnvironment environment) => environment.GetEnvironmentVariable(
42-
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
43-
? "USERPROFILE"
44-
: "HOME")
45-
?? Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
46-
?? throw new NotSupportedException("HOME or USERPROFILE environment variable is not defined, the environment is not supported");
47-
4841
private static string GetGlobalSettingsDir(string? settingsLocation)
4942
{
5043
var definedSettingsLocation = string.IsNullOrWhiteSpace(settingsLocation)

src/Cli/dotnet/SudoEnvironmentDirectoryOverride.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.CommandLine;
55
using Microsoft.DotNet.Cli.Utils;
6+
using Microsoft.DotNet.Configurer;
67
using NuGet.Common;
78
using NuGet.Configuration;
89

@@ -32,8 +33,8 @@ public static void OverrideEnvironmentVariableToTmp(ParseResult parseResult)
3233
if (!OperatingSystem.IsWindows() && IsRunningUnderSudo() && IsRunningWorkloadCommand(parseResult))
3334
{
3435
string sudoHome = PathUtilities.CreateTempSubdirectory();
35-
var homeBeforeOverride = Path.Combine(Environment.GetEnvironmentVariable("HOME"));
36-
Environment.SetEnvironmentVariable("HOME", sudoHome);
36+
var homeBeforeOverride = Path.Combine(Environment.GetEnvironmentVariable(CliFolderPathCalculator.DotnetHomeVariableName));
37+
Environment.SetEnvironmentVariable(CliFolderPathCalculator.DotnetHomeVariableName, sudoHome);
3738

3839
CopyUserNuGetConfigToOverriddenHome(homeBeforeOverride);
3940
}

src/Common/CliFolderPathCalculatorCore.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ static class CliFolderPathCalculatorCore
77
{
88
public const string DotnetHomeVariableName = "DOTNET_CLI_HOME";
99
public const string DotnetProfileDirectoryName = ".dotnet";
10-
public static readonly string PlatformHomeVariableName =
11-
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "USERPROFILE" : "HOME";
1210

1311
public static string? GetDotnetUserProfileFolderPath()
1412
{
@@ -26,14 +24,10 @@ static class CliFolderPathCalculatorCore
2624
var home = Environment.GetEnvironmentVariable(DotnetHomeVariableName);
2725
if (string.IsNullOrEmpty(home))
2826
{
29-
home = Environment.GetEnvironmentVariable(PlatformHomeVariableName);
27+
home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
3028
if (string.IsNullOrEmpty(home))
3129
{
32-
home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
33-
if (string.IsNullOrEmpty(home))
34-
{
35-
return null;
36-
}
30+
return null;
3731
}
3832
}
3933

test/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildANetCoreApp.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
78
using Microsoft.Extensions.DependencyModel;
89
using Microsoft.NET.Build.Tasks;
910
using Newtonsoft.Json.Linq;
@@ -167,7 +168,8 @@ private void It_targets_the_right_framework(
167168

168169
var additionalProbingPaths = ((JArray)devruntimeConfig["runtimeOptions"]["additionalProbingPaths"]).Values<string>();
169170
// can't use Path.Combine on segments with an illegal `|` character
170-
var expectedPath = $"{Path.Combine(FileConstants.UserProfileFolder, ".dotnet", "store")}{Path.DirectorySeparatorChar}|arch|{Path.DirectorySeparatorChar}|tfm|";
171+
var homePath = Environment.GetEnvironmentVariable(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "USERPROFILE" : "HOME");
172+
var expectedPath = $"{Path.Combine(homePath, ".dotnet", "store")}{Path.DirectorySeparatorChar}|arch|{Path.DirectorySeparatorChar}|tfm|";
171173
additionalProbingPaths.Should().Contain(expectedPath);
172174
}
173175

@@ -269,7 +271,7 @@ public void It_runs_the_app_from_the_output_folder(string targetFramework)
269271
[InlineData("net7.0")]
270272
[InlineData(ToolsetInfo.CurrentTargetFramework)]
271273
public void It_runs_a_rid_specific_app_from_the_output_folder(string targetFramework)
272-
{
274+
{
273275
RunAppFromOutputFolder("RunFromOutputFolderWithRID_" + targetFramework, true, false, targetFramework);
274276
}
275277

@@ -1001,7 +1003,7 @@ public void It_warns_on_nonportable_rids(string targetFramework, string[] rids,
10011003
IsExe = true
10021004
};
10031005

1004-
// Reference the package, add it to restore sources, and use a test-specific packages folder
1006+
// Reference the package, add it to restore sources, and use a test-specific packages folder
10051007
testProject.PackageReferences.Add(package);
10061008
testProject.AdditionalProperties["RestoreAdditionalProjectSources"] = Path.GetDirectoryName(package.NupkgPath);
10071009
testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\packages";
@@ -1054,7 +1056,7 @@ public void It_does_not_warn_on_rids_if_no_framework_references()
10541056
IsExe = true
10551057
};
10561058

1057-
// Reference the package, add it to restore sources, and use a test-specific packages folder
1059+
// Reference the package, add it to restore sources, and use a test-specific packages folder
10581060
testProject.PackageReferences.Add(package);
10591061
testProject.AdditionalProperties["RestoreAdditionalProjectSources"] = Path.GetDirectoryName(package.NupkgPath);
10601062
testProject.AdditionalProperties["RestorePackagesPath"] = @"$(MSBuildProjectDirectory)\packages";

test/Microsoft.NET.Build.Tests/GivenThatWeWantToUseAnalyzers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
#nullable disable
5+
using Microsoft.DotNet.Configurer;
56

67
namespace Microsoft.NET.Build.Tests
78
{
@@ -258,7 +259,7 @@ public void It_resolves_multitargeted_analyzers()
258259
static readonly List<string> nugetRoots = new()
259260
{
260261
TestContext.Current.NuGetCachePath,
261-
Path.Combine(FileConstants.UserProfileFolder, ".dotnet", "NuGetFallbackFolder"),
262+
Path.Combine(CliFolderPathCalculator.DotnetHomePath, ".dotnet", "NuGetFallbackFolder"),
262263
Path.Combine(TestContext.Current.ToolsetUnderTest.DotNetRoot, "packs")
263264
};
264265

test/Microsoft.NET.Build.Tests/Microsoft.NET.Build.Tests.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@
3434
<ItemGroup>
3535
<!-- Make sure tasks project is built, but don't directly reference it as an assembly. -->
3636
<ProjectReference Include="..\..\src\Tasks\Microsoft.NET.Build.Tasks\Microsoft.NET.Build.Tasks.csproj" PrivateAssets="all" ReferenceOutputAssembly="false" />
37-
37+
3838
<ProjectReference Include="..\Microsoft.NET.TestFramework\Microsoft.NET.TestFramework.csproj" />
3939
<ProjectReference Include="..\TelemetryStdOutLogger\TelemetryStdOutLogger.csproj" />
40+
<ProjectReference Include="..\..\src\Cli\Microsoft.DotNet.Configurer\Microsoft.DotNet.Configurer.csproj" />
4041
</ItemGroup>
4142

4243
<ItemGroup>
@@ -53,5 +54,5 @@
5354
</ItemGroup>
5455

5556
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
56-
57+
5758
</Project>

test/Microsoft.NET.TestFramework/FileConstants.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,5 @@ public static class FileConstants
99

1010
public static readonly string DynamicLibSuffix = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".dll" :
1111
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? ".dylib" : ".so";
12-
public static readonly string? UserProfileFolder = Environment.GetEnvironmentVariable(
13-
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
14-
"USERPROFILE" : "HOME");
1512
}
1613
}

test/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using System.CommandLine;
7+
using Microsoft.DotNet.Configurer;
78

89
namespace Microsoft.DotNet.Cli.Build.Tests
910
{
@@ -379,7 +380,7 @@ public void It_resolves_analyzers_targeting_mulitple_roslyn_versions(string comp
379380
static readonly List<string> nugetRoots = new()
380381
{
381382
TestContext.Current.NuGetCachePath,
382-
Path.Combine(FileConstants.UserProfileFolder, ".dotnet", "NuGetFallbackFolder")
383+
Path.Combine(CliFolderPathCalculator.DotnetHomePath, ".dotnet", "NuGetFallbackFolder")
383384
};
384385

385386
static string RelativeNuGetPath(string absoluteNuGetPath)

test/dotnet-new.Tests/DotnetNewDebugOptionsTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using Microsoft.DotNet.Cli.Utils;
5+
using Microsoft.DotNet.Configurer;
56

67
namespace Microsoft.DotNet.Cli.New.IntegrationTests
78
{
@@ -93,7 +94,7 @@ public Task CanShowConfigWithDebugShowConfig()
9394
public void DoesNotCreateCacheWhenVirtualHiveIsUsed()
9495
{
9596
string home = CreateTemporaryFolder(folderName: "Home");
96-
string envVariable = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "USERPROFILE" : "HOME";
97+
string envVariable = CliFolderPathCalculator.DotnetHomePath;
9798

9899
new DotnetNewCommand(_log, "--debug:ephemeral-hive")
99100
.WithoutCustomHive()

0 commit comments

Comments
 (0)