Skip to content
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
15 changes: 7 additions & 8 deletions src/Build/BackEnd/BuildManager/BuildParameters.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Threading;
Expand Down Expand Up @@ -129,7 +129,7 @@ public class BuildParameters : ITranslatable
/// <summary>
/// The original process environment.
/// </summary>
private Dictionary<string, string> _buildProcessEnvironment;
private FrozenDictionary<string, string> _buildProcessEnvironment;

/// <summary>
/// The environment properties for the build.
Expand Down Expand Up @@ -282,9 +282,7 @@ internal BuildParameters(BuildParameters other, bool resetEnvironment = false)
_enableRarNode = other._enableRarNode;
_buildProcessEnvironment = resetEnvironment
? CommunicationsUtilities.GetEnvironmentVariables()
: other._buildProcessEnvironment != null
? new Dictionary<string, string>(other._buildProcessEnvironment)
: null;
: other._buildProcessEnvironment;
_environmentProperties = other._environmentProperties != null ? new PropertyDictionary<ProjectPropertyInstance>(other._environmentProperties) : null;
_forwardingLoggers = other._forwardingLoggers != null ? new List<ForwardingLoggerRecord>(other._forwardingLoggers) : null;
_globalProperties = other._globalProperties != null ? new PropertyDictionary<ProjectPropertyInstance>(other._globalProperties) : null;
Expand Down Expand Up @@ -356,8 +354,7 @@ public bool UseSynchronousLogging
/// <summary>
/// Gets the environment variables which were set when this build was created.
/// </summary>
public IDictionary<string, string> BuildProcessEnvironment => new ReadOnlyDictionary<string, string>(
_buildProcessEnvironment ?? new Dictionary<string, string>(0));
public IDictionary<string, string> BuildProcessEnvironment => BuildProcessEnvironmentInternal;

/// <summary>
/// The name of the culture to use during the build.
Expand Down Expand Up @@ -720,6 +717,8 @@ internal int BuildId
set => _buildId = value;
}

internal FrozenDictionary<string, string> BuildProcessEnvironmentInternal => _buildProcessEnvironment ?? FrozenDictionary<string, string>.Empty;

/// <summary>
/// Gets or sets the environment properties.
/// </summary>
Expand Down
11 changes: 6 additions & 5 deletions src/Build/BackEnd/Components/RequestBuilder/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
Expand Down Expand Up @@ -1378,7 +1379,7 @@ private void InitializeOperatingEnvironment()
else
{
// Restore the original build environment variables.
SetEnvironmentVariableBlock(_componentHost.BuildParameters.BuildProcessEnvironment);
SetEnvironmentVariableBlock(_componentHost.BuildParameters.BuildProcessEnvironmentInternal);
}
}

Expand All @@ -1401,17 +1402,17 @@ private void RestoreOperatingEnvironment()
/// <summary>
/// Sets the environment block to the set of saved variables.
/// </summary>
private void SetEnvironmentVariableBlock(IDictionary<string, string> savedEnvironment)
private void SetEnvironmentVariableBlock(FrozenDictionary<string, string> savedEnvironment)
{
IDictionary<string, string> currentEnvironment = CommunicationsUtilities.GetEnvironmentVariables();
FrozenDictionary<string, string> currentEnvironment = CommunicationsUtilities.GetEnvironmentVariables();
ClearVariablesNotInEnvironment(savedEnvironment, currentEnvironment);
UpdateEnvironmentVariables(savedEnvironment, currentEnvironment);
}

/// <summary>
/// Clears from the current environment any variables which do not exist in the saved environment
/// </summary>
private void ClearVariablesNotInEnvironment(IDictionary<string, string> savedEnvironment, IDictionary<string, string> currentEnvironment)
private void ClearVariablesNotInEnvironment(FrozenDictionary<string, string> savedEnvironment, FrozenDictionary<string, string> currentEnvironment)
{
foreach (KeyValuePair<string, string> entry in currentEnvironment)
{
Expand All @@ -1425,7 +1426,7 @@ private void ClearVariablesNotInEnvironment(IDictionary<string, string> savedEnv
/// <summary>
/// Updates the current environment with values in the saved environment which differ or are not yet set.
/// </summary>
private void UpdateEnvironmentVariables(IDictionary<string, string> savedEnvironment, IDictionary<string, string> currentEnvironment)
private void UpdateEnvironmentVariables(FrozenDictionary<string, string> savedEnvironment, FrozenDictionary<string, string> currentEnvironment)
{
foreach (KeyValuePair<string, string> entry in savedEnvironment)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Build/BackEnd/Node/InProcNode.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Frozen;
using System.Globalization;
using System.Threading;
using Microsoft.Build.BackEnd.Components.Caching;
Expand Down Expand Up @@ -31,7 +31,7 @@ internal class InProcNode : INode, INodePacketFactory
/// <summary>
/// The environment at the time the build is started.
/// </summary>
private IDictionary<string, string> _savedEnvironment;
private FrozenDictionary<string, string> _savedEnvironment;

/// <summary>
/// The current directory at the time the build is started.
Expand Down
5 changes: 3 additions & 2 deletions src/Build/BackEnd/Node/OutOfProcNode.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class OutOfProcNode : INode, IBuildComponentHost, INodePacketFactory, INo
/// <summary>
/// The saved environment for the process.
/// </summary>
private IDictionary<string, string> _savedEnvironment;
private FrozenDictionary<string, string> _savedEnvironment;

/// <summary>
/// The component factories.
Expand Down
7 changes: 4 additions & 3 deletions src/Build/BackEnd/Shared/BuildRequestConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -131,7 +132,7 @@ internal class BuildRequestConfiguration : IEquatable<BuildRequestConfiguration>
/// <summary>
/// Holds a snapshot of the environment at the time we blocked.
/// </summary>
private Dictionary<string, string> _savedEnvironmentVariables;
private FrozenDictionary<string, string> _savedEnvironmentVariables;

/// <summary>
/// Holds a snapshot of the current working directory at the time we blocked.
Expand Down Expand Up @@ -611,7 +612,7 @@ public Lookup BaseLookup
/// <summary>
/// Holds a snapshot of the environment at the time we blocked.
/// </summary>
public Dictionary<string, string> SavedEnvironmentVariables
public FrozenDictionary<string, string> SavedEnvironmentVariables
{
get => _savedEnvironmentVariables;

Expand Down
13 changes: 8 additions & 5 deletions src/Build/BackEnd/Shared/BuildResult.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -107,7 +108,7 @@ public class BuildResult : BuildResultBase, INodePacket, IBuildResults
/// Snapshot of the environment from the configuration this results comes from.
/// This should only be populated when the configuration for this result is moved between nodes.
/// </summary>
private Dictionary<string, string>? _savedEnvironmentVariables;
private FrozenDictionary<string, string>? _savedEnvironmentVariables;

/// <summary>
/// When this key is in the dictionary <see cref="_savedEnvironmentVariables"/>, serialize the build result version.
Expand Down Expand Up @@ -437,7 +438,7 @@ NodePacketType INodePacket.Type
/// <summary>
/// Holds a snapshot of the environment at the time we blocked.
/// </summary>
Dictionary<string, string>? IBuildResults.SavedEnvironmentVariables
FrozenDictionary<string, string>? IBuildResults.SavedEnvironmentVariables
{
get => _savedEnvironmentVariables;

Expand Down Expand Up @@ -655,6 +656,7 @@ void ITranslatable.Translate(ITranslator translator)
}
else
{
IDictionary<string, string>? savedEnvironmentVariables = _savedEnvironmentVariables;
Dictionary<string, string> additionalEntries = new();

if (translator.Mode == TranslationDirection.WriteToStream)
Expand All @@ -663,15 +665,16 @@ void ITranslatable.Translate(ITranslator translator)
additionalEntries.Add(SpecialKeyForVersion, String.Empty);

// Serialize the special key together with _savedEnvironmentVariables dictionary using the workaround overload of TranslateDictionary:
translator.TranslateDictionary(ref _savedEnvironmentVariables, StringComparer.OrdinalIgnoreCase, ref additionalEntries, s_additionalEntriesKeys);
translator.TranslateDictionary(ref savedEnvironmentVariables, StringComparer.OrdinalIgnoreCase, ref additionalEntries, s_additionalEntriesKeys);

// Serialize version
translator.Translate(ref _version);
}
else if (translator.Mode == TranslationDirection.ReadFromStream)
{
// Read the dictionary using the workaround overload of TranslateDictionary: special keys (additionalEntriesKeys) would be read to additionalEntries instead of the _savedEnvironmentVariables dictionary.
translator.TranslateDictionary(ref _savedEnvironmentVariables, StringComparer.OrdinalIgnoreCase, ref additionalEntries, s_additionalEntriesKeys);
translator.TranslateDictionary(ref savedEnvironmentVariables, StringComparer.OrdinalIgnoreCase, ref additionalEntries, s_additionalEntriesKeys);
_savedEnvironmentVariables = savedEnvironmentVariables?.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);

// If the special key SpecialKeyForVersion present in additionalEntries, also read a version, otherwise set it to 0.
if (additionalEntries is not null && additionalEntries.ContainsKey(SpecialKeyForVersion))
Expand Down
3 changes: 2 additions & 1 deletion src/Build/BackEnd/Shared/IBuildResults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using Microsoft.Build.Execution;

Expand Down Expand Up @@ -32,7 +33,7 @@ internal interface IBuildResults
/// <summary>
/// Set of environment variables for the configuration this result came from
/// </summary>
Dictionary<string, string> SavedEnvironmentVariables { get; set; }
FrozenDictionary<string, string> SavedEnvironmentVariables { get; set; }

/// <summary>
/// The current directory for the configuration this result came from
Expand Down
4 changes: 2 additions & 2 deletions src/Framework/BinaryTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ public void TranslateDictionary(ref Dictionary<string, string> dictionary, IEqua
/// This overload is needed for a workaround concerning serializing BuildResult with a version.
/// It deserializes additional entries together with the main dictionary.
/// </remarks>
public void TranslateDictionary(ref Dictionary<string, string> dictionary, IEqualityComparer<string> comparer, ref Dictionary<string, string> additionalEntries, HashSet<string> additionalEntriesKeys)
public void TranslateDictionary(ref IDictionary<string, string> dictionary, IEqualityComparer<string> comparer, ref Dictionary<string, string> additionalEntries, HashSet<string> additionalEntriesKeys)
{
if (!TranslateNullable(dictionary))
{
Expand Down Expand Up @@ -1383,7 +1383,7 @@ public void TranslateDictionary(ref Dictionary<string, string> dictionary, IEqua
/// This overload is needed for a workaround concerning serializing BuildResult with a version.
/// It serializes additional entries together with the main dictionary.
/// </remarks>
public void TranslateDictionary(ref Dictionary<string, string> dictionary, IEqualityComparer<string> comparer, ref Dictionary<string, string> additionalEntries, HashSet<string> additionalEntriesKeys)
public void TranslateDictionary(ref IDictionary<string, string> dictionary, IEqualityComparer<string> comparer, ref Dictionary<string, string> additionalEntries, HashSet<string> additionalEntriesKeys)
{
// Translate whether object is null
if ((dictionary is null) && ((additionalEntries is null) || (additionalEntries.Count == 0)))
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/ITranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ void TranslateArray<T>(ref T[] array)
/// This overload is needed for a workaround concerning serializing BuildResult with a version.
/// It serializes/deserializes additional entries together with the main dictionary.
/// </remarks>
void TranslateDictionary(ref Dictionary<string, string> dictionary, IEqualityComparer<string> comparer, ref Dictionary<string, string> additionalEntries, HashSet<string> additionalEntriesKeys);
void TranslateDictionary(ref IDictionary<string, string> dictionary, IEqualityComparer<string> comparer, ref Dictionary<string, string> additionalEntries, HashSet<string> additionalEntriesKeys);

void TranslateDictionary(ref IDictionary<string, string> dictionary, NodePacketCollectionCreator<IDictionary<string, string>> collectionCreator);

Expand Down
Loading