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

Do not print-out version info in terminal logger #9831

Merged
merged 6 commits into from
Mar 13, 2024
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
37 changes: 37 additions & 0 deletions src/MSBuild.UnitTests/XMake_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.Build.CommandLine;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Logging;
using Microsoft.Build.Shared;
Expand Down Expand Up @@ -890,6 +891,7 @@ public void SetConsoleUICulture()

// Restore the current UI culture back to the way it was at the beginning of this unit test.
thisThread.CurrentUICulture = originalUICulture;
MSBuildApp.SetConsoleUI();
}


Expand Down Expand Up @@ -2605,6 +2607,41 @@ public override bool Execute()
}
}

[Theory]
[InlineData("", true)]
[InlineData("/tl:true", false)]
[InlineData("/nologo", false)]
[InlineData("/getProperty:p", false)]
public void EndToEndVersionMessage(string arguments, bool shouldContainVersionMessage)
{
using TestEnvironment testEnvironment = UnitTests.TestEnvironment.Create();

string projectContents = ObjectModelHelpers.CleanupFileContents("""
<Project>
<Target Name="Hello">
</Target>
</Project>
""");

TransientTestProjectWithFiles testProject = testEnvironment.CreateTestProjectWithFiles(projectContents);

string output = RunnerUtilities.ExecMSBuild($"{arguments} \"{testProject.ProjectFile}\"", out bool success, _output);
success.ShouldBeTrue();

string expectedVersionString =
ResourceUtilities.FormatResourceStringStripCodeAndKeyword("MSBuildVersionMessage",
ProjectCollection.DisplayVersion, NativeMethodsShared.FrameworkName);

if (shouldContainVersionMessage)
{
output.ShouldContain(expectedVersionString);
}
else
{
output.ShouldNotContain(expectedVersionString);
}
}

[Theory]
[InlineData("/v:diagnostic", MessageImportance.Low)]
[InlineData("/v:detailed", MessageImportance.Low)]
Expand Down
42 changes: 24 additions & 18 deletions src/MSBuild/XMake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2455,20 +2455,8 @@ private static bool ProcessCommandLineSwitches(
}
#endif

bool shouldShowLogo = !commandLineSwitches[CommandLineSwitches.ParameterlessSwitch.NoLogo] &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Preprocess) &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.GetProperty) &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.GetItem) &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.GetTargetResult) &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.FeatureAvailability);

// show copyright message if nologo switch is not set
// NOTE: we heed the nologo switch even if there are switch errors
if (!recursing && shouldShowLogo)
{
DisplayVersionMessage();
}

bool useTerminalLogger = ProcessTerminalLoggerConfiguration(commandLineSwitches, out string aggregatedTerminalLoggerParameters);
DisplayVersionMessageIfNeeded(recursing, useTerminalLogger, commandLineSwitches);

// Idle priority would prevent the build from proceeding as the user does normal actions.
// This switch is processed early to capture both the command line case (main node should
Expand Down Expand Up @@ -2658,8 +2646,6 @@ private static bool ProcessCommandLineSwitches(

outputResultsCache = ProcessOutputResultsCache(commandLineSwitches);

bool useTerminalLogger = ProcessTerminalLoggerConfiguration(commandLineSwitches, out string aggregatedTerminalLoggerParameters);

// figure out which loggers are going to listen to build events
string[][] groupedFileLoggerParameters = commandLineSwitches.GetFileLoggerParameters();

Expand Down Expand Up @@ -4465,9 +4451,29 @@ private static void ThrowInvalidToolsVersionInitializationException(IEnumerable<
/// <summary>
/// Displays the application version message/logo.
/// </summary>
private static void DisplayVersionMessage()
private static void DisplayVersionMessageIfNeeded(bool recursing, bool useTerminalLogger, CommandLineSwitches commandLineSwitches)
{
Console.WriteLine(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("MSBuildVersionMessage", ProjectCollection.DisplayVersion, NativeMethods.FrameworkName));
if (recursing)
{
return;
}

// Show the versioning information if the user has not disabled it or msbuild is not running in a mode
// where it is not appropriate to show the versioning information (information querying mode that can be plugged into CLI scripts,
// terminal logger mode, where we want to display only the most relevant info, while output is not meant for investigation).
// NOTE: response files are not reflected in this check. So enabling TL in response file will lead to version message still being shown.
bool shouldShowLogo = !commandLineSwitches[CommandLineSwitches.ParameterlessSwitch.NoLogo] &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.Preprocess) &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.GetProperty) &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.GetItem) &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.GetTargetResult) &&
!commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.FeatureAvailability) &&
!useTerminalLogger;

if (shouldShowLogo)
{
Console.WriteLine(ResourceUtilities.FormatResourceStringStripCodeAndKeyword("MSBuildVersionMessage", ProjectCollection.DisplayVersion, NativeMethods.FrameworkName));
}
}

/// <summary>
Expand Down