Remove temp project path from tool install warnings and errors.

This commit attempts to filter the diagnostic messages emitted during tool
installation.  The diagnostic messages may be prefixed with the temporary
project; since this is an implementation detail that only causes confusion and
clutter in the diagnostic messages, the prefix is removed if present.

Fixes #8707.
This commit is contained in:
Peter Huene 2018-03-06 16:15:27 -08:00
parent 0a2013e74b
commit e2d9c2f892
No known key found for this signature in database
GPG key ID: E1D265D820213D6A
4 changed files with 59 additions and 6 deletions

View file

@ -16,7 +16,7 @@ namespace Microsoft.DotNet.ToolPackage
IToolPackageStore toolPackageStore = CreateToolPackageStore(nonGlobalLocation); IToolPackageStore toolPackageStore = CreateToolPackageStore(nonGlobalLocation);
var toolPackageInstaller = new ToolPackageInstaller( var toolPackageInstaller = new ToolPackageInstaller(
toolPackageStore, toolPackageStore,
new ProjectRestorer(Reporter.Output)); new ProjectRestorer());
return (toolPackageStore, toolPackageInstaller); return (toolPackageStore, toolPackageInstaller);
} }

View file

@ -119,7 +119,7 @@ namespace Microsoft.DotNet.ToolPackage
{ {
var tempProject = _tempProject ?? new DirectoryPath(Path.GetTempPath()) var tempProject = _tempProject ?? new DirectoryPath(Path.GetTempPath())
.WithSubDirectories(Path.GetRandomFileName()) .WithSubDirectories(Path.GetRandomFileName())
.WithFile(Path.GetRandomFileName() + ".csproj"); .WithFile("restore.csproj");
if (Path.GetExtension(tempProject.Value) != "csproj") if (Path.GetExtension(tempProject.Value) != "csproj")
{ {

View file

@ -16,10 +16,14 @@ namespace Microsoft.DotNet.Tools.Install.Tool
{ {
private const string AnyRid = "any"; private const string AnyRid = "any";
private readonly IReporter _reporter; private readonly IReporter _reporter;
private readonly IReporter _errorReporter;
private readonly bool _forceOutputRedirection;
public ProjectRestorer(IReporter reporter = null) public ProjectRestorer(IReporter reporter = null)
{ {
_reporter = reporter; _reporter = reporter ?? Reporter.Output;
_errorReporter = reporter ?? Reporter.Error;
_forceOutputRedirection = reporter != null;
} }
public void Restore( public void Restore(
@ -56,11 +60,11 @@ namespace Microsoft.DotNet.Tools.Install.Tool
var command = new DotNetCommandFactory(alwaysRunOutOfProc: true) var command = new DotNetCommandFactory(alwaysRunOutOfProc: true)
.Create("restore", argsToPassToRestore); .Create("restore", argsToPassToRestore);
if (_reporter != null) if (verbosity == null || _forceOutputRedirection)
{ {
command = command command = command
.OnOutputLine((line) => _reporter.WriteLine(line)) .OnOutputLine(line => WriteLine(_reporter, line, project))
.OnErrorLine((line) => _reporter.WriteLine(line)); .OnErrorLine(line => WriteLine(_errorReporter, line, project));
} }
var result = command.Execute(); var result = command.Execute();
@ -69,5 +73,28 @@ namespace Microsoft.DotNet.Tools.Install.Tool
throw new ToolPackageException(LocalizableStrings.ToolInstallationRestoreFailed); throw new ToolPackageException(LocalizableStrings.ToolInstallationRestoreFailed);
} }
} }
private static void WriteLine(IReporter reporter, string line, FilePath project)
{
line = line ?? "";
// Remove the temp project prefix if present
if (line.StartsWith($"{project.Value} : ", StringComparison.OrdinalIgnoreCase))
{
line = line.Substring(project.Value.Length + 3);
}
// Note: MSBuild intentionally does not localize "warning" and "error" for diagnostic messages
if (line.StartsWith("warning ", StringComparison.Ordinal))
{
line = line.Yellow();
}
else if (line.StartsWith("error ", StringComparison.Ordinal))
{
line = line.Red();
}
reporter.WriteLine(line);
}
} }
} }

View file

@ -498,6 +498,32 @@ namespace Microsoft.DotNet.ToolPackage.Tests
package.Uninstall(); package.Uninstall();
} }
[Fact]
public void GivenANuGetDiagnosticMessageItShouldNotContainTheTempProject()
{
var nugetConfigPath = WriteNugetConfigFileToPointToTheFeed();
var tempProject = GetUniqueTempProjectPathEachTest();
var (store, installer, reporter, fileSystem) = Setup(
useMock: false,
tempProject: tempProject);
var package = installer.InstallPackage(
packageId: TestPackageId,
versionRange: VersionRange.Parse("1.0.*"),
targetFramework: _testTargetframework,
nugetConfig: nugetConfigPath);
reporter.Lines.Should().NotBeEmpty();
reporter.Lines.Should().Contain(l => l.Contains("warning"));
reporter.Lines.Should().NotContain(l => l.Contains(tempProject.Value));
reporter.Lines.Clear();
AssertPackageInstall(reporter, fileSystem, package, store);
package.Uninstall();
}
private static void AssertPackageInstall( private static void AssertPackageInstall(
BufferedReporter reporter, BufferedReporter reporter,
IFileSystem fileSystem, IFileSystem fileSystem,