Fix crash when user home directory cannot be determined.

Currently, dotnet will crash with an `ArgumentNullException` if `USERPROFILE`
(Windows) or `HOME` (macOS and Linux) is not set in the environment.  This
is because there is a missing null check after retrieving the environment
variable's value.  Additionally, if either variable is set to an empty string,
a `.dotnet` directory is created in the current directory where dotnet is being
run.

This commit fixes this by printing a graceful error informing the user the home
directory could not be determined and to set `DOTNET_CLI_HOME` to the directory
to use.  This variable will be respected before `USERPROFILE` or `HOME`. It is
likely that CI environments where `HOME` is not set can use `DOTNET_CLI_HOME`
to specify a local temporary location; by using this variable rather than
setting `HOME`, it is guaranteed to only affect dotnet.

It was discussed that we should perhaps fallback to some temporary location if
the home directory could not be determined, but NuGet currently requires `HOME`
to be set to work.  Because of this, it was decided that we should just handle
this case gracefully and provide a way for users to override the home directory
without relying on `USERPROFILE`/`HOME` entirely.

Closes #8053.
This commit is contained in:
Peter Huene 2018-05-18 16:41:16 -07:00
parent 3e962bc131
commit b1f8eb1d8d
No known key found for this signature in database
GPG key ID: E1D265D820213D6A
20 changed files with 137 additions and 27 deletions

View file

@ -7,6 +7,7 @@ using System.Runtime.CompilerServices;
[assembly: AssemblyMetadataAttribute("Serviceable", "True")]
[assembly: InternalsVisibleTo("dotnet, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("dotnet.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.Configurer, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.Tools.Test, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.Cli.Utils.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]
[assembly: InternalsVisibleTo("Microsoft.DotNet.TestFramework, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")]

View file

@ -4,12 +4,14 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.DotNet.Cli.Utils;
using NuGet.Common;
namespace Microsoft.DotNet.Configurer
{
public static class CliFolderPathCalculator
{
public const string DotnetHomeVariableName = "DOTNET_CLI_HOME";
private const string DotnetProfileDirectoryName = ".dotnet";
private const string ToolsShimFolderName = "tools";
@ -21,23 +23,36 @@ namespace Microsoft.DotNet.Configurer
public static string ToolsPackagePath => ToolPackageFolderPathCalculator.GetToolPackageFolderPath(ToolsShimPath);
public static BashPathUnderHomeDirectory ToolsShimPathInUnix
public static BashPathUnderHomeDirectory ToolsShimPathInUnix =>
new BashPathUnderHomeDirectory(
DotnetHomePath,
Path.Combine(DotnetProfileDirectoryName, ToolsShimFolderName));
public static string DotnetUserProfileFolderPath =>
Path.Combine(DotnetHomePath, DotnetProfileDirectoryName);
public static string PlatformHomeVariableName =>
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "USERPROFILE" : "HOME";
public static string DotnetHomePath
{
get
{
return new BashPathUnderHomeDirectory(Environment.GetEnvironmentVariable("HOME"),
Path.Combine(DotnetProfileDirectoryName, ToolsShimFolderName));
}
}
var home = Environment.GetEnvironmentVariable(DotnetHomeVariableName);
if (string.IsNullOrEmpty(home))
{
home = Environment.GetEnvironmentVariable(PlatformHomeVariableName);
if (string.IsNullOrEmpty(home))
{
throw new ConfigurationException(
string.Format(
LocalizableStrings.FailedToDetermineUserHomeDirectory,
DotnetHomeVariableName))
.DisplayAsError();
}
}
public static string DotnetUserProfileFolderPath
{
get
{
string profileDir = Environment.GetEnvironmentVariable(
RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "USERPROFILE" : "HOME");
return Path.Combine(profileDir, DotnetProfileDirectoryName);
return home;
}
}

View file

@ -0,0 +1,22 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
namespace Microsoft.DotNet.Configurer
{
internal class ConfigurationException : Exception
{
public ConfigurationException()
{
}
public ConfigurationException(string message) : base(message)
{
}
public ConfigurationException(string message, Exception innerException) : base(message, innerException)
{
}
}
}

View file

@ -154,4 +154,7 @@ Successfully installed the ASP.NET Core HTTPS Development Certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only). For establishing trust on other platforms refer to the platform specific documentation.
For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?linkid=848054.</value>
</data>
<data name="FailedToDetermineUserHomeDirectory" xml:space="preserve">
<value>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</value>
</data>
</root>

View file

@ -17,6 +17,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="NETStandard.Library" Version="2.0.0" />
<PackageReference Include="NuGet.Common" Version="$(NuGetCommonPackageVersion)" />
<PackageReference Include="NuGet.Configuration" Version="$(NuGetConfigurationPackageVersion)" />
<PackageReference Include="Microsoft.DotNet.Archive" Version="$(MicrosoftDotNetArchivePackageVersion)" />

View file

@ -71,6 +71,11 @@ Pokud chcete certifikátu důvěřovat (platí jenom pro Windows a macOS), nains
Další informace o konfiguraci protokolu HTTPS najdete na webu https://go.microsoft.com/fwlink/?linkid=848054.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ Um dem Zertifikat zu vertrauen (nur Windows und macOS), installieren Sie zuerst
Weitere Informationen zur Konfiguration von HTTPS finden Sie unter https://go.microsoft.com/fwlink/?linkid=848054.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -70,6 +70,11 @@ Para confiar en el certificado (solo Windows y macOS), instale primero la herram
Para obtener más información sobre la configuración HTTPS, vea https://go.microsoft.com/fwlink/?linkid=848054.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ Pour approuver le certificat (Windows et macOS uniquement), installez d'abord l'
Pour plus d'informations sur la configuration du protocole HTTPS, consultez https://go.microsoft.com/fwlink/?linkid=848054.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ Per considerare attendibile il certificato (solo Windows e macOS), installare pr
Per altre informazioni sulla configurazione di HTTPS, vedere https://go.microsoft.com/fwlink/?linkid=848054.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ ASP.NET Core HTTPS 開発証明書が正常にインストールされました
HTTPS を構成する方法の詳細については、https://go.microsoft.com/fwlink/?linkid=848054 をご覧ください。</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ ASP.NET Core HTTPS 개발 인증서를 설치했습니다.
HTTPS 구성에 대한 자세한 내용은 https://go.microsoft.com/fwlink/?linkid=848054를 참조하세요.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ Aby ufać temu certyfikatowi (dotyczy tylko systemów Windows i macOS), najpierw
Aby uzyskać więcej informacji dotyczących konfigurowania protokołu HTTPS, zobacz https://go.microsoft.com/fwlink/?linkid=848054.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ Para confiar no certificado (apenas Windows e macOS), primeiramente instale a fe
Para saber mais sobre a configuração de HTTPS, acesse https://go.microsoft.com/fwlink/?linkid=848054.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?l
Дополнительные сведения о настройке HTTPS: https://go.microsoft.com/fwlink/?linkid=848054.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ Sertifikaya güvenmek için (yalnızca Windows ve macOS) önce 'dotnet tool inst
HTTPS yapılandırması hakkında daha fazla bilgi için bkz. https://go.microsoft.com/fwlink/?linkid=848054.</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?l
有关配置 HTTPS 的详细信息,请参阅 https://go.microsoft.com/fwlink/?linkid=848054。</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -71,6 +71,11 @@ For more information on configuring HTTPS see https://go.microsoft.com/fwlink/?l
如需如何設定 HTTPS 的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?linkid=848054。</target>
<note />
</trans-unit>
<trans-unit id="FailedToDetermineUserHomeDirectory">
<source>The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</source>
<target state="new">The user's home directory could not be determined. Set the '{0}' environment variable to specify the directory to use.</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

View file

@ -5,9 +5,12 @@ using System;
using System.IO;
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.Configurer;
using Microsoft.DotNet.Tools.Test.Utilities;
using Xunit;
using LocalizableStrings = Microsoft.DotNet.Cli.Utils.LocalizableStrings;
namespace Microsoft.DotNet.Tests
{
public class GivenThatDotNetRunsCommands : TestBase
@ -32,5 +35,19 @@ namespace Microsoft.DotNet.Tests
.Should().Fail()
.And.HaveStdErrContaining(string.Format(LocalizableStrings.NoExecutableFoundMatchingCommand, "dotnet-crash"));
}
[Theory]
[InlineData("")]
[InlineData(null)]
public void GivenAMissingHomeVariableItPrintsErrorMessage(string value)
{
new TestCommand("dotnet")
.WithEnvironmentVariable(CliFolderPathCalculator.PlatformHomeVariableName, value)
.ExecuteWithCapturedOutput("--help")
.Should()
.Fail()
.And
.HaveStdErrContaining(CliFolderPathCalculator.DotnetHomeVariableName);
}
}
}

View file

@ -59,20 +59,6 @@ namespace Microsoft.DotNet.Tests
.Should().BeFalse("Because multicore JIT is disabled");
}
[Fact]
public void WhenTheProfileRootIsUndefinedThenDotnetDoesNotCrash()
{
var testDirectory = TestAssets.CreateTestDirectory();
var testStartTime = GetTruncatedDateTime();
var optimizationProfileFilePath = GetOptimizationProfileFilePath(testDirectory.FullName);
new TestCommand("dotnet")
.WithUserProfileRoot("")
.ExecuteWithCapturedOutput("--help")
.Should().Pass();
}
[Fact]
public void WhenCliRepoBuildsThenDotnetWritesOptimizationDataToTheDefaultProfileRoot()
{