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

Update Traits when environment has been changed #9655

Merged
merged 7 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 3 additions & 0 deletions documentation/wiki/ChangeWaves.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ A wave of features is set to "rotate out" (i.e. become standard functionality) t
- [Warning on serialization custom events by default in .NET framework](https://github.com/dotnet/msbuild/pull/9318)
- [Cache SDK resolver data process-wide](https://github.com/dotnet/msbuild/pull/9335)
- [Target parameters will be unquoted](https://github.com/dotnet/msbuild/pull/9452), meaning the ';' symbol in the parameter target name will always be treated as separator
- [Add Link metadata to Resources in AssignLinkMetadata target](https://github.com/dotnet/msbuild/pull/9464)
- [Change Version switch output to finish with a newline](https://github.com/dotnet/msbuild/pull/9485)
- [Load Microsoft.DotNet.MSBuildSdkResolver into default load context (MSBuild.exe only)](https://github.com/dotnet/msbuild/pull/9439)
- [Load NuGet.Frameworks into secondary AppDomain (MSBuild.exe only)](https://github.com/dotnet/msbuild/pull/9446)
- [ResultsCache ignores some of the BuildRequest data, may return incorrect results](https://github.com/dotnet/msbuild/pull/9565)
- [Update Traits when environment has been changed](https://github.com/dotnet/msbuild/pull/9655)


### 17.8
- [[RAR] Don't do I/O on SDK-provided references](https://github.com/dotnet/msbuild/pull/8688)
Expand Down
13 changes: 1 addition & 12 deletions src/Build/BackEnd/Node/InProcNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,18 +336,7 @@ private NodeEngineShutdownReason HandleShutdown(out Exception exception)
NativeMethodsShared.SetCurrentDirectory(_savedCurrentDirectory);

// Restore the original environment.
foreach (KeyValuePair<string, string> entry in CommunicationsUtilities.GetEnvironmentVariables())
{
if (!_savedEnvironment.ContainsKey(entry.Key))
{
Environment.SetEnvironmentVariable(entry.Key, null);
}
}

foreach (KeyValuePair<string, string> entry in _savedEnvironment)
{
Environment.SetEnvironmentVariable(entry.Key, entry.Value);
}
CommunicationsUtilities.SetEnvironment(_savedEnvironment);
}

exception = _shutdownException;
Expand Down
19 changes: 5 additions & 14 deletions src/Build/BackEnd/Node/OutOfProcNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -476,20 +476,9 @@ private NodeEngineShutdownReason HandleShutdown(out Exception exception)
// If the node was never configured, this will be null.
if (_savedEnvironment != null)
{
foreach (KeyValuePair<string, string> entry in CommunicationsUtilities.GetEnvironmentVariables())
{
if (!_savedEnvironment.ContainsKey(entry.Key))
{
Environment.SetEnvironmentVariable(entry.Key, null);
}
}

foreach (KeyValuePair<string, string> entry in _savedEnvironment)
{
Environment.SetEnvironmentVariable(entry.Key, entry.Value);
}
CommunicationsUtilities.SetEnvironment(_savedEnvironment);
Traits.UpdateFromEnvironment();
AR-May marked this conversation as resolved.
Show resolved Hide resolved
}

try
{
// Shut down logging, which will cause all queued logging messages to be sent.
Expand Down Expand Up @@ -722,12 +711,14 @@ private void HandleNodeConfiguration(NodeConfiguration configuration)
}
}

// Now set the new environment
// Now set the new environment and update Traits class accordingly
foreach (KeyValuePair<string, string> environmentPair in _buildParameters.BuildProcessEnvironment)
{
Environment.SetEnvironmentVariable(environmentPair.Key, environmentPair.Value);
}

Traits.UpdateFromEnvironment();

// We want to make sure the global project collection has the toolsets which were defined on the parent
// so that any custom toolsets defined can be picked up by tasks who may use the global project collection but are
// executed on the child node.
Expand Down
4 changes: 4 additions & 0 deletions src/Build/BackEnd/Node/OutOfProcServerNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.Build.BackEnd;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Framework.Telemetry;
using Microsoft.Build.Internal;
using Microsoft.Build.Shared;
Expand Down Expand Up @@ -357,7 +358,10 @@ private void HandleServerNodeBuildCommand(ServerNodeBuildCommand command)

// Set build process context
Directory.SetCurrentDirectory(command.StartupDirectory);

CommunicationsUtilities.SetEnvironment(command.BuildProcessEnvironment);
Traits.UpdateFromEnvironment();

Thread.CurrentThread.CurrentCulture = command.Culture;
Thread.CurrentThread.CurrentUICulture = command.UICulture;

Expand Down
11 changes: 10 additions & 1 deletion src/Framework/Traits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Build.Framework
/// </summary>
internal class Traits
{
private static readonly Traits _instance = new Traits();
private static Traits _instance = new Traits();
public static Traits Instance
{
get
Expand Down Expand Up @@ -136,6 +136,15 @@ public Traits()

public readonly bool InProcNodeDisabled = Environment.GetEnvironmentVariable("MSBUILDNOINPROCNODE") == "1";

public static void UpdateFromEnvironment()
{
// Re-create Traits instance to update values in Traits according to current environment.
if (ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_10))
{
_instance = new Traits();
}
}

private static int ParseIntFromEnvironmentVariableOrDefault(string environmentVariable, int defaultValue)
{
return int.TryParse(Environment.GetEnvironmentVariable(environmentVariable), out int result)
Expand Down
4 changes: 2 additions & 2 deletions src/Shared/CommunicationsUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ internal static void SetEnvironment(IDictionary<string, string> newEnvironment)
{
if (newEnvironment != null)
{
// First, empty out any new variables
// First, empty out any old variables
AR-May marked this conversation as resolved.
Show resolved Hide resolved
foreach (KeyValuePair<string, string> entry in CommunicationsUtilities.GetEnvironmentVariables())
{
if (!newEnvironment.ContainsKey(entry.Key))
Expand All @@ -386,7 +386,7 @@ internal static void SetEnvironment(IDictionary<string, string> newEnvironment)
}
}

// Then, make sure the old ones have their old values.
// Then, make sure the new ones have their new values.
foreach (KeyValuePair<string, string> entry in newEnvironment)
{
Environment.SetEnvironmentVariable(entry.Key, entry.Value);
Expand Down