diff --git a/.changeset/funny-rivers-check.md b/.changeset/funny-rivers-check.md new file mode 100644 index 00000000..72b5ea7b --- /dev/null +++ b/.changeset/funny-rivers-check.md @@ -0,0 +1,5 @@ +--- +"claude-dev": patch +--- + +Added confirmation of a successful cd to cwd before executing commands in the active terminal diff --git a/src/integrations/terminal/TerminalManager.ts b/src/integrations/terminal/TerminalManager.ts index cfc20b01..fa902c3e 100644 --- a/src/integrations/terminal/TerminalManager.ts +++ b/src/integrations/terminal/TerminalManager.ts @@ -108,6 +108,47 @@ export class TerminalManager { if (disposable) { this.disposables.push(disposable) } + + // Add a listener for terminal state changes to detect CWD updates + try { + const stateChangeDisposable = vscode.window.onDidChangeTerminalState((terminal) => { + const terminalInfo = this.findTerminalInfoByTerminal(terminal) + if (terminalInfo && terminalInfo.pendingCwdChange && terminalInfo.cwdResolved) { + // Check if CWD has been updated to match the expected path + if (this.isCwdMatchingExpected(terminalInfo)) { + const resolver = terminalInfo.cwdResolved.resolve + terminalInfo.pendingCwdChange = undefined + terminalInfo.cwdResolved = undefined + resolver() + } + } + }) + this.disposables.push(stateChangeDisposable) + } catch (error) { + console.error("Error setting up onDidChangeTerminalState", error) + } + } + + //Find a TerminalInfo by its VSCode Terminal instance + private findTerminalInfoByTerminal(terminal: vscode.Terminal): TerminalInfo | undefined { + const terminals = TerminalRegistry.getAllTerminals() + return terminals.find((t) => t.terminal === terminal) + } + + //Check if a terminal's CWD matches its expected pending change + private isCwdMatchingExpected(terminalInfo: TerminalInfo): boolean { + if (!terminalInfo.pendingCwdChange) { + return false + } + + const currentCwd = terminalInfo.terminal.shellIntegration?.cwd?.fsPath + const targetCwd = vscode.Uri.file(terminalInfo.pendingCwdChange).fsPath + + if (!currentCwd) { + return false + } + + return arePathsEqual(currentCwd, targetCwd) } runCommand(terminalInfo: TerminalInfo, command: string): TerminalProcessResultPromise { @@ -196,8 +237,37 @@ export class TerminalManager { // If no matching terminal exists, try to find any non-busy terminal const availableTerminal = terminals.find((t) => !t.busy) if (availableTerminal) { + // Set up promise and tracking for CWD change + const cwdPromise = new Promise((resolve, reject) => { + availableTerminal.pendingCwdChange = cwd + availableTerminal.cwdResolved = { resolve, reject } + }) + // Navigate back to the desired directory await this.runCommand(availableTerminal, `cd "${cwd}"`) + + // Either resolve immediately if CWD already updated or wait for event/timeout + if (this.isCwdMatchingExpected(availableTerminal)) { + if (availableTerminal.cwdResolved) { + availableTerminal.cwdResolved.resolve() + } + availableTerminal.pendingCwdChange = undefined + availableTerminal.cwdResolved = undefined + } else { + try { + // Wait with a timeout for state change event to resolve + await Promise.race([ + cwdPromise, + new Promise((_, reject) => + setTimeout(() => reject(new Error(`CWD timeout: Failed to update to ${cwd}`)), 1000), + ), + ]) + } catch (err) { + // Clear pending state on timeout + availableTerminal.pendingCwdChange = undefined + availableTerminal.cwdResolved = undefined + } + } this.terminalIds.add(availableTerminal.id) return availableTerminal } diff --git a/src/integrations/terminal/TerminalRegistry.ts b/src/integrations/terminal/TerminalRegistry.ts index ac0ed30f..9a1f72d6 100644 --- a/src/integrations/terminal/TerminalRegistry.ts +++ b/src/integrations/terminal/TerminalRegistry.ts @@ -5,6 +5,11 @@ export interface TerminalInfo { busy: boolean lastCommand: string id: number + pendingCwdChange?: string + cwdResolved?: { + resolve: () => void + reject: (error: Error) => void + } } // Although vscode.window.terminals provides a list of all open terminals, there's no way to know whether they're busy or not (exitStatus does not provide useful information for most commands). In order to prevent creating too many terminals, we need to keep track of terminals through the life of the extension, as well as session specific terminals for the life of a task (to get latest unretrieved output).