Skip to content

Commit 87077e1

Browse files
zoomote[bot]roomote
andauthored
fix(integrations): propagate async failures (#1351)
Co-authored-by: Roomote <roomote@roomote.dev>
1 parent f7ec103 commit 87077e1

10 files changed

Lines changed: 81 additions & 18 deletions

‎src/eslint.config.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default [
5858
"core/task/**/*.ts",
5959
"core/tools/**/*.ts",
6060
"core/webview/**/*.ts",
61+
"integrations/**/*.ts",
6162
],
6263
languageOptions: {
6364
parserOptions: {

‎src/integrations/terminal/ExecaTerminal.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class ExecaTerminal extends BaseTerminal {
3030
const promise = new Promise<void>((resolve, reject) => {
3131
process.once("continue", () => resolve())
3232
process.once("error", (error) => reject(error))
33-
process.run(command)
33+
void process.run(command).catch((error) => process.emit("error", error))
3434
})
3535

3636
return mergePromise(process, promise)

‎src/integrations/terminal/Terminal.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class Terminal extends BaseTerminal {
127127
ShellIntegrationManager.zshCleanupTmpDir(this.id)
128128

129129
// Run the command in the terminal
130-
process.run(command)
130+
void process.run(command).catch((error) => process.emit("error", error))
131131
})
132132
.catch(() => {
133133
console.log(`[Terminal ${this.id}] Shell integration not available. Command execution aborted.`)

‎src/integrations/terminal/__tests__/ExecaTerminal.spec.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,26 @@
22

33
import { RooTerminalCallbacks } from "../types"
44
import { ExecaTerminal } from "../ExecaTerminal"
5+
import { ExecaTerminalProcess } from "../ExecaTerminalProcess"
56

67
describe("ExecaTerminal", () => {
8+
it("rejects the command promise when process startup rejects", async () => {
9+
const startupError = new Error("execa startup failed")
10+
const runSpy = vi.spyOn(ExecaTerminalProcess.prototype, "run").mockRejectedValueOnce(startupError)
11+
const terminal = new ExecaTerminal(1, "/tmp")
12+
13+
const commandPromise = terminal.runCommand("echo test", {
14+
onLine: vi.fn(),
15+
onCompleted: vi.fn(),
16+
onShellExecutionStarted: vi.fn(),
17+
onShellExecutionComplete: vi.fn(),
18+
})
19+
20+
await expect(commandPromise).rejects.toThrow("execa startup failed")
21+
expect(runSpy).toHaveBeenCalledWith("echo test")
22+
runSpy.mockRestore()
23+
})
24+
725
it("should run terminal commands and collect output", async () => {
826
// TODO: Run the equivalent test for Windows.
927
if (process.platform === "win32") {

‎src/integrations/terminal/__tests__/TerminalProcess.spec.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ describe("TerminalProcess", () => {
5959
})
6060

6161
describe("run", () => {
62+
it("rejects the command promise when terminal process startup rejects", async () => {
63+
const startupError = new Error("terminal startup failed")
64+
const runSpy = vi.spyOn(TerminalProcess.prototype, "run").mockRejectedValueOnce(startupError)
65+
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => undefined)
66+
67+
const commandPromise = mockTerminalInfo.runCommand("test command", {
68+
onLine: vi.fn(),
69+
onCompleted: vi.fn(),
70+
onShellExecutionStarted: vi.fn(),
71+
onShellExecutionComplete: vi.fn(),
72+
})
73+
74+
await expect(commandPromise).rejects.toThrow("terminal startup failed")
75+
expect(runSpy).toHaveBeenCalledWith("test command")
76+
77+
runSpy.mockRestore()
78+
consoleErrorSpy.mockRestore()
79+
})
80+
6281
it("emits no_shell_integration with commandSubmitted=false when shell integration startup times out", async () => {
6382
vi.useFakeTimers()
6483
const previousTimeout = Terminal.getShellIntegrationTimeout()

‎src/integrations/terminal/__tests__/TerminalProcessExec.bash.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ async function testTerminalCommand(
219219
const eventHandlers = (vscode as any).__eventHandlers
220220

221221
// Execute the command first to set up the process
222-
terminalProcess.run(command)
222+
const runPromise = terminalProcess.run(command)
223223

224224
// Trigger the start terminal shell execution event through VSCode mock
225225
if (eventHandlers.startTerminalShellExecution) {
@@ -258,6 +258,7 @@ async function testTerminalCommand(
258258

259259
// Wait for the command to complete or timeout
260260
await Promise.race([completedPromise, timeoutPromise])
261+
await runPromise
261262
// Calculate execution time in microseconds
262263
// If endTime wasn't set (unlikely but possible), set it now
263264
if (!timeRecorded) {

‎src/integrations/terminal/__tests__/TerminalProcessExec.cmd.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ async function testCmdCommand(
158158
const eventHandlers = (vscode as any).__eventHandlers
159159

160160
// Execute the command first to set up the process
161-
terminalProcess.run(command)
161+
const runPromise = terminalProcess.run(command)
162162

163163
// Trigger the start terminal shell execution event through VSCode mock
164164
if (eventHandlers.startTerminalShellExecution) {
@@ -214,6 +214,7 @@ async function testCmdCommand(
214214

215215
// Wait for the command to complete or timeout
216216
await Promise.race([completedPromise, timeoutPromise])
217+
await runPromise
217218

218219
// Calculate execution time in microseconds
219220
if (!timeRecorded) {

‎src/integrations/terminal/__tests__/TerminalProcessExec.pwsh.spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ async function testPowerShellCommand(
159159
const eventHandlers = (vscode as any).__eventHandlers
160160

161161
// Execute the command first to set up the process
162-
terminalProcess.run(command)
162+
const runPromise = terminalProcess.run(command)
163163

164164
// Trigger the start terminal shell execution event through VSCode mock
165165
if (eventHandlers.startTerminalShellExecution) {
@@ -208,6 +208,7 @@ async function testPowerShellCommand(
208208

209209
// Wait for the command to complete or timeout
210210
await Promise.race([completedPromise, timeoutPromise])
211+
await runPromise
211212

212213
// Calculate execution time in microseconds
213214
if (!timeRecorded) {

‎src/integrations/workspace/WorkspaceTracker.ts‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,30 @@ class WorkspaceTracker {
9191
)
9292
}
9393

94-
private async workspaceDidReset() {
94+
private workspaceDidReset() {
9595
if (this.resetTimer) {
9696
clearTimeout(this.resetTimer)
9797
}
98-
this.resetTimer = setTimeout(async () => {
99-
if (this.prevWorkSpacePath !== this.cwd) {
100-
await this.providerRef.deref()?.postMessageToWebview({
101-
type: "workspaceUpdated",
102-
filePaths: [],
103-
openedTabs: this.getOpenedTabsInfo(),
104-
})
105-
this.filePaths.clear()
106-
this.prevWorkSpacePath = this.cwd
107-
this.initializeFilePaths()
108-
}
98+
this.resetTimer = setTimeout(() => {
99+
void this.resetWorkspace().catch((error) => {
100+
console.error("[WorkspaceTracker] Failed to reset workspace:", error)
101+
})
109102
}, 300) // Debounce for 300ms
110103
}
111104

105+
private async resetWorkspace() {
106+
if (this.prevWorkSpacePath !== this.cwd) {
107+
await this.providerRef.deref()?.postMessageToWebview({
108+
type: "workspaceUpdated",
109+
filePaths: [],
110+
openedTabs: this.getOpenedTabsInfo(),
111+
})
112+
this.filePaths.clear()
113+
this.prevWorkSpacePath = this.cwd
114+
await this.initializeFilePaths()
115+
}
116+
}
117+
112118
private workspaceDidUpdate() {
113119
if (this.updateTimer) {
114120
clearTimeout(this.updateTimer)
@@ -119,7 +125,7 @@ class WorkspaceTracker {
119125
}
120126

121127
const relativeFilePaths = Array.from(this.filePaths).map((file) => toRelativePath(file, this.cwd))
122-
this.providerRef.deref()?.postMessageToWebview({
128+
void this.providerRef.deref()?.postMessageToWebview({
123129
type: "workspaceUpdated",
124130
filePaths: relativeFilePaths,
125131
openedTabs: this.getOpenedTabsInfo(),

‎src/integrations/workspace/__tests__/WorkspaceTracker.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,22 @@ describe("WorkspaceTracker", () => {
254254
vitest.runAllTimers()
255255
})
256256

257+
it("should catch workspace reinitialization failures after a path change", async () => {
258+
const initializationError = new Error("workspace scan failed")
259+
const consoleErrorSpy = vitest.spyOn(console, "error").mockImplementation(() => undefined)
260+
;(listFiles as Mock).mockRejectedValue(initializationError)
261+
;(getWorkspacePath as Mock).mockReturnValue("/test/new-workspace")
262+
263+
await registeredTabChangeCallback!()
264+
await vitest.advanceTimersByTimeAsync(300)
265+
266+
expect(consoleErrorSpy).toHaveBeenCalledWith(
267+
"[WorkspaceTracker] Failed to reset workspace:",
268+
initializationError,
269+
)
270+
consoleErrorSpy.mockRestore()
271+
})
272+
257273
it("should not update file paths if workspace changes during initialization", async () => {
258274
// Setup initial workspace path
259275
;(getWorkspacePath as Mock).mockReturnValue("/test/workspace")

0 commit comments

Comments
 (0)