Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 BUG: require('debug') resolves to @cloudflare/vitest-pool-workers/dist/worker/lib/debug.mjs instead of to the correct file #6405

Open
hansottowirtz opened this issue Aug 2, 2024 · 1 comment
Labels
bug Something that isn't working vitest Relating to the Workers Vitest integration

Comments

@hansottowirtz
Copy link

hansottowirtz commented Aug 2, 2024

Which Cloudflare product(s) does this pertain to?

Workers Vitest Integration

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

0.4.17

What version of Node are you using?

20

What operating system and version are you using?

macOS Sonoma 14.5

Describe the Bug

Observed behavior

When importing knex, a CJS module importing debug, an error is thrown: Cannot use require() to import an ES Module.
This is because require('debug') resolves to @cloudflare/vitest-pool-workers/dist/worker/lib/debug.mjs, due to these lines:

// `workerd` will always try to resolve modules relative to the referencing
// dir first. Built-in `node:*`/`cloudflare:*` imports only exist at the root.
// We need to ensure we only load a single copy of these modules, therefore,
// we return a redirect to the root here. Note `workerd` will automatically
// look in the root if we return 404 from the fallback service when
// *import*ing `node:*`/`cloudflare:*` modules, but not when *require()*ing
// them. For the sake of consistency (and a nice return type on this function)
// we return a redirect for `import`s too.
if (referrerDir !== "/" && workerdBuiltinModules.has(specifier)) {
return `/${specifier}`;
}
const specifierLibPath = posixPath.join(
libPath,
specifier.replaceAll(":", "/")
);
filePath = maybeGetTargetFilePath(specifierLibPath);
if (filePath !== undefined) {
return filePath;
}

This results in a redirect:

VITEST-POOL-WORKERS:MODULE-FALLBACK 24525: require("<root>/node_modules/knex/lib/debug") relative to <root>/node_modules/knex/lib/client.js?mf_vitest_no_cjs_esm_shim: redirect: <root>/node_modules/@cloudflare/vitest-pool-workers/dist/worker/lib/debug.mjs?mf_vitest_no_cjs_esm_shim

Expected behavior

The file should resolve to the correct module.

Steps to reproduce

Make a worker with import Knex from "knex", and use it in a Vitest test.

Please provide a link to a minimal reproduction

https://github.com/hansottowirtz/workers-sdk-require-debug-repro

Please provide any relevant error logs

No response

@hansottowirtz hansottowirtz added the bug Something that isn't working label Aug 2, 2024
@github-project-automation github-project-automation bot moved this to Untriaged in workers-sdk Aug 2, 2024
@hansottowirtz hansottowirtz changed the title 🐛 BUG: require('debug') resolves to @cloudflare/vitest-pool-workers/dist/worker/lib/debug.mjs` instead of to the correct file 🐛 BUG: require('debug') resolves to @cloudflare/vitest-pool-workers/dist/worker/lib/debug.mjs instead of to the correct file Aug 2, 2024
@petebacondarwin petebacondarwin added the vitest Relating to the Workers Vitest integration label Aug 5, 2024
@petebacondarwin petebacondarwin moved this from Untriaged to Backlog in workers-sdk Aug 5, 2024
@edmundhung
Copy link
Member

We have just added Vite dependency pre-bundling support on v0.6.14 that should help resolving this issue.

For example, you can get ext-dep working with the following config:

export default defineWorkersConfig({
  test: {
    deps: {
      optimizer: {
        ssr: {
          enabled: true,
          include: ["ext-dep"],
        },
      },
    },
    poolOptions: {
      workers: {
        // ...
      },
    },
  },
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that isn't working vitest Relating to the Workers Vitest integration
Projects
Status: Backlog
Development

No branches or pull requests

3 participants