From 2979d47e01e0eeb64b67d8b95bf9d9249dfc8320 Mon Sep 17 00:00:00 2001 From: Sarah Fortune Date: Thu, 3 Jul 2025 15:08:52 -0700 Subject: [PATCH] Replace cwd from the workspaceFolders with the host bridge. (#4645) Don't export cwd from task/index.ts. This top-level property cwd will be removed in a following PR because await cannot be used at the top-level. Use the hostbridge util getCwd() in createRuleFile.ts and refreshRules.ts instead of import the cwd from `task`. Add a util function to get the desktop directory instead of constructing it multiple places, update uses with the new function getDesktopDir() Replace function getCwd in FileContextTracker.ts with just getCwd from paths.ts. --- .../context-tracking/FileContextTracker.ts | 17 ++++------------- src/core/controller/file/createRuleFile.ts | 3 ++- src/core/controller/file/refreshRules.ts | 3 ++- src/core/task/index.ts | 11 +++++------ src/core/task/message-state.ts | 4 ++-- src/core/task/tools/autoApprove.ts | 4 ++-- src/integrations/checkpoints/CheckpointUtils.ts | 4 ++-- src/utils/path.ts | 6 +++++- 8 files changed, 24 insertions(+), 28 deletions(-) diff --git a/src/core/context/context-tracking/FileContextTracker.ts b/src/core/context/context-tracking/FileContextTracker.ts index a0bc5b07..a0d3fa36 100644 --- a/src/core/context/context-tracking/FileContextTracker.ts +++ b/src/core/context/context-tracking/FileContextTracker.ts @@ -36,17 +36,6 @@ export class FileContextTracker { this.taskId = taskId } - /** - * Gets the current working directory or returns undefined if it cannot be determined - */ - private async getCwd(): Promise { - const cwd = await getCwd(undefined) - if (!cwd) { - console.info("No workspace folder available - cannot determine current working directory") - } - return cwd - } - /** * File watchers are set up for each file that is tracked in the task metadata. */ @@ -56,8 +45,9 @@ export class FileContextTracker { return } - const cwd = await this.getCwd() + const cwd = await getCwd() if (!cwd) { + console.info("No workspace folder available - cannot determine current working directory") return } @@ -87,8 +77,9 @@ export class FileContextTracker { */ async trackFileContext(filePath: string, operation: "read_tool" | "user_edited" | "cline_edited" | "file_mentioned") { try { - const cwd = await this.getCwd() + const cwd = await getCwd() if (!cwd) { + console.info("No workspace folder available - cannot determine current working directory") return } diff --git a/src/core/controller/file/createRuleFile.ts b/src/core/controller/file/createRuleFile.ts index 30a2fdff..2c204603 100644 --- a/src/core/controller/file/createRuleFile.ts +++ b/src/core/controller/file/createRuleFile.ts @@ -6,8 +6,8 @@ import { createRuleFile as createRuleFileImpl } from "@core/context/instructions import * as vscode from "vscode" import * as path from "path" import { handleFileServiceRequest } from "./index" -import { cwd } from "@core/task" import { refreshWorkflowToggles } from "@/core/context/instructions/user-instructions/workflows" +import { getCwd, getDesktopDir } from "@/utils/path" /** * Creates a rule file in either global or workspace rules directory @@ -32,6 +32,7 @@ export const createRuleFile: FileMethodHandler = async (controller: Controller, throw new Error("Missing or invalid parameters") } + const cwd = await getCwd(getDesktopDir()) const { filePath, fileExists } = await createRuleFileImpl(request.isGlobal, request.filename, cwd, request.type) if (!filePath) { diff --git a/src/core/controller/file/refreshRules.ts b/src/core/controller/file/refreshRules.ts index f8c466ba..2c9e7a3e 100644 --- a/src/core/controller/file/refreshRules.ts +++ b/src/core/controller/file/refreshRules.ts @@ -4,7 +4,7 @@ import type { Controller } from "../index" import { refreshClineRulesToggles } from "@core/context/instructions/user-instructions/cline-rules" import { refreshExternalRulesToggles } from "@core/context/instructions/user-instructions/external-rules" import { refreshWorkflowToggles } from "@core/context/instructions/user-instructions/workflows" -import { cwd } from "@core/task" +import { getCwd, getDesktopDir } from "@/utils/path" /** * Refreshes all rule toggles (Cline, External, and Workflows) @@ -14,6 +14,7 @@ import { cwd } from "@core/task" */ export async function refreshRules(controller: Controller, _request: EmptyRequest): Promise { try { + const cwd = await getCwd(getDesktopDir()) const { globalToggles, localToggles } = await refreshClineRulesToggles(controller.context, cwd) const { cursorLocalToggles, windsurfLocalToggles } = await refreshExternalRulesToggles(controller.context, cwd) const { localWorkflowToggles, globalWorkflowToggles } = await refreshWorkflowToggles(controller.context, cwd) diff --git a/src/core/task/index.ts b/src/core/task/index.ts index b7bad52b..1bfda153 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -26,7 +26,8 @@ import { getApiMetrics } from "@shared/getApiMetrics" import { HistoryItem } from "@shared/HistoryItem" import { DEFAULT_LANGUAGE_SETTINGS, getLanguageKey, LanguageDisplay } from "@shared/Languages" import { ClineAskResponse, ClineCheckpointRestore } from "@shared/WebviewMessage" -import { arePathsEqual } from "@utils/path" +import { getGitRemoteUrls } from "@utils/git" +import { arePathsEqual, getDesktopDir } from "@utils/path" import cloneDeep from "clone-deep" import { execa } from "execa" import { setTimeout as setTimeoutPromise } from "node:timers/promises" @@ -35,7 +36,6 @@ import pTimeout from "p-timeout" import pWaitFor from "p-wait-for" import * as path from "path" import * as vscode from "vscode" -import { getGitRemoteUrls } from "@utils/git" import { parseAssistantMessageV2, parseAssistantMessageV3, ToolUseName } from "@core/assistant-message" import { @@ -70,7 +70,7 @@ import { getSavedClineMessages, GlobalFileNames, } from "@core/storage/disk" -import { getGlobalState, getWorkspaceState } from "@core/storage/state" +import { getGlobalState } from "@core/storage/state" import { processFilesIntoText } from "@integrations/misc/extract-text" import WorkspaceTracker from "@integrations/workspace/WorkspaceTracker" import { McpHub } from "@services/mcp/McpHub" @@ -86,8 +86,7 @@ import { formatErrorWithStatusCode, updateApiReqMsg } from "./utils" export const USE_EXPERIMENTAL_CLAUDE4_FEATURES = false -export const cwd = - vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution +const cwd = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath).at(0) ?? path.join(os.homedir(), "Desktop") // may or may not exist but fs checking existence would immediately ask for permission which would be bad UX, need to come up with a better solution export type ToolResponse = string | Array type UserContent = Array @@ -2572,7 +2571,7 @@ export class Task { if (includeFileDetails) { details += `\n\n# Current Working Directory (${cwd.toPosix()}) Files\n` - const isDesktop = arePathsEqual(cwd, path.join(os.homedir(), "Desktop")) + const isDesktop = arePathsEqual(cwd, getDesktopDir()) if (isDesktop) { // don't want to immediately access desktop since it would show permission popup details += "(Desktop files not shown automatically. Use list_files to explore if needed.)" diff --git a/src/core/task/message-state.ts b/src/core/task/message-state.ts index 04d794a9..29a651e1 100644 --- a/src/core/task/message-state.ts +++ b/src/core/task/message-state.ts @@ -12,7 +12,7 @@ import CheckpointTracker from "@integrations/checkpoints/CheckpointTracker" import { HistoryItem } from "@/shared/HistoryItem" import Anthropic from "@anthropic-ai/sdk" import { TaskState } from "./TaskState" -import { getCwd } from "@/utils/path" +import { getCwd, getDesktopDir } from "@/utils/path" interface MessageStateHandlerParams { context: vscode.ExtensionContext @@ -83,7 +83,7 @@ export class MessageStateHandler { } catch (error) { console.error("Failed to get task directory size:", taskDir, error) } - const cwd = await getCwd(path.join(os.homedir(), "Desktop")) + const cwd = await getCwd(getDesktopDir()) await this.updateTaskHistory({ id: this.taskId, ts: lastRelevantMessage.ts, diff --git a/src/core/task/tools/autoApprove.ts b/src/core/task/tools/autoApprove.ts index 8150d4aa..ede57c14 100644 --- a/src/core/task/tools/autoApprove.ts +++ b/src/core/task/tools/autoApprove.ts @@ -2,7 +2,7 @@ import { AutoApprovalSettings } from "@shared/AutoApprovalSettings" import { ToolUseName } from "@core/assistant-message" import * as path from "path" import os from "os" -import { getCwd } from "@/utils/path" +import { getCwd, getDesktopDir } from "@/utils/path" export class AutoApprove { autoApprovalSettings: AutoApprovalSettings @@ -54,7 +54,7 @@ export class AutoApprove { async shouldAutoApproveToolWithPath(blockname: ToolUseName, autoApproveActionpath: string | undefined): Promise { let isLocalRead: boolean = false if (autoApproveActionpath) { - const cwd = await getCwd(path.join(os.homedir(), "Desktop")) + const cwd = await getCwd(getDesktopDir()) const absolutePath = path.resolve(cwd, autoApproveActionpath) isLocalRead = absolutePath.startsWith(cwd) } else { diff --git a/src/integrations/checkpoints/CheckpointUtils.ts b/src/integrations/checkpoints/CheckpointUtils.ts index 36e58ea0..aab4c1d5 100644 --- a/src/integrations/checkpoints/CheckpointUtils.ts +++ b/src/integrations/checkpoints/CheckpointUtils.ts @@ -2,7 +2,7 @@ import { mkdir, access, constants } from "fs/promises" import * as path from "path" import * as vscode from "vscode" import os from "os" -import { getCwd } from "@/utils/path" +import { getCwd, getDesktopDir } from "@/utils/path" /** * Gets the path to the shadow Git repository in globalStorage. @@ -61,7 +61,7 @@ export async function getWorkingDirectory(): Promise { } const homedir = os.homedir() - const desktopPath = path.join(homedir, "Desktop") + const desktopPath = getDesktopDir() const documentsPath = path.join(homedir, "Documents") const downloadsPath = path.join(homedir, "Downloads") diff --git a/src/utils/path.ts b/src/utils/path.ts index fd1f85e7..c4a223ee 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -84,7 +84,7 @@ export function getReadablePath(cwd: string, relPath?: string): string { relPath = relPath || "" // path.resolve is flexible in that it will resolve relative paths like '../../' to the cwd and even ignore the cwd if the relPath is actually an absolute path const absolutePath = path.resolve(cwd, relPath) - if (arePathsEqual(cwd, path.join(os.homedir(), "Desktop"))) { + if (arePathsEqual(cwd, getDesktopDir())) { // User opened vscode without a workspace, so cwd is the Desktop. Show the full absolute path to keep the user aware of where files are being created return absolutePath.toPosix() } @@ -108,6 +108,10 @@ export const getCwd = async (defaultCwdPath = ""): Promise => { return workspaceFolders.paths.shift() || defaultCwdPath } +export function getDesktopDir() { + return path.join(os.homedir(), "Desktop") +} + // Returns the workspace path of the file in the current editor. // If there is no path, it returns the top level workspace directory. export const getWorkspacePath = async (defaultCwdPath = "") => {