Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/restore-safe-powershell-detection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@kilocode/cli": patch
"kilo-code": patch
---

Prevent inaccessible Windows PowerShell execution aliases from blocking CLI and extension startup.
20 changes: 1 addition & 19 deletions packages/core/src/kilocode/powershell.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import { statSync } from "fs"
import path from "path"
import { which } from "../util/which"

export function args(command: string) {
return ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", script(command)]
}

export const locations = (env: NodeJS.ProcessEnv = process.env) =>
[
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))
.map((root) => path.join(root, "pwsh.exe"))

export const probe = (env: NodeJS.ProcessEnv = process.env) =>
locations(env).filter((file) => statSync(file, { throwIfNoEntry: false })?.isFile())

export const pwsh = (env: NodeJS.ProcessEnv = process.env) => which("pwsh", env) ?? probe(env)[0]

const setup = `[Console]::InputEncoding = [System.Text.UTF8Encoding]::new($false);
[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false);
$OutputEncoding = [Console]::OutputEncoding;
Expand Down Expand Up @@ -141,4 +123,4 @@ function block(command: string, start: number, open: string, close: string) {
}
}

export const PowerShell = { args, locations, probe, pwsh }
export const PowerShell = { args }
3 changes: 1 addition & 2 deletions packages/core/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ function resolve(file: string) {
function win() {
return Array.from(
new Set(
// kilocode_change - probe known PowerShell 7 install locations so legacy 5.1 is not picked when pwsh is off PATH
[PowerShell.pwsh(), which("powershell"), gitbash(), process.env.COMSPEC || "cmd.exe"] // kilocode_change
[which("pwsh"), which("powershell"), gitbash(), process.env.COMSPEC || "cmd.exe"]
.filter((item): item is string => Boolean(item))
.map(full),
),
Expand Down
120 changes: 0 additions & 120 deletions packages/core/test/kilocode/powershell.test.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/kilo-vscode/src/agent-manager/SetupScriptRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* actual execution to an injected RunTask callback (provided by the caller).
*/

import { powershellCommand } from "../util/powershell"
import { SetupScriptService, type SetupScriptInfo } from "./SetupScriptService"

interface SetupScriptEnvironment {
Expand All @@ -32,7 +31,7 @@ function quoteCmdArg(value: string): string {
export function buildSetupTaskCommand(script: SetupScriptInfo): { command: string; args: string[] } {
if (script.kind === "powershell") {
return {
command: powershellCommand(),
command: "powershell.exe",
args: ["-NoLogo", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", script.path],
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/kilo-vscode/src/agent-manager/run/service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from "node:fs"
import * as path from "node:path"
import { KILO_DIR } from "../constants"
import { powershellCommand } from "../../util/powershell"

const RUN_SCRIPT_FILENAME = "run-script"
const RUN_SCRIPT_SHELL_FILENAME = "run-script.sh"
Expand Down Expand Up @@ -86,7 +85,7 @@ function validated(file: string, dir: string): boolean {
export function buildRunTaskCommand(script: RunScriptInfo): { command: string; args: string[] } {
if (script.kind === "powershell") {
return {
command: powershellCommand(),
command: "powershell.exe",
args: ["-NoLogo", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", script.path],
}
}
Expand Down
33 changes: 0 additions & 33 deletions packages/kilo-vscode/src/util/powershell.ts

This file was deleted.

38 changes: 0 additions & 38 deletions packages/kilo-vscode/tests/unit/powershell.test.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/kilo-vscode/tests/unit/run-script-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as fs from "node:fs"
import * as os from "node:os"
import * as path from "node:path"
import { buildRunTaskCommand, RunScriptService } from "../../src/agent-manager/run/service"
import { powershellCommand } from "../../src/util/powershell"

function tmpdir(): string {
return fs.mkdtempSync(path.join(os.tmpdir(), "run-script-service-test-"))
Expand Down Expand Up @@ -60,7 +59,7 @@ describe("RunScriptService", () => {
args: ["/tmp/run-script"],
})
expect(buildRunTaskCommand({ path: "C:\\repo\\.kilo\\run-script.ps1", kind: "powershell" })).toEqual({
command: powershellCommand(),
command: "powershell.exe",
args: ["-NoLogo", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", "C:\\repo\\.kilo\\run-script.ps1"],
})
expect(buildRunTaskCommand({ path: "C:\\repo path\\.kilo\\run-script.cmd", kind: "cmd" })).toEqual({
Expand Down
4 changes: 1 addition & 3 deletions packages/opencode/src/kilocode/background-process/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Instance, type InstanceContext } from "@/kilocode/instance"
import { KiloShutdown } from "@/kilocode/cli/shutdown"
import { model as modelEnv } from "@/kilocode/process/env"
import { SessionID } from "@/session/schema"
import { PowerShell } from "@/kilocode/shell/shell"
import { Shell } from "@opencode-ai/core/shell"
import { ProjectV2 } from "@opencode-ai/core/project"
import { Process } from "@/util/process"
Expand All @@ -33,7 +32,6 @@ import * as Ports from "./ports"

export namespace BackgroundProcess {
const log = Log.create({ service: "background-process" })
const pwsh = PowerShell.pwsh() ?? "powershell.exe"
const MAX = 200 * 1024
const KILL_MS = 3_000
const READY_MS = 30_000
Expand Down Expand Up @@ -671,7 +669,7 @@ export namespace BackgroundProcess {
const token = active.token
if (!pid || !token) return "unknown"
const query = `$p=Get-CimInstance Win32_Process -Filter "ProcessId = ${pid}"; if ($p) { [Console]::Out.Write($p.CommandLine) }`
const out = await Process.text([pwsh, "-NoProfile", "-NonInteractive", "-Command", query], {
const out = await Process.text(["powershell.exe", "-NoProfile", "-NonInteractive", "-Command", query], {
nothrow: true,
abort: AbortSignal.timeout(2_000),
timeout: 2_000,
Expand Down
4 changes: 1 addition & 3 deletions packages/opencode/src/kilocode/background-process/runner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { KiloPtySelfCommand } from "@/kilocode/pty/self-command"
import { PowerShell } from "@/kilocode/shell/shell"
import { Filesystem } from "@/util/filesystem"
import { Process } from "@/util/process"
import { isRecord } from "@/util/record"
Expand All @@ -12,7 +11,6 @@ export namespace BackgroundProcessRunner {
const MODE = 0o600
const MAX = 1024 * 1024
const KEEP = 200 * 1024
const pwsh = PowerShell.pwsh() ?? "powershell.exe"

export type Input = {
token: string
Expand Down Expand Up @@ -98,7 +96,7 @@ export namespace BackgroundProcessRunner {
async function descendants(root: number, seen: Map<number, string>, active: boolean) {
const query =
"Get-CimInstance Win32_Process | Select-Object ProcessId,ParentProcessId,CreationDate | ConvertTo-Json -Compress"
const out = await Process.text([pwsh, "-NoProfile", "-NonInteractive", "-Command", query], {
const out = await Process.text(["powershell.exe", "-NoProfile", "-NonInteractive", "-Command", query], {
nothrow: true,
abort: AbortSignal.timeout(2_000),
timeout: 2_000,
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/kilocode/shell/shell.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { args, PowerShell, pwsh } from "@opencode-ai/core/kilocode/powershell"
export { args, PowerShell } from "@opencode-ai/core/kilocode/powershell"
Loading