Skip to content

fix(node): use /bundle as cwd when evaluating dynamically imported modules - #8797

Open
Jeremiah-D wants to merge 2 commits into
cloudflare:mainfrom
Jeremiah-D:fix/8786-process-cwd-lazy
Open

Jeremiah-D wants to merge 2 commits into
cloudflare:mainfrom
Jeremiah-D:fix/8786-process-cwd-lazy

Conversation

@Jeremiah-D

Copy link
Copy Markdown

Fixes #8786.

With the new module registry, the top level of a dynamically imported module saw process.cwd() return / instead of /bundle, and chdir() threw EPERM, so relative reads failed on //.... The eval callback in the new module registry suppresses the IoContext before evaluating, and nothing provided a TmpDirStoreScope, so getCurrentWorkingDirectory() had nothing to read. The legacy dynamic import path and the top-level bootstrap both declare a scope for exactly this reason, so this does the same in the callback — only when none is current yet, since the bootstrap already provides one while the main module is evaluated and the scope asserts against nesting. Temporary files created during evaluation are discarded with the scope, matching top-level behavior.

Added a regression test: a lazily imported module asserts cwd is /bundle at its top level, chdirs to /tmp and back, and reads a sibling bundle module by relative path. I could not run the wd-test locally (no bazel in this environment), so that part relies on CI.

…dules

Signed-off-by: Jeremiah-D <235112616+Jeremiah-D@users.noreply.github.com>
@Jeremiah-D
Jeremiah-D requested review from a team as code owners November 21, 2026 23:20
Copilot AI balanced review requested due to automatic review settings November 21, 2026 23:20
@github-actions

Copy link
Copy Markdown


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@clanky-the-clanker

Copy link
Copy Markdown

This pull request was not reviewed automatically because its author does not have write access to this repository. A maintainer with write access can start a review by commenting clanky review (or clanker review or @clanky-the-clanker review), optionally followed by instructions (e.g. clanky review focus on the retry logic).

@bartlomieju

Copy link
Copy Markdown
Collaborator

Hi @Jeremiah-D, thanks for the PR. Could you please sign the CLA so we can land it?

The wd-test embeds process-cwd-lazy.js, but the file was not listed
in the wd_test data attribute, so all three test variants failed in
CI with: Couldn't read file for embed: process-cwd-lazy.js

Signed-off-by: Jeremiah-D <235112616+Jeremiah-D@users.noreply.github.com>
@Jeremiah-D

Copy link
Copy Markdown
Author

Thanks for taking a look! One thing I found while checking CI: the three process-nodejs-test variants were failing in the 11-25 run with Couldn't read file for embed: process-cwd-lazy.js — the new lazy test module was embedded in the .wd-test but missing from the wd_test data list. Fixed in 2122698 (BUILD.bazel: added process-cwd-lazy.js to the process-nodejs-test data). The new head is up — one wrinkle, the internal build run on it failed in 11 seconds at the 'Check fork status and org membership' step, so the build itself never ran; looks like it'll need someone on your side to kick it off. Happy to fix anything it flags once it runs.

On the CLA: noted — that's on my side to sign, I'll get it done so this can land.

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.

process.cwd() returns / at the top level of a dynamically imported module under the new module registry

4 participants