Skip to content

MySQL void/db crashes in workerd dev: mysql2 imported by absolute path bypasses the dep optimizer #410

Closed

Description

@Brooooooklyn

Summary

With the MySQL dialect, void/db crashes under vp dev (the workerd runtime) with:

ReferenceError: require is not defined
    at runInRunnerObject (workers/runner-worker/index.js)

Postgres is not affected.

Cause

The virtual void/db module imports mysql2 by absolute file path (packages/void/src/plugin-drizzle.ts):

const mysql2Resolved = fileURLToPath(import.meta.resolve('mysql2')); // line 19
// ...
import { createPool } from ${JSON.stringify(mysql2Resolved)};   // line 159

mysql2/index.js is CommonJS only. Vite redirects only bare specifiers to prebundled deps (tryOptimizedResolve). An absolute path skips the dep optimizer, so workerd gets the raw CommonJS file. This happens whether void is installed or workspace-linked, because the resolved path is always under node_modules.

Adding optimizeDeps.include: ['void > mysql2', 'void > mysql2/promise'] doesn't help. Both get prebundled, but the virtual module still imports the unconverted file.

Postgres works because its virtual module imports bare "pg" (line 201), which gets discovered and prebundled. Later import('pg') calls from Void's runtime then reuse that copy.

Other places that dynamically import mysql2/promise from Void's dist and are probably affected too:

  • runtime/better-auth-mysql.ts
  • runtime/migration-handler-mysql.ts

Reproduction

  1. Copy playground/pg-test, switch it to database: 'mysql' with a mysqlTable schema, and set DATABASE_URL in .env.
  2. Run vp dev and request a route that runs db.select().from(users).
  3. The response is 500 ReferenceError: require is not defined. With the pg dialect, the same app gets as far as a connection error.

Suggested fix

Found while reviewing #407 (its description mentions mysql2 as a possible case).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions