Add a field to the HostProvider for extension install dir (#6239)

* Add a field to the HostProvider `extensionFsPath`

Replace vscode.ExtensionContext `extensionUri` with `HostProvider.extensionFsPath`.

This part of removing uses of the VSCode API fom the codebase except for in platform specific code (these are src/hosts/vscode and src/extension.ts).

* Remove extra leading slash from webview URL on JetBrains

* Remove vscode.Uri from the WebviewProvider

Replace `vscode.Uri` with URL strings.
Replace Uri with Url in variable names.
This commit is contained in:
Sarah Fortune authored and GitHub committed 2025-09-16 09:49:32 -07:00
1 parent 0b5e8f5c37
commit 3723288250
8 files changed
+48 -40

No files matched your search

+24 -26
View File
@@ -5,7 +5,6 @@ import axios from "axios"
import { readFile } from "fs/promises"
import { v4 as uuidv4 } from "uuid"
import * as vscode from "vscode"
import { Uri } from "vscode"
import { HostProvider } from "@/hosts/host-provider"
import { ShowMessageType } from "@/shared/proto/host/window"
import { WebviewProviderType } from "@/shared/webview/types"
@@ -108,12 +107,12 @@ export abstract class WebviewProvider {
}
/**
* Converts a local URI to a webview URI that can be used within the webview.
* Converts a local filesystem path to a URL that can be used within the webview.
*
* @param uri - The local URI to convert
* @returns A URI that can be used within the webview
* @param path - The local path to convert
* @returns A URL that can be used within the webview
*/
abstract getWebviewUri(uri: Uri): Uri
abstract getWebviewUrl(path: string): string
/**
* Gets the Content Security Policy source for the webview.
@@ -135,25 +134,23 @@ export abstract class WebviewProvider {
* @remarks This is also the place where references to the React webview build files
* are created and inserted into the webview HTML.
*
* @param webview A reference to the extension webview
* @param extensionUri The URI of the directory containing the extension
* @returns A template string literal containing the HTML that should be
* rendered within the webview panel
*/
public getHtmlContent(): string {
// Get the local path to main script run in the webview,
// then convert it to a uri we can use in the webview.
// then convert it to a url we can use in the webview.
// The JS file from the React build output
const scriptUrl = this.getExtensionUrl("webview-ui", "build", "assets", "index.js")
// The CSS file from the React build output
const stylesUri = this.getExtensionUri("webview-ui", "build", "assets", "index.css")
// The JS file from the React build output
const scriptUri = this.getExtensionUri("webview-ui", "build", "assets", "index.js")
const stylesUrl = this.getExtensionUrl("webview-ui", "build", "assets", "index.css")
// The codicon font from the React build output
// https://github.com/microsoft/vscode-extension-samples/blob/main/webview-codicons-sample/src/extension.ts
// we installed this package in the extension so that we can access it how its intended from the extension (the font file is likely bundled in vscode), and we just import the css fileinto our react app we don't have access to it
// don't forget to add font-src ${webview.cspSource};
const codiconsUri = this.getExtensionUri("node_modules", "@vscode", "codicons", "dist", "codicon.css")
const codiconsUrl = this.getExtensionUrl("node_modules", "@vscode", "codicons", "dist", "codicon.css")
// Use a nonce to only allow a specific script to be run.
/*
@@ -176,8 +173,8 @@ export abstract class WebviewProvider {
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta name="theme-color" content="#000000">
<link rel="stylesheet" type="text/css" href="${stylesUri}">
<link href="${codiconsUri}" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="${stylesUrl}">
<link href="${codiconsUrl}" rel="stylesheet" />
<meta http-equiv="Content-Security-Policy" content="default-src 'none';
connect-src https://*.posthog.com https://*.cline.bot https://*.firebaseauth.com https://*.firebaseio.com https://*.googleapis.com https://*.firebase.com;
font-src ${this.getCspSource()} data:;
@@ -196,7 +193,7 @@ export abstract class WebviewProvider {
// Inject the client ID
window.clineClientId = "${this.clientId}";
</script>
<script type="module" nonce="${nonce}" src="${scriptUri}"></script>
<script type="module" nonce="${nonce}" src="${scriptUrl}"></script>
<script src="http://localhost:8097"></script>
</body>
</html>
@@ -256,11 +253,11 @@ export abstract class WebviewProvider {
}
const nonce = getNonce()
const stylesUri = this.getExtensionUri("webview-ui", "build", "assets", "index.css")
const codiconsUri = this.getExtensionUri("node_modules", "@vscode", "codicons", "dist", "codicon.css")
const stylesUrl = this.getExtensionUrl("webview-ui", "build", "assets", "index.css")
const codiconsUrl = this.getExtensionUrl("node_modules", "@vscode", "codicons", "dist", "codicon.css")
const scriptEntrypoint = "src/main.tsx"
const scriptUri = `http://${localServerUrl}/${scriptEntrypoint}`
const scriptUrl = `http://${localServerUrl}/${scriptEntrypoint}`
const reactRefresh = /*html*/ `
<script nonce="${nonce}" type="module">
@@ -289,8 +286,8 @@ export abstract class WebviewProvider {
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta http-equiv="Content-Security-Policy" content="${csp.join("; ")}">
<link rel="stylesheet" type="text/css" href="${stylesUri}">
<link href="${codiconsUri}" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="${stylesUrl}">
<link href="${codiconsUrl}" rel="stylesheet" />
<title>Cline</title>
</head>
<body>
@@ -303,21 +300,22 @@ export abstract class WebviewProvider {
window.clineClientId = "${this.clientId}";
</script>
${reactRefresh}
<script type="module" src="${scriptUri}"></script>
<script type="module" src="${scriptUrl}"></script>
</body>
</html>
`
}
/**
* A helper function which will get the webview URI of a given file or resource in the extension directory.
* A helper function which will get the webview URL of a given file or resource in the extension directory.
*
* @remarks This URI can be used within a webview's HTML as a link to the
* @remarks This URL can be used within a webview's HTML as a link to the
* given file/resource.
*
* @param pathList An array of strings representing the path to a file/resource in the extension directory.
* @returns A URI pointing to the file/resource
* @returns A URL pointing to the file/resource
*/
private getExtensionUri(...pathList: string[]): Uri {
return this.getWebviewUri(Uri.joinPath(this.context.extensionUri, ...pathList))
private getExtensionUrl(...pathList: string[]): string {
const assetPath = path.resolve(HostProvider.get().extensionFsPath, ...pathList)
return this.getWebviewUrl(assetPath)
}
}
+2 -1
View File
@@ -143,7 +143,7 @@ export async function activate(context: vscode.ExtensionContext) {
const panel = vscode.window.createWebviewPanel(VscodeWebviewProvider.TAB_PANEL_ID, "Cline", targetCol, {
enableScripts: true,
retainContextWhenHidden: true,
localResourceRoots: [context.extensionUri],
localResourceRoots: [vscode.Uri.file(HostProvider.get().extensionFsPath)],
})
// TODO: use better svg icon with light and dark variants (see https://stackoverflow.com/questions/58365687/vscode-extension-iconpath)
@@ -536,6 +536,7 @@ function setupHostProvider(context: ExtensionContext) {
outputChannel.appendLine,
getCallbackUrl,
getBinaryLocation,
context.extensionUri.fsPath,
)
}
+4 -6
View File
@@ -1,5 +1,4 @@
import * as vscode from "vscode"
import { URI } from "vscode-uri"
import { WebviewProvider } from "@/core/webview"
import { WebviewProviderType } from "@/shared/webview/types"
@@ -11,11 +10,10 @@ export class ExternalWebviewProvider extends WebviewProvider {
super(context, providerType)
}
override getWebviewUri(uri: URI) {
if (uri.scheme !== "file") {
return uri
}
return URI.from({ scheme: "https", authority: this.RESOURCE_HOSTNAME, path: uri.fsPath })
override getWebviewUrl(path: string) {
const url = new URL(`https://${this.RESOURCE_HOSTNAME}/`)
url.pathname = path
return url.toString()
}
override getCspSource() {
return `'self' https://${this.RESOURCE_HOSTNAME}`
+8
View File
@@ -35,6 +35,10 @@ export class HostProvider {
// extension on Windows.
getBinaryLocation: (name: string) => Promise<string>
// The absolute file system path where the extension is installed.
// Use to this to get the location of extension assets.
extensionFsPath: string
// Private constructor to enforce singleton pattern
private constructor(
createWebviewProvider: WebviewProviderCreator,
@@ -43,6 +47,7 @@ export class HostProvider {
logToChannel: LogToChannel,
getCallbackUrl: () => Promise<string>,
getBinaryLocation: (name: string) => Promise<string>,
extensionFsPath: string,
) {
this.createWebviewProvider = createWebviewProvider
this.createDiffViewProvider = createDiffViewProvider
@@ -50,6 +55,7 @@ export class HostProvider {
this.logToChannel = logToChannel
this.getCallbackUrl = getCallbackUrl
this.getBinaryLocation = getBinaryLocation
this.extensionFsPath = extensionFsPath
}
public static initialize(
@@ -59,6 +65,7 @@ export class HostProvider {
logToChannel: LogToChannel,
getCallbackUrl: () => Promise<string>,
getBinaryLocation: (name: string) => Promise<string>,
extensionFsPath: string,
): HostProvider {
if (HostProvider.instance) {
throw new Error("Host provider has already been initialized.")
@@ -70,6 +77,7 @@ export class HostProvider {
logToChannel,
getCallbackUrl,
getBinaryLocation,
extensionFsPath,
)
return HostProvider.instance
}
+4 -4
View File
@@ -1,6 +1,5 @@
import { sendDidBecomeVisibleEvent } from "@core/controller/ui/subscribeToDidBecomeVisible"
import { WebviewProvider } from "@core/webview"
import type { Uri } from "vscode"
import * as vscode from "vscode"
import { handleGrpcRequest, handleGrpcRequestCancel } from "@/core/controller/grpc-handler"
import { HostProvider } from "@/hosts/host-provider"
@@ -27,11 +26,12 @@ export class VscodeWebviewProvider extends WebviewProvider implements vscode.Web
super(context, providerType)
}
override getWebviewUri(uri: Uri) {
override getWebviewUrl(path: string) {
if (!this.webview) {
throw new Error("Webview not initialized")
}
return this.webview.webview.asWebviewUri(uri)
const uri = this.webview.webview.asWebviewUri(vscode.Uri.file(path))
return uri.toString()
}
override getCspSource() {
@@ -68,7 +68,7 @@ export class VscodeWebviewProvider extends WebviewProvider implements vscode.Web
webviewView.webview.options = {
// Allow scripts in the webview
enableScripts: true,
localResourceRoots: [this.context.extensionUri],
localResourceRoots: [vscode.Uri.file(HostProvider.get().extensionFsPath)],
}
webviewView.webview.html =
+2 -1
View File
@@ -11,7 +11,7 @@ import { DiffViewProvider } from "@/integrations/editor/DiffViewProvider"
import { waitForHostBridgeReady } from "./hostbridge-client"
import { startProtobusService } from "./protobus-service"
import { log } from "./utils"
import { extensionContext } from "./vscode-context"
import { EXTENSION_DIR, extensionContext } from "./vscode-context"
async function main() {
log("\n\n\nStarting cline-core service...\n\n\n")
@@ -55,6 +55,7 @@ function setupHostProvider() {
log,
getCallbackUrl,
getBinaryLocation,
EXTENSION_DIR,
)
}
+1 -1
View File
@@ -19,7 +19,7 @@ mkdirSync(DATA_DIR, { recursive: true })
mkdirSync(WORKSPACE_STORAGE_DIR, { recursive: true })
log("Using settings dir:", DATA_DIR)
const EXTENSION_DIR = path.join(INSTALL_DIR, "extension")
export const EXTENSION_DIR = path.join(INSTALL_DIR, "extension")
const EXTENSION_MODE = process.env.IS_DEV === "true" ? ExtensionMode.Development : ExtensionMode.Production
const extension: Extension<void> = {
+3 -1
View File
@@ -15,6 +15,7 @@ export function setVscodeHostProviderMock(options?: {
logToChannel?: (message: string) => void
getCallbackUri?: () => Promise<string>
getBinaryLocation?: (name: string) => Promise<string>
extensionFsPath?: string
}) {
HostProvider.reset()
HostProvider.initialize(
@@ -23,6 +24,7 @@ export function setVscodeHostProviderMock(options?: {
options?.hostBridgeClient ?? vscodeHostBridgeClient,
options?.logToChannel ?? ((_) => {}),
options?.getCallbackUri ?? (async () => "http://example.com:1234/"),
options?.getBinaryLocation ?? (async (n) => `/mock/path/${n}`),
options?.getBinaryLocation ?? (async (n) => `/mock/path/to/binary/${n}`),
options?.extensionFsPath ?? "/mock/path/to/extension",
)
}