Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

csi.exe should emit exception to stderr, not stdout #9791

Merged
merged 3 commits into from
May 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Interactive/CsiCore/Csi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal static int Main(string[] args)
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.Error.WriteLine(ex.ToString());
return 1;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Scripting/CSharpTest.Desktop/CsiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void ReferenceSearchPaths_LIB()
var result = ProcessUtilities.Run(CsiPath, "/r:C.dll a.csx", workingDirectory: cwd.Path, additionalEnvironmentVars: new[] { KeyValuePair.Create("LIB", dir.Path) });

// error CS0006: Metadata file 'C.dll' could not be found
Assert.True(result.Output.StartsWith("error CS0006", StringComparison.Ordinal));
Assert.True(result.Errors.StartsWith("error CS0006", StringComparison.Ordinal));
Assert.True(result.ContainsErrors);
}

Expand Down
48 changes: 39 additions & 9 deletions src/Scripting/CSharpTest/CommandLineRunnerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public void Await()
. select x * x
Enumerable.WhereSelectArrayIterator<int, int> {{ 9, 16, 25 }}
> ", runner.Console.Out.ToString());

AssertEx.AssertEqualToleratingWhitespaceDifferences(
@"(1,19): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.",
runner.Console.Error.ToString());
}

[Fact]
Expand Down Expand Up @@ -185,6 +189,11 @@ > div(10, 0)
+ Submission#0.div(int, int)
«Gray»
> ", runner.Console.Out.ToString());

Assert.Equal(
$@"{new System.DivideByZeroException().Message}
+ Submission#0.div(int, int)
", runner.Console.Error.ToString());
}

[Fact]
Expand All @@ -211,6 +220,11 @@ > C<string>.div<bool>(10, 0)
+ Submission#0.C<T>.div<U>(int, int)
«Gray»
> ", runner.Console.Out.ToString());

Assert.Equal(
$@"{new System.DivideByZeroException().Message}
+ Submission#0.C<T>.div<U>(int, int)
", runner.Console.Error.ToString());
}

[Fact]
Expand Down Expand Up @@ -241,9 +255,9 @@ public void Args_Interactive2()

runner.RunInteractive();

AssertEx.AssertEqualToleratingWhitespaceDifferences(
$@"error CS2001: Source file '{Path.Combine(AppContext.BaseDirectory, "@arg1")}' could not be found.",
runner.Console.Out.ToString());
var error = $@"error CS2001: Source file '{Path.Combine(AppContext.BaseDirectory, "@arg1")}' could not be found.";
AssertEx.AssertEqualToleratingWhitespaceDifferences(error, runner.Console.Out.ToString());
AssertEx.AssertEqualToleratingWhitespaceDifferences(error, runner.Console.Error.ToString());
}

[Fact]
Expand Down Expand Up @@ -389,9 +403,9 @@ public void Script_NonExistingFile()

Assert.Equal(1, runner.RunInteractive());

AssertEx.AssertEqualToleratingWhitespaceDifferences($@"
error CS2001: Source file '{Path.Combine(AppContext.BaseDirectory, "a + b")}' could not be found.
", runner.Console.Out.ToString());
var error = $@"error CS2001: Source file '{Path.Combine(AppContext.BaseDirectory, "a + b")}' could not be found.";
AssertEx.AssertEqualToleratingWhitespaceDifferences(error, runner.Console.Out.ToString());
AssertEx.AssertEqualToleratingWhitespaceDifferences(error, runner.Console.Error.ToString());
}

[Fact]
Expand Down Expand Up @@ -431,9 +445,9 @@ public void Script_BadUsings()

Assert.Equal(1, runner.RunInteractive());

AssertEx.AssertEqualToleratingWhitespaceDifferences(@"
error CS0246: The type or namespace name 'Foo' could not be found (are you missing a using directive or an assembly reference?)
", runner.Console.Out.ToString());
const string error = @"error CS0246: The type or namespace name 'Foo' could not be found (are you missing a using directive or an assembly reference?)";
AssertEx.AssertEqualToleratingWhitespaceDifferences(error, runner.Console.Out.ToString());
AssertEx.AssertEqualToleratingWhitespaceDifferences(error, runner.Console.Error.ToString());
}

[Fact]
Expand All @@ -453,6 +467,10 @@ > nameof(Microsoft.CodeAnalysis)
(1,8): error CS0234: The type or namespace name 'CodeAnalysis' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
«Gray»
> ", runner.Console.Out.ToString());

AssertEx.AssertEqualToleratingWhitespaceDifferences(
"(1,8): error CS0234: The type or namespace name 'CodeAnalysis' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)",
runner.Console.Error.ToString());
}

[Fact]
Expand Down Expand Up @@ -574,6 +592,10 @@ > X
1
>
", runner.Console.Out.ToString());

AssertEx.AssertEqualToleratingWhitespaceDifferences(
@"(1,7): error CS1504: Source file 'a.csx' could not be opened -- Could not find file.",
runner.Console.Error.ToString());
}

[Fact]
Expand Down Expand Up @@ -609,6 +631,10 @@ > ReferencePaths.Add(@""{dir.Path}"")
C {{ }}
>
", runner.Console.Out.ToString());

AssertEx.AssertEqualToleratingWhitespaceDifferences(
@"(1,1): error CS0006: Metadata file 'C.dll' could not be found",
runner.Console.Error.ToString());
}

[Fact]
Expand Down Expand Up @@ -689,6 +715,10 @@ 1 1
C {{ }}
>
", runner.Console.Out.ToString());

AssertEx.AssertEqualToleratingWhitespaceDifferences(
$@"{init.Path}(2,3): error CS1002: ; expected",
runner.Console.Error.ToString());
}

[Fact]
Expand Down
14 changes: 7 additions & 7 deletions src/Scripting/Core/Hosting/CommandLine/CommandLineRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal int RunInteractive()
ErrorLogger errorLogger = null;
if (_compiler.Arguments.ErrorLogPath != null)
{
errorLogger = _compiler.GetErrorLogger(_console.Out, CancellationToken.None);
errorLogger = _compiler.GetErrorLogger(_console.Error, CancellationToken.None);
if (errorLogger == null)
{
return CommonCompiler.Failed;
Expand Down Expand Up @@ -106,7 +106,7 @@ private int RunInteractiveCore(ErrorLogger errorLogger)
var scriptOptions = GetScriptOptions(_compiler.Arguments, scriptPathOpt, _compiler.MessageProvider, diagnosticsInfos);

var errors = _compiler.Arguments.Errors.Concat(diagnosticsInfos.Select(Diagnostic.Create));
if (_compiler.ReportErrors(errors, _console.Out, errorLogger))
if (_compiler.ReportErrors(errors, _console.Error, errorLogger))
{
return CommonCompiler.Failed;
}
Expand Down Expand Up @@ -176,7 +176,7 @@ private int RunScript(ScriptOptions options, string code, ErrorLogger errorLogge
}
catch (CompilationErrorException e)
{
_compiler.ReportErrors(e.Diagnostics, _console.Out, errorLogger);
_compiler.ReportErrors(e.Diagnostics, _console.Error, errorLogger);
return CommonCompiler.Failed;
}
}
Expand Down Expand Up @@ -281,7 +281,7 @@ private bool TryBuildAndRun(Script<object> newScript, InteractiveScriptGlobals g
catch (FileLoadException e) when (e.InnerException is InteractiveAssemblyLoaderException)
{
_console.ForegroundColor = ConsoleColor.Red;
_console.Out.WriteLine(e.InnerException.Message);
_console.Error.WriteLine(e.InnerException.Message);
_console.ResetColor();

return false;
Expand Down Expand Up @@ -324,7 +324,7 @@ private void DisplayException(Exception e)
try
{
_console.ForegroundColor = ConsoleColor.Red;
_console.Out.Write(_objectFormatter.FormatException(e));
_console.Error.Write(_objectFormatter.FormatException(e));
}
finally
{
Expand Down Expand Up @@ -363,14 +363,14 @@ private void DisplayDiagnostics(IEnumerable<Diagnostic> diagnostics)
foreach (var diagnostic in ordered.Take(MaxDisplayCount))
{
_console.ForegroundColor = (diagnostic.Severity == DiagnosticSeverity.Error) ? ConsoleColor.Red : ConsoleColor.Yellow;
_console.Out.WriteLine(diagnostic.ToString());
_console.Error.WriteLine(diagnostic.ToString());
}

if (errorsAndWarnings.Length > MaxDisplayCount)
{
int notShown = errorsAndWarnings.Length - MaxDisplayCount;
_console.ForegroundColor = ConsoleColor.DarkRed;
_console.Out.WriteLine(string.Format((notShown == 1) ? ScriptingResources.PlusAdditionalError : ScriptingResources.PlusAdditionalError, notShown));
_console.Error.WriteLine(string.Format((notShown == 1) ? ScriptingResources.PlusAdditionalError : ScriptingResources.PlusAdditionalError, notShown));
}
}
finally
Expand Down
42 changes: 41 additions & 1 deletion src/Scripting/CoreTest/TestConsoleIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public TestConsoleIO(string input)
}

private TestConsoleIO(Reader reader)
: base(output: new Writer(reader), error: new StringWriter(), input: reader)
: this(reader, new Writer(reader))
{
}

private TestConsoleIO(Reader reader, TextWriter output)
: base(output: output, error: new TeeWriter(output), input: reader)
{
}

Expand Down Expand Up @@ -102,5 +107,40 @@ public override void WriteLine()
base.WriteLine();
}
}

private sealed class TeeWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
private readonly TextWriter _other;

public TeeWriter(TextWriter other)
{
_other = other;
}

public override void Write(char value)
{
_other.Write(value);
base.Write(value);
}

public override void Write(string value)
{
_other.Write(value);
base.Write(value);
}

public override void WriteLine(string value)
{
_other.WriteLine(value);
base.WriteLine(value);
}

public override void WriteLine()
{
_other.WriteLine();
base.WriteLine();
}
}
}
}