-
Notifications
You must be signed in to change notification settings - Fork 3.1k
fix: prefer PowerShell 7 over legacy Windows PowerShell 5.1 #13365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
81d3649
test(core): expect powershell 7 selection over legacy 5.1 on windows
marius-kilocode 98ea338
fix: prefer powershell 7 over legacy 5.1 on windows
marius-kilocode 7669119
fix: annotate pwsh probe line for kilocode_change checker
marius-kilocode 06d20fb
fix: repair shell selection syntax after marker edit
marius-kilocode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "@kilocode/cli": patch | ||
| "kilo-code": patch | ||
| --- | ||
|
|
||
| Prefer PowerShell 7 over legacy Windows PowerShell 5.1 when running agent commands on Windows. PowerShell 7 installs are now found even when `pwsh` is missing from PATH, Agent Manager setup and run scripts launch pwsh when available, and an explicit `shell` in kilo.json still overrides detection. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "fs" | ||
| import { tmpdir } from "os" | ||
| import path from "path" | ||
| import { Shell } from "@opencode-ai/core/shell" | ||
| import { PowerShell } from "@opencode-ai/core/kilocode/powershell" | ||
| import { which } from "@opencode-ai/core/util/which" | ||
|
|
||
| const LEGACY = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" | ||
|
|
||
| const knownLocations = () => { | ||
| const roots = [ | ||
| process.env.ProgramFiles && path.join(process.env.ProgramFiles, "PowerShell", "7"), | ||
| process.env["ProgramFiles(x86)"] && path.join(process.env["ProgramFiles(x86)"], "PowerShell", "7"), | ||
| process.env.LOCALAPPDATA && path.join(process.env.LOCALAPPDATA, "Microsoft", "WindowsApps"), | ||
| ].filter((item): item is string => Boolean(item)) | ||
| return roots.map((root) => path.join(root, "pwsh.exe")).filter((file) => existsSync(file)) | ||
| } | ||
|
|
||
| const pwshInstalled = () => Boolean(which("pwsh")) || knownLocations().length > 0 | ||
|
|
||
| // Remove every PATH directory that can resolve pwsh or powershell so detection | ||
| // cannot fall back to PATH lookup and must find installs on its own. | ||
| const withoutPowershellDirs = () => | ||
| (process.env.PATH ?? "") | ||
| .split(path.delimiter) | ||
| .filter(Boolean) | ||
| .filter((dir) => !/powershell/i.test(dir) && !existsSync(path.join(dir, "pwsh.exe"))) | ||
| .join(path.delimiter) | ||
|
|
||
| function withEnv(env: { PATH?: string; SHELL?: string }, fn: () => void) { | ||
| const prevPath = process.env.PATH | ||
| const prevShell = process.env.SHELL | ||
| if (env.PATH === undefined) delete process.env.PATH | ||
| else process.env.PATH = env.PATH | ||
| if (env.SHELL === undefined) delete process.env.SHELL | ||
| else process.env.SHELL = env.SHELL | ||
| Shell.preferred.reset() | ||
| Shell.acceptable.reset() | ||
| try { | ||
| fn() | ||
| } finally { | ||
| if (prevPath === undefined) delete process.env.PATH | ||
| else process.env.PATH = prevPath | ||
| if (prevShell === undefined) delete process.env.SHELL | ||
| else process.env.SHELL = prevShell | ||
| Shell.preferred.reset() | ||
| Shell.acceptable.reset() | ||
| } | ||
| } | ||
|
|
||
| if (process.platform === "win32") { | ||
| describe("windows powershell selection", () => { | ||
| test("prefers an installed powershell 7 when pwsh is absent from PATH", () => { | ||
| if (!pwshInstalled()) return | ||
| withEnv({ PATH: withoutPowershellDirs(), SHELL: undefined }, () => { | ||
| expect(Shell.name(Shell.preferred())).toBe("pwsh") | ||
| expect(Shell.name(Shell.acceptable())).toBe("pwsh") | ||
| }) | ||
| }) | ||
|
|
||
| test("prefers pwsh over legacy 5.1 on the unmodified PATH", () => { | ||
| if (!pwshInstalled()) return | ||
| withEnv({ SHELL: undefined }, () => { | ||
| expect(Shell.name(Shell.preferred())).toBe("pwsh") | ||
| }) | ||
| }) | ||
|
|
||
| test("explicit shell config still overrides detection", () => { | ||
| if (!existsSync(LEGACY)) return | ||
| expect(Shell.preferred(LEGACY)).toBe(LEGACY) | ||
| expect(Shell.acceptable(LEGACY)).toBe(LEGACY) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| describe("powershell install probing", () => { | ||
| test("lists known locations in priority order", () => { | ||
| expect( | ||
| PowerShell.locations({ | ||
| ProgramFiles: "C:\\Program Files", | ||
| "ProgramFiles(x86)": "C:\\Program Files (x86)", | ||
| LOCALAPPDATA: "C:\\Users\\u\\AppData\\Local", | ||
| }), | ||
| ).toEqual([ | ||
| path.join("C:\\Program Files", "PowerShell", "7", "pwsh.exe"), | ||
| path.join("C:\\Program Files (x86)", "PowerShell", "7", "pwsh.exe"), | ||
| path.join("C:\\Users\\u\\AppData\\Local", "Microsoft", "WindowsApps", "pwsh.exe"), | ||
| ]) | ||
| }) | ||
|
|
||
| test("skips unset environment roots", () => { | ||
| expect(PowerShell.locations({})).toEqual([]) | ||
| }) | ||
|
|
||
| test("probe and pwsh resolve an installed pwsh outside PATH", () => { | ||
| const root = mkdtempSync(path.join(tmpdir(), "pwsh-probe-")) | ||
| try { | ||
| const dir = path.join(root, "PowerShell", "7") | ||
| mkdirSync(dir, { recursive: true }) | ||
| const file = path.join(dir, "pwsh.exe") | ||
| writeFileSync(file, "") | ||
| expect(PowerShell.probe({ ProgramFiles: root })).toEqual([file]) | ||
| expect(PowerShell.pwsh({ PATH: "", ProgramFiles: root })).toBe(file) | ||
| } finally { | ||
| rmSync(root, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| test("probe ignores location roots without pwsh", () => { | ||
| const root = mkdtempSync(path.join(tmpdir(), "pwsh-probe-empty-")) | ||
| try { | ||
| mkdirSync(path.join(root, "PowerShell", "7"), { recursive: true }) | ||
| expect(PowerShell.probe({ ProgramFiles: root })).toEqual([]) | ||
| expect(PowerShell.pwsh({ PATH: "", ProgramFiles: root })).toBeUndefined() | ||
| } finally { | ||
| rmSync(root, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { statSync } from "node:fs" | ||
| import * as path from "node:path" | ||
|
|
||
| /** | ||
| * Well-known PowerShell 7 install locations on Windows. The Store install only | ||
| * exposes `pwsh.exe` through the WindowsApps execution alias, which is often | ||
| * missing from the PATH of spawned child processes. | ||
| */ | ||
| export function locations(env: NodeJS.ProcessEnv = process.env): string[] { | ||
| const roots = [ | ||
| env["ProgramFiles"] && path.join(env["ProgramFiles"], "PowerShell", "7"), | ||
| env["ProgramFiles(x86)"] && path.join(env["ProgramFiles(x86)"], "PowerShell", "7"), | ||
| env["LOCALAPPDATA"] && path.join(env["LOCALAPPDATA"], "Microsoft", "WindowsApps"), | ||
| ].filter((item): item is string => Boolean(item)) | ||
| return roots.map((root) => path.join(root, "pwsh.exe")) | ||
| } | ||
|
|
||
| function exists(file: string): boolean { | ||
| return statSync(file, { throwIfNoEntry: false })?.isFile() === true | ||
| } | ||
|
|
||
| export function pwshPath(env: NodeJS.ProcessEnv = process.env): string | undefined { | ||
| const dirs = [...(env.PATH ?? env.Path ?? "").split(path.delimiter), ...locations(env)] | ||
| return dirs | ||
| .filter(Boolean) | ||
| .map((dir) => path.join(dir, "pwsh.exe")) | ||
| .find(exists) | ||
| } | ||
|
|
||
| /** Prefer PowerShell 7; legacy 5.1 writes UTF-16LE BOM output on redirection. */ | ||
| export function powershellCommand(env: NodeJS.ProcessEnv = process.env): string { | ||
| return pwshPath(env) ?? "powershell.exe" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { describe, it, expect } from "bun:test" | ||
| import * as fs from "node:fs" | ||
| import * as os from "node:os" | ||
| import * as path from "node:path" | ||
| import { locations, powershellCommand, pwshPath } from "../../src/util/powershell" | ||
|
|
||
| describe("powershellCommand", () => { | ||
| it("lists known Windows install locations in priority order", () => { | ||
| expect( | ||
| locations({ | ||
| ProgramFiles: "C:\\Program Files", | ||
| "ProgramFiles(x86)": "C:\\Program Files (x86)", | ||
| LOCALAPPDATA: "C:\\Users\\u\\AppData\\Local", | ||
| }), | ||
| ).toEqual([ | ||
| path.join("C:\\Program Files", "PowerShell", "7", "pwsh.exe"), | ||
| path.join("C:\\Program Files (x86)", "PowerShell", "7", "pwsh.exe"), | ||
| path.join("C:\\Users\\u\\AppData\\Local", "Microsoft", "WindowsApps", "pwsh.exe"), | ||
| ]) | ||
| expect(locations({})).toEqual([]) | ||
| }) | ||
|
|
||
| it("falls back to legacy powershell.exe when nothing is found", () => { | ||
| expect(powershellCommand({ PATH: "" })).toBe("powershell.exe") | ||
| }) | ||
|
|
||
| it("prefers a pwsh.exe found on the injected PATH", () => { | ||
| const root = fs.mkdtempSync(path.join(os.tmpdir(), "pwsh-path-")) | ||
| try { | ||
| const file = path.join(root, "pwsh.exe") | ||
| fs.writeFileSync(file, "") | ||
| expect(pwshPath({ PATH: root })).toBe(file) | ||
| expect(powershellCommand({ PATH: root })).toBe(file) | ||
| } finally { | ||
| fs.rmSync(root, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| export { args, PowerShell } from "@opencode-ai/core/kilocode/powershell" | ||
| export { args, PowerShell, pwsh } from "@opencode-ai/core/kilocode/powershell" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not a big deal, neither a blocker, rather a FYI, nowadays it'd be
probe(env).at(0)to avoid "messing" with arrays (because if thelengthis0it would access an undefined index)