Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/rare-seals-exist.md
Original file line number Diff line number Diff line change
@@ -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`.
37 changes: 32 additions & 5 deletions packages/quick-edit-extension/src/cfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FromQuickEditMessage, ToQuickEditMessage>) {
this.channel = channel;
this.disposable = Disposable.from(
Expand Down Expand Up @@ -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,
},
});
Expand All @@ -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,
},
});
Expand Down Expand Up @@ -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),
},
});
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions packages/quick-edit/editor-files/workbench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
2 changes: 1 addition & 1 deletion packages/quick-edit/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading