Skip to content

Stop altering the PATH env variable by honoring the PSHome path in CommandDiscovery - #27757

Closed
Dongbo Wang (daxian-dbw) wants to merge 10 commits into
PowerShell:masterfrom
daxian-dbw:env-path
Closed

Dongbo Wang (daxian-dbw) wants to merge 10 commits into
PowerShell:masterfrom
daxian-dbw:env-path

Conversation

@daxian-dbw

@daxian-dbw Dongbo Wang (daxian-dbw) commented Aug 3, 2026 •

Copy link
Copy Markdown
Member

Context

Altering the PATH env variable causes a problem to cmake-based build system when running in the MSIX PowerShell installation because they cache the location of PowerShell sometimes.

At startup, PowerShell adds $PSHOME to the beginning of PATH, and for MSIX installation, $PSHOME contains version numbers that change when PowerShell is updated.

When cmake is started from MSIX PowerShell, the path it caches will be that $PSHOME, which will become invalid after an update of the MSIX PowerShell.

PR Summary

The main change of this PR is to move the logic for ensuring the current PowerShell's executable directory ($PSHOME) is prioritized in command lookup from prepend it to PATH at startup to the CommandDiscovery process itself. This results in a more accurate and less intrusive handling of PATH. The PR also improves test coverage for these scenarios.

Refactoring of PATH handling:

  • The logic for prepending PSHOME to the PATH environment variable is removed from ConsoleHost.cs, so PowerShell no longer alters PATH during startup.
  • The logic to ensure PSHOME is always the first lookup directory is now implemented in CommandDiscovery.cs within the GetLookupDirectoryPaths method, regardless of the state of the PATH variable. This includes handling cases where PATH is unset and expanding ~ to the user home directory.
  • Skip the duplicate path of _psHome when constructing _cachedLookupPaths.

Code cleanup and caching improvements:

  • Removed the _cachedPath field and streamlined the caching logic for lookup paths in CommandDiscovery.cs.

Test updates:

  • Updated and added tests in ConsoleHost.Tests.ps1 to verify that running pwsh always starts the current version and that PATH remains unaltered during startup, including when PATH is unset.

PR Checklist

@daxian-dbw
Dongbo Wang (daxian-dbw) requested a review from a team as a code owner August 3, 2026 21:56
Copilot AI review requested due to automatic review settings August 3, 2026 21:56
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR stops PowerShell from modifying the PATH environment variable at startup and instead ensures the current PowerShell installation directory ($PSHOME) is prioritized during command lookup inside CommandDiscovery, addressing issues with tools (e.g., CMake) caching an MSIX-versioned pwsh path. It also updates/extends tests to validate pwsh resolution and behavior when PATH is unset.

Changes:

  • Removed startup-time PATH mutation from ConsoleHost.
  • Updated CommandDiscovery.GetLookupDirectoryPaths() to always prioritize the current pwsh location (including global tool path correction) without editing PATH.
  • Updated Pester tests to validate pwsh version resolution and that startup does not populate PATH when it is unset.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs Removes logic that prepended $PSHOME to PATH during startup.
src/System.Management.Automation/engine/CommandDiscovery.cs Prioritizes $PSHOME in lookup paths and streamlines path caching/tilde expansion.
test/powershell/Host/ConsoleHost.Tests.ps1 Adjusts/adds tests for pwsh resolution and verifies PATH remains unaltered when unset.

Comment thread test/powershell/Host/ConsoleHost.Tests.ps1 Outdated
Comment thread test/powershell/Host/ConsoleHost.Tests.ps1
Comment thread test/powershell/Host/ConsoleHost.Tests.ps1 Outdated
Comment thread src/System.Management.Automation/engine/CommandDiscovery.cs Outdated
@daxian-dbw Dongbo Wang (daxian-dbw) added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Aug 3, 2026
Comment thread src/System.Management.Automation/engine/CommandDiscovery.cs
@jshigetomi

Justin Chung (jshigetomi) commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Would this effect embedded pwsh runspaces and their command discovery look up order? For embedded pwsh, I believe the location of SMA.dll would be the pshome that gets prepended to the path by command discovery.

Any name collision in that SMA.dll location would be unintended. I can't imagine this happening often.

@daxian-dbw

Dongbo Wang (daxian-dbw) commented Aug 4, 2026 •

Copy link
Copy Markdown
Member Author

That's a good point Justin Chung (@jshigetomi). Let me think about the impact to the applications that host PowerShell using the NuGet packages.

@daxian-dbw

Copy link
Copy Markdown
Member Author

Justin Chung (@jshigetomi) I've updated the changes to take into account the "PowerShell hosted by application via NuGet packages" scenario. In that case, we will use the PATH as is. Can you please review again?

@jshigetomi Justin Chung (jshigetomi) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

Comment thread src/System.Management.Automation/engine/CommandDiscovery.cs Outdated
@daxian-dbw
Dongbo Wang (daxian-dbw) marked this pull request as draft August 7, 2026 18:31
@daxian-dbw

Copy link
Copy Markdown
Member Author

Converted this PR to draft.
There are 2 concerns about this change:

  1. It will cause behavior change to console apps started from pwsh. For example, when you use fzf with a preview command that launches pwsh, it may be using a different pwsh after this change.
  2. Do it in command discovery adds a bit of magic to gcm pwsh. If something went wrong, it'd be hard for a user to reason the root cause.

I submitted another draft PR at #27782 as a different approach for solving the cmake issue stemmed from MSIX installation. Ilya (@iSazonov), please take a look and share your thoughts.

@daxian-dbw

Dongbo Wang (daxian-dbw) commented Aug 10, 2026 •

Copy link
Copy Markdown
Member Author
  1. Stop altering the PATH env variable by honoring the PSHome path in CommandDiscovery by daxian-dbw · Pull Request #27757 · PowerShell/PowerShell

    • Pros:
      • Less work during startup.
      • No PATH change so less intrusive.
    • Cons:
      • Behavior change for console app started from pwsh. For example, when you use ‘fzf’ with a preview command that launches ‘pwsh’, it may use a different pwsh after this change.
      • "gcm pwsh" acts with a bit of magic after the change.
      • A nested ‘pwsh’ will inherit the PSModulePath of the top-level ‘pwsh’ and inserts its PSHOME module path before the top-level pwsh’s PSHOME module path in majority cases, but it’s still possible for a user to mess with the PSModulePath with the intent to break the nested ‘pwsh’
  2. Handle MSIX installation specially when prepend to PATH by daxian-dbw · Pull Request #27782 · PowerShell/PowerShell

    • Pros:
      • No behavior change to console app started from pwsh.
      • No magic to “gcm pwsh”
      • Less possible for a nested pwsh to go wrong with the PSModulePath
    • Cons:
      • More work during startup
      • More future issues caused by altering PATH? Not sure about it.

The team discussed the pros/cons of those 2 approaches and decided to go with #27782, because it's less risky.
So, closing this PR.

@daxian-dbw
Dongbo Wang (daxian-dbw) deleted the env-path branch August 10, 2026 22:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants