Skip to content

Commit cd55d39

Browse files
authored
Merge pull request #8908 from continuedev/dallin/cwd-fix
fix: unify terminal cwd and add missing case
2 parents c86fa19 + 814cc20 commit cd55d39

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

core/tools/implementations/runTerminalCommand.ts

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import iconv from "iconv-lite";
22
import childProcess from "node:child_process";
33
import os from "node:os";
4-
import util from "node:util";
54
import { ContinueError, ContinueErrorReason } from "../../util/errors";
65
// Automatically decode the buffer according to the platform to avoid garbled Chinese
76
function getDecodedOutput(data: Buffer): string {
@@ -44,7 +43,25 @@ import {
4443
} from "../../util/processTerminalStates";
4544
import { getBooleanArg, getStringArg } from "../parseArgs";
4645

47-
const asyncExec = util.promisify(childProcess.exec);
46+
/**
47+
* Resolves the working directory from workspace dirs.
48+
* Falls back to home directory or temp directory if no workspace is available.
49+
*/
50+
function resolveWorkingDirectory(workspaceDirs: string[]): string {
51+
const fileWorkspaceDir = workspaceDirs.find((dir) =>
52+
dir.startsWith("file:/"),
53+
);
54+
if (fileWorkspaceDir) {
55+
return fileURLToPath(fileWorkspaceDir);
56+
}
57+
// Default to user's home directory with fallbacks
58+
try {
59+
return process.env.HOME || process.env.USERPROFILE || process.cwd();
60+
} catch {
61+
// Final fallback if even process.cwd() fails - use system temp directory
62+
return os.tmpdir();
63+
}
64+
}
4865

4966
// Add color-supporting environment variables
5067
const getColorEnv = () => ({
@@ -82,20 +99,7 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
8299
if (extras.onPartialOutput) {
83100
try {
84101
const workspaceDirs = await extras.ide.getWorkspaceDirs();
85-
86-
// Handle case where no workspace is available
87-
let cwd: string;
88-
if (workspaceDirs.length > 0) {
89-
cwd = fileURLToPath(workspaceDirs[0]);
90-
} else {
91-
// Default to user's home directory with fallbacks
92-
try {
93-
cwd = process.env.HOME || process.env.USERPROFILE || process.cwd();
94-
} catch (error) {
95-
// Final fallback if even process.cwd() fails - use system temp directory
96-
cwd = os.tmpdir();
97-
}
98-
}
102+
const cwd = resolveWorkingDirectory(workspaceDirs);
99103

100104
return new Promise((resolve, reject) => {
101105
let terminalOutput = "";
@@ -281,23 +285,7 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
281285
} else {
282286
// Fallback to non-streaming for older clients
283287
const workspaceDirs = await extras.ide.getWorkspaceDirs();
284-
285-
// Handle case where no workspace is available
286-
let cwd: string;
287-
const fileWorkspaceDir = workspaceDirs.find((dir) =>
288-
dir.startsWith("file:/"),
289-
);
290-
if (fileWorkspaceDir) {
291-
cwd = fileURLToPath(fileWorkspaceDir);
292-
} else {
293-
// Default to user's home directory with fallbacks
294-
try {
295-
cwd = process.env.HOME || process.env.USERPROFILE || process.cwd();
296-
} catch (error) {
297-
// Final fallback if even process.cwd() fails - use system temp directory
298-
cwd = os.tmpdir();
299-
}
300-
}
288+
const cwd = resolveWorkingDirectory(workspaceDirs);
301289

302290
if (waitForCompletion) {
303291
// Standard execution, waiting for completion

0 commit comments

Comments
 (0)