Skip to content

url: fix extra slash on file URL roots - #67955

Open
jaideeppyne wants to merge 1 commit into
nodejs:mainfrom
jaideeppyne:fix-url-pathtofileurl-root
Open

jaideeppyne wants to merge 1 commit into
nodejs:mainfrom
jaideeppyne:fix-url-pathtofileurl-root

Conversation

@jaideeppyne

Copy link
Copy Markdown

pathToFileURL() puts back a trailing slash that path.resolve() drops for non-root paths, but it compared the resolved path with path.sep. That is the host separator, not the separator selected by options.windows.

On a POSIX host, pathToFileURL('C:\\', { windows: true }) appended an extra slash and returned file:///C://. The same input with a resolved root that did not go through that append (C:\\.) already returned file:///C:/. On a Windows host, pathToFileURL('/', { windows: false }) returned file:////. A root already ends in the selected platform's separator, so it must not gain another one. Other directories still keep a single trailing slash.

The new test fails on current main (file:///C://) and passes with this change. test/parallel/test-url-pathtofileurl.js still passes.

Refs: #67091

A closed-source coding agent helped locate this bug and draft the patch. I read pathToFileURL in lib/internal/url.js, reproduced pathToFileURL('C:\\', { windows: true }).href === 'file:///C://' on Node.js v26.7.0, and ran the new test on a binary built from this tree with --node-builtin-modules-path: it failed on unmodified main and passed after this change. The existing pathToFileURL test passed on that binary as well.

pathToFileURL() restores a trailing separator by comparing the
resolved path with path.sep. That is the host separator, so it
disagrees with options.windows. "C:\" on a POSIX host was encoded
as file:///C://, and "/" on a Windows host with { windows: false }
as file:////. Compare against the separator for the selected
platform instead.

Refs: nodejs#67091
Signed-off-by: jaideeppyne <jaideeppyne1997@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

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

Labels

needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants