-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Refresh PATH in configuration remoting server #5511
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
4 commits
Select commit
Hold shift + click to select a range
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
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
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
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
69 changes: 69 additions & 0 deletions
69
src/Microsoft.Management.Configuration.Processor/Public/PathEnvironmentVariableHandler.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,69 @@ | ||
// ----------------------------------------------------------------------------- | ||
// <copyright file="PathEnvironmentVariableHandler.cs" company="Microsoft Corporation"> | ||
// Copyright (c) Microsoft Corporation. Licensed under the MIT License. | ||
// </copyright> | ||
// ----------------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Management.Configuration.Processor.Helpers | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
/// <summary> | ||
/// Class for handling PATH environment variable. | ||
/// </summary> | ||
public static class PathEnvironmentVariableHandler | ||
{ | ||
private const string PathEnvironmentVariable = "PATH"; | ||
|
||
private static readonly object EnvironmentVariableLock = new object(); | ||
|
||
/// <summary> | ||
/// Gets the lock to read or write PATH environment variable. | ||
/// </summary> | ||
public static object Lock | ||
{ | ||
get { return EnvironmentVariableLock; } | ||
} | ||
|
||
/// <summary> | ||
/// Updates the process's PATH environment variable if new paths added. | ||
/// Only adds new paths since we add to PATH in other code which may not be in the registry. | ||
/// </summary> | ||
public static void UpdatePath() | ||
{ | ||
HashSet<string> paths = new HashSet<string>(Environment.GetEnvironmentVariable(PathEnvironmentVariable)?.Split(';') ?? Array.Empty<string>()); | ||
var originalPathsSize = paths.Count; | ||
|
||
AddPathsIfNotExist(paths, Environment.GetEnvironmentVariable(PathEnvironmentVariable, EnvironmentVariableTarget.Machine)?.Split(';')); | ||
AddPathsIfNotExist(paths, Environment.GetEnvironmentVariable(PathEnvironmentVariable, EnvironmentVariableTarget.User)?.Split(';')); | ||
|
||
if (paths.Count > originalPathsSize) | ||
{ | ||
lock (Lock) | ||
{ | ||
Environment.SetEnvironmentVariable(PathEnvironmentVariable, string.Join(';', paths)); | ||
} | ||
} | ||
} | ||
|
||
// TODO: Currently it always adds new paths to the end. The "proper" thing to do would probably be to calculate | ||
// the full new list of paths (what one would expect to get from a new process launch) and use a line merge algorithm | ||
// with a strategy that puts the ephemeral entries before the new permanent ones. | ||
#pragma warning disable SA1011 // Closing square brackets should be spaced correctly | ||
private static void AddPathsIfNotExist(HashSet<string> currentPaths, string[]? paths) | ||
#pragma warning restore SA1011 // Closing square brackets should be spaced correctly | ||
{ | ||
if (paths is not null) | ||
{ | ||
foreach (var path in paths) | ||
{ | ||
if (!currentPaths.Contains(path)) | ||
{ | ||
currentPaths.Add(path); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This always adds to the end, making it the last location to look. That may cause issues in some corner case; probably ok for now but maybe add a comment.
The "proper" thing to do would probably be to calculate the full new list of paths (what one would expect to get from a new process launch) and use a line merge algorithm with a strategy that puts the ephemeral entries before the new permanent ones. But that is a fair bit more complicated and could wait until later.