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

[17.12] Adding a static factory for the TerminalLogger #11016

Draft
wants to merge 3 commits into
base: vs17.12
Choose a base branch
from

Conversation

MichalPavlik
Copy link
Member

@MichalPavlik MichalPavlik commented Nov 21, 2024

Fixes #10998

Summary

dotnet run directly creates an instance of TerminalLogger, bypassing all checks. This can lead to situations where ANSI escape sequences are emitted to the terminal, even if the terminal does not support them. These escape sequences are also emitted when the standard output is redirected, which can break a CI build that relies on the command's output.

Changes Made

Added a static factory that can return instance of Terminal or Console logger based on current environment. The usage of TerminalLogger in this scenario can be explicitly disabled by using of already existing env. variable, which wasn't possible before.

Customer Impact

Some customers reported unexpected behavior of dotnet run command and CI builds failures.

Regression?

Yes.

Testing

Manual testing with locally updated SDK.

Notes

There will be a servicing PR for SDK 9.0.2xx that uses this new method. In 9.0.1xx, TL will be disabled for the last part of dotnet run completely.

@MichalPavlik MichalPavlik requested a review from a team as a code owner November 21, 2024 12:55
@MichalPavlik
Copy link
Member Author

MichalPavlik commented Nov 21, 2024

SDK PR was created: dotnet/sdk#45009

Hotfix for 9.0.1xx: dotnet/sdk#45015

public static ILogger CreateTerminalOrConsoleLogger(LoggerVerbosity verbosity)
{
bool isDisabled = (Environment.GetEnvironmentVariable("MSBUILDTERMINALLOGGER") ?? string.Empty).Equals("off", StringComparison.InvariantCultureIgnoreCase);
(bool supportsAnsi, bool outputIsScreen, uint? originalConsoleMode) = NativeMethodsShared.QueryIsScreenAndTryEnableAnsiColorCodes();
Copy link
Member

Choose a reason for hiding this comment

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

nit: we can skip this if TL is requested to be disabled

Copy link
Member Author

Choose a reason for hiding this comment

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

I started with that intention, but this approach has lower complexity and the performance impact is negligible.

/// Creates a Terminal logger or Console logger based on the environment.
/// This method is called by reflection from dotnet. Do not modify the name or parameters without adapting the SDK.
/// </summary>
public static ILogger CreateTerminalOrConsoleLogger(LoggerVerbosity verbosity)
Copy link
Member

Choose a reason for hiding this comment

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

One missing piece here is that while this does check the environment variable, the CLI flags (--tl:off, etc) are not respected. We may need a way to pass in an argv and have this method check for the presence/absence of the TL-related (and --console logger parameter!) flags to ensure uniform behavior.

@baronfel
Copy link
Member

One question I had was: the main problem was that the silent TL verbosity still caused the 'progress' indicators to be written - could those be gated behind the verbosity check as well? Ideally we'd be able to say something like "when silent verbosity is used, only the diagnostic reports will be written to stdout".

@MichalPavlik
Copy link
Member Author

@baronfel, thanks for the great feedback. While your points are valid, would you like to expand the fix in this PR? I'm asking because of the US Thanksgiving holiday. I'm not sure how much more time it will take to deliver this fix if we miss this week...

@MichalPavlik
Copy link
Member Author

As discussed offline, we have time for 9.0.200, so I'll convert this PR to draft and I will cover Chet's comments.

@MichalPavlik MichalPavlik marked this pull request as draft November 21, 2024 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants