Skip to content

Instantly share code, notes, and snippets.

@mizchi
Last active May 6, 2025 14:45
Show Gist options
  • Save mizchi/604da3424917f4b0980645be0d40545b to your computer and use it in GitHub Desktop.
Save mizchi/604da3424917f4b0980645be0d40545b to your computer and use it in GitHub Desktop.

Investigating bugs related to prisma + opennext + auth-js

This is an investigation log written with the assumption that it will be translated in order to report it as an issue on GitHub.

Basically, I am responsible for trying to use prisma's experimental query-compiler, but the interfaces of opennext, prisma, and @auth/adapter-prisma all did not mesh, and the only solution to avoid the problem was to directly patch @auth/adapter-prisma.

What happened with @auth/adapter-prisma is a typical example, and there are two issues: how prisma itself will design its interface in the future, and how opennext should handle cjs.

Code where the problem occurred

An error occurred while validating opennext + next-auth + prisma + prisma-adatper.

import NextAuth, { NextAuthConfig, User } from "next-auth";
import Github from "next-auth/providers/github";
import Credentials from "next-auth/providers/credentials";
import { prisma } from "@/db";
import { PrismaAdapter } from "@auth/adapter-prisma";
export const authConfig: NextAuthConfig = {
adapter: PrismaAdapter(prisma),
providers: [
Github({}),
],
secret: process.env.AUTH_SECRET!,
};
export const { handlers, auth, signIn, signOut } = NextAuth(authConfig);

When you run a preview build in the opennext environment, an error occurs in the following runtime code.

bP=rh({".open-next/server-functions/default/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.mjs"(){D0=dS.fileURLToPath(import.meta.url),Jl=BS.dirname(D0),tf=Ph(import.meta.url),

This is simply an error that calls fileURLToPath(undefined). The wrangler ultimately makes import.meta.url undefined.

Final workaround

The workaround is simple, given the complexity of the problem.@auth/adapter-prisma The code is relatively simple, so you just need to rewrite and patch the TS code directly.

// Avoid this
// import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"

/// check only error code
// If token already used/deleted, just return null
// https://www.prisma.io/docs/reference/api-reference/error-reference#p2025
if (
// error instanceof PrismaClientKnownRequestError &&
(error as any).code === "P2025"
)
return null;
}

Detailed situation

  • prisma generate --no-engine (query-compiler) generates code under src/generated/prisma
  • At this time, a file equivalent to @prisma/client/runtime/library.mjs is not generated
  • @auth/prisma-adapter tries to reference an instance with import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"
  • At this time, fileURLToPath(import.meta.url) in @prisma/client/runtime/library.mjs is evaluated
  • opennext compiles server-side code to cjs
  • import.meta.url in the cjs environment is not statically deployed by the bundler (webpack/esbuild)
  • import.meta.url becomes undefined in the cloudflare workerd environment
  • Prisma issue
  • @prisma/client/runtime/library.mjs is not generated
  • Even if it is generated, porting existing code will not work in the workerd environment
  • @auth/prisma-adapter issue
  • output = "../src/generated/prisma" pattern is not taken into consideration
  • Opennext issue
  • Should import.meta.url in cjs be statically expanded?

Multiple issues

  • prisma generate --no-engine (query-copmiler) issue
  • @prisma/client/runtime/library.mjs is not generated
  • Even if this is generated, porting existing code will not work in a workerd environment
  • @auth/prisma-adapter issue
  • output = "../src/generated/prisma" pattern is not taken into consideration
  • Opennext issue
  • Should import.meta.url in cjs be statically expanded?

I'm very tired.

Simply. To just work around my problem, I would simply build opennext with only libraries that don't reference @prisma/client/runtime/library. But I feel like I'm getting a glimpse of potential issues with each library.

prisma + opennext + auth-js にまつわるバグの調査

これは GitHub の Issue として報告するために翻訳前提で書いた調査ログです。

基本的に、 prisma の実験的な query-compiler を使おうとした私に責任があるのですが、 opennext, prisma, @auth/adapter-prisma いずれもインターフェースが噛み合わず、問題の回避には @auth/adapter-prisma に直接パッチを当てる以外の解決方法がない状態になりました。

@auth/adapter-prisma で発生したのは端的な例で、prisma 自体が今後どのようにインターフェースを設計するか、opennext が cjs をどのように扱うべきかの2つの課題があります。

問題が発生したコード

opennext + next-auth + prisma + prisma-adatper の検証中にエラーが発生しました。

import NextAuth, { NextAuthConfig, User } from "next-auth";
import Github from "next-auth/providers/github";
import Credentials from "next-auth/providers/credentials";
import { prisma } from "@/db";
import { PrismaAdapter } from "@auth/adapter-prisma";
export const authConfig: NextAuthConfig = {
  adapter: PrismaAdapter(prisma),
  providers: [
    Github({}),
  ],
  secret: process.env.AUTH_SECRET!,
};
export const { handlers, auth, signIn, signOut } = NextAuth(authConfig);

opennext 環境で preview ビルドを行うと、以下のランタイムコードでエラーが発生します。

bP=rh({".open-next/server-functions/default/node_modules/.pnpm/@[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/library.mjs"(){D0=dS.fileURLToPath(import.meta.url),Jl=BS.dirname(D0),tf=Ph(import.meta.url),

これは端的に fileURLToPath(undefined) を呼ぶエラーです。 wrangler は最終的に import.meta.url を undefined にします。

最終的なワークアラウンド

問題の複雑さに対して、ワークアラウンドはシンプルです。@auth/adapter-prisma のコードは比較的単純なので、直接TSのコードを書き換えてパッチするだけです。

// Avoid this
// import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"

/// check only error code
        // If token already used/deleted, just return null
        // https://www.prisma.io/docs/reference/api-reference/error-reference#p2025
        if (
          // error instanceof PrismaClientKnownRequestError &&
          (error as any).code === "P2025"
        )
          return null;
      }

詳細な状況

  • prisma generate --no-engine (query-compiler) は src/generated/prisma 以下にコードを生成する
    • この時、 @prisma/client/runtime/library.mjs 相当のファイルを生成しない
  • @auth/prisma-adapterimport { PrismaClientKnownRequestError } from "@prisma/client/runtime/library" で インスタンスを参照しようとする
    • このとき、@prisma/client/runtime/library.mjsfileURLToPath(import.meta.url) を評価する
  • opennext はサーバーサイドコードを cjs にコンパイルする
    • cjs 環境の import.meta.url はバンドラー(webpack/esbuild)によって静的展開されない
    • cloudflare workerd 環境では import.meta.url が undefined になる
  • Prisma の問題
    • @prisma/client/runtime/library.mjs が生成されていない
    • 仮にこれを生成するとしても、既存のコードを移植しても workerd 環境で動かない
  • @auth/prisma-adapter の問題
    • output = "../src/generated/prisma" のパターンを考慮していない
  • opennext の問題
    • cjs 内の import.meta.url を静的展開するべきか?

まとめると

  • prisma generate --no-engine (query-copmiler) の問題
    • @prisma/client/runtime/library.mjs 相当のコードが生成されていない
    • 仮にこれを生成するとしても、既存のコードを移植しても workerd 環境で動かない
  • @auth/prisma-adapter の問題
    • output = "../src/generated/prisma" のパターンを考慮していない
  • opennext の問題
    • cjs 内の import.meta.url を静的展開するべきか?

非常に疲れました。

単純に。私の問題を回避するだけならば、 @prisma/client/runtime/library を参照しないライブラリだけで opennext をビルドするだけです。 しかし各ライブラリの潜在的な問題が垣間見えてる気がします。

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