diff --git a/packages/core/src/shell/select.ts b/packages/core/src/shell/select.ts index 7d7a9d0fcbf..816c395807a 100644 --- a/packages/core/src/shell/select.ts +++ b/packages/core/src/shell/select.ts @@ -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 = { 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 diff --git a/packages/core/src/util/which.ts b/packages/core/src/util/which.ts index a59d9000cb9..ab84accd8e8 100644 --- a/packages/core/src/util/which.ts +++ b/packages/core/src/util/which.ts @@ -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 + ) } diff --git a/packages/core/test/fixture/app-execution-alias.ts b/packages/core/test/fixture/app-execution-alias.ts new file mode 100644 index 00000000000..153f58f74ee --- /dev/null +++ b/packages/core/test/fixture/app-execution-alias.ts @@ -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 +} diff --git a/packages/core/test/shell.test.ts b/packages/core/test/shell.test.ts index 204d6e146d7..9218dbbf2bd 100644 --- a/packages/core/test/shell.test.ts +++ b/packages/core/test/shell.test.ts @@ -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) => { 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) + }) } }) diff --git a/packages/core/test/util/which.test.ts b/packages/core/test/util/which.test.ts index bbf8821e406..d19b4b11617 100644 --- a/packages/core/test/util/which.test.ts +++ b/packages/core/test/util/which.test.ts @@ -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