Replace vscode.workspace.getWorkspaceFolder() with the host bridge (#4659)

* Replace vscode.workspace.getWorkspaceFolder() with the host bridge

Use the hostbridge getWorkspacePaths() and use the result to
check for the workspaceFolder of the current file open in the IDE.

* Organize imports

* Update isLocatedInWorkspace() to check all the workspace directories, not just the first.

Add utility function to check if a path is inside a directory instead of duplicating the logic.

* Remove stubs for workspaceFolders that are not needed anymore.
This commit is contained in:
Sarah Fortune authored and GitHub committed 2025-07-07 14:23:26 -07:00
1 parent 4e27e06670
commit f97ef745d9
3 files changed
+47 -56

No files matched your search

+45 -26
View File
@@ -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<string> => {
const workspaceFolders = await getHostBridgeProvider().workspaceClient.getWorkspacePaths({})
return workspaceFolders.paths.shift() || defaultCwdPath
export async function getCwd(defaultCwd = ""): Promise<string> {
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<string> {
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<boolean> => {
const workspacePath = await getWorkspacePath()
export async function isLocatedInWorkspace(pathToCheck: string = ""): Promise<boolean> {
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
}
@@ -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...")
@@ -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")