trigger.dev icon indicating copy to clipboard operation
trigger.dev copied to clipboard

bug: `prismaExtension`'s version detection only works when "@prisma/client" is added to `additionalPackages`

Open gmathieu opened this issue 1 year ago • 6 comments

Provide environment information

System: OS: macOS 15.1 CPU: (11) arm64 Apple M3 Pro Memory: 133.08 MB / 18.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.18.0 - /opt/homebrew/opt/node@20/bin/node npm: 10.8.2 - /opt/homebrew/opt/node@20/bin/npm pnpm: 9.12.3 - /opt/homebrew/opt/node@20/bin/pnpm

Describe the bug

Running pnpm dlx trigger.dev@latest deploy fails when using prismaExtension's default config.

Based on this doc, the prismaExtension should automatically detect the installed version. Without a version, the deployment succeeds and silently reports an error:

% pnpm trigger deploy

Trigger.dev (3.2.0)
------------------------------------------------------
┌  Deploying project
│
◇  Retrieved your account details for ...
│
◇  Successfully built project
✘ [ERROR] Failed to apply extension PrismaExtension onBuildComplete Error: PrismaExtension could not determine the version of @prisma/client. It's possible that the @prisma/client was not used in the project. If this isn't the case, please provide a version in the PrismaExtension options.

      at PrismaExtension.onBuildComplete
  (/Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/@[email protected][email protected]/node_modules/@trigger.dev/build/dist/commonjs/extensions/prisma.js:55:19)
      at notifyExtensionOnBuildComplete
  (file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/build/extensions.js:16:33)
      at async buildWorker
  (file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/build/buildWorker.js:70:21)
      at async _deployCommand
  (file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/commands/deploy.js:120:27)
      at async
  file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/commands/deploy.js:77:16
      at async
  file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/cli/common.js:54:28
      at async wrapCommandAction
  (file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/cli/common.js:40:12)
      at async deployCommand
  (file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/commands/deploy.js:76:12)
      at async
  file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/commands/deploy.js:71:13
      at async handleTelemetry
  (file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/cli/common.js:30:9)
      at async Command.<anonymous>
  (file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/commands/deploy.js:69:9)
      at async Command.parseAsync
  (/Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected]/node_modules/commander/lib/command.js:935:5)
      at async main
  (file:///Users/guillaume/alephic/trigger-prisma-bug/node_modules/.pnpm/[email protected][email protected]/node_modules/trigger.dev/dist/esm/index.js:5:5)


│
◇  Successfully deployed version 20241117.8
│
└  Version 20241117.8 deployed with 1 detected task 

When adding version: "5.22.0", the deployment fails entirely:

% pnpm trigger deploy  

Trigger.dev (3.2.0)
------------------------------------------------------
┌  Deploying project
│
◇  Retrieved your account details for ...
│
◇  Successfully built project
▲ [WARNING] prismaExtension could not resolve the DATABASE_URL environment variable. Make sure you add it to your environment variables. See our docs for more info: https://trigger.dev/docs/deploy-environment-variables


│
◇  Failed to deploy project
│
└  Error: Error building image. Full build logs have been saved to /var/folders/4j/sp9fwh5s369b28x_jjckbgd40000gn/T/trigger-Z2WsDu/build-gwivrttk.log

Here's a snippet of the build error:

------
> [build 6/7] RUN node node_modules/prisma/build/index.js generate --schema=./prisma/schema.prisma:
#17 0.456 Prisma schema loaded from prisma/schema.prisma
#17 0.579 Error: Command failed with ENOENT: pnpm add @prisma/[email protected] --silent
#17 0.579 spawn pnpm ENOENT
------
Error: failed to solve: process "/bin/sh -c node node_modules/prisma/build/index.js generate --schema=./prisma/schema.prisma" did not complete successfully: exit code: 1

Workaround

Adding "@prisma/client" to additionalPackages fixes the issue:

import { defineConfig } from "@trigger.dev/sdk/v3";
import { additionalPackages } from "@trigger.dev/build/extensions/core";
import { prismaExtension } from "@trigger.dev/build/extensions/prisma";

export default defineConfig({
  build: {
    extensions: [
      prismaExtension({ schema: "prisma/schema.prisma" }),
      additionalPackages({ packages: ["@prisma/[email protected]"] }),
    ]
  }
})

Reproduction repo

https://github.com/gmathieu/trigger-prisma-bug

To reproduce

  • Check out the https://github.com/gmathieu/trigger-prisma-bug
  • run pnpm install
  • Replace project with your project ID in trigger.config.ts

Note: Don't worry about DATABASE_URL warnings, we're only testing the deployment cycle.

Scenario 1: silent failure

  • run pnpm trigger deploy

Scenario 2: build failure with version

Scenario 3: successful deployment with additionalPackages

Note: additionalPackages properly populates the manifest's externals here, so version can be omitted.

Additional information

No response

gmathieu avatar Nov 17 '24 20:11 gmathieu

Hi! I understand the issue you're experiencing with the prismaExtension's version detection. This appears to be a known limitation in how the extension currently works with Prisma dependencies.

Here's how to resolve this issue:

  1. Update your trigger.config.ts to use both the prismaExtension and additionalPackages together:
import { defineConfig } from "@trigger.dev/sdk/v3";
import { additionalPackages } from "@trigger.dev/build/extensions/core";
import { prismaExtension } from "@trigger.dev/build/extensions/prisma";

export default defineConfig({
  build: {
    extensions: [
      prismaExtension({ 
        schema: "prisma/schema.prisma"
        // Note: You don't need to specify version when using additionalPackages
      }),
      additionalPackages({ 
        packages: ["@prisma/[email protected]"] 
      }),
    ]
  }
})

This configuration ensures that:

  • The Prisma schema is properly loaded
  • The correct version of @prisma/client is available during build
  • The version detection will work correctly

Be aware that:

  • You should match the version of @prisma/client to what's in your project's package.json
  • Make sure your DATABASE_URL environment variable is properly configured for deployment

This is currently the recommended workaround until the version detection functionality is improved in a future release.

Let me know if you need any clarification or run into any other issues!

Cirr0e avatar Nov 25 '24 04:11 Cirr0e

the build also fails if using prismaSchemaFolder extension.

If schema.prisma `generator client { provider = "prisma-client-js" previewFeatures = ["postgresqlExtensions", "prismaSchemaFolder", "fullTextSearchPostgres"] }

datasource db { provider = "postgresql" url = env("DATABASE_URL") } `

And then you create other.prisma and user.prisma.. everything works fine in NextJS.. however trigger.dev build says that schema.prisma is empty and errors out.

RobertHH-IS avatar Dec 14 '24 17:12 RobertHH-IS

the build also fails if using prismaSchemaFolder extension.

If schema.prisma `generator client { provider = "prisma-client-js" previewFeatures = ["postgresqlExtensions", "prismaSchemaFolder", "fullTextSearchPostgres"] }

datasource db { provider = "postgresql" url = env("DATABASE_URL") } `

And then you create other.prisma and user.prisma.. everything works fine in NextJS.. however trigger.dev build says that schema.prisma is empty and errors out.

Yes, I'm currently having the same issue..

rharkor avatar Feb 03 '25 17:02 rharkor

@rharkor can you share your trigger.config file?

We do support the folders, by following the convention that the enclosing folder should be called schema. You can see the code here: https://github.com/triggerdotdev/trigger.dev/blob/6f52b005603e6aac6635f5fb25c8fbd64b4b7579/packages/build/src/extensions/prisma.ts#L115

I know that people are using this, so maybe there's some new case here we don't deal with

matt-aitken avatar Feb 03 '25 19:02 matt-aitken

@rharkor can you share your trigger.config file?

We do support the folders, by following the convention that the enclosing folder should be called schema. You can see the code here:

trigger.dev/packages/build/src/extensions/prisma.ts

Line 115 in 6f52b00 const usingSchemaFolder = dirname(this._resolvedSchemaPath).endsWith("schema");

I know that people are using this, so maybe there's some new case here we don't deal with

Hey thanks for the reply, I've found the issue, it was because I am currently using multiple databases so I finally made my own small extension to handle that, if anyone have the same problem feel free to check the code below (please note that the paths aren't dynamic)

import fs from "fs/promises"
import path from "path"

import { BuildExtension } from "@trigger.dev/build"

export function prismaExtension(): BuildExtension {
  return {
    name: "prisma-extension",
    externalsForTarget(target) {
      if (target === "dev") {
        return []
      }
      return ["@prisma/client", "@prisma/engines"]
    },
    onBuildComplete: async (context, manifest) => {
      context.logger.debug("Copying the prisma schema to the output directory")
      //* Main database
      await fs.cp(
        path.join(__dirname, "../../../../../packages/database/main/prisma"),
        path.join(manifest.outputPath, "main-prisma", "prisma"),
        { recursive: true }
      )
      //* Logs database
      await fs.cp(
        path.join(__dirname, "../../../../../packages/database/logs/prisma"),
        path.join(manifest.outputPath, "logs-prisma", "prisma"),
        { recursive: true }
      )

      context.logger.debug(`Adding the prisma layer with the following commands`, {
        commands: ["cd main-prisma && npx prisma generate", "cd logs-prisma && npx prisma generate"],
        env: {},
        dependencies: {
          prisma: "latest",
        },
      })
      context.addLayer({
        id: "prisma",
        commands: ["cd main-prisma && npx prisma generate", "cd logs-prisma && npx prisma generate"],
        dependencies: {
          prisma: "latest",
        },
        build: {
          env: {},
        },
      })
    },
  }
}

rharkor avatar Feb 04 '25 17:02 rharkor

@rharkor nice!

matt-aitken avatar Feb 04 '25 18:02 matt-aitken