Skip to content

fix: keep sub-paths of open tabs when a folder is renamed or moved - #3140

Open
divya0795 wants to merge 3 commits into
Acode-Foundation:mainfrom
divya0795:fix/folder-rename-tab-paths
Open

divya0795 wants to merge 3 commits into
Acode-Foundation:mainfrom
divya0795:fix/folder-rename-tab-paths

Conversation

@divya0795

@divya0795 divya0795 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Bug

When a folder is renamed, moved (cut/paste) or deleted, helpers.updateUriOfAllActiveFiles (src/utils/helpers.js) updates the open editor tabs that live under it. It has two defects:

  1. Nested files lose their sub-path. The new URI is built as Url.join(newUrl, file.filename), i.e. new folder + basename. After renaming src to lib, a tab for src/components/Button.js points at lib/Button.js.
  2. Bare prefix match. Matching is new RegExp("^" + escapeStringRegexp(url)) with no / boundary, so renaming or deleting src also rewrites or detaches tabs under src-old/… and src2/…. Renaming a single open file via openFolder.renameItem(old.js, new.js) gives new.js/old.js, and also hits old.json.

Impact. The tab now points at a path that doesn't exist, so the file is reported as deleted and Save fails. If a same-named file exists at the flattened location (src/components/index.js → lib/index.js), Save reads that other file, shows the "file has changed, overwrite?" prompt and, if confirmed, overwrites it with this tab's content.

Callers: openFolder.js (rename folder, delete folder, cut/paste folder, openFolder.renameItem) and fileBrowser.js.

Fix

Match with the existing Url.isSameOrDescendant (boundary-aware) and keep the file's path relative to the renamed folder:

const base = url.endsWith("/") ? url.slice(0, -1) : url;
...
if (!Url.isSameOrDescendant(fileUrl, base)) continue;
if (!newUrl) { file.uri = null; continue; }
const relative = fileUrl.slice(base.length + 1);
file.uri = relative ? Url.join(newUrl, relative) : newUrl;

When detaching (newUrl is null), a file is also treated as inside the root if its URI starts with <root>::. removeStorage passes a bare SAF tree URI (…/tree/primary%3AX), and its documents are …/tree/primary%3AX::primary:X/…. isSameOrDescendant alone would miss them.

escapeStringRegexp is no longer used in helpers.js, so its import is removed. Other files still use it.

Tests

New tests/unit/updateUriOfAllActiveFiles.test.js (5 cases) runs the real helpers.js. All 5 fail on main. The fifth case removes a SAF storage: it detaches that storage's tabs and leaves a …Testhtml2 tree alone.

- "file:///sdcard/proj/lib/components/Button.js",
+ "file:///sdcard/proj/lib/Button.js",
- "file:///sdcard/proj/src-old/legacy.js",
+ "file:///sdcard/proj/lib/legacy.js",
- "file:///sdcard/proj/new.js",
+ "file:///sdcard/proj/new.js/old.js",
Tests  5 failed (5)

With the fix all 5 pass. I also compared old and new behaviour over file, SAF (content://…::primary:…), SFTP, FTP-with-query, root-folder, delete and unrelated-path cases. Only the buggy cases changed. Direct children, SAF/SFTP/FTP direct children, deletes of files inside the folder and unrelated paths produce the same URI as before.

Local gate (macOS, Node 24): npx vitest run 1210 passed / 1 skipped (1205 + 5 new on main), npm run typecheck clean, biome ci . clean, typos clean on the changed files. Not tested on a device.

🤖 Generated with Claude Code

updateUriOfAllActiveFiles rebuilt each matching tab's URI as
newUrl + basename, so a nested file such as src/components/Button.js
became lib/Button.js after renaming src to lib. It also matched on a
bare string prefix, so renaming or deleting src also rewrote or
detached tabs under src-old/ or src2/.

Match with Url.isSameOrDescendant and keep the file's path relative to
the renamed folder. Renaming a single open file (openFolder.renameItem)
now gives the new file URL instead of new.js/old.js.
@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[Medium impact] The change reaches open-tab updates across file-management workflows.

The PR still needs to limit the extra :: match to SAF tree roots before merging.

Findings

  1. P1 Unrelated tabs lose their location ▶

Summary

Keeps nested paths when open files move with a renamed or moved folder.

  • Open tabs keep their paths when files or folders change.

Reviews (3) · Last reviewed commit: "fix: detach tabs from a removed SAF stor..." · Reviewed by Greptile

Comment thread src/utils/helpers.js Resolved
removeStorage passes a bare SAF tree URI, whose documents are
<tree>::<docId>. Url.isSameOrDescendant only accepts a "/" boundary,
so those tabs were no longer detached. Treat "<tree>::" as inside the
removed root when detaching.
Comment thread src/utils/helpers.js Outdated
// A SAF storage root is a bare tree URI; its documents are `<tree>::<docId>`.
if (
Url.isSameOrDescendant(fileUrl, base) ||
fileUrl.startsWith(`${base}::`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Unrelated tabs lose their location

The new :: match applies to ordinary folders, not just SAF tree roots. Deleting sftp://profile/project/src also detaches a tab at sftp://profile/project/src::backup/index.js, even though that separate folder still exists. SFTP listings preserve these valid server filenames. The tab becomes marked deleted and loses its save location.

Limit this extra match to bare SAF tree URIs, and add a test that leaves a non-SAF sibling containing :: attached.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, thanks. The :: match ran for any removed URL, so deleting sftp://profile/project/src also detached a tab under the sibling src::backup. Fixed in 5b7b6d5: the :: match now applies only when Uri.parse reports the removed URL as its own tree root, i.e. a bare SAF tree URI. I added two regression tests ("deleting a non-SAF folder leaves a sibling whose name continues with :: attached", and the same for a folder inside a SAF tree). Both fail without the change (expected [ null, null ] to deeply equal [ null, … ]) and pass with it. The SAF storage removal test still passes. Full suite: 1212 passed / 1 skipped. Biome and typecheck are clean.

Deleting an ordinary folder such as sftp://host/project/src also detached
tabs under a sibling named src::backup. Only a bare SAF tree URI has
documents addressed as <tree>::<docId>, so apply the :: match only when
Uri.parse reports the removed URL as its own tree root.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

2 participants