Skip to content

Commit

Permalink
Restore console mode after server build (#8016)
Browse files Browse the repository at this point in the history
Fixes #8008

Context
With MSBuild server, we adjust the console mode to be able to handle VT100 codes. We should undo that after each build to make it obvious to users mistakenly emitting VT100 codes that they should add code to properly handle them.

Customer Impact
Console settings we enable while building with MSBuild server persist after the build. In particular, if a customer is outputting raw VT100 codes, they will be properly formatted if their code is run using dotnet run, but their customers will see the raw codes because they will not inherit anything from MSBuild.

Testing
Verified that building the project from #8008 no longer had unexpected spacing when these private bits were deployed to an SDK.
Verified that the bug motivating our original support for VT100 codes is still resolved.
Unit tests.

Code Reviewers
rainersigwald, rokonec

Description of the fix
Reset VT100 codes after each build.
  • Loading branch information
Forgind committed Sep 30, 2022
1 parent 70db3ac commit e1930c2
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/Build/BackEnd/Client/MSBuildClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public sealed class MSBuildClient
/// </summary>
private readonly Dictionary<string, string> _serverEnvironmentVariables;

/// <summary>
/// The console mode we had before the build.
/// </summary>
private uint? _originalConsoleMode;

/// <summary>
/// Full path to current MSBuild.exe if executable is MSBuild.exe,
/// or to version of MSBuild.dll found to be associated with the current process.
Expand Down Expand Up @@ -195,18 +200,23 @@ public MSBuildClientExitResult Execute(CancellationToken cancellationToken)
// Send build command.
// Let's send it outside the packet pump so that we easier and quicker deal with possible issues with connection to server.
MSBuildEventSource.Log.MSBuildServerBuildStart(descriptiveCommandLine);
if (!TrySendBuildCommand())
if (TrySendBuildCommand())
{
return _exitResult;
}
_numConsoleWritePackets = 0;
_sizeOfConsoleWritePackets = 0;

_numConsoleWritePackets = 0;
_sizeOfConsoleWritePackets = 0;
ReadPacketsLoop(cancellationToken);

ReadPacketsLoop(cancellationToken);
MSBuildEventSource.Log.MSBuildServerBuildStop(descriptiveCommandLine, _numConsoleWritePackets, _sizeOfConsoleWritePackets, _exitResult.MSBuildClientExitType.ToString(), _exitResult.MSBuildAppExitTypeString);
CommunicationsUtilities.Trace("Build finished.");
}

if (NativeMethodsShared.IsWindows && _originalConsoleMode is not null)
{
IntPtr stdOut = NativeMethodsShared.GetStdHandle(NativeMethodsShared.STD_OUTPUT_HANDLE);
NativeMethodsShared.SetConsoleMode(stdOut, _originalConsoleMode.Value);
}

MSBuildEventSource.Log.MSBuildServerBuildStop(descriptiveCommandLine, _numConsoleWritePackets, _sizeOfConsoleWritePackets, _exitResult.MSBuildClientExitType.ToString(), _exitResult.MSBuildAppExitTypeString);
CommunicationsUtilities.Trace("Build finished.");
return _exitResult;
}

Expand Down Expand Up @@ -362,6 +372,7 @@ private void ConfigureAndQueryConsoleProperties()
}
else
{
_originalConsoleMode = consoleMode;
consoleMode |= NativeMethodsShared.ENABLE_VIRTUAL_TERMINAL_PROCESSING | NativeMethodsShared.DISABLE_NEWLINE_AUTO_RETURN;
success = NativeMethodsShared.SetConsoleMode(stdOut, consoleMode);
}
Expand Down

0 comments on commit e1930c2

Please sign in to comment.