diff --git a/.changeset/rare-seals-exist.md b/.changeset/rare-seals-exist.md new file mode 100644 index 000000000000..650b1c100b73 --- /dev/null +++ b/.changeset/rare-seals-exist.md @@ -0,0 +1,9 @@ +--- +"@cloudflare/quick-edit": patch +--- + +Fix relative path computation when the root folder name appears multiple times in a path + +Previously, the logic assumed the root folder appeared exactly once in the path. When the root folder name appeared more than once, file modifications were not correctly detected. + +For example, if the root folder is `my-worker`, a path like `/my-worker/my-worker/util.js` would incorrectly return `/` instead of `/my-worker/util.js`. diff --git a/packages/quick-edit-extension/src/cfs.ts b/packages/quick-edit-extension/src/cfs.ts index 3b83bf61429d..dd5f2cac192d 100644 --- a/packages/quick-edit-extension/src/cfs.ts +++ b/packages/quick-edit-extension/src/cfs.ts @@ -106,6 +106,33 @@ export class CFS private readRoot: ((value: [string, FileType][]) => void) | null = null; + /** + * + * Given a path this function returns the path relative to the root folder, this function also handles the case in which the root folder + * is not present in the path, or present in the middle of it. + * + * @example + * Assuming that the root folder is `my-worker`: + * - given `/my-worker/worker.js` returns `/worker.js` + * - given `/workspace/my-worker/worker.js` returns `/worker.js` + * - given `/workspace/my-worker/sub-dir/my-worker/worker.js` returns `/sub-dir/my-worker/worker.js` + * - given `/my-worker/my-worker/util.js` returns `/my-worker/util.js` + * + * @param path The target path + * @returns The path relative to the root folder + */ + private getRootRelativePath(path: string): string { + const rootFolderStr = `/${this.rootFolder}/`; + const indexOfRoot = path.indexOf(rootFolderStr); + + if (indexOfRoot < 0) { + // The root folder is not in the path so let's return the path as is + return path; + } + + return path.slice(indexOfRoot + rootFolderStr.length); + } + constructor(channel: Channel) { this.channel = channel; this.disposable = Disposable.from( @@ -268,7 +295,7 @@ declare module "*.bin" { this.channel.postMessage({ type: "CreateFile", body: { - path: uri.path.split(this.rootFolder)[1], + path: this.getRootRelativePath(uri.path), contents: content, }, }); @@ -285,7 +312,7 @@ declare module "*.bin" { this.channel.postMessage({ type: "UpdateFile", body: { - path: uri.path.split(this.rootFolder)[1], + path: this.getRootRelativePath(uri.path), contents: content, }, }); @@ -315,13 +342,13 @@ declare module "*.bin" { this.channel.postMessage({ type: "DeleteFile", body: { - path: oldUri.path.split(this.rootFolder)[1], + path: this.getRootRelativePath(oldUri.path), }, }); this.channel.postMessage({ type: "CreateFile", body: { - path: newUri.path.split(this.rootFolder)[1], + path: this.getRootRelativePath(newUri.path), contents: await this.readFile(newUri), }, }); @@ -353,7 +380,7 @@ declare module "*.bin" { this.channel.postMessage({ type: "DeleteFile", body: { - path: uri.path.split(this.rootFolder)[1], + path: this.getRootRelativePath(uri.path), }, }); this._fireSoon( diff --git a/packages/quick-edit/editor-files/workbench.ts b/packages/quick-edit/editor-files/workbench.ts index 39c87f8fe624..3801a6cc9490 100644 --- a/packages/quick-edit/editor-files/workbench.ts +++ b/packages/quick-edit/editor-files/workbench.ts @@ -62,8 +62,8 @@ function createEditor(port: MessagePort) { const messagePorts = new Map(); - // This passes the MessagePort through to the `cloudflare.quick-edit-extension` VSCode extension, which is preloaded - messagePorts.set("cloudflare.quick-edit-extension", port); + // This passes the MessagePort through to the `cloudflare.@cloudflare/quick-edit-extension` VSCode extension, which is preloaded + messagePorts.set("cloudflare.@cloudflare/quick-edit-extension", port); const folderUri = searchParams.get("worker"); diff --git a/packages/quick-edit/src/index.ts b/packages/quick-edit/src/index.ts index a732d8426356..ea856ab4fe6a 100644 --- a/packages/quick-edit/src/index.ts +++ b/packages/quick-edit/src/index.ts @@ -29,7 +29,7 @@ export default { dataFolderName: ".quick-edit", version: "1.76.0", extensionEnabledApiProposals: { - "cloudflare.quick-edit-extension": [ + "cloudflare.@cloudflare/quick-edit-extension": [ "fileSearchProvider", "textSearchProvider", "ipc",