Skip to content

Fix live preview breaking on # in file or folder names - #857

Open
Tech Guy (lukiod) wants to merge 1 commit into
microsoft:mainfrom
lukiod:fix-hash-in-path-url-encoding
Open

Fix live preview breaking on # in file or folder names#857
Tech Guy (lukiod) wants to merge 1 commit into
microsoft:mainfrom
lukiod:fix-hash-in-path-url-encoding

Conversation

@lukiod

Copy link
Copy Markdown

Bug

PathUtil.EscapePathParts escapes each path segment with encodeURI, which deliberately leaves # (and a few other reserved characters like ?, &) unescaped — it's designed for encoding a full URI, where those characters are structural delimiters, not for encoding an individual path segment.

A file or folder name containing # therefore produces a preview URL where the browser treats everything from # onward as a fragment and never sends it to the server, so the file isn't found — the server falls back to showing the parent directory listing instead.

Fix

Switched EscapePathParts/UnescapePathParts to encodeURIComponent/decodeURIComponent, which escape (and correctly reverse) all reserved characters in a path segment rather than only some of them.

Verification

EscapePathParts/UnescapePathParts don't touch the VS Code API directly, so I could reproduce the exact old vs. new behavior standalone with Node's own encodeURI/encodeURIComponent:

--- old (buggy) ---
escapeOld('#folder/index.html') -> '#folder/index.html'   // # survives, browser truncates here
--- new (fixed) ---
escapeNew('#folder/index.html') -> '%23folder/index.html'
unescapeNew(that)                -> '#folder/index.html'  // round-trips correctly

Added 3 regression tests to src/test/suite/pathUtil.test.ts following the existing file's describe/it pattern, covering escape, round-trip, and a leading #. tsc -p ./ and eslint src are both clean. I wasn't able to run the full Electron-based extension-host test suite in this environment (no Xvfb, and installing one needs root here), so I'm disclosing that rather than claiming it — the new tests are pure (no vscode.* calls, matching the neighboring getEndpointParent tests in the same file) so they should run cleanly in CI.

Fixes #750

PathUtil.EscapePathParts used encodeURI on each path segment, which
deliberately leaves # unescaped (it's meant for encoding a full URI,
where # is a structural delimiter, not an individual path segment). A
folder or file name containing # therefore produced a preview URL
where everything from # onward is treated by the browser as a
fragment and never reaches the server, showing the parent directory
listing instead of the file.

Switched EscapePathParts/UnescapePathParts to
encodeURIComponent/decodeURIComponent, which escape all reserved
characters in a path segment rather than only some of them.

Fixes microsoft#750
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.

Live preview cannot open pages if the containing folder has a "#" in the name

1 participant