Skip to content

fix(studio-server): renaming a nested folder rewrites the paths files beside it use - #6367

Open
miguel-heygen wants to merge 10 commits into
mainfrom
fix/rename-nested-folder-references
Open

miguel-heygen wants to merge 10 commits into
mainfrom
fix/rename-nested-folder-references

Conversation

@miguel-heygen

@miguel-heygen miguel-heygen commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

What changed

Renaming a folder inside another folder (say assets/clips to assets/takes) left references that files in assets/ write relative to themselves, such as url(clips/a.png), pointing at the old name. The rename already rewrote project-root paths; each file in a folder that holds the renamed path now also gets its own relative spelling rewritten (bare or ./), with the same sibling and existing-path guards.

CSS takes that reading always (a stylesheet resolves paths relative to itself). HTML takes it only where the complete target is an existing file in that folder, else the path stays for the project-root pass, the same rule the runtime uses. Scripts, JSON, Markdown and inline <script> blocks resolve against the page, so they take the project-root pass only.

Edits from both readings are collected from the original text and applied once, so neither rewrites the other's output. Only the renamed path's parent folders get the second reading, so the work stays one pass per file.

This re-ports the earlier change onto the current reference rewriter (entity, URL, srcset and CSS-escape spellings): referenceRewriter now builds edits per reading and rewriteWith applies them together.

What I measured

The 8 new route tests in files.renameReferences.test.ts cover: a stylesheet's bare and ./ paths, root paths and sibling-folder paths left alone, a new path that holds the old folder's name, the folder reading winning where both match, paths that climb back out of the folder, HTML existence rules, and a path the runtime reads from the root. With the folder reading switched off, 6 of the 65 tests in the file fail; with it on, all 65 pass, and the whole routes suite passes (657/657). A page that moves its folder out (assets/clips to media/clips, page assets/i.html) now gets ../media/clips/a.png: the existence check looks at every project file relative to the page's folder, not only those inside it. The new test passes 3 runs in a row (66/66) and fails with that change reverted.

fails without the fix: packages/studio-server/src/routes/files.renameReferences.test.ts "in HTML follows a folder moved out of the page's folder"

What I did NOT exercise

  • A file in a sibling folder naming the renamed path through .. (../clips/a.png from assets/other/) is left as written.
  • A file inside the moved folder naming the folder through .. is left as written, as before.
  • Whether a path leaves the folder is read from at most 256 characters after the match; a .. further out is not seen, which can only skip a rewrite, never make a wrong one.
  • Event-handler attributes in HTML are read like the rest of the file.
  • tsc for the package was not clean on the test machine because the core build output was missing there; no type error comes from this change.

Replaces #4959.

Not covered

Moving a folder out of the folder an HTML file reads beside (for example assets/clips to media/clips, with assets/i.html holding <img src="clips/a.png">) rewrites the CSS beside it but leaves that HTML reference as it was. The rename case in this PR is unchanged by it.

@heygengenesis heygengenesis Bot 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.

Verdict: REQUEST CHANGES · 1 blocker · 1 important · 0 nits · head 2027b9d5

  • blocker — Comments is red on this head: comment-ratchet.mjs reports files.ts comment share 5.8% → 5.9% (225/3853 → 231/3938, 6 comment lines added). CONTRIBUTING.md: "A package file's comment share may not rise", and the PR body doesn't explain it. Cutting 2 comment lines gets it back to ≤ base; the rewriteWith and reaches comments repeat the PR description.
Scope

Read files.ts diff end-to-end and the 8 new route tests. Built parsers/lint/studio-server/core and ran src/routes/: 657/657 pass. Probed over the route: file rename (url(a.png), ./, ?v=), a 3-level nested rename, srcset + inline style + inline script in HTML, md/json left alone, cross-folder move. All correct except the cross-folder HTML case inline. Required CI: green so far, edit-accuracy shards and windows engine-cli still running when I posted.

const [from, to] = [oldPath.slice(at + 1), posix.relative(dir, newPath)];
parentFolderEdits.set(dir, {
css: referenceEdits(from, to, isDirectory, inside, { targets: null }),
html: referenceEdits(from, to, isDirectory, inside, { targets: filesOnly(inside) }),

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.

important — HTML never takes the folder reading on a cross-folder move. targets holds only paths under dir/, but to = posix.relative(dir, newPath) starts with ../ when the folder leaves dir, so reaches can't match it. Repro: assets/clips/a.png plus assets/i.html <img src="clips/a.png">, then move assets/clips → media/clips. The CSS beside it becomes url(../media/clips/a.png), but the HTML keeps clips/a.png. Before the move that path resolved beside the page; afterwards it resolves to nothing. This isn't a wrong rewrite, just one that's missing, and the PR's list of what it didn't exercise doesn't mention it. A fix is to check existence project-wide against posix.join(dir, to + rest), or note it in the body.

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 273 (base branch 271), smooth 75 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Newly passing (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Not banked yet, reported and not gated (bank a full-grid run's baseline.json) (2)

  • seqscaletwice-scale-px-r0-nested-z100
  • seqscaletwice-scale-px-r0-root-z100

Quarantined, measured but not gated (0)

@miguel-heygen
miguel-heygen force-pushed the fix/rename-nested-folder-references branch from 906e7ac to b9978f7 Compare November 4, 2026 00:02
@github-actions

Copy link
Copy Markdown
Contributor

This PR has had no activity for 14 days, so it is marked stale and will close in 7 days. Push a commit or leave a comment to keep it open. A maintainer can add keep-open to exempt it.

@github-actions github-actions Bot added the stale label Nov 18, 2026
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Merged main in at 8660dacd and answered the review at 2027b9d5:

  • Blocker, comment share: the earlier cuts (ec0ea540) stand. In the merge, the rewrite now lives in helpers/fileReferences.ts, where main moved the rename matcher. It adds no comment lines there, so the ratchet passes.
  • Important, HTML on a cross-folder move: fixed in b9978f77. "in HTML follows a folder moved out of the page's folder" covers your repro: assets/clips → media/clips with <img src="clips/a.png"> in assets/i.html.

The merge carries the change onto main's refactored referenceRewriter. It is now a set of edit producers (referenceEdits) combined by rewriteWith, and renameRewriter(old, new, isDirectory, existing)(text, file) is what updateReferences calls. referenceRewriter keeps its signature for fileNames and the tests. referenceSyntax is no longer exported, since nothing outside the module reads it.

On Linux:

  • With main's fileReferences.ts and files.ts, 7 of the 66 tests in files.renameReferences.test.ts fail, all of them this PR's.
  • At 8660dacd: studio-server tsc passes; src/routes and src/helpers pass 1,867 tests with 3 skipped, 3 runs in a row; Fallow audit against origin/main passes.
  • One earlier run hit an existing load flake in files.heldFile.test.ts ("keeps the app answering while it waits", a 100 ms stall bound on a busy machine). That code isn't touched here, and its fix is going up as its own PR.

@eddy-heygen eddy-heygen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

(Eddy's agent) Reviewed by Eddy's Claude agent at commit 8660dac (the non-test diff read in full by me). This is my first review of this PR; the open change request on it is another reviewer's.

Verdict: approved, good to ship. A correctness fix for renames, not a security change; I found no defect by reading.

Summary
Renaming a nested file or folder rewrote references written from the project root, but not the ones a neighbouring file writes relative to its own folder. The rename now also builds, for each parent folder of the renamed path, a second matcher for folder-relative references, used only for HTML and CSS files that sit in that folder, and applies both sets of edits in one pass.

Blockers

None.

What I checked

  • Which files are rewritten and how they are written is unchanged: the same walk, the same safe-path and writable checks, the same atomic replace with the file's mode kept.
  • The folder-relative matcher is narrower than the root one: only a bare or ./ lead, never in script files, never when the rest of the reference climbs out of the renamed folder, and in HTML only when the rewritten path names a file that exists.
  • Edits from the two matchers are sorted and an overlapping later edit is dropped, so one reference is never rewritten twice.
  • The text scanned after a match is capped at 256 characters, so the existence check stays bounded.

Non-blockers

  • In CSS the folder-relative rewrite does not check that the new path exists (HTML does). A CSS string that merely looks like the old relative name would be rewritten; the tests may cover this, but it is the looser of the two.

CI: 64 pass, 17 skipped, none failing; 2 pending when I posted (Windows jobs for the engine and CLI packages, which this PR does not touch). The studio-server tests, Fallow audit and Typecheck pass.

@miguel-heygen

miguel-heygen commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks. On the non-blocker (CSS doesn't check that the new path exists): that's deliberate. A relative url() or @import in a stylesheet always resolves against the stylesheet's own folder, so for a .css file in the folder, the folder-relative reading is the only one. Requiring the file to exist would skip a rewrite that is plainly right, for example a font or image the CSS names before it has been added. An HTML page is different: Studio and the runtime can also read it from the project root (an inlined sub-composition), so only there do the two readings compete, and the existence check decides between them.

You're right that it's looser in one case: a CSS string outside url()/@import that spells the old relative name (say content: "clips/a.png") is rewritten too. The root-relative pass has always done the same for root paths, so this keeps the two consistent rather than adding a new kind of edit.

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

@heygengenesis review: both findings from your review at 2027b9d5 are answered at 8660dacd. The comment share passes the ratchet (the rewrite now lives in helpers/fileReferences.ts, with no added comment lines), and the cross-folder HTML move is fixed in b9978f77, with "in HTML follows a folder moved out of the page's folder" as its test.

@github-actions github-actions Bot removed the stale label Nov 21, 2026
…der-references

# Conflicts:
#	packages/studio-server/src/routes/files.ts
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Fixed the non-blocking finding in a2e0a90: a page's minified <style> now follows a rename of a folder beside it.

  • Cause: the folder reading in HTML checks that the new path names an existing file, and REFERENCE_END only accepted end, whitespace, ; or , after a closed url(). Minified CSS puts } there, or !important.
  • Fix: it now also accepts }, !, ) (a url() inside image-set(...)) and / (a following comment). A ) followed by anything else, like x(1).png, still doesn't end the reference.
  • Test: "in HTML follows a folder-relative url() in a minified style" covers )}, )!important and image-set(url(...) 1x). Before the fix the first two stayed unrewritten; it passes 3 runs in a row, 67 of 67 in the file. The comment ratchet and every pre-commit check pass.

@vanceingalls vanceingalls left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Re-review at a2e0a90a. Approving.

The delta since my approval at 73b26a0d is one commit, a2e0a90a, touching two files: a one-line change to REFERENCE_END in fileReferences.ts and one new route test. Nothing from main came in.

  • Fix: the closing ) of a url() may now be followed by }, !, ) or /, as well as whitespace, ; and ,. That covers url(clips/a.png)} and url(clips/a.png)!important in a minified <style>, and a nested image-set(url(...)). It is only used by the existence check for HTML, so CSS files and the root pass are unchanged.
  • Quoted and data URLs: a quoted url ends at the quote, so the text after the match is empty and still hits $. A data: url cannot name a project file. The existing quoted-url tests still cover this.
  • Test: it runs } and !important through a rename, and the image-set(... 1x) case rides along. It carries the fails without the fix note. The nested )) and / endings have no test of their own. I'm not asking for one.
  • Genesis's earlier points: the cross-folder HTML case is fixed in b9978f77, and the comment cut is in ec0ea540. Neither is reintroduced, and the Comments check is green.
  • CI: every check is green on this head, including the Windows engine-cli job.
  • Visible UI: none. This is studio-server only.

One non-blocking item: the PR body's "Not covered" section still describes the cross-folder move as unhandled, which b9978f77 fixed. Worth deleting that line.

@jrusso1020 jrusso1020 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Verdict: APPROVE at a2e0a90a1bf50dc6a59ed522a59149bf300bc111.

There is one commit since my approval at 73b26a0d, and it fixes my non-blocking nit:

  • fileReferences.ts:412: REFERENCE_END now also accepts }, !, ) and / after a closing ). A folder-relative url() at the end of a minified rule (url(clips/a.png)}), before !important, or inside image-set(url(...) 1x) now follows a rename.
  • The new test "in HTML follows a folder-relative url() in a minified style" covers all three shapes and checks the file as written.

Local runs (files.renameReferences.test.ts):

  • At this head: 67/67 pass.
  • With fileReferences.ts set back to 73b26a0d, the new test fails (1 fail / 66 pass). That matches its 'fails without the fix:' line.
  • The merge with main is unchanged from 73b26a0d, and only those two files changed.

Need · Size · Reuse · Simpler · Structure: closes the url() gap · one regex and a test · n/a · minimal · ok
Risk: low. The boundary only accepts more of the characters that can follow a closing ) in CSS.

— Rames

@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

Correction to the description's "Not covered" section, which can't be edited here: that case is covered now. Moving a folder out of the folder an HTML file reads beside (assets/clips to media/clips, with assets/i.html holding <img src="clips/a.png">) rewrites that reference to ../media/clips/a.png, as of b9978f7. The test "in HTML follows a folder moved out of the page's folder" in files.renameReferences.test.ts covers it and fails with that change reverted.

@miguel-heygen miguel-heygen added the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 27, 2026

@jerry-heygen jerry-heygen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Changes requested at 414aef56. The HTML folder-relative pass checks whether the rewritten path exists, but it scans past the end of an unquoted URL into the next attribute. For example, with assets/clips/a.png and assets/i.html containing <img src=clips/a.png alt=preview>, renaming assets/clips to assets/takes leaves the image pointing at clips/a.png. In packages/studio-server/src/helpers/fileReferences.ts:349–360, restAfter passes /a.png alt=preview to the existence check. REFERENCE_END does not accept the next attribute as a boundary, and the project-root pass does not match clips/a.png.

Please bound the existence check to the URL span the scanner has already identified, and cover an unquoted src followed by another attribute in the route tests. The prior cross-folder HTML and minified-style fixes are present; the exact-head preview regression and Windows Studio server files checks passed. I did not run a live Studio rename.

— Jerrai (pr-review)

@jerry-heygen jerry-heygen removed the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 27, 2026
@miguel-heygen

Copy link
Copy Markdown
Collaborator Author

@jerry-heygen fixed at c4dcdcc7. The folder-relative existence check now reads only up to the end of the URL span the scanner already found (restAfter takes that span's end), so <img src=clips/a.png alt=preview> checks takes/a.png and not takes/a.png alt=preview. Contexts that aren't URLs keep the old bound.

  • fails without the fix: files.renameReferences.test.ts "in HTML follows an unquoted folder-relative URL followed by another attribute" (an unquoted src and href, each followed by another attribute). At 414aef56 both URLs stay clips/a.png. With the fix both become takes/a.png, and alt/title are left as they were.
  • That file passed 68/68 three runs in a row, and all ten files.* route test files passed (397 passed, 3 skipped), on Linux.

@heygengenesis your 10-26 blocker at 2027b9d5 (comment share in files.ts) is answered: scripts/comment-ratchet.mjs passes at this head against the merge-base (2 changed source files at or under their share), and this commit adds no comments.

Not exercised: a live Studio rename, Windows.

@miguel-heygen miguel-heygen added the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 27, 2026

@jerry-heygen jerry-heygen left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Approved at c4dcdcc7. The folder-relative existence check now stops at the scanner’s URL boundary, so it no longer mistakes the next HTML attribute for part of an unquoted path. The new route test covers both src and href and checks that the following attributes are preserved. The earlier cross-folder and minified-style cases remain covered.

The exact-head preview-regression and Windows Studio server files workflows passed. I did not run a live Studio rename.

— Jerrai (pr-review)

@jerry-heygen jerry-heygen removed the needs-jerrai-review Request a review from Jerrai. Removed after the review posts; re-add to request another. label Nov 27, 2026

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 participants