Skip to content

Commit 0cd3169

Browse files
Fix quick-edit relative path computation when the root folder name appears multiple times in a path
1 parent 455361b commit 0cd3169

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

.changeset/rare-seals-exist.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@cloudflare/quick-edit": patch
3+
---
4+
5+
Fix relative path computation when the root folder name appears multiple times in a path
6+
7+
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.
8+
9+
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`.

packages/quick-edit-extension/src/cfs.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,33 @@ export class CFS
106106

107107
private readRoot: ((value: [string, FileType][]) => void) | null = null;
108108

109+
/**
110+
*
111+
* Given a path this function returns the path relative to the root folder, this function also handles the case in which the root folder
112+
* is not present in the path, or present in the middle of it.
113+
*
114+
* @example
115+
* Assuming that the root folder is `my-worker`:
116+
* - given `/my-worker/worker.js` returns `/worker.js`
117+
* - given `/workspace/my-worker/worker.js` returns `/worker.js`
118+
* - given `/workspace/my-worker/sub-dir/my-worker/worker.js` returns `/sub-dir/my-worker/worker.js`
119+
* - given `/my-worker/my-worker/util.js` returns `/my-worker/util.js`
120+
*
121+
* @param path The target path
122+
* @returns The path relative to the root folder
123+
*/
124+
private getRootRelativePath(path: string): string {
125+
const rootFolderStr = `/${this.rootFolder}/`;
126+
const indexOfRoot = path.indexOf(rootFolderStr);
127+
128+
if (indexOfRoot < 0) {
129+
// The root folder is not in the path so let's return the path as is
130+
return path;
131+
}
132+
133+
return path.slice(indexOfRoot + rootFolderStr.length);
134+
}
135+
109136
constructor(channel: Channel<FromQuickEditMessage, ToQuickEditMessage>) {
110137
this.channel = channel;
111138
this.disposable = Disposable.from(
@@ -268,7 +295,7 @@ declare module "*.bin" {
268295
this.channel.postMessage({
269296
type: "CreateFile",
270297
body: {
271-
path: uri.path.split(this.rootFolder)[1],
298+
path: this.getRootRelativePath(uri.path),
272299
contents: content,
273300
},
274301
});
@@ -285,7 +312,7 @@ declare module "*.bin" {
285312
this.channel.postMessage({
286313
type: "UpdateFile",
287314
body: {
288-
path: uri.path.split(this.rootFolder)[1],
315+
path: this.getRootRelativePath(uri.path),
289316
contents: content,
290317
},
291318
});
@@ -315,13 +342,13 @@ declare module "*.bin" {
315342
this.channel.postMessage({
316343
type: "DeleteFile",
317344
body: {
318-
path: oldUri.path.split(this.rootFolder)[1],
345+
path: this.getRootRelativePath(oldUri.path),
319346
},
320347
});
321348
this.channel.postMessage({
322349
type: "CreateFile",
323350
body: {
324-
path: newUri.path.split(this.rootFolder)[1],
351+
path: this.getRootRelativePath(newUri.path),
325352
contents: await this.readFile(newUri),
326353
},
327354
});
@@ -353,7 +380,7 @@ declare module "*.bin" {
353380
this.channel.postMessage({
354381
type: "DeleteFile",
355382
body: {
356-
path: uri.path.split(this.rootFolder)[1],
383+
path: this.getRootRelativePath(uri.path),
357384
},
358385
});
359386
this._fireSoon(

0 commit comments

Comments
 (0)