Quellcode durchsuchen

fix: execute shell commands in workspace directory

Add workspace path atom to track the working directory for shell commands.
When --workspace flag is provided, bash commands now execute in the
specified directory instead of always using process.cwd().

Fixes #3634
Krtin Shet vor 3 Monaten
Ursprung
Commit
94bc43af22
3 geänderte Dateien mit 28 neuen und 1 gelöschten Zeilen
  1. 5 0
      .changeset/tasty-ducks-jump.md
  2. 14 1
      cli/src/state/atoms/shell.ts
  3. 9 0
      cli/src/ui/UI.tsx

+ 5 - 0
.changeset/tasty-ducks-jump.md

@@ -0,0 +1,5 @@
+---
+"@kilocode/cli": minor
+---
+
+Fix workspace path resolution when using relative paths with --workspace flag. Bash commands now execute in the correct directory.

+ 14 - 1
cli/src/state/atoms/shell.ts

@@ -7,6 +7,15 @@ import { addMessageAtom, inputModeAtom, type InputMode } from "./ui.js"
 import { exec } from "child_process"
 import { clearTextAtom, setTextAtom, textBufferIsEmptyAtom } from "./textBuffer.js"
 
+// ============================================================================
+// Workspace Path Atom
+// ============================================================================
+
+/**
+ * The workspace directory where shell commands should be executed
+ */
+export const workspacePathAtom = atom<string | null>(null)
+
 // ============================================================================
 // Shell Mode Atoms
 // ============================================================================
@@ -134,9 +143,13 @@ export const executeShellCommandAtom = atom(null, async (get, set, command: stri
 
 	// Execute the command immediately (no approval needed)
 	try {
+		// Get the workspace path for command execution
+		const workspacePath = get(workspacePathAtom)
+		const executionDir = workspacePath || process.cwd()
+
 		// Execute command and capture output
 		const childProcess = exec(command, {
-			cwd: process.cwd(),
+			cwd: executionDir,
 			timeout: 30000, // 30 second timeout
 		})
 

+ 9 - 0
cli/src/ui/UI.tsx

@@ -32,6 +32,7 @@ import { createConfigErrorInstructions, createWelcomeMessage } from "./utils/wel
 import { generateUpdateAvailableMessage, getAutoUpdateStatus } from "../utils/auto-update.js"
 import { generateNotificationMessage } from "../utils/notifications.js"
 import { notificationsAtom } from "../state/atoms/notifications.js"
+import { workspacePathAtom } from "../state/atoms/shell.js"
 import { useTerminal } from "../state/hooks/useTerminal.js"
 
 // Initialize commands on module load
@@ -58,6 +59,7 @@ export const UI: React.FC<UIAppProps> = ({ options, onExit }) => {
 	const resetHistoryNavigation = useSetAtom(resetHistoryNavigationAtom)
 	const exitHistoryMode = useSetAtom(exitHistoryModeAtom)
 	const setIsParallelMode = useSetAtom(isParallelModeAtom)
+	const setWorkspacePath = useSetAtom(workspacePathAtom)
 
 	// Use specialized hooks for command and message handling
 	const { executeCommand, isExecuting: isExecutingCommand } = useCommandHandler()
@@ -110,6 +112,13 @@ export const UI: React.FC<UIAppProps> = ({ options, onExit }) => {
 		}
 	}, [options.parallel, setIsParallelMode])
 
+	// Initialize workspace path for shell commands
+	useEffect(() => {
+		if (options.workspace) {
+			setWorkspacePath(options.workspace)
+		}
+	}, [options.workspace, setWorkspacePath])
+
 	// Handle CI mode exit
 	useEffect(() => {
 		if (shouldExit && options.ci) {