Skip to content

Use MSBuild CommandLineParser for terminal logger detection#53835

Open
MichalPavlik wants to merge 1 commit intomainfrom
dev/mipavlik/use-msbuild-cmdline-parser
Open

Use MSBuild CommandLineParser for terminal logger detection#53835
MichalPavlik wants to merge 1 commit intomainfrom
dev/mipavlik/use-msbuild-cmdline-parser

Conversation

@MichalPavlik
Copy link
Copy Markdown
Member

Fixes #50441

Replaces custom TerminalLoggerDetector parsing logic (TryFind, Switch) with Microsoft.Build.CommandLine.Experimental.CommandLineParser to support response files and align with MSBuild's switch parsing.

Replace custom TerminalLoggerDetector parsing logic (TryFind, Switch) with
Microsoft.Build.CommandLine.Experimental.CommandLineParser to support
response files and align with MSBuild's switch parsing.
@MichalPavlik MichalPavlik requested a review from a team as a code owner April 13, 2026 14:32
Copilot AI review requested due to automatic review settings April 13, 2026 14:32
@MichalPavlik MichalPavlik requested a review from baronfel April 13, 2026 14:33
@Youssef1313 Youssef1313 requested a review from nohwnd April 13, 2026 14:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates dotnet test’s terminal logger detection to use MSBuild’s command-line parsing implementation, addressing cases where switches come from response files (e.g., Directory.Build.rsp) and aligning behavior with MSBuild switch parsing rules.

Changes:

  • Replace custom -tl/-tlp token scanning with Microsoft.Build.CommandLine.Experimental.CommandLineParser in TerminalLoggerDetector.
  • Add a new unit test suite for TerminalLoggerDetector.ProcessTerminalLoggerConfiguration.
  • Add a Microsoft.Build.Runtime dependency to support the new MSBuild command-line parser usage.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
test/dotnet.Tests/CommandTests/VSTest/TerminalLoggerDetectorTests.cs Adds tests for terminal logger detection behavior (switches, env vars, tlp defaults).
src/Cli/dotnet/dotnet.csproj Adds MSBuild runtime package dependency needed for the new parser usage.
src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs Replaces custom parsing with MSBuild CommandLineParser-based detection for terminal logger switches.

Comment on lines +20 to +24
public void ProcessTerminalLoggerConfiguration_WithExplicitSwitch_ReturnsExpectedMode(string tlArg, TerminalLoggerMode expectedMode)
{
var parseResult = Parser.Parse($"dotnet test {tlArg}");

var result = TerminalLoggerDetector.ProcessTerminalLoggerConfiguration(parseResult);
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parser.Parse(...) here likely won't compile because this file doesn't import/alias the CLI parser type. Most other tests use using Parser = Microsoft.DotNet.Cli.Parser; (to avoid ambiguity with System.CommandLine parser types). Add that alias (or fully-qualify) so Parser.Parse resolves correctly.

Copilot uses AI. Check for mistakes.
Comment on lines +29 to +40
[Fact]
public void ProcessTerminalLoggerConfiguration_WithEnvironmentVariable_ReturnsExpectedMode()
{
string? previousValue = Environment.GetEnvironmentVariable("MSBUILDTERMINALLOGGER");
try
{
Environment.SetEnvironmentVariable("MSBUILDTERMINALLOGGER", "off");

var parseResult = Parser.Parse("dotnet test");
var result = TerminalLoggerDetector.ProcessTerminalLoggerConfiguration(parseResult);

result.Should().Be(TerminalLoggerMode.Off);
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests don’t currently cover the scenario that motivated the change (switches coming from response files, especially the implicit Directory.Build.rsp). Consider adding a test that creates a temp directory with Directory.Build.rsp containing -tl:off, sets Environment.CurrentDirectory to that directory, parses dotnet test, and asserts TerminalLoggerMode.Off so the regression is protected.

Copilot uses AI. Check for mistakes.
Comment on lines 435 to +472
@@ -466,21 +465,19 @@ static bool CheckIfTerminalIsSupportedAndTryEnableAnsiColorCodes()

bool TryFromCommandLine(IReadOnlyList<string> unmatchedTokens, [NotNullWhen(true)] out string? value)
{
Switch? terminalLogger = TryFind(unmatchedTokens, ["tl", "terminalLogger", "ll", "livelogger"]);
if (terminalLogger == null)
var parser = new Microsoft.Build.CommandLine.Experimental.CommandLineParser();
var switches = parser.Parse(unmatchedTokens);

string[]? tlValues = switches.TerminalLogger;
if (tlValues is not { Length: > 0 })
Copy link

Copilot AI Apr 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unmatchedTokens are parsed twice (once in TryFromCommandLine and again in FindDefaultValue) by constructing a new CommandLineParser each time. Consider parsing once and reusing the resulting switches for both checks to reduce duplication and avoid unnecessary allocations.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Broken Test output when specifying -tl:off in Directory.Build.rsp

2 participants