fix(core): find Microsoft Store PowerShell 7 on Windows (#61952)

This commit is contained in:
Luke Parker
2026-11-04 23:51:41 -06:00
committed by GitHub
Unverified
parent 8fd7c61cf4
commit b6cfbec4cc
5 changed files with 65 additions and 7 deletions
+3 -2
View File
@@ -8,7 +8,7 @@ import { makeLocationNode } from "@opencode/util/effect/app-node"
import { FSUtil } from "@opencode/util/fs-util"
import { Global } from "@opencode/util/global"
import { State } from "../state.js"
import { which } from "../util/which.js"
import { isAppExecutionAlias, which } from "../util/which.js"
const META: Record<string, { deny?: boolean; login?: boolean; ps?: boolean }> = {
bash: { login: true },
@@ -86,7 +86,8 @@ function rooted(file: string) {
function executable(file: string, options?: Options, bin?: string) {
const shell = full(file, options, bin)
if (rooted(shell)) {
if (stat(shell)?.isFile()) return shell
// Check the alias first: stat throws EACCES for it.
if (isAppExecutionAlias(shell) || stat(shell)?.isFile()) return shell
return
}
return findExecutable(shell, bin) ?? undefined
+30 -5
View File
@@ -1,12 +1,37 @@
import whichPkg from "which"
import path from "path"
import { existsSync, lstatSync } from "fs"
export function which(cmd: string, env?: NodeJS.ProcessEnv, bin?: string) {
const base = env?.PATH ?? env?.Path ?? process.env.PATH ?? process.env.Path ?? ""
const full = base && bin ? base + path.delimiter + bin : base || bin
return whichPkg.sync(cmd, {
nothrow: true,
path: full,
pathExt: env?.PATHEXT ?? env?.PathExt ?? process.env.PATHEXT ?? process.env.PathExt,
})
return (
whichPkg.sync(cmd, {
nothrow: true,
path: full,
pathExt: env?.PATHEXT ?? env?.PathExt ?? process.env.PATHEXT ?? process.env.PathExt,
}) ?? appExecutionAlias(cmd, full)
)
}
// Microsoft Store apps, such as PowerShell 7, put app execution aliases in WindowsApps on PATH. They launch like
// any executable, but stat rejects them with EACCES, so `which` skips them.
export function isAppExecutionAlias(file: string) {
return (
process.platform === "win32" &&
lstatSync(file, { throwIfNoEntry: false })?.isSymbolicLink() === true &&
existsSync(file)
)
}
function appExecutionAlias(cmd: string, search: string | undefined) {
if (process.platform !== "win32" || !search || path.win32.dirname(cmd) !== ".") return null
const name = path.win32.extname(cmd).toLowerCase() === ".exe" ? cmd : cmd + ".exe"
return (
search
.split(path.win32.delimiter)
.filter(Boolean)
.map((dir) => path.win32.join(dir.replace(/^"(.*)"$/, "$1"), name))
.find(isAppExecutionAlias) ?? null
)
}
@@ -0,0 +1,15 @@
import { existsSync, lstatSync, readdirSync } from "fs"
import path from "path"
// Tests cannot create an app execution alias (an IO_REPARSE_TAG_APPEXECLINK reparse point), so they borrow one that
// a Microsoft Store app installed for the current user. Returns undefined when the machine has none.
export function appExecutionAlias() {
if (process.platform !== "win32" || !process.env.LOCALAPPDATA) return
const dir = path.join(process.env.LOCALAPPDATA, "Microsoft", "WindowsApps")
if (!existsSync(dir)) return
const name = readdirSync(dir, { withFileTypes: true })
.filter((entry) => entry.name.toLowerCase().endsWith(".exe"))
.map((entry) => entry.name)
.find((entry) => lstatSync(path.join(dir, entry), { throwIfNoEntry: false })?.isSymbolicLink())
return name ? { dir, name, file: path.join(dir, name) } : undefined
}
+9
View File
@@ -4,8 +4,11 @@ import { ShellSelect } from "@opencode/core/shell/select"
import { FSUtil } from "@opencode/util/fs-util"
import { which } from "@opencode/core/util/which"
import fs from "node:fs/promises"
import { appExecutionAlias } from "./fixture/app-execution-alias"
import { tmpdir } from "./fixture/tmpdir"
const alias = appExecutionAlias()
const withShell = async (shell: string | undefined, fn: () => void | Promise<void>) => {
const prev = process.env.SHELL
if (shell === undefined) delete process.env.SHELL
@@ -119,5 +122,11 @@ describe("shell", () => {
expect(ShellSelect.resolve({ priority: "config" })).toBe(shell)
})
})
// Store installs of PowerShell 7 are app execution aliases; stat throws EACCES for them. Skipped where no
// Microsoft Store app is installed for the user; see the fixture.
test.skipIf(!alias)("resolves a configured app execution alias", () => {
expect(ShellSelect.resolve({ priority: "config" }, alias!.file)).toBe(alias!.file)
})
}
})
+8
View File
@@ -2,8 +2,11 @@ import { describe, expect, test } from "bun:test"
import fs from "fs/promises"
import path from "path"
import { which } from "@opencode/core/util/which"
import { appExecutionAlias } from "../fixture/app-execution-alias"
import { tmpdir } from "../fixture/tmpdir"
const alias = appExecutionAlias()
async function cmd(dir: string, name: string, exec = true) {
const ext = process.platform === "win32" ? ".cmd" : ""
const file = path.join(dir, name + ext)
@@ -87,6 +90,11 @@ describe("util.which", () => {
expect(which("pathext", { PATH: bin, PATHEXT: ".CMD" })).toBe(file)
})
// Skipped where no Microsoft Store app is installed for the user; see the fixture.
test.skipIf(!alias)("finds a Microsoft Store app execution alias on windows", () => {
same(which(path.parse(alias!.name).name, env(alias!.dir)), alias!.file)
})
test("uses Windows Path casing fallback", async () => {
if (process.platform !== "win32") return