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

@ -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);
}
}
}