Skip to content

Fix renaming folders with differently cased subfolder paths - #20804

Open
brandonkelly wants to merge 5 commits into
5.xfrom
bugfix/20773-folder-rename-case
Open

brandonkelly wants to merge 5 commits into
5.xfrom
bugfix/20773-folder-rename-case

Conversation

@brandonkelly

@brandonkelly brandonkelly commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Assets::renameFolderById() found descendants with a case-insensitive LIKE on MySQL, then rewrote their paths with a case-sensitive regex. Subfolders stored as uploads/my-entry/ under a folder at Uploads/ kept their old path after a rename, even when the filesystem had moved their directory with the parent.

The rename now finds every subfolder by parentId and swaps the renamed folder's old path for its new one, keeping the rest of each stored path:

-$descendantFolder->path = preg_replace('#^' . $folder->path . '#', $newFolderPath, $descendantFolder->path);
+$newChildPath = $newFolderPath . substr($oldChildPath, strlen($oldFolderPath));
+// same-case prefix: always rewrite (as before)
+// different case: rewrite only if the directory now exists at the new path and not at the old one
Uploads/ → Renamed/
├─ Uploads/record-only/   → Renamed/record-only/   (same case, as before)
├─ Uploads/Photos/        → Renamed/Photos/
│  └─ Uploads/photos/trip/ → Renamed/photos/trip/  (rest of the path keeps its case)
├─ uploads/moved/         → Renamed/moved/         (directory moved with parent: case-insensitive storage)
└─ uploads/separate/      → uploads/separate/      (separate directory: case-sensitive storage, files stay put)
   └─ Uploads/separate/inside/ → Renamed/separate/inside/

Finding subfolders by parentId also stops the rename from treating regex characters in the old path as a pattern, and from touching folders that only matched the LIKE prefix.

Evidence

tests/unit/services/AssetsTest.php builds the tree above on a Local filesystem, renames Uploads to Renamed, and checks each subfolder's path. The separate expectation depends on whether the filesystem moved it. It fails on 5.x (uploads/moved/ and its child keep their old paths) and passes with this change on MySQL and PostgreSQL.

Related issues

Rebuild descendant folder paths from the parent/child hierarchy when a
folder is renamed, and make ensureFolderByFullPathAndVolume() continue
from an existing folder's actual path, since its lookup can be
case-insensitive.

Fixes #20773

Amp-Thread-ID: https://ampcode.com/threads/T-d76fdf59-13ae-40ce-bedf-b3d57d14879d
Co-authored-by: Amp <amp@ampcode.com>
Copilot AI balanced review requested due to automatic review settings November 17, 2026 17:41

Copilot AI left a comment

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.

🟡 Changes recommended

Renaming and indexing can record asset paths without moving the corresponding files on case-sensitive storage, breaking access to existing assets.

2 open issues
What changed in this PR

This PR addresses #20773 by updating Craft’s asset-folder handling to keep descendant paths consistent when folder casing differs.

Changes:

  • Rebuilds descendant paths through parent-child relationships during renames.
  • Uses existing folders’ stored casing when resolving subfolder paths.
  • Adds regression tests and changelog entries for both behaviors.
File Description
tests/​unit/​services/​AssetsTest.php Adds regression tests for mixed-case renames and subfolder creation.
src/​services/​Assets.php Updates descendant traversal and path-case handling.
CHANGELOG.md Documents both asset-folder fixes.

Review effort: Balanced

Comment thread src/services/Assets.php Resolved
Comment thread src/services/Assets.php Resolved
Keep the original path for a subfolder whose path differs in case from
its renamed parent's, unless its directory moved along with the parent,
as on case-insensitive storage. Revert the ensureFolderByFullPathAndVolume()
change, since the asset indexer uses it to record existing paths.

Amp-Thread-ID: https://ampcode.com/threads/T-d76fdf59-13ae-40ce-bedf-b3d57d14879d
Co-authored-by: Amp <amp@ampcode.com>

Copilot AI left a comment

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.

🟡 Changes recommended

Folder renames can leave descendant paths stale because of casing mismatches and skipped traversal, while the regression test can fail on case-insensitive filesystems.

2 open issues
2 resolved since last review

Review effort: Balanced

Comment thread src/services/Assets.php Resolved
Comment thread tests/unit/services/AssetsTest.php Resolved
Keep the rest of each subfolder's stored path, rather than rebuilding it
from its parent's, and check every subfolder, including those below one
that kept its path.

Amp-Thread-ID: https://ampcode.com/threads/T-d76fdf59-13ae-40ce-bedf-b3d57d14879d
Co-authored-by: Amp <amp@ampcode.com>

Copilot AI left a comment

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.

🟡 Changes recommended

Byte-based path slicing mishandles Unicode case variants with differing UTF-8 lengths, causing moved descendant directories to be skipped.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread src/services/Assets.php Resolved
Case variants such as Ⱥ and ⱥ differ in UTF-8 byte length, so slicing
the prefix by bytes could skip subfolders that moved.

Amp-Thread-ID: https://ampcode.com/threads/T-d76fdf59-13ae-40ce-bedf-b3d57d14879d
Co-authored-by: Amp <amp@ampcode.com>

Copilot AI left a comment

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.

🔵 Needs a closer look

Human assessment is needed because Unicode case variants can evade the prefix comparison, potentially leaving moved child assets pointing to old paths.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Case variants can differ in character count too, such as ß and SS, so
take the prefix by path segment and compare it with full case folding.

Amp-Thread-ID: https://ampcode.com/threads/T-d76fdf59-13ae-40ce-bedf-b3d57d14879d
Co-authored-by: Amp <amp@ampcode.com>

Copy link
Copy Markdown
Member Author

Following up on the "Needs a closer look" note: case variants can also differ in character count (ß folds to ss), so 783cb3d takes the prefix by path segment and compares it with MB_CASE_FOLD, instead of slicing by length. The test adds Straße/ with a subfolder recorded as STRASSE/moved/.

Copilot AI left a comment

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.

🟢 Approval recommended

No active review issues remain for the asset-folder renaming fix.

No open issues 🎉

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[5.x]: Renaming a folder leaves subfolders whose path differs in case behind

2 participants