Repository navigation
Description
With the new module registry, process.cwd() returns / while a module loaded with import() from a request evaluates its top level, up to its first await. After that await, it reads /bundle again. The request handler and the top level of a statically imported module both read /bundle. So a relative read at that top level, fs.readFileSync('sub/x.txt'), throws for //sub/x.txt.
This reproduces on workerd 1.20261121.1 with compatibility date 2026-11-21 and no flags, since newModuleRegistry turns on by default from 2026-11-17 (compatibility-date.capnp). I hit it in a lane that runs my site's contract tests a second time inside workerd: tests that read files at the top level of a dynamically imported test module failed or silently skipped.
Repro
In sh on macOS 27.3 (arm64) with Node 26.13.1:
mkdir -p cwd-repro && cd cwd-repro
cat > top.js <<'EOF'
import fs from 'node:fs';
let read;
try { read = fs.readFileSync('sub/x.txt', 'utf8'); } catch (e) { read = 'throws: ' + e.message; }
export const top = { cwd: process.cwd(), read };
EOF
cp top.js lazy.js
cat > worker.js <<'EOF'
import { top as staticTopLevel } from 'top';
export default {
async test() {
const { top: dynamicTopLevel } = await import('lazy');
console.log(JSON.stringify({ inTest: process.cwd(), staticTopLevel, dynamicTopLevel }));
},
};
EOF
for flags in '' '"legacy_module_registry"'; do
name=cwd${flags:+-legacy}
cat > "$name.wd-test" <<EOF
using Workerd = import "/workerd/workerd.capnp";
const unitTests :Workerd.Config = (
services = [(
name = "cwd-test",
worker = (
modules = [
(name = "worker", esModule = embed "worker.js"),
(name = "top", esModule = embed "top.js"),
(name = "lazy", esModule = embed "lazy.js"),
(name = "sub/x.txt", text = "hi"),
],
compatibilityDate = "2026-11-21",
compatibilityFlags = [$flags],
),
)],
);
EOF
done
npx --yes workerd@1.20261121.1 test cwd.wd-test
npx --yes workerd@1.20261121.1 test cwd-legacy.wd-test
# Node control: the same module text, the same relative read
mkdir -p sub && printf hi > sub/x.txt && cp lazy.js lazy.mjs
node --input-type=module -e "const cwd = process.cwd(); const { top } = await import('./lazy.mjs'); console.log(JSON.stringify({ sameCwd: top.cwd === cwd, read: top.read }))"Output, one line per run (default registry, legacy_module_registry, Node), with workerd's [ TEST ] and [ PASS ] lines left out:
{"inTest":"/bundle","staticTopLevel":{"cwd":"/bundle","read":"hi"},"dynamicTopLevel":{"cwd":"/","read":"throws: no such file or directory, readAll '//sub/x.txt'"}}
{"inTest":"/bundle","staticTopLevel":{"cwd":"/bundle","read":"hi"},"dynamicTopLevel":{"cwd":"/bundle","read":"hi"}}
{"sameCwd":true,"read":"hi"}
Expected: dynamicTopLevel is {"cwd":"/bundle","read":"hi"}, as it is under legacy_module_registry and as the same module reads in Node.
Actual: {"cwd":"/","read":"throws: no such file or directory, readAll '//sub/x.txt'"}.
Where it seems to come from
This is my reading of the source at 42ee37e, not checked with a build. The legacy handleDynamicImport declares a TmpDirStoreScope before it evaluates the module (worker.c++). The new registry's eval callback moves the IoContext aside with SuppressIoContextScope and declares no such scope (worker-modules.h). With neither present, ProcessModule::getCwd falls back to "/" (process.c++).
The intent on record is /bundle everywhere: #4921 is titled "Use /bundle as the starting cwd always", and the Node.js process docs say process.cwd() "is initialized to /bundle".
Two smaller things I saw on the same path:
- At that top level,
process.chdir('/bundle')throwsEPERM operation not permitted, chdir 'bundle'; underlegacy_module_registryit succeeds. So the workarounds arelegacy_module_registryor absolute/bundle/...paths. - The
//in//sub/x.txtcomes from the base URL in internal_fs_utils.ts, which appends/to a cwd of/. Lookups still resolve; only the reported path looks wrong.
Searched issues, PRs, and Discussions for process.cwd, cwd import, cwd new_module_registry, cwd bundle, new module registry cwd, and dynamic import IoContext. Nothing matches. The nearest is #8663, which edits that internal_fs_utils.ts line for encoding but doesn't change where the cwd comes from.
AI-assisted. Recon, repro, and issue text by houseguest.
Activity