Skip to content

Handle MSIX installation specially when prepend to PATH - #27782

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

Handle MSIX installation specially when prepend to PATH#27782
Dongbo Wang (daxian-dbw) wants to merge 2 commits into
PowerShell:masterfrom
daxian-dbw:env-path-2

Conversation

@daxian-dbw

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

Copy link
Copy Markdown
Member

Context

Prepend $PSHOME to PATH env variable at startup causes a problem to cmake-based build system when it runs in the MSIX PowerShell installation because it caches the location of PowerShell on its first run from within PowerShell.

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 for the 1st time from MSIX PowerShell, the path it caches will be that $PSHOME, which will become invalid after an update of the MSIX PowerShell.

PR Summary

This PR updated the code that prepend $PSHOME to PATH. It now handles the MSIX package installation specially -- it uses the directory that contains the ExecutionAlias of the MSIX installation instead of $PSHOME. For example:

Microsoft.PowerShell -> $env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.PowerShell_8wekyb3d8bbwe
Microsoft.PowerShell-LTS -> $env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.PowerShell-LTS_8wekyb3d8bbwe
Microsoft.PowerShellPreview -> $env:LOCALAPPDATA\Microsoft\WindowsApps\Microsoft.PowerShellPreview_8wekyb3d8bbwe

Those are the stable paths that contain the pwsh.exe alias pointing to corresponding channels of MSIX. They won't change when the MSIX packages get updated.

PR Checklist

@azure-pipelines

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

@daxian-dbw Dongbo Wang (daxian-dbw) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Aug 7, 2026

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 adjusts how ConsoleHost prepends the PowerShell executable location to PATH at startup, with special handling for MSIX installs so that pwsh resolves to a stable path (not a versioned MSIX package folder), preventing downstream tools (e.g., CMake) from caching an update-volatile path.

Changes:

  • Replace $PSHOME-based PATH prepending with a computed “pwsh executable home” via GetPSExecutableHome().
  • Add MSIX-specific path stabilization (ResolveStablePathIfMsix) to prefer the WindowsApps execution-alias directory over the versioned package directory.
  • Update the console host PATH test to invoke pwsh -v (via command resolution) instead of invoking the known $powershell path directly.

Reviewed changes

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

File Description
src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs Computes the executable home path for PATH-prepending and adds MSIX-specific stable-path resolution.
test/powershell/Host/ConsoleHost.Tests.ps1 Adjusts the PATH behavior test to validate pwsh command resolution matches the current running build.

Comment on lines +383 to +388
string psExePath = Environment.ProcessPath;
string psExeHome = Path.GetDirectoryName(psExePath);
string processName = Path.GetFileName(psExePath);

// Use 'Environment.ProcessPath' if it points to 'pwsh.exe' or 'pwsh'.
if (pwshName.Equals(processName, StringComparison.Ordinal))
Comment on lines +442 to +444
/// That path contains a version string and will change with every update. Prepend that path to the PATH environment variable
/// caused a problem to the cmake-based build system, where cmake cached the path to 'pwsh.exe' when running for the 1st time
/// from the MSIX PowerShell. That cached path became invalid after the MSIX PowerShell got updated, which broke cmake.
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.

2 participants