FIX: Detect directory change when reusing active terminals (#3503)
* Added confirmation of a sucessful cd prior to executing commands in active terminals * typo fix, fine tuning * cleanup
This commit is contained in:
1 parent
8f37543800
commit
65f1b05420
3 files changed
+80
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"claude-dev": patch
|
||||
---
|
||||
|
||||
Added confirmation of a successful cd to cwd before executing commands in the active terminal
|
||||
@@ -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<void>((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<void>((_, 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
|
||||
}
|
||||
|
||||
@@ -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).
|
||||
|
||||
Reference in new issue
Block a user