fix: unify cwd and add missing case

This commit is contained in:
Dallin Romney 2025-11-27 02:29:13 -08:00
parent 9a333f5809
commit 814cc205fb

View file

@ -1,7 +1,6 @@
import iconv from "iconv-lite";
import childProcess from "node:child_process";
import os from "node:os";
import util from "node:util";
import { ContinueError, ContinueErrorReason } from "../../util/errors";
// Automatically decode the buffer according to the platform to avoid garbled Chinese
function getDecodedOutput(data: Buffer): string {
@ -44,7 +43,25 @@ import {
} from "../../util/processTerminalStates";
import { getBooleanArg, getStringArg } from "../parseArgs";
const asyncExec = util.promisify(childProcess.exec);
/**
* Resolves the working directory from workspace dirs.
* Falls back to home directory or temp directory if no workspace is available.
*/
function resolveWorkingDirectory(workspaceDirs: string[]): string {
const fileWorkspaceDir = workspaceDirs.find((dir) =>
dir.startsWith("file:/"),
);
if (fileWorkspaceDir) {
return fileURLToPath(fileWorkspaceDir);
}
// Default to user's home directory with fallbacks
try {
return process.env.HOME || process.env.USERPROFILE || process.cwd();
} catch {
// Final fallback if even process.cwd() fails - use system temp directory
return os.tmpdir();
}
}
// Add color-supporting environment variables
const getColorEnv = () => ({
@ -82,20 +99,7 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
if (extras.onPartialOutput) {
try {
const workspaceDirs = await extras.ide.getWorkspaceDirs();
// Handle case where no workspace is available
let cwd: string;
if (workspaceDirs.length > 0) {
cwd = fileURLToPath(workspaceDirs[0]);
} else {
// Default to user's home directory with fallbacks
try {
cwd = process.env.HOME || process.env.USERPROFILE || process.cwd();
} catch (error) {
// Final fallback if even process.cwd() fails - use system temp directory
cwd = os.tmpdir();
}
}
const cwd = resolveWorkingDirectory(workspaceDirs);
return new Promise((resolve, reject) => {
let terminalOutput = "";
@ -281,23 +285,7 @@ export const runTerminalCommandImpl: ToolImpl = async (args, extras) => {
} else {
// Fallback to non-streaming for older clients
const workspaceDirs = await extras.ide.getWorkspaceDirs();
// Handle case where no workspace is available
let cwd: string;
const fileWorkspaceDir = workspaceDirs.find((dir) =>
dir.startsWith("file:/"),
);
if (fileWorkspaceDir) {
cwd = fileURLToPath(fileWorkspaceDir);
} else {
// Default to user's home directory with fallbacks
try {
cwd = process.env.HOME || process.env.USERPROFILE || process.cwd();
} catch (error) {
// Final fallback if even process.cwd() fails - use system temp directory
cwd = os.tmpdir();
}
}
const cwd = resolveWorkingDirectory(workspaceDirs);
if (waitForCompletion) {
// Standard execution, waiting for completion