Elffuss BraidHub

kikocis/continue public

fix: :bug: Fix terminal use without a workspace
Changeset245005345e6eeeeebedd901dee4edee4f0df8bc3f34ff4bb84967e77c702b3bf
Authorchezsmithy <chezsmithy@me.com> · 2025-07-28 17:37 -07:00
Committerchezsmithy <chezsmithy@me.com> · 2025-08-07 23:56 -07:00
Parents76e6219ef348

Browse the files at this changeset

2 files changed

modified core/tools/implementations/runTerminalCommand.test.ts +114 −1
@@ -352,4 +352,117 @@
352352 expect(result[0].content).toBe("Command is running in the background...");
353353 expect(result[0].status).toBe("Command is running in the background...");
354354 });
355-});
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.fn().mockReturnValue(Promise.resolve({ remoteName: "local" })),
363+ getWorkspaceDirs: mockEmptyWorkspace,
364+ runCommand: jest.fn(),
365+ getIdeSettings: jest.fn(),
366+ getDiff: jest.fn(),
367+ getClipboardContent: jest.fn(),
368+ isTelemetryEnabled: jest.fn(),
369+ readFile: jest.fn(),
370+ writeFile: jest.fn(),
371+ renameFile: jest.fn(),
372+ deleteFile: jest.fn(),
373+ globFiles: jest.fn(),
374+ ls: jest.fn(),
375+ };
376+
377+ const extras = {
378+ ide: mockIde as unknown as IDE,
379+ llm: {} as any,
380+ fetch: {} as any,
381+ tool: {} as any,
382+ toolCallId: "test-tool-call",
383+ } as ToolExtras;
384+
385+ const command = `node -e "console.log('no workspace test')"`;
386+ const args = { command, waitForCompletion: true };
387+
388+ const result = await runTerminalCommandImpl(args, extras);
389+
390+ // Should still work - falling back to HOME or cwd
391+ expect(result).toHaveLength(1);
392+ expect(result[0].name).toBe("Terminal");
393+ expect(result[0].description).toBe("Terminal command output");
394+ expect(result[0].content).toContain("no workspace test");
395+ expect(result[0].status).toBe("Command completed");
396+
397+ // Verify workspace dirs was called but returned empty
398+ expect(mockEmptyWorkspace).toHaveBeenCalled();
399+ });
400+
401+ it("should handle case where cwd fallbacks all fail", async () => {
402+ // Mock IDE to return empty workspace directories
403+ const mockEmptyWorkspace = jest.fn().mockReturnValue(Promise.resolve([]));
404+
405+ // Save original environment variables and process.cwd
406+ const originalHome = process.env.HOME;
407+ const originalUserProfile = process.env.USERPROFILE;
408+ const originalCwd = process.cwd;
409+
410+ try {
411+ // Mock all fallbacks to fail
412+ delete process.env.HOME;
413+ delete process.env.USERPROFILE;
414+ process.cwd = jest.fn().mockImplementation(() => {
415+ throw new Error("Current directory unavailable");
416+ });
417+
418+ // Create IDE mock with empty workspace
419+ const mockIde = {
420+ getIdeInfo: jest.fn().mockReturnValue(Promise.resolve({ remoteName: "local" })),
421+ getWorkspaceDirs: mockEmptyWorkspace,
422+ runCommand: jest.fn(),
423+ getIdeSettings: jest.fn(),
424+ getDiff: jest.fn(),
425+ getClipboardContent: jest.fn(),
426+ isTelemetryEnabled: jest.fn(),
427+ readFile: jest.fn(),
428+ writeFile: jest.fn(),
429+ renameFile: jest.fn(),
430+ deleteFile: jest.fn(),
431+ globFiles: jest.fn(),
432+ ls: jest.fn(),
433+ };
434+
435+ const extras = {
436+ ide: mockIde as unknown as IDE,
437+ llm: {} as any,
438+ fetch: {} as any,
439+ tool: {} as any,
440+ toolCallId: "test-tool-call",
441+ } as ToolExtras;
442+
443+ const command = `node -e "console.log('fallback test')"`;
444+ const args = { command, waitForCompletion: true };
445+
446+ // This should now handle the case gracefully by falling back to temp directory
447+ const result = await runTerminalCommandImpl(args, extras);
448+
449+ // Should work using the temp directory as fallback
450+ expect(result).toHaveLength(1);
451+ expect(result[0].name).toBe("Terminal");
452+ expect(result[0].content).toContain("fallback test");
453+ expect(result[0].status).toBe("Command completed");
454+
455+ console.log("Successfully handled cwd fallback to temp directory:", result[0].status);
456+
457+ } finally {
458+ // Always restore original values
459+ if (originalHome !== undefined) {
460+ process.env.HOME = originalHome;
461+ }
462+ if (originalUserProfile !== undefined) {
463+ process.env.USERPROFILE = originalUserProfile;
464+ }
465+ process.cwd = originalCwd;
466+ }
467+ });
468+});
No newline at the end of the file
modified core/tools/implementations/runTerminalCommand.ts +38 −16
@@ -1,5 +1,6 @@
11 import iconv from "iconv-lite";
22 import childProcess from "node:child_process";
3+import os from "node:os";
34 import util from "node:util";
45 // Automatically decode the buffer according to the platform to avoid garbled Chinese
56 function getDecodedOutput(data: Buffer): string {
@@ -75,13 +76,25 @@
7576 if (ENABLED_FOR_REMOTES.includes(ideInfo.remoteName)) {
7677 // For streaming output
7778 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 = "";
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+ }
95+
96+ return new Promise((resolve, reject) => {
97+ let terminalOutput = "";
8598
8699 if (!waitForCompletion) {
87100 const status = "Command is running in the background...";
@@ -229,18 +242,27 @@
229242
230243 reject(error);
231244 });
232- })
233- .catch((error) => {
234- reject(error);
235245 });
236- } catch (error: any) {
237- reject(error);
238- }
239- });
246+ } catch (error: any) {
247+ throw error;
248+ }
240249 } else {
241250 // Fallback to non-streaming for older clients
242251 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+ }
244266
245267 if (waitForCompletion) {
246268 // Standard execution, waiting for completion
@@ -375,4 +397,4 @@
375397 status: "Command failed",
376398 },
377399 ];
378-};
400+};
No newline at the end of the file