-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Implement task environment APIs #12651
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
Merged
Merged
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 38b6b37
Update src/Framework/PathHelpers/AbsolutePath.cs
AR-May dec8a43
Have environment variables case sensitivity defined by the platform
AR-May cb3796a
Fix mac os tests failures
AR-May 3ee6525
Merge branch 'main' into implement-mt-apis
AR-May dbe28b6
Merge branch 'main' into implement-mt-apis
AR-May 3a53037
Address some PR comments.
AR-May 6e4af2d
Address PR comments - 2
AR-May e0bd256
Try to fix mac os tests.
AR-May 5a84f3b
Add more tests for absolute paths.
AR-May 5b28d23
Remove unnecessary test
AR-May 1cfba8c
fix test reseting
JanProvaznik 740d75e
fix macos symlinks
JanProvaznik 42a28ab
Address PR comments - 3
AR-May 1baaa4c
AbsolutePath naming and docs
JanProvaznik 8e97c0a
minor doc changes
JanProvaznik abb7f4f
Consolidate environment variable utilities
JanProvaznik abba41e
Add cached FrozenDictionary-based GetEnvironmentVariables for Framework
JanProvaznik 1a83cd4
add IEquatable, clarify docs of AbsolutePath
JanProvaznik dc771d0
move drivers to build from framework
JanProvaznik 4c89a50
undo environmentutilities refactors after moving drivers to build
JanProvaznik ccc8144
fun with case sensitivity of file systems
JanProvaznik 9c0a270
docs
JanProvaznik 8c162d6
env vars are case sensitive on unix
JanProvaznik 59ba721
remove unnecessary assemblyinfo
JanProvaznik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
src/Build/BackEnd/TaskExecutionHost/MultiProcessTaskEnvironmentDriver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
JanProvaznik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return CommunicationsUtilities.GetEnvironmentVariables(); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void SetEnvironmentVariable(string name, string? value) | ||
| { | ||
| CommunicationsUtilities.SetEnvironmentVariable(name, value); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public void SetEnvironment(IDictionary<string, string> newEnvironment) | ||
JanProvaznik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| CommunicationsUtilities.SetEnvironment(newEnvironment); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public ProcessStartInfo GetProcessStartInfo() | ||
| { | ||
| return new ProcessStartInfo(); | ||
JanProvaznik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
123 changes: 123 additions & 0 deletions
123
src/Build/BackEnd/TaskExecutionHost/MultiThreadedTaskEnvironmentDriver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
AR-May marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// </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, | ||
JanProvaznik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
JanProvaznik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <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; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.