diff --git a/src/integrations/terminal/TerminalProcess.ts b/src/integrations/terminal/TerminalProcess.ts index cb2b117f59..70917ff1e6 100644 --- a/src/integrations/terminal/TerminalProcess.ts +++ b/src/integrations/terminal/TerminalProcess.ts @@ -13,7 +13,6 @@ import { inspect } from "util" import type { ExitCodeDetails } from "./types" import { BaseTerminalProcess } from "./BaseTerminalProcess" import { Terminal } from "./Terminal" -import { parseCommand, CommandSegment } from "./commandParser" export class TerminalProcess extends BaseTerminalProcess { private terminalRef: WeakRef @@ -73,37 +72,18 @@ export class TerminalProcess extends BaseTerminalProcess { return } - // Parse the command to check for compound operators - const parsedCommand = parseCommand(command) - - if (parsedCommand.isCompound) { - console.info(`[TerminalProcess] Detected compound command with ${parsedCommand.segments.length} segments`) - console.info(`[TerminalProcess] Executing compound command as a single shell command to preserve context`) - // Execute compound commands as a single command to preserve shell context - // This ensures operators like && and || work correctly and state is maintained - await this.runSingleCommand(command) - return + // Check if command contains compound operators (&&, ||, ;, |) + if (this.isCompoundCommand(command)) { + console.info( + `[TerminalProcess] Detected compound command, executing as single shell command to preserve context`, + ) } - // Execute single command as before + // Execute all commands (simple or compound) through shell integration + // This preserves shell context for compound commands (e.g., cd affects subsequent commands) await this.runSingleCommand(command) } - /** - * @deprecated This method is no longer used. Compound commands are now executed - * as a single shell command to preserve context and proper operator behavior. - * Keeping for reference only. - * - * Previously attempted to execute compound commands by splitting them into segments, - * but this approach lost shell context between commands (e.g., cd wouldn't affect - * subsequent commands). - */ - private async runCompoundCommand_DEPRECATED(segments: CommandSegment[]) { - // This method is intentionally left empty and deprecated - // Compound commands are now handled by executing them as a single command - throw new Error("runCompoundCommand is deprecated. Compound commands should be executed as a single command.") - } - private lastExitCode?: number /** @@ -508,4 +488,63 @@ export class TerminalProcess extends BaseTerminalProcess { return match133 !== undefined ? match133 : match633 } + + /** + * Checks if a command contains compound operators (&&, ||, ;, |) + * that would typically spawn multiple processes. + * + * @param command The command string to check + * @returns True if the command contains compound operators + */ + private isCompoundCommand(command: string): boolean { + // Quick check for compound operators outside of quotes + let inSingleQuote = false + let inDoubleQuote = false + let escaped = false + + for (let i = 0; i < command.length; i++) { + const char = command[i] + const nextChar = command[i + 1] + + // Handle escape sequences + if (escaped) { + escaped = false + continue + } + + if (char === "\\" && !inSingleQuote) { + escaped = true + continue + } + + // Handle quotes + if (char === "'" && !inDoubleQuote) { + inSingleQuote = !inSingleQuote + continue + } + + if (char === '"' && !inSingleQuote) { + inDoubleQuote = !inDoubleQuote + continue + } + + // If we're inside quotes, skip operator detection + if (inSingleQuote || inDoubleQuote) { + continue + } + + // Check for operators (only outside quotes) + if (char === "&" && nextChar === "&") { + return true + } else if (char === "|" && nextChar === "|") { + return true + } else if (char === ";") { + return true + } else if (char === "|" && nextChar !== "|") { + return true + } + } + + return false + } } diff --git a/src/integrations/terminal/__tests__/TerminalProcess.spec.ts b/src/integrations/terminal/__tests__/TerminalProcess.spec.ts index 80f6461548..39e444c56a 100644 --- a/src/integrations/terminal/__tests__/TerminalProcess.spec.ts +++ b/src/integrations/terminal/__tests__/TerminalProcess.spec.ts @@ -6,7 +6,6 @@ import { mergePromise } from "../mergePromise" import { TerminalProcess } from "../TerminalProcess" import { Terminal } from "../Terminal" import { TerminalRegistry } from "../TerminalRegistry" -import { parseCommand } from "../commandParser" vi.mock("execa", () => ({ execa: vi.fn(), @@ -203,23 +202,6 @@ describe("TerminalProcess", () => { expect(mockTerminal.shellIntegration.executeCommand).toHaveBeenCalledWith(compoundCommand) }) - it("detects compound commands correctly", async () => { - // Test various compound command patterns - const testCases = [ - { command: "cd foo && npm test", isCompound: true }, - { command: "npm test || echo 'Tests failed'", isCompound: true }, - { command: "echo start; npm test; echo done", isCompound: true }, - { command: "ls -la | grep test", isCompound: true }, - { command: "npm test", isCompound: false }, - { command: "echo 'test && test'", isCompound: false }, // Quoted operators shouldn't be detected - ] - - for (const testCase of testCases) { - const parsed = parseCommand(testCase.command) - expect(parsed.isCompound).toBe(testCase.isCompound) - } - }) - it("preserves shell context in compound commands", async () => { // This test verifies that compound commands maintain context // For example, cd in the first part affects the second part diff --git a/src/integrations/terminal/__tests__/commandParser.spec.ts b/src/integrations/terminal/__tests__/commandParser.spec.ts deleted file mode 100644 index 6a82781521..0000000000 --- a/src/integrations/terminal/__tests__/commandParser.spec.ts +++ /dev/null @@ -1,230 +0,0 @@ -import { describe, it, expect } from "vitest" -import { parseCommand, isCompoundCommand, splitCompoundCommand } from "../commandParser" - -describe("commandParser", () => { - describe("parseCommand", () => { - it("should identify simple commands as non-compound", () => { - const result = parseCommand("ls -la") - expect(result.isCompound).toBe(false) - expect(result.segments).toHaveLength(1) - expect(result.segments[0].command).toBe("ls -la") - expect(result.segments[0].operator).toBeUndefined() - }) - - it("should parse && operator correctly", () => { - const result = parseCommand("cd foo && npm test") - expect(result.isCompound).toBe(true) - expect(result.segments).toHaveLength(2) - expect(result.segments[0].command).toBe("cd foo") - expect(result.segments[0].operator).toBe("&&") - expect(result.segments[1].command).toBe("npm test") - expect(result.segments[1].operator).toBeUndefined() - }) - - it("should parse || operator correctly", () => { - const result = parseCommand("npm test || echo 'Tests failed'") - expect(result.isCompound).toBe(true) - expect(result.segments).toHaveLength(2) - expect(result.segments[0].command).toBe("npm test") - expect(result.segments[0].operator).toBe("||") - expect(result.segments[1].command).toBe("echo 'Tests failed'") - }) - - it("should parse semicolon operator correctly", () => { - const result = parseCommand("echo 'Starting'; npm test; echo 'Done'") - expect(result.isCompound).toBe(true) - expect(result.segments).toHaveLength(3) - expect(result.segments[0].command).toBe("echo 'Starting'") - expect(result.segments[0].operator).toBe(";") - expect(result.segments[1].command).toBe("npm test") - expect(result.segments[1].operator).toBe(";") - expect(result.segments[2].command).toBe("echo 'Done'") - expect(result.segments[2].operator).toBeUndefined() - }) - - it("should parse pipe operator correctly", () => { - const result = parseCommand("ls -la | grep test") - expect(result.isCompound).toBe(true) - expect(result.segments).toHaveLength(2) - expect(result.segments[0].command).toBe("ls -la") - expect(result.segments[0].operator).toBe("|") - expect(result.segments[1].command).toBe("grep test") - }) - - it("should handle mixed operators", () => { - const result = parseCommand("cd app && npm install || echo 'Install failed'; npm test") - expect(result.isCompound).toBe(true) - expect(result.segments).toHaveLength(4) - expect(result.segments[0].operator).toBe("&&") - expect(result.segments[1].operator).toBe("||") - expect(result.segments[2].operator).toBe(";") - expect(result.segments[3].operator).toBeUndefined() - }) - - it("should respect single quotes", () => { - const result = parseCommand("echo 'test && test'") - expect(result.isCompound).toBe(false) - expect(result.segments).toHaveLength(1) - expect(result.segments[0].command).toBe("echo 'test && test'") - }) - - it("should respect double quotes", () => { - const result = parseCommand('echo "test || test"') - expect(result.isCompound).toBe(false) - expect(result.segments).toHaveLength(1) - expect(result.segments[0].command).toBe('echo "test || test"') - }) - - it("should handle escaped characters", () => { - const result = parseCommand("echo test \\&& echo test2") - expect(result.isCompound).toBe(false) - expect(result.segments).toHaveLength(1) - expect(result.segments[0].command).toBe("echo test \\&& echo test2") - }) - - it("should handle complex quoted strings", () => { - const result = parseCommand(`echo "It's a test" && echo 'He said "hello"'`) - expect(result.isCompound).toBe(true) - expect(result.segments).toHaveLength(2) - expect(result.segments[0].command).toBe(`echo "It's a test"`) - expect(result.segments[1].command).toBe(`echo 'He said "hello"'`) - }) - - it("should handle empty segments gracefully", () => { - const result = parseCommand("&& npm test") - expect(result.segments).toHaveLength(1) - expect(result.segments[0].command).toBe("npm test") - }) - - it("should handle trailing operators", () => { - const result = parseCommand("npm test &&") - expect(result.segments).toHaveLength(1) - expect(result.segments[0].command).toBe("npm test") - expect(result.segments[0].operator).toBe("&&") - }) - }) - - describe("isCompoundCommand", () => { - it("should return false for simple commands", () => { - expect(isCompoundCommand("ls -la")).toBe(false) - expect(isCompoundCommand("npm test")).toBe(false) - expect(isCompoundCommand("echo 'test && test'")).toBe(false) - }) - - it("should return true for compound commands", () => { - expect(isCompoundCommand("cd foo && npm test")).toBe(true) - expect(isCompoundCommand("npm test || echo failed")).toBe(true) - expect(isCompoundCommand("echo start; npm test")).toBe(true) - expect(isCompoundCommand("ls | grep test")).toBe(true) - }) - }) - - describe("splitCompoundCommand", () => { - it("should return single segment for simple commands", () => { - const segments = splitCompoundCommand("npm test") - expect(segments).toHaveLength(1) - expect(segments[0].command).toBe("npm test") - }) - - it("should split compound commands correctly", () => { - const segments = splitCompoundCommand("cd app && npm install && npm test") - expect(segments).toHaveLength(3) - expect(segments[0].command).toBe("cd app") - expect(segments[1].command).toBe("npm install") - expect(segments[2].command).toBe("npm test") - }) - }) - - describe("CommandSegment.shouldExecute", () => { - it("should handle && operator logic correctly", () => { - const result = parseCommand("cmd1 && cmd2") - const segment2 = result.segments[1] - - // Should execute if previous command succeeded (exit code 0) - expect(segment2.shouldExecute(0)).toBe(true) - // Should not execute if previous command failed - expect(segment2.shouldExecute(1)).toBe(false) - expect(segment2.shouldExecute(127)).toBe(false) - }) - - it("should handle || operator logic correctly", () => { - const result = parseCommand("cmd1 || cmd2") - const segment2 = result.segments[1] - - // Should not execute if previous command succeeded - expect(segment2.shouldExecute(0)).toBe(false) - // Should execute if previous command failed - expect(segment2.shouldExecute(1)).toBe(true) - expect(segment2.shouldExecute(127)).toBe(true) - }) - - it("should handle ; operator logic correctly", () => { - const result = parseCommand("cmd1; cmd2") - const segment2 = result.segments[1] - - // Should always execute regardless of previous exit code - expect(segment2.shouldExecute(0)).toBe(true) - expect(segment2.shouldExecute(1)).toBe(true) - expect(segment2.shouldExecute(127)).toBe(true) - }) - - it("should handle | operator logic correctly", () => { - const result = parseCommand("cmd1 | cmd2") - const segment2 = result.segments[1] - - // Should always execute (pipes always run) - expect(segment2.shouldExecute(0)).toBe(true) - expect(segment2.shouldExecute(1)).toBe(true) - }) - - it("should handle first segment correctly", () => { - const result = parseCommand("cmd1 && cmd2 || cmd3") - const segment1 = result.segments[0] - - // First segment should always execute - expect(segment1.shouldExecute(0)).toBe(true) - expect(segment1.shouldExecute(1)).toBe(true) - }) - }) - - describe("edge cases", () => { - it("should handle multiple spaces between commands and operators", () => { - const result = parseCommand("cmd1 && cmd2") - expect(result.segments).toHaveLength(2) - expect(result.segments[0].command).toBe("cmd1") - expect(result.segments[1].command).toBe("cmd2") - }) - - it("should handle tabs and other whitespace", () => { - const result = parseCommand("cmd1\t&&\tcmd2") - expect(result.segments).toHaveLength(2) - expect(result.segments[0].command).toBe("cmd1") - expect(result.segments[1].command).toBe("cmd2") - }) - - it("should handle complex real-world commands", () => { - const cmd = "git add . && git commit -m 'feat: add feature' && git push origin main || echo 'Push failed'" - const result = parseCommand(cmd) - expect(result.isCompound).toBe(true) - expect(result.segments).toHaveLength(4) - expect(result.segments[0].command).toBe("git add .") - expect(result.segments[1].command).toBe("git commit -m 'feat: add feature'") - expect(result.segments[2].command).toBe("git push origin main") - expect(result.segments[3].command).toBe("echo 'Push failed'") - }) - - it("should handle commands with redirections", () => { - const result = parseCommand("echo test > file.txt && cat file.txt") - expect(result.segments).toHaveLength(2) - expect(result.segments[0].command).toBe("echo test > file.txt") - expect(result.segments[1].command).toBe("cat file.txt") - }) - - it("should handle commands with environment variables", () => { - const result = parseCommand("NODE_ENV=test npm test && echo $NODE_ENV") - expect(result.segments).toHaveLength(2) - expect(result.segments[0].command).toBe("NODE_ENV=test npm test") - expect(result.segments[1].command).toBe("echo $NODE_ENV") - }) - }) -}) diff --git a/src/integrations/terminal/commandParser.ts b/src/integrations/terminal/commandParser.ts deleted file mode 100644 index 61c702c1d0..0000000000 --- a/src/integrations/terminal/commandParser.ts +++ /dev/null @@ -1,187 +0,0 @@ -/** - * Utility module for parsing and handling compound shell commands. - * Detects and splits commands with operators like &&, ||, ;, and | - * to enable sequential execution and proper process tracking. - */ - -export interface ParsedCommand { - /** The original full command string */ - original: string - /** Whether this is a compound command with operators */ - isCompound: boolean - /** Individual command segments if compound, otherwise array with single command */ - segments: CommandSegment[] -} - -export interface CommandSegment { - /** The command text */ - command: string - /** The operator that follows this command (if any) */ - operator?: "&&" | "||" | ";" | "|" - /** Whether execution should continue based on previous exit code */ - shouldExecute: (previousExitCode: number) => boolean -} - -/** - * Parses a command string to detect compound command operators. - * Handles &&, ||, ;, and | operators while respecting quotes and escapes. - * - * @param command The command string to parse - * @returns Parsed command information - */ -export function parseCommand(command: string): ParsedCommand { - const segments: CommandSegment[] = [] - let current = "" - let inSingleQuote = false - let inDoubleQuote = false - let escaped = false - let i = 0 - - while (i < command.length) { - const char = command[i] - const nextChar = command[i + 1] - - // Handle escape sequences - if (escaped) { - current += char - escaped = false - i++ - continue - } - - if (char === "\\" && !inSingleQuote) { - escaped = true - current += char - i++ - continue - } - - // Handle quotes - if (char === "'" && !inDoubleQuote) { - inSingleQuote = !inSingleQuote - current += char - i++ - continue - } - - if (char === '"' && !inSingleQuote) { - inDoubleQuote = !inDoubleQuote - current += char - i++ - continue - } - - // If we're inside quotes, just add the character - if (inSingleQuote || inDoubleQuote) { - current += char - i++ - continue - } - - // Check for operators (only outside quotes) - let operator: CommandSegment["operator"] | undefined - let operatorLength = 0 - - if (char === "&" && nextChar === "&") { - operator = "&&" - operatorLength = 2 - } else if (char === "|" && nextChar === "|") { - operator = "||" - operatorLength = 2 - } else if (char === ";") { - operator = ";" - operatorLength = 1 - } else if (char === "|" && nextChar !== "|") { - operator = "|" - operatorLength = 1 - } - - if (operator) { - // Found an operator, save current segment - const trimmedCommand = current.trim() - if (trimmedCommand) { - const segment = createSegment(trimmedCommand, operator) - segments.push(segment) - } - current = "" - i += operatorLength - } else { - current += char - i++ - } - } - - // Add the last segment - const trimmedCommand = current.trim() - if (trimmedCommand) { - segments.push(createSegment(trimmedCommand, undefined)) - } - - // Now set the shouldExecute logic based on the PREVIOUS segment's operator - for (let i = 1; i < segments.length; i++) { - const prevOperator = segments[i - 1].operator - - switch (prevOperator) { - case "&&": - // Execute only if previous command succeeded (exit code 0) - segments[i].shouldExecute = (exitCode) => exitCode === 0 - break - case "||": - // Execute only if previous command failed (non-zero exit code) - segments[i].shouldExecute = (exitCode) => exitCode !== 0 - break - case ";": - case "|": - default: - // Always execute regardless of previous exit code - segments[i].shouldExecute = () => true - break - } - } - - // If we only have one segment with no operator, it's not compound - const isCompound = segments.length > 1 || (segments.length === 1 && segments[0].operator !== undefined) - - return { - original: command, - isCompound, - segments, - } -} - -/** - * Creates a command segment with appropriate execution logic based on operator - */ -function createSegment(command: string, operator: CommandSegment["operator"]): CommandSegment { - // The shouldExecute function determines if THIS segment should execute - // based on the PREVIOUS segment's exit code and the PREVIOUS segment's operator - // By default, segments always execute (first segment or after semicolon) - let shouldExecute: (previousExitCode: number) => boolean = () => true - - return { - command, - operator, - shouldExecute, - } -} - -/** - * Checks if a command contains compound operators that would spawn multiple processes - * - * @param command The command to check - * @returns True if the command contains compound operators - */ -export function isCompoundCommand(command: string): boolean { - return parseCommand(command).isCompound -} - -/** - * Splits a compound command into individual commands for sequential execution. - * Preserves the execution logic of operators like && and ||. - * - * @param command The compound command to split - * @returns Array of individual commands with execution conditions - */ -export function splitCompoundCommand(command: string): CommandSegment[] { - return parseCommand(command).segments -}