Skip to content

Fix kysely resolve in SQLite/libSQL build#861

Merged
ascorbic merged 2 commits intomainfrom
opencode/issue741-20260430142321
Apr 30, 2026
Merged

Fix kysely resolve in SQLite/libSQL build#861
ascorbic merged 2 commits intomainfrom
opencode/issue741-20260430142321

Conversation

@ask-bonk
Copy link
Copy Markdown
Contributor

@ask-bonk ask-bonk Bot commented Apr 30, 2026

What does this PR do?

Fixes "Cannot find module 'kysely'" at runtime after astro build followed by astro preview or node dist/server/entry.mjs on Node deployments using SQLite or libSQL. Reproduced the issue exactly in demos/simple (same dialect_DAsZyBoz.mjs chunk hash as the user's stack trace), confirmed the fix resolves it.

Root cause. packages/core/src/db/sqlite.ts and packages/core/src/db/libsql.ts used CJS require("kysely") / require("better-sqlite3") / require("@libsql/kysely-libsql") — ostensibly to defer loading at config time. In practice these modules are only ever loaded at runtime via virtual:emdash/dialect, so the deferral served no purpose: astro.config.mjs only reaches db/adapters.ts (the descriptor factory).

The Astro integration's Vite SSR config (packages/core/src/astro/integration/vite-config.ts:417-420) sets noExternal: ["emdash"], which pulls emdash's runtime modules into the user's dist/server/chunks/. Vite preserves dynamic require() calls verbatim in the bundled output, so the user's chunk ended up with a literal require("kysely"). Under pnpm's strict node_modules layout, kysely is only available at node_modules/.pnpm/emdash@x/node_modules/kysely — Node's CJS resolver walks up from dist/server/chunks/ and never finds it.

The peer file db/postgres.ts already used static imports and worked correctly. This change brings the SQLite/libSQL adapters in line with that shape; Vite then resolves the imports at build time and either bundles them or externalizes them through the right resolution context.

Closes #741

Type of change

  • Bug fix
  • Feature
  • Refactor
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes (pnpm --silent lint:json | jq '.diagnostics | length' → 0)
  • pnpm test passes (3059 tests in packages/core)
  • pnpm format has been run
  • I have added a regression test (committed separately, before the fix, to preserve the TDD shape — verified the test fails on the broken code via git stash)
  • I have added a changeset (emdash patch)
  • New features link to an approved Discussion (n/a)

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 4.7 (via /bonk)

Reproduction

In demos/simple (with node 22.22.2):

  1. pnpm install, pnpm build (monorepo)
  2. cd demos/simple && pnpm run build && node ./dist/server/entry.mjs
  3. curl http://localhost:4321/_emdash/admin
  4. Observe EmDash middleware error: Error: Cannot find module 'kysely' from dialect_DAsZyBoz.mjs:9 — same path as the user's stack trace.

After the fix, the same flow boots cleanly and serves the setup wizard.

Notes for reviewer

  • The dialect runtime modules are reached only via the dynamic await import() in astro/middleware.ts:15 and loader.ts:222 for virtual:emdash/dialect, never at config time. The original "to defer loading" comment was protecting against a load chain that doesn't exist.
  • Cloudflare adapter is untouched — it uses its own dialect entrypoints (@emdash-cms/cloudflare/db/{d1,do,playground}) and a separate Vite SSR branch, neither of which goes through these files.
  • A postgres-only or libsql-only site won't accidentally pull better-sqlite3: each adapter is a separate Vite chunk and only the configured one is reachable from the user's graph. The libsql adapter's static import of @libsql/kysely-libsql (an optionalDependencies entry) is the same shape db/postgres.ts has used for pg since it was written, and is only resolved when a site actually configures libsql().
  • Adversarial review pass run via sub-agent; addressed both findings (test regex now strips comments/strings; changeset language clarified).

Closes #741

github run

ask-bonk Bot added 2 commits April 30, 2026 14:41
Add a unit test that fails when packages/core/src/db/{sqlite,libsql,postgres}.ts use a CJS require() call to load an external. Vite preserves dynamic require() calls in the bundled SSR chunks; under pnpm's strict node_modules layout, require("kysely") then fails to resolve from the user's dist/server/chunks/ directory because kysely is only a transitive dep of emdash.
Replace require() in db/sqlite.ts and db/libsql.ts with static imports, matching the existing db/postgres.ts shape. With noExternal: ["emdash"] in the Vite SSR config, these files get bundled into the user's site dist; Vite preserves literal require() calls verbatim, and Node's CJS resolver could not find kysely (a transitive dep of emdash) from the user's dist/server/chunks/ under pnpm's strict node_modules layout.
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 30, 2026

🦋 Changeset detected

Latest commit: efaf216

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 30, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-perf-coordinator efaf216 Apr 30 2026, 02:42 PM

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 30, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
docs efaf216 Apr 30 2026, 02:43 PM

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 30, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-i18n efaf216 Apr 30 2026, 02:43 PM

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 30, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-demo-cache efaf216 Apr 30 2026, 02:44 PM

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented Apr 30, 2026

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
emdash-playground efaf216 Apr 30 2026, 02:44 PM

@github-actions
Copy link
Copy Markdown
Contributor

PR template validation failed

Please fix the following issues by editing your PR description:

  • Check the "I have read CONTRIBUTING.md" checkbox.

See CONTRIBUTING.md for the full contribution policy.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 30, 2026

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@861

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@861

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@861

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@861

emdash

npm i https://pkg.pr.new/emdash@861

create-emdash

npm i https://pkg.pr.new/create-emdash@861

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@861

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@861

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@861

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@861

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@861

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@861

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@861

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@861

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@861

commit: efaf216

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a Node SSR runtime failure in pnpm installs where SQLite/libSQL sites could crash after astro build with Cannot find module 'kysely' due to preserved CJS require() calls inside dialect runtime chunks.

Changes:

  • Switch SQLite and libSQL dialect runtime adapters from CJS require() to static ESM imports so Vite can resolve/bundle dependencies correctly.
  • Add a regression unit test to prevent reintroducing require() in dialect runtime modules.
  • Add a patch changeset documenting the fix and its root cause.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
packages/core/src/db/sqlite.ts Replaces runtime require() usage with static imports for better-sqlite3 and kysely’s SqliteDialect.
packages/core/src/db/libsql.ts Replaces runtime require() usage with a static import of @libsql/kysely-libsql.
packages/core/tests/unit/db/dialect-runtime-imports.test.ts Adds regression coverage to ensure dialect runtime adapters don’t introduce require() calls.
.changeset/fix-dialect-runtime-require-kysely.md Adds release notes for the runtime resolution fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ascorbic ascorbic merged commit 22a16ee into main Apr 30, 2026
39 of 40 checks passed
@ascorbic ascorbic deleted the opencode/issue741-20260430142321 branch April 30, 2026 15:54
@emdashbot emdashbot Bot mentioned this pull request Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot find module 'kysely' on pnpm run preview after build (v0.6.0, Node 22)

2 participants