diff --git a/src/core/webview/WebviewProvider.ts b/src/core/webview/WebviewProvider.ts index 6a2ecb8d..ed87fe44 100644 --- a/src/core/webview/WebviewProvider.ts +++ b/src/core/webview/WebviewProvider.ts @@ -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 { - - + + - + @@ -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*/ ` ${reactRefresh} - + ` } /** - * 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) } } diff --git a/src/extension.ts b/src/extension.ts index 16cb1bf5..cfdba539 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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, ) } diff --git a/src/hosts/external/ExternalWebviewProvider.ts b/src/hosts/external/ExternalWebviewProvider.ts index f43dd5c4..39c663ac 100644 --- a/src/hosts/external/ExternalWebviewProvider.ts +++ b/src/hosts/external/ExternalWebviewProvider.ts @@ -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}` diff --git a/src/hosts/host-provider.ts b/src/hosts/host-provider.ts index 55f0fb63..d496145f 100644 --- a/src/hosts/host-provider.ts +++ b/src/hosts/host-provider.ts @@ -35,6 +35,10 @@ export class HostProvider { // extension on Windows. getBinaryLocation: (name: string) => Promise + // 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, getBinaryLocation: (name: string) => Promise, + 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, getBinaryLocation: (name: string) => Promise, + 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 } diff --git a/src/hosts/vscode/VscodeWebviewProvider.ts b/src/hosts/vscode/VscodeWebviewProvider.ts index 59f43ba1..2a194c44 100644 --- a/src/hosts/vscode/VscodeWebviewProvider.ts +++ b/src/hosts/vscode/VscodeWebviewProvider.ts @@ -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 = diff --git a/src/standalone/cline-core.ts b/src/standalone/cline-core.ts index 388382c3..9bdb91da 100644 --- a/src/standalone/cline-core.ts +++ b/src/standalone/cline-core.ts @@ -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, ) } diff --git a/src/standalone/vscode-context.ts b/src/standalone/vscode-context.ts index f57602e7..ca23b256 100644 --- a/src/standalone/vscode-context.ts +++ b/src/standalone/vscode-context.ts @@ -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 = { diff --git a/src/test/host-provider-test-utils.ts b/src/test/host-provider-test-utils.ts index 82d43191..dc25ffc6 100644 --- a/src/test/host-provider-test-utils.ts +++ b/src/test/host-provider-test-utils.ts @@ -15,6 +15,7 @@ export function setVscodeHostProviderMock(options?: { logToChannel?: (message: string) => void getCallbackUri?: () => Promise getBinaryLocation?: (name: string) => Promise + 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", ) }