Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
521b9c3
Implement task environment APIs
AR-May Oct 15, 2025
38b6b37
Update src/Framework/PathHelpers/AbsolutePath.cs
AR-May Oct 15, 2025
dec8a43
Have environment variables case sensitivity defined by the platform
AR-May Oct 16, 2025
cb3796a
Fix mac os tests failures
AR-May Oct 16, 2025
3ee6525
Merge branch 'main' into implement-mt-apis
AR-May Oct 16, 2025
dbe28b6
Merge branch 'main' into implement-mt-apis
AR-May Nov 21, 2025
3a53037
Address some PR comments.
AR-May Nov 21, 2025
6e4af2d
Address PR comments - 2
AR-May Nov 24, 2025
e0bd256
Try to fix mac os tests.
AR-May Nov 24, 2025
5a84f3b
Add more tests for absolute paths.
AR-May Nov 24, 2025
5b28d23
Remove unnecessary test
AR-May Nov 24, 2025
1cfba8c
fix test reseting
JanProvaznik Nov 24, 2025
740d75e
fix macos symlinks
JanProvaznik Nov 25, 2025
42a28ab
Address PR comments - 3
AR-May Nov 25, 2025
1baaa4c
AbsolutePath naming and docs
JanProvaznik Nov 26, 2025
8e97c0a
minor doc changes
JanProvaznik Nov 26, 2025
abb7f4f
Consolidate environment variable utilities
JanProvaznik Nov 26, 2025
abba41e
Add cached FrozenDictionary-based GetEnvironmentVariables for Framework
JanProvaznik Nov 26, 2025
1a83cd4
add IEquatable, clarify docs of AbsolutePath
JanProvaznik Nov 26, 2025
dc771d0
move drivers to build from framework
JanProvaznik Nov 26, 2025
4c89a50
undo environmentutilities refactors after moving drivers to build
JanProvaznik Nov 26, 2025
ccc8144
fun with case sensitivity of file systems
JanProvaznik Nov 26, 2025
9c0a270
docs
JanProvaznik Nov 26, 2025
8c162d6
env vars are case sensitive on unix
JanProvaznik Nov 27, 2025
59ba721
remove unnecessary assemblyinfo
JanProvaznik Nov 27, 2025
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
8 changes: 5 additions & 3 deletions documentation/specs/multithreading/thread-safe-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,17 @@ To prevent common thread-safety issues related to path handling, we introduce pa

```csharp
namespace Microsoft.Build.Framework;
public readonly struct AbsolutePath
public readonly struct AbsolutePath : IEquatable<AbsolutePath>
{
// Default value returns string.Empty for Path property
public string Path { get; }
public string Value { get; }
internal AbsolutePath(string path, bool ignoreRootedCheck) { }
public AbsolutePath(string path); // Checks Path.IsPathRooted
public AbsolutePath(string path, AbsolutePath basePath) { }
public static implicit operator string(AbsolutePath path) { }
public override string ToString() => Path;
public override string ToString() => Value;

// overrides for equality and hashcode
}
```

Expand Down
410 changes: 410 additions & 0 deletions src/Build.UnitTests/BackEnd/TaskEnvironment_Tests.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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.Generic;
using System.Diagnostics;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Internal;

namespace Microsoft.Build.BackEnd
{
/// <summary>
/// Default implementation of <see cref="ITaskEnvironmentDriver"/> that directly interacts with the file system
/// and environment variables. Used in multi-process mode of execution.
/// </summary>
/// <remarks>
/// Implemented as a singleton since it has no instance state.
/// </remarks>
internal sealed class MultiProcessTaskEnvironmentDriver : ITaskEnvironmentDriver
{
/// <summary>
/// The singleton instance.
/// </summary>
private static readonly MultiProcessTaskEnvironmentDriver s_instance = new MultiProcessTaskEnvironmentDriver();

/// <summary>
/// Gets the singleton instance of <see cref="MultiProcessTaskEnvironmentDriver"/>.
/// </summary>
public static MultiProcessTaskEnvironmentDriver Instance => s_instance;

private MultiProcessTaskEnvironmentDriver() { }

/// <inheritdoc/>
public AbsolutePath ProjectDirectory
{
get => new AbsolutePath(Directory.GetCurrentDirectory(), ignoreRootedCheck: true);
set => Directory.SetCurrentDirectory(value.Value);
}

/// <inheritdoc/>
public AbsolutePath GetAbsolutePath(string path)
{
return new AbsolutePath(Path.GetFullPath(path), ignoreRootedCheck: true);
}

/// <inheritdoc/>
public string? GetEnvironmentVariable(string name)
{
return Environment.GetEnvironmentVariable(name);
}

/// <inheritdoc/>
public IReadOnlyDictionary<string, string> GetEnvironmentVariables()
{
return CommunicationsUtilities.GetEnvironmentVariables();
}

/// <inheritdoc/>
public void SetEnvironmentVariable(string name, string? value)
{
CommunicationsUtilities.SetEnvironmentVariable(name, value);
}

/// <inheritdoc/>
public void SetEnvironment(IDictionary<string, string> newEnvironment)
{
CommunicationsUtilities.SetEnvironment(newEnvironment);
}

/// <inheritdoc/>
public ProcessStartInfo GetProcessStartInfo()
{
return new ProcessStartInfo();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// 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;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Build.Framework;
using Microsoft.Build.Internal;

namespace Microsoft.Build.BackEnd
{
/// <summary>
/// Implementation of <see cref="ITaskEnvironmentDriver"/> that virtualizes environment variables and current directory
/// for use in multithreaded mode where tasks may be executed in parallel. This allows each project to maintain its own
/// isolated environment state without affecting other concurrently building projects.
/// </summary>
/// <remarks>
/// This class is not accessed from multiple threads. Each msbuild thread node has its own instance to work with.
/// </remarks>
internal sealed class MultiThreadedTaskEnvironmentDriver : ITaskEnvironmentDriver
{
private readonly Dictionary<string, string> _environmentVariables;
private AbsolutePath _currentDirectory;

/// <summary>
/// Initializes a new instance of the <see cref="MultiThreadedTaskEnvironmentDriver"/> class
/// with the specified working directory and optional environment variables.
/// </summary>
/// <param name="currentDirectoryFullPath">The initial working directory.</param>
/// <param name="environmentVariables">Dictionary of environment variables to use.</param>
public MultiThreadedTaskEnvironmentDriver(
string currentDirectoryFullPath,
IDictionary<string, string> environmentVariables)
{
_environmentVariables = new Dictionary<string, string>(environmentVariables, CommunicationsUtilities.EnvironmentVariableComparer);
_currentDirectory = new AbsolutePath(currentDirectoryFullPath, ignoreRootedCheck: true);
}

/// <summary>
/// Initializes a new instance of the <see cref="MultiThreadedTaskEnvironmentDriver"/> class
/// with the specified working directory and environment variables from the current process.
/// </summary>
/// <param name="currentDirectoryFullPath">The initial working directory.</param>
public MultiThreadedTaskEnvironmentDriver(string currentDirectoryFullPath)
{
IDictionary variables = Environment.GetEnvironmentVariables();
_environmentVariables = new Dictionary<string, string>(variables.Count, CommunicationsUtilities.EnvironmentVariableComparer);
foreach (DictionaryEntry entry in variables)
{
_environmentVariables[(string)entry.Key] = (string)entry.Value!;
}

_currentDirectory = new AbsolutePath(currentDirectoryFullPath, ignoreRootedCheck: true);
}

/// <inheritdoc/>
public AbsolutePath ProjectDirectory
{
get => _currentDirectory;
set => _currentDirectory = value;
}

/// <inheritdoc/>
public AbsolutePath GetAbsolutePath(string path)
{
return new AbsolutePath(path, ProjectDirectory);
}

/// <inheritdoc/>
public string? GetEnvironmentVariable(string name)
{
return _environmentVariables.TryGetValue(name, out string? value) ? value : null;
}

/// <inheritdoc/>
public IReadOnlyDictionary<string, string> GetEnvironmentVariables()
{
return _environmentVariables;
}

/// <inheritdoc/>
public void SetEnvironmentVariable(string name, string? value)
{
if (value == null)
{
_environmentVariables.Remove(name);
}
else
{
_environmentVariables[name] = value;
}
}

/// <inheritdoc/>
public void SetEnvironment(IDictionary<string, string> newEnvironment)
{
// Simply replace the entire environment dictionary
_environmentVariables.Clear();
foreach (KeyValuePair<string, string> entry in newEnvironment)
{
_environmentVariables[entry.Key] = entry.Value;
}
}

/// <inheritdoc/>
public ProcessStartInfo GetProcessStartInfo()
{
var startInfo = new ProcessStartInfo
{
WorkingDirectory = ProjectDirectory.Value
};

// Set environment variables
foreach (var kvp in _environmentVariables)
{
startInfo.EnvironmentVariables[kvp.Key] = kvp.Value;
}

return startInfo;
}
}
}
2 changes: 2 additions & 0 deletions src/Build/Microsoft.Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@
<Compile Include="BackEnd\Node\INode.cs" />
<!-- ########################## -->
<Compile Include="BackEnd\TaskExecutionHost\TaskExecutionHost.cs" />
<Compile Include="BackEnd\TaskExecutionHost\MultiProcessTaskEnvironmentDriver.cs" />
<Compile Include="BackEnd\TaskExecutionHost\MultiThreadedTaskEnvironmentDriver.cs" />
<!-- #### COLLECTIONS ### -->
<Compile Include="..\Shared\CollectionHelpers.cs" />
<Compile Include="Collections\ArrayDictionary.cs" />
Expand Down
Loading