zenstackhq / zenstack

Fullstack TypeScript toolkit that enhances Prisma ORM with flexible Authorization layer for RBAC/ABAC/PBAC/ReBAC, offering auto-generated type-safe APIs and frontend hooks.
https://zenstack.dev
MIT License
2.07k stars 88 forks source link

@zenstack/runtime errors when using trpc plugin and Next 13 #433

Closed tlancina closed 1 year ago

tlancina commented 1 year ago

The generated helper.ts has import { isPrismaClientKnownRequestError } from '@zenstackhq/runtime' which prints the following stack:

../../node_modules/vscode-languageserver-textdocument/lib/umd/main.js
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted

Import trace for requested module:
../../node_modules/vscode-languageserver-textdocument/lib/umd/main.js
../../node_modules/langium/lib/default-module.js
../../node_modules/langium/lib/index.js
../../node_modules/@zenstackhq/language/generated/ast.js
../../node_modules/@zenstackhq/language/ast.js
../../node_modules/@zenstackhq/sdk/utils.js
../../node_modules/@zenstackhq/sdk/index.js
../../node_modules/@zenstackhq/runtime/enhancements/policy/handler.js
../../node_modules/@zenstackhq/runtime/enhancements/policy/index.js
../../node_modules/@zenstackhq/runtime/index.js
../../packages/database/src/trpc/_generated/helper.ts
../../packages/database/src/trpc/_generated/routers/Product.router.ts
../../packages/database/src/trpc/_generated/routers/index.ts
../../packages/database/src/trpc/routers/index.ts
../../packages/database/src/trpc/handlers.ts
../../packages/database/src/index.ts

I can work around it by changing the import to import { isPrismaClientKnownRequestError } from '@zenstackhq/runtime/error' so it doesn't pull in the enhancements stuff, but since the file is generated it would be nice if it didn't throw 😄

Environment

Additional context This is a NextJS Turborepo.

I tried to quickly repro in the todos repo, but was unable to. I sadly don't have time at the moment to dig further, so am opening this in case it is immediately obvious what the issue is.

ymc9 commented 1 year ago

Hi @tlancina , thanks for reporting this. I don't have any immediate thoughts about the cause but we'll definitely look into this.

@jiashengguo , it seems runtime's dependency to langium indirectly introduces dependency to vscode stuff. Not sure why this broke, but I think maybe we should investigate to eliminate that?

tlancina commented 1 year ago

Thanks, I wasn't sure it wasn't user error since I was still unable to reproduce it simply within sample-todo-nextjs.

There were actually quite a few other errors thrown, which I missed because I thought it was log spam of the same error.

Additional errors:

../../node_modules/@zenstackhq/runtime/version.js
Module not found: Can't resolve '../package.json' in '[...]/node_modules/@zenstackhq/runtime'

Import trace for requested module:
../../node_modules/@zenstackhq/runtime/version.js
../../node_modules/@zenstackhq/runtime/enhancements/policy/policy-utils.js
../../node_modules/@zenstackhq/runtime/enhancements/policy/handler.js
../../node_modules/@zenstackhq/runtime/enhancements/policy/index.js
../../node_modules/@zenstackhq/runtime/index.js
../../packages/database/src/trpc/_generated/helper.ts
../../packages/database/src/trpc/_generated/routers/Product.router.ts
../../packages/database/src/trpc/_generated/routers/index.ts
../../packages/database/src/trpc/routers/index.ts
../../packages/database/src/trpc/handlers.ts
../../packages/database/src/index.ts
./src/lib/auth.ts
../../node_modules/@zenstackhq/runtime/enhancements/utils.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
../../node_modules/@zenstackhq/runtime/enhancements/utils.js
../../node_modules/@zenstackhq/runtime/enhancements/omit.js
../../node_modules/@zenstackhq/runtime/enhancements/index.js
../../node_modules/@zenstackhq/runtime/index.js
../../packages/database/src/trpc/_generated/helper.ts
../../packages/database/src/trpc/_generated/routers/Product.router.ts
../../packages/database/src/trpc/_generated/routers/index.ts
../../packages/database/src/trpc/routers/index.ts
../../packages/database/src/trpc/handlers.ts
../../packages/database/src/index.ts
./src/lib/auth.ts
../../node_modules/@ts-morph/common/dist/typescript.js
Module not found: Can't resolve 'source-map-support' in '[...]/node_modules/@ts-morph/common/dist'

Import trace for requested module:
../../node_modules/@ts-morph/common/dist/typescript.js
../../node_modules/@ts-morph/common/dist/ts-morph-common.js
../../node_modules/ts-morph/dist/ts-morph.js
../../node_modules/@zenstackhq/sdk/code-gen.js
../../node_modules/@zenstackhq/sdk/index.js
../../node_modules/@zenstackhq/runtime/enhancements/policy/handler.js
../../node_modules/@zenstackhq/runtime/enhancements/policy/index.js
../../node_modules/@zenstackhq/runtime/index.js
../../packages/database/src/trpc/_generated/helper.ts
../../packages/database/src/trpc/_generated/routers/Product.router.ts
../../packages/database/src/trpc/_generated/routers/index.ts
../../packages/database/src/trpc/routers/index.ts
../../packages/database/src/trpc/handlers.ts
../../packages/database/src/index.ts
./src/lib/auth.ts

I believe this might be related to NextJS 13 and this: https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackages.

There seem to be other issues with similar messages: https://github.com/vercel/next.js/issues/50042, https://github.com/vercel/next.js/issues/50035, https://github.com/vercel/next.js/issues/46493, https://github.com/vercel/next.js/issues/42277.

If I add langium to serverComponentsExternalPackages it still throws some of the other errors, but compiles successfully.

I'm not sure I fully understand the issue or what NextJS bundling entails, but feel free to close as I don't think this is a ZenStack issue.

tlancina commented 1 year ago

Just realized I mistyped serverComponentsExternalPackages last night as zenstack/runtime instead of @zenstackhq/runtime.

Closing as this is a Next config issue. If anyone else has this problem, the solution is to add serverComponentsExternalPackages: ["@zenstackhq/runtime"] to the Next config as documented here: https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackages.

ymc9 commented 1 year ago

Thank you for the update @tlancina . I'm reopening it as a reminder to include a FAQ for the website.

Sanskar696 commented 1 year ago

hey there @ymc9 facing similar kind of error while calling sequelize models in middleware.js

Import trace for requested module: ./node_modules/sequelize/lib/dialects/abstract/connection-manager.js ./node_modules/sequelize/lib/dialects/mariadb/connection-manager.js ./node_modules/sequelize/lib/dialects/mariadb/index.js ./node_modules/sequelize/lib/sequelize.js ./node_modules/sequelize/lib/index.js ./node_modules/sequelize/lib/index.mjs ./models/init_models.js

./node_modules/sequelize/lib/dialects/abstract/connection-manager.js Critical dependency: the request of a dependency is an expression

Import trace for requested module: ./node_modules/sequelize/lib/dialects/abstract/connection-manager.js ./node_modules/sequelize/lib/dialects/mariadb/connection-manager.js ./node_modules/sequelize/lib/dialects/mariadb/index.js ./node_modules/sequelize/lib/sequelize.js ./node_modules/sequelize/lib/index.js ./node_modules/sequelize/lib/index.mjs ./models/init_models.js

Sanskar696 commented 1 year ago

image

Middleware code hey can you help me out @tlancina

ymc9 commented 1 year ago

Have you tried using the setting @tlancina suggested to exclude sequelize from bundling?

serverComponentsExternalPackages: ["sequelize"] 
ymc9 commented 1 year ago

Closing for now.

space1worm commented 1 year ago

@ymc9 I have similar issue, I also tried serverComponentsExternalPackages: ["sequelize"] but didn't work I am facing this issue ONLY when I am using middleware in project

additional info: version: 13.4.13 router: APP

ymc9 commented 1 year ago

Hi @space1worm , which version of ZenStack are you on? Do you mind sharing the error stack ?