Skip to content

Update PATH envrionment variable for package manager executable on Windows #25847

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 9 commits into from
Aug 20, 2025

Conversation

daxian-dbw
Copy link
Member

@daxian-dbw daxian-dbw commented Aug 11, 2025

PR Summary

Implementation for PowerShell/PowerShell-RFC#391. For the PS team members, here is the internal PR for the equivalent change in CMD: https://microsoft.visualstudio.com/OS/_git/os.2020/pullrequest/10131240

A few implementation decisions:

  1. Check the package manager list only once per process (same as CMD). So, changing the package manager list in registry doesn't affect a PowerShell session that is already running.

  2. When detecting user/system PATH value changes, only check new items PREPENDED or APPENDED to the original string (same as CMD). The detection is made simple, which is not intended to detect complex changes to the user/system PATH.

  3. The PATH update feature is disabled when the Environment provider is not available, or when the current session is restricted.

    • Both Environment provider and IsSessionRestricted are checked only once per PowerShell session.
    • Use ConditionalWeakTable to cache whether the feature is enabled or not for a given session.
    • We cannot use thread-static variable for the caching because it's possible for Runspace to reuse threads.

Tests have been added.

PR Checklist

@daxian-dbw daxian-dbw added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Aug 11, 2025
@daxian-dbw daxian-dbw marked this pull request as ready for review August 14, 2025 01:31
@daxian-dbw daxian-dbw requested review from jshigetomi and a team as code owners August 14, 2025 01:31
@daxian-dbw
Copy link
Member Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@iSazonov
Copy link
Collaborator

Re-run failed MacOS CIs...

@daxian-dbw
Copy link
Member Author

It constantly fails, and the macOS CI in #25864 fails the same way. I will check with @TravisEz13 tomorrow to find out why.

Copy link
Collaborator

@theJasonHelmick theJasonHelmick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@adityapatwardhan adityapatwardhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@TravisEz13 TravisEz13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a suggestion

@daxian-dbw
Copy link
Member Author

daxian-dbw commented Aug 19, 2025

A Runspace in PowerShell could reuse the same thread, so a thread-static variable is not guaranteed to work as expected.
Instead, I changed to use ConditionalWeakTable with ExecutionContext as the key to cache whether the "Path Update" feature is enabled for a given session.

}

// Disable Path Update if 'EnvironmentProvider' is disabled in the current session, or the current session is restricted.
bool enabled = context.EngineSessionState.Providers.ContainsKey(EnvironmentProvider.ProviderName)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which brings us back to the question of is the cache worth it? In my opinion, containsKey is much easier than ConditionalWeakTable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now there is also a call to Utils.IsSessionRestricted(context) for the check, which does a command lookup. And it's possible to have more checks in future depending on the need in restricted scenarios.

@daxian-dbw
Copy link
Member Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@daxian-dbw daxian-dbw merged commit d68c15e into PowerShell:master Aug 20, 2025
46 of 49 checks passed
Copy link
Contributor

microsoft-github-policy-service bot commented Aug 20, 2025

📣 Hey @@daxian-dbw, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@daxian-dbw daxian-dbw deleted the path_update branch August 20, 2025 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants