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

Should I need to use copy-webpack-plugin for schema.prisma in a monorepo? #14

Closed
jschuur opened this issue Aug 9, 2022 · 4 comments
Closed

Comments

@jschuur
Copy link

jschuur commented Aug 9, 2022

I've used serverless-webpack-prisma before in another codebase and it worked fine without giving it any specific help to find schema.prisma. Once I started to use it in a yarn workspaces monorepo test project, I initially got errors like this:

Error: ENOENT: no such file or directory, open '/Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/services/backend/.webpack/service/src/schema.prisma'

Only after I explicitly copied it over using copy-webpack-plugin (based on this comment) with this as part of my webpack.config.js, the error went away:

module.exports = {
  // ...
  plugins: [
    new CopyWebpackPlugin({
      patterns: [
        {
          from: '../../packages/prisma/prisma/schema.prisma',
          to: 'src',
        },
      ],
    }),
  ],
  // ...
}

Even using prismaPath in serverless.yml doesn't seem to help:

custom:
  webpack:
    packager: yarn     # not sure if this is needed?
    includeModules: true
  prisma:
    installDeps: false # or this?
    prismaPath: ../../packages/prisma

So, it works, but I thought the point of @pcrodrigues0's #6 was that you can just give it the path and it shouldn't need anything else?

I say it 'works', but I am still dealing with a Prisma engine/generator error. While there is a general issue with a similar error on the main Prisma repo, I think this is more related to my serverless-webpack-prisma implementation, since in my test monorepo project, Next.js also uses the same Prisma client and it works fine there.

PrismaClientInitializationError: 
Invalid `prisma.video.findMany()` invocation:


  Query engine library for current platform "darwin-arm64" could not be found.
You incorrectly pinned it to darwin-arm64

This probably happens, because you built Prisma Client on a different platform.
(Prisma Client looked in "/Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/services/backend/.webpack/service/src/libquery_engine-darwin-arm64.dylib.node")

Searched Locations:

  /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/services/backend/.prisma/client
  /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/node_modules/@prisma/client
  /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/services/backend/.webpack/service
  /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/services/backend/.webpack/service/src
  /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/services/backend/.webpack/service/src
  /tmp/prisma-engines
  /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/services/backend/.webpack/service/src

You already added the platform "darwin-arm64" to the "generator" block
in the "schema.prisma" file as described in https://pris.ly/d/client-generator,
but something went wrong. That's suboptimal.
@jschuur
Copy link
Author

jschuur commented Aug 9, 2022

For the second part of this issue, my guess is that this plugin is not looking in the right location due to the monorepo structure.

It's definitely creating the engine files in the root node_modules/.prisma/client, but that is not in the list of paths mentioned in the error message:

❯ ls -l node_modules/.prisma/client                                                                                                              .rw-r--r-- 1.1k joostschuur  9 Aug 07:52 edge.d.ts
.rw-r--r--  335 joostschuur  9 Aug 07:52 edge.js
.rw-r--r-- 8.1k joostschuur  9 Aug 13:32 index-browser.js
.rw-r--r-- 251k joostschuur  9 Aug 13:32 index.d.ts
.rw-r--r--  30k joostschuur  9 Aug 13:32 index.js
.rwxr-xr-x  30M joostschuur  9 Aug 07:53 libquery_engine-darwin-arm64.dylib.node
.rwxr-xr-x  33M joostschuur  9 Aug 12:52 libquery_engine-darwin.dylib.node
.rwxr-xr-x  44M joostschuur  9 Aug 13:32 libquery_engine-rhel-openssl-1.0.x.so.node
.rw-r--r--  110 joostschuur  9 Aug 13:32 package.json
.rw-r--r-- 4.7k joostschuur  9 Aug 13:32 schema.prisma

In fact, I can trick it, by creating a symlink to the root node_modules/.prisma folder in services/backend/node_modules (ln -s ../../../node_modules/.prisma . ) and then it runs fine.

Full debug output from prisma generate:

DEBUG="*" yarn prisma:generate                                                                                                                 13:32 Lise
  prisma:engines  binaries to download libquery-engine, migration-engine, introspection-engine, prisma-fmt +0ms
  prisma:loadEnv  project root found at /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/packages/prisma/package.json +0ms
  prisma:tryLoadEnv  Environment variables loaded from /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/packages/prisma/.env +0ms
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
  prisma:getConfig  Using CLI Query Engine (Node-API Library) at: /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/node_modules/prisma/libquery_engine-darwin-arm64.dylib.node +0ms
  prisma:getConfig  Loaded Node-API Library +5ms
  prisma:getConfig  config data retrieved without errors in getConfigNodeAPI +1ms
  prisma:getDMMF  Using CLI Query Engine (Node-API Library) at: /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/node_modules/prisma/libquery_engine-darwin-arm64.dylib.node +0ms
  prisma:getDMMF  Loaded Node-API Library +0ms
  prisma:getDMMF  unserialized dmmf result ready +6ms
  prisma:getDMMF  dmmf retrieved without errors in getDmmfNodeAPI +2ms
  prisma:generator  prismaCLIDir /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/node_modules/prisma +0ms
  prisma:generator  prismaClientDir /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/node_modules/@prisma/client +0ms
  prisma:generator  baseDir /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/packages/prisma/prisma +0ms
  prisma:generator  typescriptPath /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/node_modules/typescript +1ms
  prisma:GeneratorProcess  2022-08-09T12:32:27.879Z prisma:client:generator  requiredEngine: libqueryEngine +0ms
  prisma:getGenerators  neededVersions {
  "8d8414deb360336e4698a65aa45a1fbaf1ce13d8": {
    "engines": [
      "libqueryEngine"
    ],
    "binaryTargets": [
      {
        "fromEnvVar": null,
        "value": "darwin-arm64"
      },
      {
        "fromEnvVar": null,
        "value": "rhel-openssl-1.0.x"
      }
    ]
  }
} +0ms
  prisma:getGenerators  {
  generatorBinaryPaths: {
    libqueryEngine: {
      'darwin-arm64': '/Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/node_modules/prisma/libquery_engine-darwin-arm64.dylib.node',
      'rhel-openssl-1.0.x': '/Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/node_modules/prisma/libquery_engine-rhel-openssl-1.0.x.so.node'
    }
  }
} +107ms
  prisma:GeneratorProcess  2022-08-09T12:32:28.008Z prisma:loadEnv  project root found at /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/package.json +127ms

✔ Generated Prisma Client (4.1.1 | library) to ./../../node_modules/@prisma/client in 90ms
You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client
` ` `
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
` ` `
  prisma:getConfig  Using CLI Query Engine (Node-API Library) at: /Users/joostschuur/Code/Personal/_Tests/yarn-workspaces-prisma-nextjs-serverless/node_modules/@prisma/engines/libquery_engine-darwin-arm64.dylib.node +425ms
  prisma:getConfig  Loaded Node-API Library +3ms
  prisma:getConfig  config data retrieved without errors in getConfigNodeAPI +1ms

@jschuur
Copy link
Author

jschuur commented Aug 9, 2022

I think I've solved my issue. I ended up pointing the output path in schema.prisma to the root node_modules/.prisma/client folder (relative to the prisma subdir of my prisma workspace).

Seems to work with this:

generator client {
  provider        = "prisma-client-js"
  binaryTargets   = ["native", "rhel-openssl-1.0.x"]
  previewFeatures = ["fullTextSearch"]
  output          = "../../../node_modules/.prisma/client"
}

But isn't this what prismaPath was supposed to be for?

@danieluhm2004
Copy link
Owner

hello.
I appreciate for your interest.

Sorry, but that library doesn't support yarn monorepo function yet.

We are working hard to support you, but we are having difficulties in applying because of the lack of professional knowledge about monorepo. If you have solved the problem, could you please send me a pull request?

thank you

@jakemonton
Copy link

I had the same issue with M1 using npm workspaces. Seems like Prisma is looking at the wrong directory to search for the binary. I tried @jschuur solution by adding the specific output location and it did work! Thanks @jschuur

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants