Skip to content

Commit cc61994

Browse files
authored
Use stderr for watch output (#50820)
1 parent 002cab9 commit cc61994

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

src/BuiltInTools/DotNetDeltaApplier/StartupHook.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private static void Log(string message)
8989
if (!string.IsNullOrEmpty(prefix))
9090
{
9191
Console.ForegroundColor = ConsoleColor.DarkGray;
92-
Console.WriteLine($"{prefix} {message}");
92+
Console.Error.WriteLine($"{prefix} {message}");
9393
Console.ResetColor();
9494
}
9595
}

src/BuiltInTools/dotnet-watch/UI/ConsoleReporter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,24 +56,24 @@ public void Report(EventId id, Emoji emoji, MessageSeverity severity, string mes
5656
{
5757
case MessageSeverity.Error:
5858
// Use stdout for error messages to preserve ordering with respect to other output.
59-
WriteLine(console.Out, message, ConsoleColor.Red, emoji);
59+
WriteLine(console.Error, message, ConsoleColor.Red, emoji);
6060
break;
6161

6262
case MessageSeverity.Warning:
63-
WriteLine(console.Out, message, ConsoleColor.Yellow, emoji);
63+
WriteLine(console.Error, message, ConsoleColor.Yellow, emoji);
6464
break;
6565

6666
case MessageSeverity.Output:
6767
if (!IsQuiet)
6868
{
69-
WriteLine(console.Out, message, color: null, emoji);
69+
WriteLine(console.Error, message, color: null, emoji);
7070
}
7171
break;
7272

7373
case MessageSeverity.Verbose:
7474
if (IsVerbose)
7575
{
76-
WriteLine(console.Out, message, ConsoleColor.DarkGray, emoji);
76+
WriteLine(console.Error, message, ConsoleColor.DarkGray, emoji);
7777
}
7878
break;
7979
}

test/dotnet-watch.Tests/ConsoleReporterTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ public void WritesToStandardStreams(bool suppressEmojis)
1818
var reporter = new ConsoleReporter(testConsole, verbose: true, quiet: false, suppressEmojis: suppressEmojis);
1919

2020
reporter.Report(id: default, Emoji.Watch, MessageSeverity.Verbose, "verbose {0}");
21-
Assert.Equal($"dotnet watch {(suppressEmojis ? ":" : "⌚")} verbose {{0}}" + EOL, testConsole.GetOutput());
21+
Assert.Equal($"dotnet watch {(suppressEmojis ? ":" : "⌚")} verbose {{0}}" + EOL, testConsole.GetError());
2222
testConsole.Clear();
2323

2424
reporter.Report(id: default, Emoji.Watch, MessageSeverity.Output, "out");
25-
Assert.Equal($"dotnet watch {(suppressEmojis ? ":" : "⌚")} out" + EOL, testConsole.GetOutput());
25+
Assert.Equal($"dotnet watch {(suppressEmojis ? ":" : "⌚")} out" + EOL, testConsole.GetError());
2626
testConsole.Clear();
2727

2828
reporter.Report(id: default, Emoji.Warning, MessageSeverity.Warning, "warn");
29-
Assert.Equal($"dotnet watch {(suppressEmojis ? ":" : "⚠")} warn" + EOL, testConsole.GetOutput());
29+
Assert.Equal($"dotnet watch {(suppressEmojis ? ":" : "⚠")} warn" + EOL, testConsole.GetError());
3030
testConsole.Clear();
3131

3232
reporter.Report(id: default, Emoji.Error, MessageSeverity.Error, "error");
33-
Assert.Equal($"dotnet watch {(suppressEmojis ? ":" : "❌")} error" + EOL, testConsole.GetOutput());
33+
Assert.Equal($"dotnet watch {(suppressEmojis ? ":" : "❌")} error" + EOL, testConsole.GetError());
3434
testConsole.Clear();
3535
}
3636

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,10 @@ public void FilePath_AsProjectArgument()
332332
/// </summary>
333333
[Theory]
334334
// error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
335-
[InlineData("build", "MSB1003")]
335+
[InlineData("build", "MSB1003", false)]
336336
// dotnet watch: Could not find a MSBuild project file in '...'. Specify which project to use with the --project option.
337-
[InlineData("watch", "--project")]
338-
public void Precedence_BuiltInCommand(string cmd, string error)
337+
[InlineData("watch", "--project", true)]
338+
public void Precedence_BuiltInCommand(string cmd, string error, bool errorInStdErr)
339339
{
340340
var testInstance = _testAssetsManager.CreateTestDirectory();
341341
File.WriteAllText(Path.Join(testInstance.Path, cmd), """
@@ -348,18 +348,26 @@ public void Precedence_BuiltInCommand(string cmd, string error)
348348
""");
349349

350350
// dotnet build -> built-in command
351-
new DotnetCommand(Log, cmd)
351+
var failure = new DotnetCommand(Log, cmd)
352352
.WithWorkingDirectory(testInstance.Path)
353353
.Execute()
354-
.Should().Fail()
355-
.And.HaveStdOutContaining(error);
354+
.Should().Fail();
355+
356+
if (errorInStdErr)
357+
{
358+
failure.And.HaveStdErrContaining(error);
359+
}
360+
else
361+
{
362+
failure.And.HaveStdOutContaining(error);
363+
}
356364

357365
// dotnet ./build -> file-based app
358366
new DotnetCommand(Log, $"./{cmd}")
359-
.WithWorkingDirectory(testInstance.Path)
360-
.Execute()
361-
.Should().Pass()
362-
.And.HaveStdOut("hello 1");
367+
.WithWorkingDirectory(testInstance.Path)
368+
.Execute()
369+
.Should().Pass()
370+
.And.HaveStdOut("hello 1");
363371

364372
// dotnet run build -> file-based app
365373
new DotnetCommand(Log, "run", cmd)

0 commit comments

Comments
 (0)