mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-09-11 22:51:26 +00:00
Refactor Shell Detection to Use VS Code Terminal Profiles and Fallback Hierarchy (#1543)
* Provide explicit command chaining instructions * Added shell detection for powershell The default-shell library being used only returns cmd for windows users. This change will utilize VS Code API calls to determine the user's shell/terminal settings. MacOS & Linux will, for now, continue to use the existing method. Still working on tests. * Replaced default-shell, added tests Replaced default-shell with local code that replicates the old behavior on macOS & Linux Windows shell detection uses VS Code settings to get the user's default terminal profile Adjusted prompt change * One small change * Removed & attributed old package + typo * Added VSC load for other OSes, refactor, better tests * Fixed system.ts explicit git lines * Added back changes for terminal-command-chaining * One minor, but important change
This commit is contained in:
parent
a087f5e583
commit
5fd60b7000
3 changed files with 465 additions and 3 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import defaultShell from "default-shell"
|
||||
import { getShell } from "../../utils/shell"
|
||||
import os from "os"
|
||||
import osName from "os-name"
|
||||
import { McpHub } from "../../services/mcp/McpHub"
|
||||
|
|
@ -38,7 +38,7 @@ Always adhere to this format for the tool use to ensure proper parsing and execu
|
|||
# Tools
|
||||
|
||||
## execute_command
|
||||
Description: Request to execute a CLI command on the system. Use this when you need to perform system operations or run specific commands to accomplish any step in the user's task. You must tailor your command to the user's system and provide a clear explanation of what the command does. Prefer to execute complex CLI commands over creating executable scripts, as they are more flexible and easier to run. Commands will be executed in the current working directory: ${cwd.toPosix()}
|
||||
Description: Request to execute a CLI command on the system. Use this when you need to perform system operations or run specific commands to accomplish any step in the user's task. You must tailor your command to the user's system and provide a clear explanation of what the command does. For command chaining, use the appropriate chaining syntax for the user's shell. Prefer to execute complex CLI commands over creating executable scripts, as they are more flexible and easier to run. Commands will be executed in the current working directory: ${cwd.toPosix()}
|
||||
Parameters:
|
||||
- command: (required) The CLI command to execute. This should be valid for the current operating system. Ensure the command is properly formatted and does not contain any harmful instructions.
|
||||
- requires_approval: (required) A boolean indicating whether this command requires explicit user approval before execution in case the user has auto-approve mode enabled. Set to 'true' for potentially impactful operations like installing/uninstalling packages, deleting/overwriting files, system configuration changes, network operations, or any commands that could have unintended side effects. Set to 'false' for safe operations like reading files/directories, running development servers, building projects, and other non-destructive operations.
|
||||
|
|
@ -941,7 +941,7 @@ ${
|
|||
SYSTEM INFORMATION
|
||||
|
||||
Operating System: ${osName()}
|
||||
Default Shell: ${defaultShell}
|
||||
Default Shell: ${getShell()}
|
||||
Home Directory: ${os.homedir().toPosix()}
|
||||
Current Working Directory: ${cwd.toPosix()}
|
||||
|
||||
|
|
|
|||
235
src/test/shell.test.ts
Normal file
235
src/test/shell.test.ts
Normal file
|
|
@ -0,0 +1,235 @@
|
|||
import { describe, it, beforeEach, afterEach } from "mocha"
|
||||
import { expect } from "chai"
|
||||
import { getShell } from "../utils/shell"
|
||||
import * as vscode from "vscode"
|
||||
import { userInfo } from "os"
|
||||
|
||||
describe("Shell Detection Tests", () => {
|
||||
let originalPlatform: string
|
||||
let originalEnv: NodeJS.ProcessEnv
|
||||
let originalGetConfig: any
|
||||
let originalUserInfo: any
|
||||
|
||||
// Helper to mock VS Code configuration
|
||||
function mockVsCodeConfig(platformKey: string, defaultProfileName: string | null, profiles: Record<string, any>) {
|
||||
vscode.workspace.getConfiguration = () =>
|
||||
({
|
||||
get: (key: string) => {
|
||||
if (key === `defaultProfile.${platformKey}`) {
|
||||
return defaultProfileName
|
||||
}
|
||||
if (key === `profiles.${platformKey}`) {
|
||||
return profiles
|
||||
}
|
||||
return undefined
|
||||
},
|
||||
}) as any
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
// Store original references
|
||||
originalPlatform = process.platform
|
||||
originalEnv = { ...process.env }
|
||||
originalGetConfig = vscode.workspace.getConfiguration
|
||||
originalUserInfo = userInfo
|
||||
|
||||
// Clear environment variables for a clean test
|
||||
delete process.env.SHELL
|
||||
delete process.env.COMSPEC
|
||||
|
||||
// Default userInfo() mock
|
||||
;(userInfo as any) = () => ({ shell: null })
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
// Restore everything
|
||||
Object.defineProperty(process, "platform", { value: originalPlatform })
|
||||
process.env = originalEnv
|
||||
vscode.workspace.getConfiguration = originalGetConfig
|
||||
;(userInfo as any) = originalUserInfo
|
||||
})
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Windows Shell Detection
|
||||
// --------------------------------------------------------------------------
|
||||
describe("Windows Shell Detection", () => {
|
||||
beforeEach(() => {
|
||||
Object.defineProperty(process, "platform", { value: "win32" })
|
||||
})
|
||||
|
||||
it("uses explicit PowerShell 7 path from VS Code config (profile path)", () => {
|
||||
mockVsCodeConfig("windows", "PowerShell", {
|
||||
PowerShell: { path: "C:\\Program Files\\PowerShell\\7\\pwsh.exe" },
|
||||
})
|
||||
expect(getShell()).to.equal("C:\\Program Files\\PowerShell\\7\\pwsh.exe")
|
||||
})
|
||||
|
||||
it("uses PowerShell 7 path if source is 'PowerShell' but no explicit path", () => {
|
||||
mockVsCodeConfig("windows", "PowerShell", {
|
||||
PowerShell: { source: "PowerShell" },
|
||||
})
|
||||
expect(getShell()).to.equal("C:\\Program Files\\PowerShell\\7\\pwsh.exe")
|
||||
})
|
||||
|
||||
it("falls back to legacy PowerShell if profile includes 'powershell' but no path/source", () => {
|
||||
mockVsCodeConfig("windows", "PowerShell", {
|
||||
PowerShell: {},
|
||||
})
|
||||
expect(getShell()).to.equal("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe")
|
||||
})
|
||||
|
||||
it("uses WSL bash when profile indicates WSL source", () => {
|
||||
mockVsCodeConfig("windows", "WSL", {
|
||||
WSL: { source: "WSL" },
|
||||
})
|
||||
expect(getShell()).to.equal("/bin/bash")
|
||||
})
|
||||
|
||||
it("uses WSL bash when profile name includes 'wsl'", () => {
|
||||
mockVsCodeConfig("windows", "Ubuntu WSL", {
|
||||
"Ubuntu WSL": {},
|
||||
})
|
||||
expect(getShell()).to.equal("/bin/bash")
|
||||
})
|
||||
|
||||
it("defaults to cmd.exe if no special profile is matched", () => {
|
||||
mockVsCodeConfig("windows", "CommandPrompt", {
|
||||
CommandPrompt: {},
|
||||
})
|
||||
expect(getShell()).to.equal("C:\\Windows\\System32\\cmd.exe")
|
||||
})
|
||||
|
||||
it("respects userInfo() if no VS Code config is available", () => {
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
;(userInfo as any) = () => ({ shell: "C:\\Custom\\PowerShell.exe" })
|
||||
|
||||
expect(getShell()).to.equal("C:\\Custom\\PowerShell.exe")
|
||||
})
|
||||
|
||||
it("respects an odd COMSPEC if no userInfo shell is available", () => {
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
process.env.COMSPEC = "D:\\CustomCmd\\cmd.exe"
|
||||
|
||||
expect(getShell()).to.equal("D:\\CustomCmd\\cmd.exe")
|
||||
})
|
||||
})
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// macOS Shell Detection
|
||||
// --------------------------------------------------------------------------
|
||||
describe("macOS Shell Detection", () => {
|
||||
beforeEach(() => {
|
||||
Object.defineProperty(process, "platform", { value: "darwin" })
|
||||
})
|
||||
|
||||
it("uses VS Code profile path if available", () => {
|
||||
mockVsCodeConfig("osx", "MyCustomShell", {
|
||||
MyCustomShell: { path: "/usr/local/bin/fish" },
|
||||
})
|
||||
expect(getShell()).to.equal("/usr/local/bin/fish")
|
||||
})
|
||||
|
||||
it("falls back to userInfo().shell if no VS Code config is available", () => {
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
;(userInfo as any) = () => ({ shell: "/opt/homebrew/bin/zsh" })
|
||||
|
||||
expect(getShell()).to.equal("/opt/homebrew/bin/zsh")
|
||||
})
|
||||
|
||||
it("falls back to SHELL env var if no userInfo shell is found", () => {
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
process.env.SHELL = "/usr/local/bin/zsh"
|
||||
|
||||
expect(getShell()).to.equal("/usr/local/bin/zsh")
|
||||
})
|
||||
|
||||
it("falls back to /bin/zsh if no config, userInfo, or env variable is set", () => {
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
// userInfo => null, SHELL => undefined
|
||||
expect(getShell()).to.equal("/bin/zsh")
|
||||
})
|
||||
})
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Linux Shell Detection
|
||||
// --------------------------------------------------------------------------
|
||||
describe("Linux Shell Detection", () => {
|
||||
beforeEach(() => {
|
||||
Object.defineProperty(process, "platform", { value: "linux" })
|
||||
})
|
||||
|
||||
it("uses VS Code profile path if available", () => {
|
||||
mockVsCodeConfig("linux", "CustomProfile", {
|
||||
CustomProfile: { path: "/usr/bin/fish" },
|
||||
})
|
||||
expect(getShell()).to.equal("/usr/bin/fish")
|
||||
})
|
||||
|
||||
it("falls back to userInfo().shell if no VS Code config is available", () => {
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
;(userInfo as any) = () => ({ shell: "/usr/bin/zsh" })
|
||||
|
||||
expect(getShell()).to.equal("/usr/bin/zsh")
|
||||
})
|
||||
|
||||
it("falls back to SHELL env var if no userInfo shell is found", () => {
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
process.env.SHELL = "/usr/bin/fish"
|
||||
|
||||
expect(getShell()).to.equal("/usr/bin/fish")
|
||||
})
|
||||
|
||||
it("falls back to /bin/bash if nothing is set", () => {
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
// userInfo => null, SHELL => undefined
|
||||
expect(getShell()).to.equal("/bin/bash")
|
||||
})
|
||||
})
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
// Unknown Platform & Error Handling
|
||||
// --------------------------------------------------------------------------
|
||||
describe("Unknown Platform / Error Handling", () => {
|
||||
it("falls back to /bin/sh for unknown platforms", () => {
|
||||
Object.defineProperty(process, "platform", { value: "sunos" })
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
|
||||
expect(getShell()).to.equal("/bin/sh")
|
||||
})
|
||||
|
||||
it("handles VS Code config errors gracefully, falling back to userInfo shell if present", () => {
|
||||
Object.defineProperty(process, "platform", { value: "linux" })
|
||||
vscode.workspace.getConfiguration = () => {
|
||||
throw new Error("Configuration error")
|
||||
}
|
||||
;(userInfo as any) = () => ({ shell: "/bin/bash" })
|
||||
|
||||
expect(getShell()).to.equal("/bin/bash")
|
||||
})
|
||||
|
||||
it("handles userInfo errors gracefully, falling back to environment variable if present", () => {
|
||||
Object.defineProperty(process, "platform", { value: "darwin" })
|
||||
vscode.workspace.getConfiguration = () => ({ get: () => undefined }) as any
|
||||
;(userInfo as any) = () => {
|
||||
throw new Error("userInfo error")
|
||||
}
|
||||
process.env.SHELL = "/bin/zsh"
|
||||
|
||||
expect(getShell()).to.equal("/bin/zsh")
|
||||
})
|
||||
|
||||
it("falls back fully to default shell paths if everything fails", () => {
|
||||
Object.defineProperty(process, "platform", { value: "linux" })
|
||||
vscode.workspace.getConfiguration = () => {
|
||||
throw new Error("Configuration error")
|
||||
}
|
||||
;(userInfo as any) = () => {
|
||||
throw new Error("userInfo error")
|
||||
}
|
||||
// No SHELL in env
|
||||
delete process.env.SHELL
|
||||
|
||||
expect(getShell()).to.equal("/bin/bash")
|
||||
})
|
||||
})
|
||||
})
|
||||
227
src/utils/shell.ts
Normal file
227
src/utils/shell.ts
Normal file
|
|
@ -0,0 +1,227 @@
|
|||
import * as vscode from "vscode"
|
||||
import { userInfo } from "os"
|
||||
|
||||
const SHELL_PATHS = {
|
||||
// Windows paths
|
||||
POWERSHELL_7: "C:\\Program Files\\PowerShell\\7\\pwsh.exe",
|
||||
POWERSHELL_LEGACY: "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
|
||||
CMD: "C:\\Windows\\System32\\cmd.exe",
|
||||
WSL_BASH: "/bin/bash",
|
||||
// Unix paths
|
||||
MAC_DEFAULT: "/bin/zsh",
|
||||
LINUX_DEFAULT: "/bin/bash",
|
||||
CSH: "/bin/csh",
|
||||
BASH: "/bin/bash",
|
||||
KSH: "/bin/ksh",
|
||||
SH: "/bin/sh",
|
||||
ZSH: "/bin/zsh",
|
||||
DASH: "/bin/dash",
|
||||
TCSH: "/bin/tcsh",
|
||||
FALLBACK: "/bin/sh",
|
||||
} as const
|
||||
|
||||
interface MacTerminalProfile {
|
||||
path?: string
|
||||
}
|
||||
|
||||
type MacTerminalProfiles = Record<string, MacTerminalProfile>
|
||||
|
||||
interface WindowsTerminalProfile {
|
||||
path?: string
|
||||
source?: "PowerShell" | "WSL"
|
||||
}
|
||||
|
||||
type WindowsTerminalProfiles = Record<string, WindowsTerminalProfile>
|
||||
|
||||
interface LinuxTerminalProfile {
|
||||
path?: string
|
||||
}
|
||||
|
||||
type LinuxTerminalProfiles = Record<string, LinuxTerminalProfile>
|
||||
|
||||
// -----------------------------------------------------
|
||||
// 1) VS Code Terminal Configuration Helpers
|
||||
// -----------------------------------------------------
|
||||
|
||||
function getWindowsTerminalConfig() {
|
||||
try {
|
||||
const config = vscode.workspace.getConfiguration("terminal.integrated")
|
||||
const defaultProfileName = config.get<string>("defaultProfile.windows")
|
||||
const profiles = config.get<WindowsTerminalProfiles>("profiles.windows") || {}
|
||||
return { defaultProfileName, profiles }
|
||||
} catch {
|
||||
return { defaultProfileName: null, profiles: {} as WindowsTerminalProfiles }
|
||||
}
|
||||
}
|
||||
|
||||
function getMacTerminalConfig() {
|
||||
try {
|
||||
const config = vscode.workspace.getConfiguration("terminal.integrated")
|
||||
const defaultProfileName = config.get<string>("defaultProfile.osx")
|
||||
const profiles = config.get<MacTerminalProfiles>("profiles.osx") || {}
|
||||
return { defaultProfileName, profiles }
|
||||
} catch {
|
||||
return { defaultProfileName: null, profiles: {} as MacTerminalProfiles }
|
||||
}
|
||||
}
|
||||
|
||||
function getLinuxTerminalConfig() {
|
||||
try {
|
||||
const config = vscode.workspace.getConfiguration("terminal.integrated")
|
||||
const defaultProfileName = config.get<string>("defaultProfile.linux")
|
||||
const profiles = config.get<LinuxTerminalProfiles>("profiles.linux") || {}
|
||||
return { defaultProfileName, profiles }
|
||||
} catch {
|
||||
return { defaultProfileName: null, profiles: {} as LinuxTerminalProfiles }
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// 2) Platform-Specific VS Code Shell Retrieval
|
||||
// -----------------------------------------------------
|
||||
|
||||
/** Attempts to retrieve a shell path from VS Code config on Windows. */
|
||||
function getWindowsShellFromVSCode(): string | null {
|
||||
const { defaultProfileName, profiles } = getWindowsTerminalConfig()
|
||||
if (!defaultProfileName) {
|
||||
return null
|
||||
}
|
||||
|
||||
const profile = profiles[defaultProfileName]
|
||||
|
||||
// If the profile name indicates PowerShell, do version-based detection.
|
||||
// In testing it was found these typically do not have a path, and this
|
||||
// implementation manages to deductively get the corect version of PowerShell
|
||||
if (defaultProfileName.toLowerCase().includes("powershell")) {
|
||||
if (profile?.path) {
|
||||
// If there's an explicit PowerShell path, return that
|
||||
return profile.path
|
||||
} else if (profile?.source === "PowerShell") {
|
||||
// If the profile is sourced from PowerShell, assume the newest
|
||||
return SHELL_PATHS.POWERSHELL_7
|
||||
}
|
||||
// Otherwise, assume legacy Windows PowerShell
|
||||
return SHELL_PATHS.POWERSHELL_LEGACY
|
||||
}
|
||||
|
||||
// If there's a specific path, return that immediately
|
||||
if (profile.path) {
|
||||
return profile.path
|
||||
}
|
||||
|
||||
// If the profile indicates WSL
|
||||
if (profile?.source === "WSL" || defaultProfileName.toLowerCase().includes("wsl")) {
|
||||
return SHELL_PATHS.WSL_BASH
|
||||
}
|
||||
|
||||
// If nothing special detected, we assume cmd
|
||||
return SHELL_PATHS.CMD
|
||||
}
|
||||
|
||||
/** Attempts to retrieve a shell path from VS Code config on macOS. */
|
||||
function getMacShellFromVSCode(): string | null {
|
||||
const { defaultProfileName, profiles } = getMacTerminalConfig()
|
||||
if (!defaultProfileName) {
|
||||
return null
|
||||
}
|
||||
|
||||
const profile = profiles[defaultProfileName]
|
||||
return profile?.path || null
|
||||
}
|
||||
|
||||
/** Attempts to retrieve a shell path from VS Code config on Linux. */
|
||||
function getLinuxShellFromVSCode(): string | null {
|
||||
const { defaultProfileName, profiles } = getLinuxTerminalConfig()
|
||||
if (!defaultProfileName) {
|
||||
return null
|
||||
}
|
||||
|
||||
const profile = profiles[defaultProfileName]
|
||||
return profile?.path || null
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// 3) General Fallback Helpers
|
||||
// -----------------------------------------------------
|
||||
|
||||
/**
|
||||
* Tries to get a user’s shell from os.userInfo() (works on Unix if the
|
||||
* underlying system call is supported). Returns null on error or if not found.
|
||||
*/
|
||||
function getShellFromUserInfo(): string | null {
|
||||
try {
|
||||
const { shell } = userInfo()
|
||||
return shell || null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns the environment-based shell variable, or null if not set. */
|
||||
function getShellFromEnv(): string | null {
|
||||
const { env } = process
|
||||
|
||||
if (process.platform === "win32") {
|
||||
// On Windows, COMSPEC typically holds cmd.exe
|
||||
return env.COMSPEC || "C:\\Windows\\System32\\cmd.exe"
|
||||
}
|
||||
|
||||
if (process.platform === "darwin") {
|
||||
// On macOS/Linux, SHELL is commonly the environment variable
|
||||
return env.SHELL || "/bin/zsh"
|
||||
}
|
||||
|
||||
if (process.platform === "linux") {
|
||||
// On Linux, SHELL is commonly the environment variable
|
||||
return env.SHELL || "/bin/bash"
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
// -----------------------------------------------------
|
||||
// 4) Publicly Exposed Shell Getter
|
||||
// -----------------------------------------------------
|
||||
|
||||
export function getShell(): string {
|
||||
// 1. Check VS Code config first.
|
||||
if (process.platform === "win32") {
|
||||
// Special logic for Windows
|
||||
const windowsShell = getWindowsShellFromVSCode()
|
||||
if (windowsShell) {
|
||||
return windowsShell
|
||||
}
|
||||
} else if (process.platform === "darwin") {
|
||||
// macOS from VS Code
|
||||
const macShell = getMacShellFromVSCode()
|
||||
if (macShell) {
|
||||
return macShell
|
||||
}
|
||||
} else if (process.platform === "linux") {
|
||||
// Linux from VS Code
|
||||
const linuxShell = getLinuxShellFromVSCode()
|
||||
if (linuxShell) {
|
||||
return linuxShell
|
||||
}
|
||||
}
|
||||
|
||||
// 2. If no shell from VS Code, try userInfo()
|
||||
const userInfoShell = getShellFromUserInfo()
|
||||
if (userInfoShell) {
|
||||
return userInfoShell
|
||||
}
|
||||
|
||||
// 3. If still nothing, try environment variable
|
||||
const envShell = getShellFromEnv()
|
||||
if (envShell) {
|
||||
return envShell
|
||||
}
|
||||
|
||||
// 4. Finally, fall back to a default
|
||||
if (process.platform === "win32") {
|
||||
// On Windows, if we got here, we have no config, no COMSPEC, and one very messed up operating system.
|
||||
// Use CMD as a last resort
|
||||
return SHELL_PATHS.CMD
|
||||
}
|
||||
// On macOS/Linux, fallback to a POSIX shell - This is the behavior of our old shell detection method.
|
||||
return SHELL_PATHS.FALLBACK
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue