Changesets · Branches · Tags · Compare
| Changeset | 477b69852d2d1e7de11909ecb4f2f405a6213039f79cd89aaf6ae86fb05afa9b |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-08-08 11:16 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-08-08 11:16 -07:00 |
| Parents | 4e2b94c62976, b68bd43d7a56 |
2 files changed
modified core/tools/implementations/runTerminalCommand.test.ts +119 −0
| @@ -352,4 +352,123 @@ | ||
| 352 | 352 | expect(result[0].content).toBe("Command is running in the background..."); |
| 353 | 353 | expect(result[0].status).toBe("Command is running in the background..."); |
| 354 | 354 | }); |
| 355 | + | |
| 356 | + it("should handle missing workspace directory gracefully", async () => { | |
| 357 | + // Mock IDE to return empty workspace directories | |
| 358 | + const mockEmptyWorkspace = jest.fn().mockReturnValue(Promise.resolve([])); | |
| 359 | + | |
| 360 | + // Create IDE mock with empty workspace | |
| 361 | + const mockIde = { | |
| 362 | + getIdeInfo: jest | |
| 363 | + .fn() | |
| 364 | + .mockReturnValue(Promise.resolve({ remoteName: "local" })), | |
| 365 | + getWorkspaceDirs: mockEmptyWorkspace, | |
| 366 | + runCommand: jest.fn(), | |
| 367 | + getIdeSettings: jest.fn(), | |
| 368 | + getDiff: jest.fn(), | |
| 369 | + getClipboardContent: jest.fn(), | |
| 370 | + isTelemetryEnabled: jest.fn(), | |
| 371 | + readFile: jest.fn(), | |
| 372 | + writeFile: jest.fn(), | |
| 373 | + renameFile: jest.fn(), | |
| 374 | + deleteFile: jest.fn(), | |
| 375 | + globFiles: jest.fn(), | |
| 376 | + ls: jest.fn(), | |
| 377 | + }; | |
| 378 | + | |
| 379 | + const extras = { | |
| 380 | + ide: mockIde as unknown as IDE, | |
| 381 | + llm: {} as any, | |
| 382 | + fetch: {} as any, | |
| 383 | + tool: {} as any, | |
| 384 | + toolCallId: "test-tool-call", | |
| 385 | + } as ToolExtras; | |
| 386 | + | |
| 387 | + const command = `node -e "console.log('no workspace test')"`; | |
| 388 | + const args = { command, waitForCompletion: true }; | |
| 389 | + | |
| 390 | + const result = await runTerminalCommandImpl(args, extras); | |
| 391 | + | |
| 392 | + // Should still work - falling back to HOME or cwd | |
| 393 | + expect(result).toHaveLength(1); | |
| 394 | + expect(result[0].name).toBe("Terminal"); | |
| 395 | + expect(result[0].description).toBe("Terminal command output"); | |
| 396 | + expect(result[0].content).toContain("no workspace test"); | |
| 397 | + expect(result[0].status).toBe("Command completed"); | |
| 398 | + | |
| 399 | + // Verify workspace dirs was called but returned empty | |
| 400 | + expect(mockEmptyWorkspace).toHaveBeenCalled(); | |
| 401 | + }); | |
| 402 | + | |
| 403 | + it("should handle case where cwd fallbacks all fail", async () => { | |
| 404 | + // Mock IDE to return empty workspace directories | |
| 405 | + const mockEmptyWorkspace = jest.fn().mockReturnValue(Promise.resolve([])); | |
| 406 | + | |
| 407 | + // Save original environment variables and process.cwd | |
| 408 | + const originalHome = process.env.HOME; | |
| 409 | + const originalUserProfile = process.env.USERPROFILE; | |
| 410 | + const originalCwd = process.cwd; | |
| 411 | + | |
| 412 | + try { | |
| 413 | + // Mock all fallbacks to fail | |
| 414 | + delete process.env.HOME; | |
| 415 | + delete process.env.USERPROFILE; | |
| 416 | + process.cwd = jest.fn().mockImplementation(() => { | |
| 417 | + throw new Error("Current directory unavailable"); | |
| 418 | + }) as jest.MockedFunction<() => string>; | |
| 419 | + | |
| 420 | + // Create IDE mock with empty workspace | |
| 421 | + const mockIde = { | |
| 422 | + getIdeInfo: jest | |
| 423 | + .fn() | |
| 424 | + .mockReturnValue(Promise.resolve({ remoteName: "local" })), | |
| 425 | + getWorkspaceDirs: mockEmptyWorkspace, | |
| 426 | + runCommand: jest.fn(), | |
| 427 | + getIdeSettings: jest.fn(), | |
| 428 | + getDiff: jest.fn(), | |
| 429 | + getClipboardContent: jest.fn(), | |
| 430 | + isTelemetryEnabled: jest.fn(), | |
| 431 | + readFile: jest.fn(), | |
| 432 | + writeFile: jest.fn(), | |
| 433 | + renameFile: jest.fn(), | |
| 434 | + deleteFile: jest.fn(), | |
| 435 | + globFiles: jest.fn(), | |
| 436 | + ls: jest.fn(), | |
| 437 | + }; | |
| 438 | + | |
| 439 | + const extras = { | |
| 440 | + ide: mockIde as unknown as IDE, | |
| 441 | + llm: {} as any, | |
| 442 | + fetch: {} as any, | |
| 443 | + tool: {} as any, | |
| 444 | + toolCallId: "test-tool-call", | |
| 445 | + } as ToolExtras; | |
| 446 | + | |
| 447 | + const command = `node -e "console.log('fallback test')"`; | |
| 448 | + const args = { command, waitForCompletion: true }; | |
| 449 | + | |
| 450 | + // This should now handle the case gracefully by falling back to temp directory | |
| 451 | + const result = await runTerminalCommandImpl(args, extras); | |
| 452 | + | |
| 453 | + // Should work using the temp directory as fallback | |
| 454 | + expect(result).toHaveLength(1); | |
| 455 | + expect(result[0].name).toBe("Terminal"); | |
| 456 | + expect(result[0].content).toContain("fallback test"); | |
| 457 | + expect(result[0].status).toBe("Command completed"); | |
| 458 | + | |
| 459 | + console.log( | |
| 460 | + "Successfully handled cwd fallback to temp directory:", | |
| 461 | + result[0].status, | |
| 462 | + ); | |
| 463 | + } finally { | |
| 464 | + // Always restore original values | |
| 465 | + if (originalHome !== undefined) { | |
| 466 | + process.env.HOME = originalHome; | |
| 467 | + } | |
| 468 | + if (originalUserProfile !== undefined) { | |
| 469 | + process.env.USERPROFILE = originalUserProfile; | |
| 470 | + } | |
| 471 | + process.cwd = originalCwd; | |
| 472 | + } | |
| 473 | + }); | |
| 355 | 474 | }); |
modified core/tools/implementations/runTerminalCommand.ts +172 −150
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import iconv from "iconv-lite"; |
| 2 | 2 | import childProcess from "node:child_process"; |
| 3 | +import os from "node:os"; | |
| 3 | 4 | import util from "node:util"; |
| 4 | 5 | // Automatically decode the buffer according to the platform to avoid garbled Chinese |
| 5 | 6 | function getDecodedOutput(data: Buffer): string { |
| @@ -75,89 +76,132 @@ | ||
| 75 | 76 | if (ENABLED_FOR_REMOTES.includes(ideInfo.remoteName)) { |
| 76 | 77 | // For streaming output |
| 77 | 78 | if (extras.onPartialOutput) { |
| 78 | - return new Promise((resolve, reject) => { | |
| 79 | - try { | |
| 80 | - const getWorkspaceDirsPromise = extras.ide.getWorkspaceDirs(); | |
| 81 | - getWorkspaceDirsPromise | |
| 82 | - .then((workspaceDirs) => { | |
| 83 | - const cwd = fileURLToPath(workspaceDirs[0]); | |
| 84 | - let terminalOutput = ""; | |
| 85 | - | |
| 86 | - if (!waitForCompletion) { | |
| 87 | - const status = "Command is running in the background..."; | |
| 88 | - if (extras.onPartialOutput) { | |
| 89 | - extras.onPartialOutput({ | |
| 90 | - toolCallId, | |
| 91 | - contextItems: [ | |
| 92 | - { | |
| 93 | - name: "Terminal", | |
| 94 | - description: "Terminal command output", | |
| 95 | - content: "", | |
| 96 | - status: status, | |
| 97 | - }, | |
| 98 | - ], | |
| 99 | - }); | |
| 100 | - } | |
| 101 | - } | |
| 79 | + try { | |
| 80 | + const workspaceDirs = await extras.ide.getWorkspaceDirs(); | |
| 81 | + | |
| 82 | + // Handle case where no workspace is available | |
| 83 | + let cwd: string; | |
| 84 | + if (workspaceDirs.length > 0) { | |
| 85 | + cwd = fileURLToPath(workspaceDirs[0]); | |
| 86 | + } else { | |
| 87 | + // Default to user's home directory with fallbacks | |
| 88 | + try { | |
| 89 | + cwd = process.env.HOME || process.env.USERPROFILE || process.cwd(); | |
| 90 | + } catch (error) { | |
| 91 | + // Final fallback if even process.cwd() fails - use system temp directory | |
| 92 | + cwd = os.tmpdir(); | |
| 93 | + } | |
| 94 | + } | |
| 102 | 95 | |
| 103 | - // Use spawn with color environment | |
| 104 | - const { shell, args } = getShellCommand(command); | |
| 105 | - const childProc = childProcess.spawn(shell, args, { | |
| 106 | - cwd, | |
| 107 | - env: getColorEnv(), // Add enhanced environment for colors | |
| 96 | + return new Promise((resolve, reject) => { | |
| 97 | + let terminalOutput = ""; | |
| 98 | + | |
| 99 | + if (!waitForCompletion) { | |
| 100 | + const status = "Command is running in the background..."; | |
| 101 | + if (extras.onPartialOutput) { | |
| 102 | + extras.onPartialOutput({ | |
| 103 | + toolCallId, | |
| 104 | + contextItems: [ | |
| 105 | + { | |
| 106 | + name: "Terminal", | |
| 107 | + description: "Terminal command output", | |
| 108 | + content: "", | |
| 109 | + status: status, | |
| 110 | + }, | |
| 111 | + ], | |
| 108 | 112 | }); |
| 109 | - | |
| 110 | - childProc.stdout?.on("data", (data) => { | |
| 111 | - // Skip if this process has been backgrounded | |
| 112 | - if (isProcessBackgrounded(toolCallId)) return; | |
| 113 | - | |
| 114 | - const newOutput = getDecodedOutput(data); | |
| 115 | - terminalOutput += newOutput; | |
| 116 | - | |
| 117 | - // Send partial output to UI | |
| 118 | - if (extras.onPartialOutput) { | |
| 119 | - const status = waitForCompletion | |
| 120 | - ? "" | |
| 121 | - : "Command is running in the background..."; | |
| 122 | - extras.onPartialOutput({ | |
| 123 | - toolCallId, | |
| 124 | - contextItems: [ | |
| 125 | - { | |
| 126 | - name: "Terminal", | |
| 127 | - description: "Terminal command output", | |
| 128 | - content: terminalOutput, | |
| 129 | - status: status, | |
| 130 | - }, | |
| 131 | - ], | |
| 132 | - }); | |
| 133 | - } | |
| 113 | + } | |
| 114 | + } | |
| 115 | + | |
| 116 | + // Use spawn with color environment | |
| 117 | + const { shell, args } = getShellCommand(command); | |
| 118 | + const childProc = childProcess.spawn(shell, args, { | |
| 119 | + cwd, | |
| 120 | + env: getColorEnv(), // Add enhanced environment for colors | |
| 121 | + }); | |
| 122 | + | |
| 123 | + childProc.stdout?.on("data", (data) => { | |
| 124 | + // Skip if this process has been backgrounded | |
| 125 | + if (isProcessBackgrounded(toolCallId)) return; | |
| 126 | + | |
| 127 | + const newOutput = getDecodedOutput(data); | |
| 128 | + terminalOutput += newOutput; | |
| 129 | + | |
| 130 | + // Send partial output to UI | |
| 131 | + if (extras.onPartialOutput) { | |
| 132 | + const status = waitForCompletion | |
| 133 | + ? "" | |
| 134 | + : "Command is running in the background..."; | |
| 135 | + extras.onPartialOutput({ | |
| 136 | + toolCallId, | |
| 137 | + contextItems: [ | |
| 138 | + { | |
| 139 | + name: "Terminal", | |
| 140 | + description: "Terminal command output", | |
| 141 | + content: terminalOutput, | |
| 142 | + status: status, | |
| 143 | + }, | |
| 144 | + ], | |
| 134 | 145 | }); |
| 135 | - | |
| 136 | - childProc.stderr?.on("data", (data) => { | |
| 137 | - // Skip if this process has been backgrounded | |
| 138 | - if (isProcessBackgrounded(toolCallId)) return; | |
| 139 | - | |
| 140 | - const newOutput = getDecodedOutput(data); | |
| 141 | - terminalOutput += newOutput; | |
| 142 | - | |
| 143 | - // Send partial output to UI, status is not required | |
| 144 | - if (extras.onPartialOutput) { | |
| 145 | - extras.onPartialOutput({ | |
| 146 | - toolCallId, | |
| 147 | - contextItems: [ | |
| 148 | - { | |
| 149 | - name: "Terminal", | |
| 150 | - description: "Terminal command output", | |
| 151 | - content: terminalOutput, | |
| 152 | - }, | |
| 153 | - ], | |
| 154 | - }); | |
| 155 | - } | |
| 146 | + } | |
| 147 | + }); | |
| 148 | + | |
| 149 | + childProc.stderr?.on("data", (data) => { | |
| 150 | + // Skip if this process has been backgrounded | |
| 151 | + if (isProcessBackgrounded(toolCallId)) return; | |
| 152 | + | |
| 153 | + const newOutput = getDecodedOutput(data); | |
| 154 | + terminalOutput += newOutput; | |
| 155 | + | |
| 156 | + // Send partial output to UI, status is not required | |
| 157 | + if (extras.onPartialOutput) { | |
| 158 | + extras.onPartialOutput({ | |
| 159 | + toolCallId, | |
| 160 | + contextItems: [ | |
| 161 | + { | |
| 162 | + name: "Terminal", | |
| 163 | + description: "Terminal command output", | |
| 164 | + content: terminalOutput, | |
| 165 | + }, | |
| 166 | + ], | |
| 156 | 167 | }); |
| 157 | - | |
| 158 | - // If we don't need to wait for completion, resolve immediately | |
| 159 | - if (!waitForCompletion) { | |
| 160 | - const status = "Command is running in the background..."; | |
| 168 | + } | |
| 169 | + }); | |
| 170 | + | |
| 171 | + // If we don't need to wait for completion, resolve immediately | |
| 172 | + if (!waitForCompletion) { | |
| 173 | + const status = "Command is running in the background..."; | |
| 174 | + resolve([ | |
| 175 | + { | |
| 176 | + name: "Terminal", | |
| 177 | + description: "Terminal command output", | |
| 178 | + content: terminalOutput, | |
| 179 | + status: status, | |
| 180 | + }, | |
| 181 | + ]); | |
| 182 | + } | |
| 183 | + | |
| 184 | + childProc.on("close", (code) => { | |
| 185 | + // If this process has been backgrounded, clean it up from the map and return | |
| 186 | + if (isProcessBackgrounded(toolCallId)) { | |
| 187 | + removeBackgroundedProcess(toolCallId); | |
| 188 | + return; | |
| 189 | + } | |
| 190 | + | |
| 191 | + if (waitForCompletion) { | |
| 192 | + // Normal completion, resolve now | |
| 193 | + if (code === 0) { | |
| 194 | + const status = "Command completed"; | |
| 195 | + resolve([ | |
| 196 | + { | |
| 197 | + name: "Terminal", | |
| 198 | + description: "Terminal command output", | |
| 199 | + content: terminalOutput, | |
| 200 | + status: status, | |
| 201 | + }, | |
| 202 | + ]); | |
| 203 | + } else { | |
| 204 | + const status = `Command failed with exit code ${code}`; | |
| 161 | 205 | resolve([ |
| 162 | 206 | { |
| 163 | 207 | name: "Terminal", |
| @@ -167,80 +211,58 @@ | ||
| 167 | 211 | }, |
| 168 | 212 | ]); |
| 169 | 213 | } |
| 170 | - | |
| 171 | - childProc.on("close", (code) => { | |
| 172 | - // If this process has been backgrounded, clean it up from the map and return | |
| 173 | - if (isProcessBackgrounded(toolCallId)) { | |
| 174 | - removeBackgroundedProcess(toolCallId); | |
| 175 | - return; | |
| 176 | - } | |
| 177 | - | |
| 178 | - if (waitForCompletion) { | |
| 179 | - // Normal completion, resolve now | |
| 180 | - if (code === 0) { | |
| 181 | - const status = "Command completed"; | |
| 182 | - resolve([ | |
| 183 | - { | |
| 184 | - name: "Terminal", | |
| 185 | - description: "Terminal command output", | |
| 186 | - content: terminalOutput, | |
| 187 | - status: status, | |
| 188 | - }, | |
| 189 | - ]); | |
| 190 | - } else { | |
| 191 | - const status = `Command failed with exit code ${code}`; | |
| 192 | - resolve([ | |
| 193 | - { | |
| 194 | - name: "Terminal", | |
| 195 | - description: "Terminal command output", | |
| 196 | - content: terminalOutput, | |
| 197 | - status: status, | |
| 198 | - }, | |
| 199 | - ]); | |
| 200 | - } | |
| 201 | - } else { | |
| 202 | - // Already resolved, just update the UI with final output | |
| 203 | - if (extras.onPartialOutput) { | |
| 204 | - const status = | |
| 205 | - code === 0 || !code | |
| 206 | - ? "\nBackground command completed" | |
| 207 | - : `\nBackground command failed with exit code ${code}`; | |
| 208 | - extras.onPartialOutput({ | |
| 209 | - toolCallId, | |
| 210 | - contextItems: [ | |
| 211 | - { | |
| 212 | - name: "Terminal", | |
| 213 | - description: "Terminal command output", | |
| 214 | - content: terminalOutput, | |
| 215 | - status: status, | |
| 216 | - }, | |
| 217 | - ], | |
| 218 | - }); | |
| 219 | - } | |
| 220 | - } | |
| 221 | - }); | |
| 222 | - | |
| 223 | - childProc.on("error", (error) => { | |
| 224 | - // If this process has been backgrounded, clean it up from the map and return | |
| 225 | - if (isProcessBackgrounded(toolCallId)) { | |
| 226 | - removeBackgroundedProcess(toolCallId); | |
| 227 | - return; | |
| 228 | - } | |
| 229 | - | |
| 230 | - reject(error); | |
| 231 | - }); | |
| 232 | - }) | |
| 233 | - .catch((error) => { | |
| 234 | - reject(error); | |
| 235 | - }); | |
| 236 | - } catch (error: any) { | |
| 237 | - reject(error); | |
| 238 | - } | |
| 239 | - }); | |
| 214 | + } else { | |
| 215 | + // Already resolved, just update the UI with final output | |
| 216 | + if (extras.onPartialOutput) { | |
| 217 | + const status = | |
| 218 | + code === 0 || !code | |
| 219 | + ? "\nBackground command completed" | |
| 220 | + : `\nBackground command failed with exit code ${code}`; | |
| 221 | + extras.onPartialOutput({ | |
| 222 | + toolCallId, | |
| 223 | + contextItems: [ | |
| 224 | + { | |
| 225 | + name: "Terminal", | |
| 226 | + description: "Terminal command output", | |
| 227 | + content: terminalOutput, | |
| 228 | + status: status, | |
| 229 | + }, | |
| 230 | + ], | |
| 231 | + }); | |
| 232 | + } | |
| 233 | + } | |
| 234 | + }); | |
| 235 | + | |
| 236 | + childProc.on("error", (error) => { | |
| 237 | + // If this process has been backgrounded, clean it up from the map and return | |
| 238 | + if (isProcessBackgrounded(toolCallId)) { | |
| 239 | + removeBackgroundedProcess(toolCallId); | |
| 240 | + return; | |
| 241 | + } | |
| 242 | + | |
| 243 | + reject(error); | |
| 244 | + }); | |
| 245 | + }); | |
| 246 | + } catch (error: any) { | |
| 247 | + throw error; | |
| 248 | + } | |
| 240 | 249 | } else { |
| 241 | 250 | // Fallback to non-streaming for older clients |
| 242 | 251 | const workspaceDirs = await extras.ide.getWorkspaceDirs(); |
| 243 | - const cwd = fileURLToPath(workspaceDirs[0]); | |
| 252 | + | |
| 253 | + // Handle case where no workspace is available | |
| 254 | + let cwd: string; | |
| 255 | + if (workspaceDirs.length > 0) { | |
| 256 | + cwd = fileURLToPath(workspaceDirs[0]); | |
| 257 | + } else { | |
| 258 | + // Default to user's home directory with fallbacks | |
| 259 | + try { | |
| 260 | + cwd = process.env.HOME || process.env.USERPROFILE || process.cwd(); | |
| 261 | + } catch (error) { | |
| 262 | + // Final fallback if even process.cwd() fails - use system temp directory | |
| 263 | + cwd = os.tmpdir(); | |
| 264 | + } | |
| 265 | + } | |
| 244 | 266 | |
| 245 | 267 | if (waitForCompletion) { |
| 246 | 268 | // Standard execution, waiting for completion |