diff --git a/src/utils/path.ts b/src/utils/path.ts index c4a223ee..3c4205a4 100644 --- a/src/utils/path.ts +++ b/src/utils/path.ts @@ -1,7 +1,7 @@ -import * as path from "path" -import os from "os" -import * as vscode from "vscode" import { getHostBridgeProvider } from "@/hosts/host-providers" +import os from "os" +import * as path from "path" +import * as vscode from "vscode" /* The Node.js 'path' module resolves and normalizes paths differently depending on the platform: @@ -103,9 +103,9 @@ export function getReadablePath(cwd: string, relPath?: string): string { } // Returns the path of the first workspace directory, or the defaultCwdPath if there is no workspace open. -export const getCwd = async (defaultCwdPath = ""): Promise => { - const workspaceFolders = await getHostBridgeProvider().workspaceClient.getWorkspacePaths({}) - return workspaceFolders.paths.shift() || defaultCwdPath +export async function getCwd(defaultCwd = ""): Promise { + const workspacePaths = await getHostBridgeProvider().workspaceClient.getWorkspacePaths({}) + return workspacePaths.paths.shift() || defaultCwd } export function getDesktopDir() { @@ -113,31 +113,50 @@ export function getDesktopDir() { } // 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 = "") => { - const currentFileUri = vscode.window.activeTextEditor?.document.uri - const cwdPath = await getCwd(defaultCwdPath) - if (currentFileUri) { - const workspaceFolder = vscode.workspace.getWorkspaceFolder(currentFileUri) - return workspaceFolder?.uri.fsPath || cwdPath +// If there is no open file, it returns the top level workspace directory. +export async function getWorkspacePath(defaultCwd = ""): Promise { + const currentFilePath = vscode.window.activeTextEditor?.document.uri.fsPath + if (!currentFilePath) { + return await getCwd(defaultCwd) } - return cwdPath + + const workspacePaths = (await getHostBridgeProvider().workspaceClient.getWorkspacePaths({})).paths + for (const workspacePath of workspacePaths) { + if (isLocatedInPath(workspacePath, currentFilePath)) { + return workspacePath + } + } + return await getCwd(defaultCwd) } -export const isLocatedInWorkspace = async (pathToCheck: string = ""): Promise => { - const workspacePath = await getWorkspacePath() +export async function isLocatedInWorkspace(pathToCheck: string = ""): Promise { + const workspacePaths = (await getHostBridgeProvider().workspaceClient.getWorkspacePaths({})).paths + for (const workspacePath of workspacePaths) { + const resolvedPath = path.resolve(workspacePath, pathToCheck) + if (isLocatedInPath(workspacePath, resolvedPath)) { + return true + } + } + return false +} +// Returns true if `pathToCheck` is located inside `dirPath`. +export function isLocatedInPath(dirPath: string, pathToCheck: string): boolean { + if (!dirPath || !pathToCheck) { + return false + } // Handle long paths in Windows - if (pathToCheck.startsWith("\\\\?\\") || workspacePath.startsWith("\\\\?\\")) { - return pathToCheck.startsWith(workspacePath) + if (dirPath.startsWith("\\\\?\\") || pathToCheck.startsWith("\\\\?\\")) { + return pathToCheck.startsWith(dirPath) } - // Normalize paths without resolving symlinks - const normalizedWorkspace = path.normalize(workspacePath) - const normalizedPath = path.normalize(path.resolve(workspacePath, pathToCheck)) - - // Use path.relative to check if the path is within the workspace - const relativePath = path.relative(normalizedWorkspace, normalizedPath) - - return !relativePath.startsWith("..") && !path.isAbsolute(relativePath) + const relativePath = path.relative(path.resolve(dirPath), path.resolve(pathToCheck)) + if (relativePath.startsWith("..")) { + return false + } + if (path.isAbsolute(relativePath)) { + // This can happen on windows when the two paths are on different drives. + return false + } + return true } diff --git a/standalone/runtime-files/vscode/vscode-impls.js b/standalone/runtime-files/vscode/vscode-impls.js index f872c71e..394b5303 100644 --- a/standalone/runtime-files/vscode/vscode-impls.js +++ b/standalone/runtime-files/vscode/vscode-impls.js @@ -498,17 +498,8 @@ Object.assign(vscode.workspace, { }, }, - // Add workspace folder configuration rootPath: process.cwd(), - workspaceFolders: [ - { - uri: vscode.Uri.file(process.cwd()), - name: path.basename(process.cwd()), - index: 0, - }, - ], name: path.basename(process.cwd()), - workspaceFile: vscode.Uri.file(path.join(process.cwd(), ".vscode", "workspace.json")), // Add other workspace methods as stubs getConfiguration: () => ({ @@ -516,14 +507,6 @@ Object.assign(vscode.workspace, { update: () => Promise.resolve(), has: () => false, }), - getWorkspaceFolder: (uri) => { - console.log("Called vscode.workspace.getWorkspaceFolder with:", uri) - // Return the first workspace folder for any URI in standalone mode - if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) { - return vscode.workspace.workspaceFolders[0] - } - return undefined - }, createFileSystemWatcher: () => ({ onDidChange: () => ({ dispose: () => {} }), onDidCreate: () => ({ dispose: () => {} }), @@ -531,7 +514,6 @@ Object.assign(vscode.workspace, { dispose: () => {}, }), onDidChangeConfiguration: () => ({ dispose: () => {} }), - onDidChangeWorkspaceFolders: () => ({ dispose: () => {} }), onDidCreateFiles: createStub("vscode.workspace.onDidCreateFiles"), onDidDeleteFiles: createStub("vscode.workspace.onDidDeleteFiles"), onDidRenameFiles: createStub("vscode.workspace.onDidRenameFiles"), @@ -827,8 +809,6 @@ Object.assign(vscode.languages, { }), }) -console.log("Finished loading stub impls...") - // Export the terminal manager globally for Cline core to use global.standaloneTerminalManager = globalTerminalManager @@ -839,3 +819,5 @@ if (typeof global !== "undefined") { } module.exports = vscode + +console.log("Finished loading stub impls...") diff --git a/standalone/runtime-files/vscode/vscode-stubs.js b/standalone/runtime-files/vscode/vscode-stubs.js index b2123d1f..88ec5ac5 100644 --- a/standalone/runtime-files/vscode/vscode-stubs.js +++ b/standalone/runtime-files/vscode/vscode-stubs.js @@ -812,22 +812,12 @@ vscode.TextDocumentSaveReason = { Manual: 0, AfterDelay: 0, FocusOut: 0 } vscode.workspace = {} vscode.workspace.fs = createStub("vscode.workspace.fs") vscode.workspace.rootPath = createStub("vscode.workspace.rootPath") -vscode.workspace.workspaceFolders = [] vscode.workspace.name = createStub("vscode.workspace.name") vscode.workspace.workspaceFile = createStub("vscode.workspace.workspaceFile") -vscode.workspace.onDidChangeWorkspaceFolders = createStub("vscode.workspace.onDidChangeWorkspaceFolders") -vscode.workspace.getWorkspaceFolder = function (uri) { - console.log("Called stubbed function: vscode.workspace.getWorkspaceFolder") - return createStub("unknown") -} vscode.workspace.asRelativePath = function (pathOrUri, includeWorkspaceFolder) { console.log("Called stubbed function: vscode.workspace.asRelativePath") return "" } -vscode.workspace.updateWorkspaceFolders = function (start, deleteCount, workspaceFoldersToAdd) { - console.log("Called stubbed function: vscode.workspace.updateWorkspaceFolders") - return false -} vscode.workspace.createFileSystemWatcher = function (globPattern, ignoreCreateEvents, ignoreChangeEvents, ignoreDeleteEvents) { console.log("Called stubbed function: vscode.workspace.createFileSystemWatcher") return createStub("unknown")