fix(win): launch Store-installed PowerShell 7 in terminals (#9437)

Resolve Microsoft Store PowerShell App Execution Aliases to their real package executable before ConPTY spawn, while preserving explicit PowerShell choices and the existing fail-safe fallback chain.
This commit is contained in:
OrcaWin
2026-07-19 01:19:46 -07:00
committed by GitHub
parent e885cec154
commit 8d5c674644
2 changed files with 76 additions and 5 deletions
@@ -19,6 +19,8 @@ const WINDOWS_POWERSHELL = 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powe
// The Microsoft Store App Execution Alias stub for pwsh — a zero-byte reparse
// point under WindowsApps that ConPTY's CreateProcessW rejects with error 5.
const PWSH_STORE_ALIAS = 'C:\\Users\\dev\\AppData\\Local\\Microsoft\\WindowsApps\\pwsh.exe'
const PWSH_STORE_EXE =
'C:\\Program Files\\WindowsApps\\Microsoft.PowerShell_7.6.3.0_x64__8wekyb3d8bbwe\\pwsh.exe'
describe('resolveWindowsPowerShellExecutablePath', () => {
it('returns null on non-Windows platforms', () => {
@@ -80,6 +82,35 @@ describe('resolveWindowsPowerShellExecutablePath', () => {
expect(resolved).not.toBe(PWSH_STORE_ALIAS)
})
it('repro: resolves a Store App Execution Alias to its real package executable', () => {
const resolved = resolveWindowsPowerShellExecutablePath('pwsh.exe', {
platform: 'win32',
env: {
...WIN_ENV,
Path: 'C:\\Users\\dev\\AppData\\Local\\Microsoft\\WindowsApps'
},
isRealExecutable: (p) => p === PWSH_STORE_EXE,
resolveAppExecutionAlias: (p) => (p === PWSH_STORE_ALIAS ? PWSH_STORE_EXE : null)
})
expect(resolved).toBe(PWSH_STORE_EXE)
})
it.each(['pwsh.exe', PWSH_STORE_ALIAS, 'C:\\Tools\\powershell.exe'])(
'rejects unsafe Store alias target %s',
(target) => {
const resolved = resolveWindowsPowerShellExecutablePath('pwsh.exe', {
platform: 'win32',
env: {
...WIN_ENV,
Path: 'C:\\Users\\dev\\AppData\\Local\\Microsoft\\WindowsApps'
},
isRealExecutable: (p) => p === target,
resolveAppExecutionAlias: (p) => (p === PWSH_STORE_ALIAS ? target : null)
})
expect(resolved).toBeNull()
}
)
it('returns null for pwsh when no real executable is found', () => {
expect(
resolveWindowsPowerShellExecutablePath('pwsh.exe', {
@@ -112,6 +143,19 @@ describe('resolveWindowsPowerShellSpawnChain', () => {
expect(chain).not.toContain(PWSH_STORE_ALIAS)
})
it('uses the real Store package executable ahead of Windows PowerShell', () => {
const chain = resolveWindowsPowerShellSpawnChain('pwsh.exe', {
platform: 'win32',
env: {
...WIN_ENV,
Path: 'C:\\Users\\dev\\AppData\\Local\\Microsoft\\WindowsApps'
},
isRealExecutable: (p) => p === PWSH_STORE_EXE || p === WINDOWS_POWERSHELL,
resolveAppExecutionAlias: (p) => (p === PWSH_STORE_ALIAS ? PWSH_STORE_EXE : null)
})
expect(chain).toEqual([PWSH_STORE_EXE, WINDOWS_POWERSHELL, WIN_ENV.ComSpec])
})
it('orders PATH-resolved pwsh before Windows PowerShell when standard roots miss', () => {
const chain = resolveWindowsPowerShellSpawnChain('pwsh.exe', {
platform: 'win32',
@@ -1,4 +1,4 @@
import { existsSync, statSync } from 'node:fs'
import { existsSync, readlinkSync, statSync } from 'node:fs'
import { win32 as pathWin32 } from 'node:path'
/** Dependency seams so the resolver can be unit-tested without a real Windows
@@ -7,6 +7,8 @@ export type WindowsPowerShellResolveOptions = {
env?: NodeJS.ProcessEnv
/** Returns true when the candidate path is a real, non-empty executable. */
isRealExecutable?: (path: string) => boolean
/** Resolves a Store App Execution Alias to its package executable. */
resolveAppExecutionAlias?: (path: string) => string | null
platform?: NodeJS.Platform
}
@@ -37,6 +39,15 @@ function defaultIsRealExecutable(candidate: string): boolean {
}
}
function defaultResolveAppExecutionAlias(candidate: string): string | null {
try {
const target = readlinkSync(candidate)
return pathWin32.resolve(pathWin32.dirname(candidate), target)
} catch {
return null
}
}
function readEnv(env: NodeJS.ProcessEnv, names: string[]): string | undefined {
for (const name of names) {
const value = env[name]
@@ -140,9 +151,8 @@ export function getWindowsCmdPath(env: NodeJS.ProcessEnv = process.env): string
/**
* Resolve a PowerShell family name to a real absolute executable path.
*
* Returns null when no real executable can be found for the family (e.g. pwsh.exe
* is only present as a Store App Execution Alias stub). Callers fall back to the
* next link in the Windows shell chain when this returns null.
* Returns null when no real executable or safely resolvable Store alias target
* can be found for the family. Callers then try the next Windows shell fallback.
*/
export function resolveWindowsPowerShellExecutablePath(
family: 'pwsh.exe' | 'powershell.exe',
@@ -154,6 +164,8 @@ export function resolveWindowsPowerShellExecutablePath(
}
const env = options.env ?? process.env
const isRealExecutable = options.isRealExecutable ?? defaultIsRealExecutable
const resolveAppExecutionAlias =
options.resolveAppExecutionAlias ?? defaultResolveAppExecutionAlias
if (family === 'powershell.exe') {
const windowsPowerShell = getWindowsPowerShellPath(env)
@@ -161,7 +173,22 @@ export function resolveWindowsPowerShellExecutablePath(
}
for (const candidate of getPwshCandidatePaths(env)) {
if (!isWindowsAppExecutionAliasPath(candidate) && isRealExecutable(candidate)) {
if (isWindowsAppExecutionAliasPath(candidate)) {
const target = resolveAppExecutionAlias(candidate)
// Why: ConPTY cannot launch the alias reparse point, but its real Store
// package target is a normal executable that preserves the user's pwsh choice.
if (
target &&
pathWin32.isAbsolute(target) &&
pathWin32.basename(target).toLowerCase() === 'pwsh.exe' &&
!isWindowsAppExecutionAliasPath(target) &&
isRealExecutable(target)
) {
return pathWin32.normalize(target)
}
continue
}
if (isRealExecutable(candidate)) {
return candidate
}
}