Skip to content

fix(core): percent-encode resolved file URLs - #847

Merged
Brooooooklyn merged 4 commits into
mainfrom
fix/percent-encode-resolved-urls
Oct 16, 2026
Merged

Brooooooklyn merged 4 commits into
mainfrom
fix/percent-encode-resolved-urls

Conversation

@Brooooooklyn

Copy link
Copy Markdown
Member

Closes #837

Problem

The resolve hook turned the resolver's path back into a file: URL without percent-encoding it: POSIX oxc_resolved_path_to_url wrote file://{path} raw, and the Windows drive-letter branch of path_to_url did too. Node then read a raw # as a fragment, ? as a query and %XX as an escape. It loaded a different file and exited 0, or found no file at all.

Fix

  • Encode resolved paths (encode_file_url_path): uses exactly the encode set pathToFileURL uses, on POSIX and on the Windows drive form, with the drive : kept literal. Windows UNC paths share the same encoder. The resolution's query and fragment are appended verbatim. They already carry their leading ? / #, so the old Windows code that pushed an extra ? / # is gone.
  • URL references round trip:
    • file_url_to_path drops ?query / #fragment before decoding.
    • ESM relative and absolute-path specifiers containing % are decoded before resolving.
    • A decoded # is passed to the resolver with its \0# escape. Two cases resolve the file name from its parent directory instead: a path holding ?, and a non-ASCII path holding #. oxc_resolver has no escape for ? and drops the \0# escape byte by byte, mangling multi-byte characters.
  • Encoded separators (%2F, %5C) are never decoded and go to Node, which still raises ERR_INVALID_MODULE_SPECIFIER. Without this, decoding made ./a%2Fb.ts run a/b.ts.
  • tsconfig discovery escapes # for ASCII paths. find_tsconfig parses its argument as a specifier, so compiler options were ignored for files under p#1/.

Tests

  • Rust unit tests compare against pathToFileURL output (POSIX and Windows drive form), and cover suffix appending, round trips, encoded-separator detection and resolver_request.
  • packages/integrate-vitest/__tests__/percent-encoded-paths.spec.ts creates the fixtures at run time, because # and % file names are awkward to commit. Every case has a decoy, and the assertions check stdout, not just the exit code.

Verification (macOS arm64, Node 24.22)

  • The issue's five cases now print 42. So do 21 more: ? in a file name, % in a directory name, relative, JSON, ?v=1#x and .cts imports from inside # directories, and import.meta.url, which matches plain Node.
  • cargo test --all-features, cargo clippy --all-targets --all-features -- -D warnings, vp run test (202 passed) and vp run lint all pass.
  • The Windows-only code paths were not compiled locally; Windows CI will be the first build of them.

Known limits (need oxc_resolver changes)

  • A file name holding ?, or holding # next to non-ASCII text, still falls back to Node's own resolution.
  • tsconfig discovery is not escaped for non-ASCII paths or paths with ?.

🤖 Generated with Claude Code

The resolve hook turned the resolver's path back into a `file:` URL
without percent-encoding it, so Node read a raw `#` as a fragment, `?`
as a query and `%XX` as an escape, and loaded a different file (exit
code 0) or none. Encode the path with the exact set `pathToFileURL`
uses on POSIX and on the Windows drive form, then append the query and
fragment verbatim.

Also make URL references round trip through the resolver: decode
percent-encoded ESM relative specifiers, escape a decoded `#` for the
resolver (resolving from the parent directory when the path is not
ASCII or holds `?`), leave encoded separators (`%2F`, `%5C`) to Node so
it still raises ERR_INVALID_MODULE_SPECIFIER, and escape `#` during
tsconfig discovery.

Closes #837

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-16T07:29:02.135129Z 45e52fc New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 171ce8c336

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/lib.rs Resolved
A relative specifier or file URL holding `%zz`, or an escape that does
not decode to UTF-8 (`%FF`), was resolved verbatim and loaded a file
literally named `a%zz.ts`, where Node throws `URIError: URI malformed`.
Hand such specifiers to Node's own resolution instead.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 37dc6ed1b7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/lib.rs Resolved
`//host/…/a%20b.ts` is a URL network-path reference whose first segment
is the file URL host; Node rejects it with ERR_INVALID_FILE_URL_HOST.
The escape-decoding branch read it as a local path and loaded
`/host/…/a b.ts`. Only decode single-slash absolute paths.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0d494bcee3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/lib.rs Resolved
The URL parser reads `\` as `/` in file URLs, so `./dir\foo%20bar.ts`
names `dir/foo bar.ts`. The resolver read it as a POSIX file-name
character, picked a file literally named `dir\foo bar.ts`, and Node then
rejected its `%5C` URL. Normalize raw backslashes in ESM relative and
absolute-path specifiers and in POSIX file URLs; CommonJS `require`
keeps its literal reading.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Brooooooklyn
Brooooooklyn merged commit e48d081 into main Oct 16, 2026
67 checks passed
@Brooooooklyn
Brooooooklyn deleted the fix/percent-encode-resolved-urls branch October 16, 2026 07:42
Brooooooklyn added a commit that referenced this pull request Oct 17, 2026
Closes #856

## Problem
In an ES module import, `#` starts a URL fragment: Node.js resolves
`import './a#b.ts'` as `./a` with the fragment `#b.ts`. Since #847,
oxc-node loaded a file named `a#b.ts` instead, whenever one existed.
oxc_resolver follows enhanced-resolve: for a specifier with a `#` and no
`?`, `load_parse` first tries the whole specifier as a path. #847
re-attached the raw `?query#fragment` to the resolver's specifier, so
`file:` URLs with a fragment were affected the same way.

## Fix
A URL reference's query and fragment never reach the resolver now. A URL
reference is a `file:` URL, or a relative or absolute-path specifier in
ESM. Its decoded pathname is resolved on its own, and the raw suffix is
appended to the resolved URL, as Node.js does. `resolver_request` no
longer takes a suffix.

`require()` is unaffected: `register.mjs` hands every `require()`
request (no `importAttributes`) to Node.js' own CommonJS resolution
before `createResolve` runs, so `#` and `%` stay file-name characters
there. The request kind isn't inferred from the `require` condition,
which `--conditions=require` adds to imports too.

## Verification
Module URLs match plain Node exactly: `./url.mjs#one` gives
`…/url.mjs#one`, `./url.mjs?q#two` gives `…/url.mjs?q#two`, and an
encoded `./a%23b.mjs` still loads `a#b.mjs`.

## Tests
- The `resolver_request` unit test is updated for the new signature.
- `percent-encoded-paths.spec.ts` gains a block for #856:
- a relative import and a `file:` URL with `#` skip the decoy `a#b.ts`;
  - the query and fragment are kept on the module URL;
  - an encoded `%23` still names the file;
- an import under `--conditions=require` still reads `#` as a fragment;
  - `require()` keeps `#` and `%` as file-name characters.

  The `#` cases fail without the change.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.

Resolved file: URLs are not percent-encoded, so paths with # or % load the wrong file

1 participant