workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: relative imports from a nested `main` in `base_dir` breaks

Open GregBrimble opened this issue 1 year ago • 0 comments

Which Cloudflare product(s) does this pertain to?

Wrangler

What version(s) of the tool(s) are you using?

3.67.1 [Wrangler]

What version of Node are you using?

v20.9.0

What operating system and version are you using?

macOS 14.5 (23F79)

Describe the Bug

Observed behavior

- some/
  - base_dir/
    - nested/
      - index.js
    - foo.js
- wrangler.toml
./some/base_dir/nested/index.js
import { x } from "../foo.js";

export default { fetch() { return new Response(x); } }

./some/base_dir/foo.js
export const x = "Hello, world!";
./wrangler.toml
main = "./some/base_dir/nested/index.js"
base_dir = "./some/base_dir"
no_bundle = true
rules = [
  { type = "ESModule", globs = ["**/*.js"] }
]

npx wrangler dev collects foo.js as an additional module, but errors with

✘ [ERROR] service core:user:worker: Uncaught Error: internal error


✘ [ERROR] MiniflareCoreError [ERR_RUNTIME_FAILURE]: The Workers runtime failed to start. There is likely additional logging output above.

Expected behavior

I'm able to import from a parent when base_dir includes those modules.

I suspect what is happening is that we correctly attach additional modules with their relative path to base_dir (e.g. /foo.js), but the main entry path is being incorrectly set as /index.js rather than /nested/index.js, so relative imports from there are busted.

Steps to reproduce

As above.

Changing the import to import { x } from "/foo.js"; "fixes" the issue, hence my theory on how we're setting the current module path.

Please provide a link to a minimal reproduction

No response

Please provide any relevant error logs

No response

GregBrimble avatar Jul 28 '24 11:07 GregBrimble