Console.Write() doesn't show output until a newline

When running an app with `dotnet run`, we are redirecting the standard out and error just to print it out to our standard out and error. However, we are batching the output until we hit a newline, which isn't ideal for console apps.

To fix this, `dotnet run` no longer redirects the standard out and error.

Fix #2777
This commit is contained in:
Eric Erhardt 2016-05-12 10:24:57 -05:00
parent 6482aa0221
commit 6bf59ffde6
7 changed files with 241 additions and 20 deletions

View file

@ -111,6 +111,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Performance", "test\Perform
EndProject EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "binding-redirects.Tests", "test\binding-redirects.Tests\binding-redirects.Tests.xproj", "{27DBF851-F2E3-4FD5-BF4D-A73C81933283}" Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "binding-redirects.Tests", "test\binding-redirects.Tests\binding-redirects.Tests.xproj", "{27DBF851-F2E3-4FD5-BF4D-A73C81933283}"
EndProject EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "dotnet-run.UnitTests", "test\dotnet-run.UnitTests\dotnet-run.UnitTests.xproj", "{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -795,6 +797,22 @@ Global
{27DBF851-F2E3-4FD5-BF4D-A73C81933283}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU {27DBF851-F2E3-4FD5-BF4D-A73C81933283}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{27DBF851-F2E3-4FD5-BF4D-A73C81933283}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU {27DBF851-F2E3-4FD5-BF4D-A73C81933283}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{27DBF851-F2E3-4FD5-BF4D-A73C81933283}.RelWithDebInfo|x64.Build.0 = Release|Any CPU {27DBF851-F2E3-4FD5-BF4D-A73C81933283}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Debug|x64.ActiveCfg = Debug|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Debug|x64.Build.0 = Debug|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.MinSizeRel|Any CPU.ActiveCfg = Debug|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.MinSizeRel|Any CPU.Build.0 = Debug|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.MinSizeRel|x64.ActiveCfg = Debug|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.MinSizeRel|x64.Build.0 = Debug|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Release|Any CPU.Build.0 = Release|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Release|x64.ActiveCfg = Release|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.Release|x64.Build.0 = Release|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.RelWithDebInfo|x64.ActiveCfg = Release|Any CPU
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D}.RelWithDebInfo|x64.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -847,5 +865,6 @@ Global
{35E3C2DC-9B38-4EC5-8DD7-C32458DC485F} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7} {35E3C2DC-9B38-4EC5-8DD7-C32458DC485F} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
{6A3095FF-A7C5-4300-85A9-C025C384401D} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7} {6A3095FF-A7C5-4300-85A9-C025C384401D} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
{27DBF851-F2E3-4FD5-BF4D-A73C81933283} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7} {27DBF851-F2E3-4FD5-BF4D-A73C81933283} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
{2A5EEC38-3030-44EF-9E58-6771E7BAB01D} = {17735A9D-BFD9-4585-A7CB-3208CA6EA8A7}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View file

@ -31,6 +31,7 @@ namespace Microsoft.DotNet.Cli.Build
"dotnet-publish.Tests", "dotnet-publish.Tests",
"dotnet-resgen.Tests", "dotnet-resgen.Tests",
"dotnet-run.Tests", "dotnet-run.Tests",
"dotnet-run.UnitTests",
"dotnet-test.Tests", "dotnet-test.Tests",
"dotnet-test.UnitTests", "dotnet-test.UnitTests",
"Kestrel.Tests", "Kestrel.Tests",

View file

@ -16,8 +16,8 @@ namespace Microsoft.DotNet.Cli.Utils
public class Command : ICommand public class Command : ICommand
{ {
private readonly Process _process; private readonly Process _process;
private readonly StreamForwarder _stdOut; private StreamForwarder _stdOut;
private readonly StreamForwarder _stdErr; private StreamForwarder _stdErr;
private bool _running = false; private bool _running = false;
@ -27,14 +27,9 @@ namespace Microsoft.DotNet.Cli.Utils
{ {
FileName = commandSpec.Path, FileName = commandSpec.Path,
Arguments = commandSpec.Args, Arguments = commandSpec.Args,
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false UseShellExecute = false
}; };
_stdOut = new StreamForwarder();
_stdErr = new StreamForwarder();
_process = new Process _process = new Process
{ {
StartInfo = psi StartInfo = psi
@ -134,11 +129,12 @@ namespace Microsoft.DotNet.Cli.Utils
Reporter.Verbose.WriteLine($"Process ID: {_process.Id}"); Reporter.Verbose.WriteLine($"Process ID: {_process.Id}");
var taskOut = _stdOut.BeginRead(_process.StandardOutput); var taskOut = _stdOut?.BeginRead(_process.StandardOutput);
var taskErr = _stdErr.BeginRead(_process.StandardError); var taskErr = _stdErr?.BeginRead(_process.StandardError);
_process.WaitForExit(); _process.WaitForExit();
Task.WaitAll(taskOut, taskErr); taskOut?.Wait();
taskErr?.Wait();
} }
var exitCode = _process.ExitCode; var exitCode = _process.ExitCode;
@ -158,8 +154,8 @@ namespace Microsoft.DotNet.Cli.Utils
return new CommandResult( return new CommandResult(
this._process.StartInfo, this._process.StartInfo,
exitCode, exitCode,
_stdOut.CapturedOutput, _stdOut?.CapturedOutput,
_stdErr.CapturedOutput); _stdErr?.CapturedOutput);
} }
public ICommand WorkingDirectory(string projectDirectory) public ICommand WorkingDirectory(string projectDirectory)
@ -181,6 +177,7 @@ namespace Microsoft.DotNet.Cli.Utils
public ICommand CaptureStdOut() public ICommand CaptureStdOut()
{ {
ThrowIfRunning(); ThrowIfRunning();
EnsureStdOut();
_stdOut.Capture(); _stdOut.Capture();
return this; return this;
} }
@ -188,6 +185,7 @@ namespace Microsoft.DotNet.Cli.Utils
public ICommand CaptureStdErr() public ICommand CaptureStdErr()
{ {
ThrowIfRunning(); ThrowIfRunning();
EnsureStdErr();
_stdErr.Capture(); _stdErr.Capture();
return this; return this;
} }
@ -197,6 +195,8 @@ namespace Microsoft.DotNet.Cli.Utils
ThrowIfRunning(); ThrowIfRunning();
if (!onlyIfVerbose || CommandContext.IsVerbose()) if (!onlyIfVerbose || CommandContext.IsVerbose())
{ {
EnsureStdOut();
if (to == null) if (to == null)
{ {
_stdOut.ForwardTo(writeLine: Reporter.Output.WriteLine); _stdOut.ForwardTo(writeLine: Reporter.Output.WriteLine);
@ -215,6 +215,8 @@ namespace Microsoft.DotNet.Cli.Utils
ThrowIfRunning(); ThrowIfRunning();
if (!onlyIfVerbose || CommandContext.IsVerbose()) if (!onlyIfVerbose || CommandContext.IsVerbose())
{ {
EnsureStdErr();
if (to == null) if (to == null)
{ {
_stdErr.ForwardTo(writeLine: Reporter.Error.WriteLine); _stdErr.ForwardTo(writeLine: Reporter.Error.WriteLine);
@ -231,6 +233,8 @@ namespace Microsoft.DotNet.Cli.Utils
public ICommand OnOutputLine(Action<string> handler) public ICommand OnOutputLine(Action<string> handler)
{ {
ThrowIfRunning(); ThrowIfRunning();
EnsureStdOut();
_stdOut.ForwardTo(writeLine: handler); _stdOut.ForwardTo(writeLine: handler);
return this; return this;
} }
@ -238,6 +242,8 @@ namespace Microsoft.DotNet.Cli.Utils
public ICommand OnErrorLine(Action<string> handler) public ICommand OnErrorLine(Action<string> handler)
{ {
ThrowIfRunning(); ThrowIfRunning();
EnsureStdErr();
_stdErr.ForwardTo(writeLine: handler); _stdErr.ForwardTo(writeLine: handler);
return this; return this;
} }
@ -258,6 +264,18 @@ namespace Microsoft.DotNet.Cli.Utils
return info.FileName + " " + info.Arguments; return info.FileName + " " + info.Arguments;
} }
private void EnsureStdOut()
{
_stdOut = _stdOut ?? new StreamForwarder();
_process.StartInfo.RedirectStandardOutput = true;
}
private void EnsureStdErr()
{
_stdErr = _stdErr ?? new StreamForwarder();
_process.StartInfo.RedirectStandardError = true;
}
private void ThrowIfRunning([CallerMemberName] string memberName = null) private void ThrowIfRunning([CallerMemberName] string memberName = null)
{ {
if (_running) if (_running)

View file

@ -20,16 +20,26 @@ namespace Microsoft.DotNet.Tools.Run
public string Project = null; public string Project = null;
public IReadOnlyList<string> Args = null; public IReadOnlyList<string> Args = null;
private readonly ICommandFactory _commandFactory;
private ProjectContext _context;
private List<string> _args;
private BuildWorkspace _workspace;
public static readonly string[] DefaultFrameworks = new[] public static readonly string[] DefaultFrameworks = new[]
{ {
FrameworkConstants.FrameworkIdentifiers.NetCoreApp, FrameworkConstants.FrameworkIdentifiers.NetCoreApp,
FrameworkConstants.FrameworkIdentifiers.NetStandardApp, FrameworkConstants.FrameworkIdentifiers.NetStandardApp,
}; };
public RunCommand()
: this(new RunCommandFactory())
{
}
ProjectContext _context; public RunCommand(ICommandFactory commandFactory)
List<string> _args; {
private BuildWorkspace _workspace; _commandFactory = commandFactory;
}
public int Start() public int Start()
{ {
@ -166,24 +176,22 @@ namespace Microsoft.DotNet.Tools.Run
} }
} }
Command command; ICommand command;
if (outputName.EndsWith(FileNameSuffixes.DotNet.DynamicLib, StringComparison.OrdinalIgnoreCase)) if (outputName.EndsWith(FileNameSuffixes.DotNet.DynamicLib, StringComparison.OrdinalIgnoreCase))
{ {
// The executable is a ".dll", we need to call it through dotnet.exe // The executable is a ".dll", we need to call it through dotnet.exe
var muxer = new Muxer(); var muxer = new Muxer();
command = Command.Create(muxer.MuxerPath, Enumerable.Concat( command = _commandFactory.Create(muxer.MuxerPath, Enumerable.Concat(
Enumerable.Concat(new string[] { "exec" }, hostArgs), Enumerable.Concat(new string[] { "exec" }, hostArgs),
Enumerable.Concat(new string[] { outputName }, _args))); Enumerable.Concat(new string[] { outputName }, _args)));
} }
else else
{ {
command = Command.Create(outputName, Enumerable.Concat(hostArgs, _args)); command = _commandFactory.Create(outputName, Enumerable.Concat(hostArgs, _args));
} }
result = command result = command
.ForwardStdOut()
.ForwardStdErr()
.Execute() .Execute()
.ExitCode; .ExitCode;
@ -198,5 +206,13 @@ namespace Microsoft.DotNet.Tools.Run
var result = command.Execute(); var result = command.Execute();
return result.ExitCode; return result.ExitCode;
} }
private class RunCommandFactory : ICommandFactory
{
public ICommand Create(string commandName, IEnumerable<string> args, NuGetFramework framework = null, string configuration = "Debug")
{
return Command.Create(commandName, args, framework, configuration);
}
}
} }
} }

View file

@ -0,0 +1,122 @@
// 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;
using System.Collections.Generic;
using System.IO;
using FluentAssertions;
using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.TestFramework;
using Microsoft.DotNet.Tools.Test.Utilities;
using NuGet.Frameworks;
using Xunit;
namespace Microsoft.DotNet.Tools.Run.Tests
{
public class GivenARunCommand : TestBase
{
private const int RunExitCode = 29;
[Fact]
public void ItDoesntRedirectStandardOutAndError()
{
TestInstance instance = TestAssetsManager.CreateTestInstance("TestAppSimple")
.WithLockFiles();
new BuildCommand(instance.TestRoot)
.Execute()
.Should()
.Pass();
RunCommand runCommand = new RunCommand(new FailOnRedirectOutputCommandFactory());
runCommand.Project = instance.TestRoot;
runCommand.Start()
.Should()
.Be(RunExitCode);
}
private class FailOnRedirectOutputCommandFactory : ICommandFactory
{
public ICommand Create(string commandName, IEnumerable<string> args, NuGetFramework framework = null, string configuration = "Debug")
{
return new FailOnRedirectOutputCommand();
}
/// <summary>
/// A Command that will fail if a caller tries redirecting StdOut or StdErr.
/// </summary>
private class FailOnRedirectOutputCommand : ICommand
{
public CommandResult Execute()
{
return new CommandResult(null, RunExitCode, null, null);
}
public ICommand CaptureStdErr()
{
throw new NotSupportedException();
}
public ICommand CaptureStdOut()
{
throw new NotSupportedException();
}
public ICommand ForwardStdErr(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
throw new NotSupportedException();
}
public ICommand ForwardStdOut(TextWriter to = null, bool onlyIfVerbose = false, bool ansiPassThrough = true)
{
throw new NotSupportedException();
}
public ICommand OnErrorLine(Action<string> handler)
{
throw new NotSupportedException();
}
public ICommand OnOutputLine(Action<string> handler)
{
throw new NotSupportedException();
}
public string CommandArgs
{
get
{
throw new NotImplementedException();
}
}
public string CommandName
{
get
{
throw new NotImplementedException();
}
}
public CommandResolutionStrategy ResolutionStrategy
{
get
{
throw new NotImplementedException();
}
}
public ICommand EnvironmentVariable(string name, string value)
{
throw new NotImplementedException();
}
public ICommand WorkingDirectory(string projectDirectory)
{
throw new NotImplementedException();
}
}
}
}
}

View file

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0.24720" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0.24720</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>2a5eec38-3030-44ef-9e58-6771e7bab01d</ProjectGuid>
<RootNamespace>Microsoft.DotNet.Tools.Run.UnitTests</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin</OutputPath>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DNX\Microsoft.DNX.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View file

@ -0,0 +1,27 @@
{
"version": "1.0.0-*",
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc3-*"
},
"System.Runtime.Serialization.Primitives": "4.1.1-rc2-24027",
"dotnet": {
"target": "project"
},
"Microsoft.DotNet.Tools.Tests.Utilities": {
"target": "project"
},
"xunit": "2.1.0",
"dotnet-test-xunit": "1.0.0-rc2-173361-36"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.4",
"portable-net451+win8"
]
}
},
"testRunner": "xunit"
}