Skip to content

fix(runtime): decode URL-encoded ESM specifiers - #167

Merged
steipete merged 3 commits into
mainfrom
claude/fix-esm-percent-specifiers
Oct 14, 2026
Merged

steipete merged 3 commits into
mainfrom
claude/fix-esm-percent-specifiers

Conversation

@steipete

@steipete steipete commented Oct 14, 2026 •

Copy link
Copy Markdown

What does this PR do?

ESM imports with URL-encoded paths fail to load files such as space # percent% helper.mjs. Decode relative and URL-rooted absolute ESM path specifiers once, after separating their query/fragment, and preserve decoded filename delimiters through the existing module-key path. This also fixes static dependencies in modules loaded through require(esm).

Node 24 is the reference. %3F names a literal ? in a filename; an unescaped ? starts a query. %2F and %5C reject with TypeError/ERR_INVALID_MODULE_SPECIFIER. Malformed escapes and malformed UTF-8 reject with URIError. Raw Windows drive-letter/UNC path extensions, ordinary CommonJS require paths, already-resolved ESM keys, plugin-resolved filesystem paths, already-decoded file URLs, and synthetic CLI entry paths stay literal. Embedded modules retain their canonical single key.

Adapts the ESM-only decoding approach from @robobun in oven-sh/bun#35614. Differences from that proposal are intentional Node parity:

  • Decode URL-rooted absolute authored ESM paths as well as ./ and ../. Raw Windows drive-letter/UNC spellings retain Bun’s literal-path behavior; Node rejects those spellings.
  • Permit encoded ? filenames, with distinct query/fragment syntax.
  • Use Node's separator error code and reject malformed UTF-8 as URIError.
  • Interpret unescaped # as a fragment; the existing literal-# test now uses %23 to name that file.

Each case passes with Node 24.21.0 through static imports, dynamic imports, and ESM graphs loaded by require(). Related upstream work: #35601. No competing upstream PR is proposed.

How did you verify your code works?

  • Node 24.21.0 passes all nine shared fixture groups (static/dynamic/require(esm) × successful paths/question-mark filenames/invalid escapes), plus the literal CLI filename and raw-fragment checks.
  • Linux x64 release build and bun run build:release test test/js/bun/resolve/resolve.test.ts -t 'ESM specifier percent-encoding': unpatched: 3 pass / 10 fail; patched: 13 pass / 0 fail. One unrelated filtered platform test is skipped in both runs.
  • bun run build:release test test/js/bun/resolve test/js/bun/module-graph test/js/node/module, with the release test-internals environment and test dependencies installed: 3,491 pass, 10 existing skips, 5 todo, 4 inherited failures across 60 files. All resolver and node:module tests pass. The four ModuleGraph failures (two constructor-column expectations, CommonJS namespace keys, and package-exports error wording) reproduce on the unpatched binary from the same base.
  • BUN_CODEGEN_DIR="$PWD/build/release/codegen" bun run rust:check-all: 2 targets pass, 10 targets skipped because their standard libraries are unavailable.
  • OpenClaw src/plugins/native-module-require.test.ts at b658d87152fbb7d667a494435b2e73c5fddc6341: unpatched Bun 15 pass / 1 fail / 1 existing skip; patched Bun 16 pass / 1 existing skip; Node 17 pass. Runs use private HOME/state/temp directories and the selected Bun first on PATH. The existing skip is gated on Node module-hook availability.
  • The expanded focused group passes 14 tests on the macOS CI binary, including a literal drive-letter/UNC-path control. This control preserves a Bun extension; it is separate from the nine shared Node fixture groups.
  • Byte-search source lint: 3 pass / 0 fail.
  • Independent Codex and Claude reviews are scoped-clean at P2.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 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-14T10:42:33.459343Z c1ffd2e 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: beb540d1f8

ℹ️ 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/jsc/VirtualMachine.rs
Comment on lines +5759 to +5761
let specifier_utf8 = if let Some(decoded) = &decoded_specifier {
decoded_specifier_utf8 = decoded.to_utf8();
&decoded_specifier_utf8

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Return the decoded path from the preserve-symlinks fast path

On POSIX with --preserve-symlinks, an absolute ESM specifier containing a literal backslash and another escape (for example, /tmp/foo\\space%20name.mjs) is checked for existence using this decoded value, but the backslash fast path later returns specifier.clone(), which is still percent-encoded. The subsequent ResolvedEsm load deliberately skips decoding, so Bun attempts to load the nonexistent %20 filename even though the decoded file was just found; preserve and return the decoded specifier on that path.

Useful? React with 👍 / 👎.

@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: fd75d01e95

ℹ️ 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/jsc/VirtualMachine.rs
Comment on lines +5785 to +5787
if bun_core::strings::contains_char(path, b'\\')
&& !bun_core::strings::contains_char(path, 0)
&& bun_paths::is_absolute(path)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve the suffix in the POSIX backslash fast path

On POSIX with the default preserve_symlinks=false, an absolute ESM path containing a legal backslash, a percent escape, and a query or fragment reaches this fast path after encoded_suffix has been separated. A successful realpath returns before the common epilogue copies that suffix into the query output, so an import such as /tmp/a\\b%20c.mjs?one is registered as plain /tmp/a\\b c.mjs; distinct query imports collapse to one module and import.meta.url loses the suffix. Populate the query before this fast return or route the result through the common epilogue.

Useful? React with 👍 / 👎.

Comment thread src/jsc/VirtualMachine.rs
format_args!("Invalid module \"{}\" must not include encoded \"/\" or \"\\\" characters imported from {}", url_path, referrer),
).to_js()));
}
let decoded = bun_url::PercentEncoding::decode_alloc(path);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Decode before applying the filesystem path-length limit

For percent-heavy but otherwise valid paths, resolve_maybe_needs_trailing_slash applies its existing 1.5 * MAX_PATH_BYTES limit to the encoded specifier before decoding occurs here. For example, on macOS a decoded path of roughly 600 spaces spread across legal components remains below the 1024-byte filesystem limit, while its %20 spelling exceeds the 1536-character guard; Bun therefore reports ENAMETOOLONG for a file Node can load. Apply the filesystem limit after splitting and decoding the pathname, or otherwise account for percent-encoding expansion.

Useful? React with 👍 / 👎.

Decode authored relative and absolute ESM path specifiers after splitting
URL query and fragment syntax. Preserve encoded filename delimiters through
module-key construction and reject encoded separators and malformed UTF-8
with Node-compatible errors.

Keep CommonJS paths, resolved plugin paths, and synthetic CLI entry paths
literal. Adapt the ESM decoding approach from oven-sh#35614.
Replace the positional authored-specifier flag with named provenance and
remove redundant borrows from the plugin lookup calls. This resolves the
Rust CI lint findings without changing module-resolution behavior.
Limit ESM URL decoding to relative and URL-rooted paths so Bun's raw
Windows drive-letter and UNC path extensions retain literal behavior.
Add a compatibility control and document the boundary. Keep the private
provenance enum copyable for its by-value resolver argument.
@steipete
steipete force-pushed the claude/fix-esm-percent-specifiers branch from 9683b7d to c1ffd2e Compare October 14, 2026 10:37

@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: c1ffd2e7e6

ℹ️ 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/jsc/VirtualMachine.rs
Comment on lines +5706 to +5708
&& (authored.starts_with(b"./")
|| authored.starts_with(b"../")
|| authored.starts_with(b"/"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve forward-slash UNC specifiers on Windows

On Windows, //server/share/literal%20.mjs is a UNC filesystem path—Bun's path helpers explicitly recognize matching // separators as UNC—but this condition treats every leading / as URL-rooted and decodes it to //server/share/literal .mjs. This changes which module is loaded for Bun's existing forward-slash UNC form, despite the intended literal handling for raw UNC paths. Use the platform-aware path classification to exclude both UNC spellings from decoding.

AGENTS.md reference: src/AGENTS.md:L199-L203

Useful? React with 👍 / 👎.

@steipete
steipete merged commit f0f3a5c into main Oct 14, 2026
10 checks passed
@steipete
steipete deleted the claude/fix-esm-percent-specifiers branch October 14, 2026 10:56
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.

1 participant