Skip to content

fix(preload): resolve file: URL preloads like import() specifiers - #22

Merged
steipete merged 1 commit into
mainfrom
claude/preload-file-url
Sep 28, 2026
Merged

steipete merged 1 commit into
mainfrom
claude/preload-file-url

Conversation

@steipete

@steipete steipete commented Sep 28, 2026 •

Copy link
Copy Markdown

What does this PR do?

Makes file: URL preloads work like import() specifiers: on Windows, and on any platform when the path needs percent-escapes.

load_preloads in src/runtime/jsc_hooks.rs resolved a preload by stripping a literal file:// prefix. On POSIX, file:///Users/x/p.mjs happened to become a valid path. On Windows, file:///C:/x/p.mjs became /C:/x/p.mjs. And on every platform, escapes like %20 were passed through literally. Both failed with preload not found. This affects the CLI (--preload, --import), bunfig preloads, and Worker execArgv (--import, --preload, --require), because JSWorker.cpp feeds a Worker's preloads into the same VM list. Node accepts file: URLs for --import on all platforms.

The fixing change is in load_preloads: a preload starting with file:// is converted with bun_url::path_from_file_url. That is the WHATWG file-URL-to-path conversion Bun.resolve and the module loader already use for file:// specifiers. The decoded string is kept alive while its bytes are resolved. node: builtins and plain paths take the same route as before, and errors still name the preload as given.

This surfaced in OpenClaw (openclaw/openclaw#160310): when run from source on Windows, its SQLite workers were started with --import file:///C:/.../tsx/dist/esm/index.mjs and failed to start. OpenClaw is also changing so it no longer hands Bun a tsx preload at all.

How did you verify your code works?

macOS arm64 release builds, on openclaw/main ee83b78:

  • New worker_threads.test.ts case "resolves file: URL preloads like import() specifiers": a preload in a directory whose name contains a space, passed as a file: URL through Worker execArgv --import and through the CLI --preload. Results:

    Build Result
    Fork, this change reverted fails with preload not found
    This branch passes
    Stock Bun 1.4.2 fails too, because it ignores Worker execArgv preloads entirely
  • CLI check with a %20 URL: stock Bun 1.4.2 and the unfixed fork print error: preload not found "file:///tmp/.../spaced%20dir/p.mjs"; this branch runs the preload.

  • execArgv preloads suite: 22 pass. test/cli/run/preload-test.test.js and test/config/bunfig/preload.test.ts: 20 pass, 2 skip, 0 fail.

  • An independent review found no issues.

Windows

Native Windows proof, AWS Windows Server 2022 x64 through Crabbox. Each probe runs with where.exe node exiting 1 and an isolated HOME, USERPROFILE and OPENCLAW_STATE_DIR.

  • Bun A: fork 1.4.3-canary.1+828214650 from run 36375975457. Same preload code as fork main.
  • Bun B: this fork change, 1.4.3-canary.1+732eb97de from run 36386527941.

Archive, ZIP and executable checksums were verified on the box.

OpenClaw source mode: a script run with Bun from the checkout root stores and loads a device auth token through src/infra/device-auth-store.ts, the same path GatewayClient's default device identity uses. BASE is 2cd81939f0 (unfixed OpenClaw), FIX is e09ae95d20 (the OpenClaw change).

OpenClaw + Bun Result
BASE + A exit 1: SqliteWorkerError: SQLite worker stopped before its result was received: BuildMessage: preload not found "file:///C:/pfu/BASE/node_modules/.pnpm/[email protected]/node_modules/tsx/dist/esm/index.mjs" (the reported failure)
FIX + A exit 0, token stored and loaded
BASE + B exit 0, token stored and loaded
FIX + B exit 0, token stored and loaded

Direct Bun checks (preload C:\pfu\...\p.mjs sets globalThis.P = 42):

Check A B
bun --preload file:///C:/pfu/plain/p.mjs -e ... exit 1, preload not found exit 0, 42
same with file:///C:/pfu/spaced%20dir/p.mjs exit 1, preload not found exit 0, 42
Worker execArgv: ["--import", <plain URL>] exit 1, preload not found exit 0, P=42
Worker with the %20 URL exit 1, preload not found exit 0, P=42

load_preloads stripped a literal "file://" before resolving a preload, so
on Windows `--import file:///C:/x/p.mjs` became `/C:/x/p.mjs` and failed
with "preload not found", and percent-escapes were never decoded on any
platform (`file:///tmp/a%20b/p.mjs`). This covers CLI --preload/--import,
bunfig preloads and Worker execArgv, which share the VM preload list.

Convert file: preloads with bun_url::path_from_file_url, the WHATWG
conversion import() specifiers already use, keeping the decoded string
alive while its bytes are resolved.

The worker_threads regression loads a preload from a directory with a
space through Worker execArgv and the CLI; it fails with "preload not
found" on the unfixed fork build and passes with this change.
@steipete
steipete merged commit b05ff51 into main Sep 28, 2026
16 of 17 checks passed
@steipete
steipete deleted the claude/preload-file-url branch September 28, 2026 22:10
steipete added a commit that referenced this pull request Sep 30, 2026
Merge upstream Bun through `ba3f27d1d1ce359d4eed842c135f0f6fba1acb00` into fork main, retaining upstream and contributor history. Land with a merge commit to retain upstream ancestry. All fork fixes #15, #7, #14, and #27 are incorporated and qualified.

The five conflict resolutions preserve the fork’s behavior while integrating upstream changes:

- Keep macOS `ProcessRetry` alongside upstream’s `Tty` event-loop flag.
- Keep the richer worker `execArgv` record and parser for preloads, TLS trust, CPU profiling, and addon/FFI restrictions; incorporate upstream’s invalid process-only flag reporting and its C++ error channel.
- Keep literal `?` file-URL handling in the module loader alongside upstream’s string-code-generation guards.
- Retain one upstream-positioned `ERR_WORKER_INVALID_EXEC_ARGV` mapping; both histories added it, and the automatic merge initially duplicated it.

The fork fixes landed since the previous sync `ee83b78b18` remain present: file-URL preloads (#22), response-finish diagnostics (#23), and macOS silent-run signal/stdio handling (#24). Earlier fork changes are retained by the merge. Upstream’s addon/FFI worker restrictions overlap the fork’s broader implementation; the shared behavior is retained in one parser, with upstream’s new invalid-flag handling added.

WebKit remains `f20ce7744553c910bcf16a33faf976af208de091` on both sides. Direct source inspection confirms that `DFGSSALoweringPhase.cpp` lowers `StringAt`, `StringCharCodeAt`, and `StringCodePointAt` to independent `CheckInBounds` nodes, and the pin includes `JSTests/stress/string-index-dce-bounds-check.js`. This is the upstream replacement for closed oven-sh/WebKit#578, so no pin rollback is needed. The release documentation now reflects that fix.

Final integration also retained both sides of the changelog conflict and added contributor credit for @SebTardif and @RomneyDa. The runtime plugin cache format remains version 34; the frozen upstream uses 33.

Native qualification exposed a pre-existing fork mismatch with two new upstream idle-sweep tests: cached idle state stays false until message timing clears, even after a bodyless response ends inside its handler. Explicit Node HTTP idle sweeps now derive response availability while preserving incomplete TLS handshakes, application-owned parser-error sockets, request bodies, partial heads, queued responses, and tunnels. Both upstream one-read/split-head tests pass unchanged, as does the existing parser-error ownership test. A TLS 1.2 relay control proves a sweep cannot close a handshake in progress. Node 26.10.0 confirms the bodyless close behavior.

Qualified head: `41dffc47212456d5ac80fc6fdddda08c4e4bfccc`, macOS arm64 release build (Bun 1.4.3, WebKit `f20ce7744553c910bcf16a33faf976af208de091`). No local SDK signpost patch was needed. Binary, hardening, duplicate-symbol, formatting, and applicable hosted checks passed. Codex review has no remaining accepted/actionable P0–P2 findings; the rejected non-regular-copy concern is ruled out by unchanged regular-file guards and eight matching baseline/candidate FIFO/device controls.

| File | Pass | Skip | Todo | Fail |
| --- | ---: | ---: | ---: | ---: |
| `test/js/bun/terminal/terminal.test.ts` | 97 | 2 | 0 | 0 |
| `test/js/bun/terminal/terminal-spawn.test.ts` | 23 | 2 | 0 | 0 |
| `test/js/bun/spawn/spawn.test.ts` | 165 | 24 | 0 | 0 |
| `test/js/node/child_process/child-process-stdio.test.js` | 9 | 0 | 0 | 0 |
| `test/js/node/http/node-http.test.ts` | 277 | 1 | 0 | 0 |
| `test/js/node/diagnostics_channel/diagnostics_channel.test.ts` | 26 | 0 | 3 | 0 |
| `test/js/node/sqlite/node-sqlite.test.ts` | 143 | 4 | 0 | 0 |
| `test/js/node/worker_threads/worker_threads.test.ts` | 172 | 0 | 0 | 0 |
| `test/cli/run/preload-test.test.js` | 2 | 0 | 3 | 0 |
| `test/cli/run/no-orphans.test.ts` | 15 | 9 | 0 | 0 |
| `test/cli/install/bun-install-lifecycle-scripts.test.ts` | 70 | 0 | 0 | 0 |
| `test/js/node/fs/fs.test.ts` | 660 | 16 | 0 | 0 |
| `test/js/node/fs/cp.test.ts` | 54 | 6 | 0 | 0 |
| `test/cli/run/transpiler-cache.test.ts` | 27 | 0 | 0 | 0 |
| `test/cli/test/isolation.test.ts` | 41 | 0 | 0 | 0 |
| `test/js/bun/plugin/plugins.test.ts` | 47 | 0 | 0 | 0 |
| `test/js/bun/plugin/plugin-namespace-drive-letter.test.ts` | 1 | 0 | 0 | 0 |
| `test/js/node/disallow-code-generation-from-strings.test.ts` | 28 | 0 | 0 | 0 |
| `test/js/node/http/node-http-server-abort-events.test.ts` | 105 | 0 | 0 | 0 |
| `test/js/node/http/node-http-server-close-drain.test.ts` | 31 | 0 | 0 | 0 |
| `test/js/node/http/node-http-connect.test.ts` | 80 | 1 | 2 | 0 |
| `test/js/node/http/node-http-upgrade-body.test.ts` | 11 | 2 | 0 | 0 |
| `test/js/node/http/node-http-req-socket-pause.test.ts` | 40 | 0 | 0 | 0 |
| `test/js/node/tls/node-tls-server.test.ts` | 99 | 0 | 0 | 0 |
| `test/js/node/tls/node-tls-wrapped-socket-close.test.ts` | 15 | 0 | 0 | 0 |
| `test/cli/install/bun-install-patch.test.ts` | 32 | 0 | 0 | 0 |
| `test/js/bun/io/bun-write.test.js (stream fallback only)` | 1 | 0 | 0 | 0 |
| `test/bundler/compile-argv.test.ts (compiled code-generation flags)` | 3 | 0 | 0 | 0 |
| **Total** | **2274** | **67** | **8** | **0** |

Filesystem and isolation suites used outer concurrency 2; nested stress work, assertions, and timeouts were unchanged. Lifecycle, TLS, and compiled-argument tests enabled the existing internal-test API at process startup (`BUN_FEATURE_FLAG_INTERNAL_FOR_TESTING=1`, `BUN_GARBAGE_COLLECTOR_LEVEL=0`). Initial lifecycle/TLS invocations without those startup flags could not load `bun:internal-for-testing`.

Earlier runs encountered recursive-readdir and copy-stress timeouts; the same readdir workload exceeded its deadline on the pre-sync baseline. Both full filesystem suites passed on the final head. The terminal ESRCH test missed its real kernel race window once (its path-reached assertion failed); the unchanged complete file passed on retry. The known broader Bun.write Response timeout was reproduced on a separately built pre-change baseline during #27 qualification and is not claimed as a passing full-suite run here; the added stream-fallback regression passes.

Additional proof: normal/strict data/blob imports and Workers behaved as expected in all 16 probes; the compiled-argument tests preserve quoted strict-mode floors. OpenClaw smoke with the built runtime passed: `OpenClaw 2026.9.6 (23ad3a5)`. No tag or release is part of this PR.
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