From 3ee3725c68594ebe31e042cacedbc568804893d6 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Wed, 16 Mar 2016 09:48:57 +0100 Subject: [PATCH 1/3] csi should emit exception to stderr, not stdout --- src/Interactive/CsiCore/Csi.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Interactive/CsiCore/Csi.cs b/src/Interactive/CsiCore/Csi.cs index 9d383690563e0..5debb538bcb80 100644 --- a/src/Interactive/CsiCore/Csi.cs +++ b/src/Interactive/CsiCore/Csi.cs @@ -36,7 +36,7 @@ internal static int Main(string[] args) } catch (Exception ex) { - Console.WriteLine(ex.ToString()); + Console.Error.WriteLine(ex.ToString()); return 1; } } From 92999b77fadbdcf1103127c2fd85335980500201 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 18 Mar 2016 18:38:03 +0100 Subject: [PATCH 2/3] Fix CommandLineRunner to emit errors et al. to stderr, not stdout Fixes #9826 --- .../CSharpTest/CommandLineRunnerTests.cs | 48 +++++++++++++++---- .../Hosting/CommandLine/CommandLineRunner.cs | 14 +++--- src/Scripting/CoreTest/TestConsoleIO.cs | 42 +++++++++++++++- 3 files changed, 87 insertions(+), 17 deletions(-) diff --git a/src/Scripting/CSharpTest/CommandLineRunnerTests.cs b/src/Scripting/CSharpTest/CommandLineRunnerTests.cs index 43d8abbaf114a..001b7031c8582 100644 --- a/src/Scripting/CSharpTest/CommandLineRunnerTests.cs +++ b/src/Scripting/CSharpTest/CommandLineRunnerTests.cs @@ -79,6 +79,10 @@ public void Await() . select x * x Enumerable.WhereSelectArrayIterator {{ 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] @@ -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] @@ -211,6 +220,11 @@ > C.div(10, 0) + Submission#0.C.div(int, int) «Gray» > ", runner.Console.Out.ToString()); + + Assert.Equal( +$@"{new System.DivideByZeroException().Message} + + Submission#0.C.div(int, int) +", runner.Console.Error.ToString()); } [Fact] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] @@ -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] diff --git a/src/Scripting/Core/Hosting/CommandLine/CommandLineRunner.cs b/src/Scripting/Core/Hosting/CommandLine/CommandLineRunner.cs index 927e9c3d2ac87..d0ad6ec1943fd 100644 --- a/src/Scripting/Core/Hosting/CommandLine/CommandLineRunner.cs +++ b/src/Scripting/Core/Hosting/CommandLine/CommandLineRunner.cs @@ -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; @@ -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; } @@ -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; } } @@ -281,7 +281,7 @@ private bool TryBuildAndRun(Script 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; @@ -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 { @@ -363,14 +363,14 @@ private void DisplayDiagnostics(IEnumerable 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 diff --git a/src/Scripting/CoreTest/TestConsoleIO.cs b/src/Scripting/CoreTest/TestConsoleIO.cs index 1a5a175776268..2f55f8465659a 100644 --- a/src/Scripting/CoreTest/TestConsoleIO.cs +++ b/src/Scripting/CoreTest/TestConsoleIO.cs @@ -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) { } @@ -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(); + } + } } } From 6958c016b344497d709103c1676013b9230ac4bd Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Fri, 18 Mar 2016 19:57:52 +0100 Subject: [PATCH 3/3] CsiTests.ReferenceSearchPaths_LIB regression fix --- src/Scripting/CSharpTest.Desktop/CsiTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Scripting/CSharpTest.Desktop/CsiTests.cs b/src/Scripting/CSharpTest.Desktop/CsiTests.cs index a6933b2bd4381..38019e740fdc8 100644 --- a/src/Scripting/CSharpTest.Desktop/CsiTests.cs +++ b/src/Scripting/CSharpTest.Desktop/CsiTests.cs @@ -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); }