diff --git a/documentation/manpages/sdk/dotnet-environment-variables.7 b/documentation/manpages/sdk/dotnet-environment-variables.7 index 501263aeb1f9..4636a502f0bc 100644 --- a/documentation/manpages/sdk/dotnet-environment-variables.7 +++ b/documentation/manpages/sdk/dotnet-environment-variables.7 @@ -552,6 +552,9 @@ Configures the default programming language for the \f[V]dotnet new\f[R] command The default value is \f[V]C#\f[R]. Valid values are \f[V]C#\f[R], \f[V]F#\f[R], or \f[V]VB\f[R]. For more information, see dotnet new. +.SS \f[V]DOTNET_TOOLS_RESOLVER_CACHE_FOLDER\f[R] +.PP +Overrides the default location ($HOME/.dotnet/toolResolverCache) for the shim files for locally installed tools. .SH \f[V]dotnet watch\f[R] ENVIRONMENT VARIABLES .PP For information about \f[V]dotnet watch\f[R] settings that are available as environment variables, see dotnet watch environment variables. diff --git a/documentation/manpages/sdk/dotnet-tool-install.1 b/documentation/manpages/sdk/dotnet-tool-install.1 index f2ae419c9f3c..2d43bc248813 100644 --- a/documentation/manpages/sdk/dotnet-tool-install.1 +++ b/documentation/manpages/sdk/dotnet-tool-install.1 @@ -111,7 +111,7 @@ They\[cq]re stored in the same way as global tools: an executable binary with th .SS Local tools .PP Local tools are stored in the NuGet global directory, whatever you\[cq]ve set that to be. -There are shim files in \f[V]$HOME/.dotnet/toolResolverCache\f[R] for each local tool that point to where the tools are within that location. +There are shim files in \f[V]$HOME/.dotnet/toolResolverCache\f[R] for each local tool that point to where the tools are within that location (this path can be overridden with the \f[V]DOTNET_TOOLS_RESOLVER_CACHE_FOLDER\f[R] environment variable). .PP References to local tools are added to a \f[I]dotnet-tools.json\f[R] file in a \f[I].config\f[R] directory under the current directory. Starting in .NET 10, if a manifest file doesn\[cq]t exist yet, one is created automatically. diff --git a/src/Cli/Microsoft.DotNet.Configurer/CliFolderPathCalculator.cs b/src/Cli/Microsoft.DotNet.Configurer/CliFolderPathCalculator.cs index 47a36788f5c0..762b9de4db70 100644 --- a/src/Cli/Microsoft.DotNet.Configurer/CliFolderPathCalculator.cs +++ b/src/Cli/Microsoft.DotNet.Configurer/CliFolderPathCalculator.cs @@ -40,7 +40,9 @@ public static string WindowsNonExpandedToolsShimPath public static string DotnetUserProfileFolderPath => Path.Combine(DotnetHomePath, DotnetProfileDirectoryName); - public static string ToolsResolverCachePath => Path.Combine(DotnetUserProfileFolderPath, ToolsResolverCacheFolderName); + public static string ToolsResolverCachePath => + Environment.GetEnvironmentVariable("DOTNET_TOOLS_RESOLVER_CACHE_FOLDER") ?? + Path.Combine(DotnetUserProfileFolderPath, ToolsResolverCacheFolderName); public static string DotnetHomePath { diff --git a/test/dotnet.Tests/CommandTests/Tool/Restore/ToolRestoreCommandTests.cs b/test/dotnet.Tests/CommandTests/Tool/Restore/ToolRestoreCommandTests.cs index c84b9c8ee372..799c755ed7e3 100644 --- a/test/dotnet.Tests/CommandTests/Tool/Restore/ToolRestoreCommandTests.cs +++ b/test/dotnet.Tests/CommandTests/Tool/Restore/ToolRestoreCommandTests.cs @@ -434,6 +434,55 @@ public void ItRestoresCorrectToolVersion() rows[0].Version.Should().Be("8.0.0-rc.1.23419.6"); } + [Fact] + public void ItRestoresCorrectToolVersionWithOverriddenResolverCachePath() + { + var testDir = _testAssetsManager.CreateTestDirectory().Path; + + string configContents = """ + { + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "8.0.0-rc.1.23419.6", + "commands": [ + "dotnet-ef" + ] + } + } + } + """; + + File.WriteAllText(Path.Combine(testDir, "dotnet-tools.json"), configContents); + + string CliHome = Path.Combine(testDir, ".home"); + Directory.CreateDirectory(CliHome); + + var toolsResolverCacheFolder = Path.Combine(testDir, "toolsResolverCache"); + var toolRestoreCommand = new DotnetCommand(Log, "tool", "restore") + .WithEnvironmentVariable("DOTNET_CLI_HOME", CliHome) + .WithEnvironmentVariable("DOTNET_SKIP_WORKLOAD_INTEGRITY_CHECK", "true") + .WithEnvironmentVariable("DOTNET_TOOLS_RESOLVER_CACHE_FOLDER", toolsResolverCacheFolder) + .WithWorkingDirectory(testDir); + + toolRestoreCommand + .Execute() + .Should() + .Pass(); + + var cacheFilePath = Path.Combine(toolsResolverCacheFolder, "1", "dotnet-ef"); + + string json = File.ReadAllText(cacheFilePath); + + var rows = JsonSerializer.Deserialize>(json); + + rows.Count.Should().Be(1); + + rows[0].Name.Should().Be("dotnet-ef"); + rows[0].Version.Should().Be("8.0.0-rc.1.23419.6"); + } + [Fact] public void WhenCannotFindManifestFileItPrintsWarning() {