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

Prisma VS Code extension with mongodb provider crashes when a relation field/type is not defined #1466

Closed
KarthikRaju391 opened this issue Jul 18, 2023 · 11 comments · Fixed by prisma/prisma-engines#4087
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. tech/engines Issue for tech Engines. topic: mongodb topic: vscode topic: wasm
Milestone

Comments

@KarthikRaju391
Copy link

Bug description

I have set up Prisma with MongoDB provider but every time I try to edit the file, the following error is thrown.
image

And this is what the terminal output of the Prisma language server shows:
image
Prisma

How to reproduce

  1. Set up a project with MongoDB as the provider for Prisma.
  2. Try editing the prisma.schema fileMongodb

Expected behavior

No response

Prisma information

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

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["mongodb"]
}

model User {
  id        String    @id @default(auto()) @map("_id") @db.ObjectId
  // account   Business?
  address   Address[]
  dob       DateTime?
  email     String    @unique
  firstName String?
  lastName  String?
  phone     String?
  gender    String?
  metadata  Json?
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
}

Environment & setup

  • OS: WSL in Windows 11
  • Database: MongoDB
  • Node.js version: 16.19.0

Prisma Version

prisma                  : 5.0.0
@prisma/client          : 5.0.0
Current platform        : debian-openssl-3.0.x
Query Engine (Node-API) : libquery-engine 6b0aef69b7cdfc787f822ecd7cdc76d5f1991584 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-3.0.x.so.node)
Schema Engine           : schema-engine-cli 6b0aef69b7cdfc787f822ecd7cdc76d5f1991584 (at node_modules/@prisma/engines/schema-engine-debian-openssl-3.0.x)
Schema Wasm             : @prisma/prisma-schema-wasm 4.17.0-26.6b0aef69b7cdfc787f822ecd7cdc76d5f1991584
Default Engines Hash    : 6b0aef69b7cdfc787f822ecd7cdc76d5f1991584
Studio                  : 0.487.0
Preview Features        : mongoDb
@KarthikRaju391 KarthikRaju391 added the kind/bug A reported bug. label Jul 18, 2023
@millsp
Copy link
Member

millsp commented Jul 19, 2023

Thanks @KarthikRaju391, I can confirm the crash on my end too.

@millsp millsp added bug/2-confirmed Bug has been reproduced and confirmed. tech/engines Issue for tech Engines. team/schema Issue for team Schema. topic: mongodb labels Jul 19, 2023
@janpio
Copy link
Member

janpio commented Jul 19, 2023

Do you get the same error when running npx prisma format?

@Jolg42
Copy link
Member

Jolg42 commented Jul 19, 2023

I can only reproduce this error with the language server, validate shows (before/after edit)
Screenshot 2023-07-19 at 10 30 38

And the language server logs
Screenshot 2023-07-19 at 10 29 59

This looks to be caused by address Address[] as Address is not defined anywhere as a type or model.

@Jolg42
Copy link
Member

Jolg42 commented Jul 19, 2023

Minimal schema for reproduction

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

generator client {
  provider        = "prisma-client-js"
}

model User {
//   id        String    @id @default(auto()) @map("_id") @db.ObjectId
  field   NotDefined
//   field   NotDefined?
//   field   NotDefined[]
  s
}

It seems that it only happens with the mongodb provider

@Jolg42
Copy link
Member

Jolg42 commented Jul 19, 2023

@KarthikRaju391 Make sure to define Address in your schema, it should work after that.

We can keep this issue to improve the experience here and avoid the unexpected crash.

@Jolg42 Jolg42 changed the title Prisma extension not working when using with mongodb provider Prisma VS Code extension with mongodb provider crashes when a relation field/type is not defined Jul 19, 2023
@KarthikRaju391
Copy link
Author

@KarthikRaju391 Make sure to define Address in your schema, it should work after that.

We can keep this issue to improve the experience here and avoid the unexpected crash.

@Jolg42, it's not because of Address not being defined because even after setting up the schema completely, the error still occurs. The prisma formatting is also working fine, but the error is still occuring when editing the file.

@KarthikRaju391
Copy link
Author

@KarthikRaju391 Make sure to define Address in your schema, it should work after that.

We can keep this issue to improve the experience here and avoid the unexpected crash.

@Jolg42, it's not because of Address not being defined because even after setting up the schema completely, the error still occurs. The prisma formatting is also working fine, but the error is still occuring when editing the file.

It's not really affecting the functionality of the extension, that is, autocompleting relations, showing common errors, etc.

But, when editing the file, the error I mentioned keeps coming up.

@Jolg42 Jolg42 transferred this issue from prisma/prisma Jul 19, 2023
@raiadi2008
Copy link

I can confirm this error is happening with me.
Using nextjs, prisma

@janpio
Copy link
Member

janpio commented Jul 21, 2023

I think this is the minimal reproduction:

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

generator client {
  provider = "prisma-client-js"
}

model Foo {
  bar Bar[]
}

Put this in a schema file, and hover over Bar - and you will get this crash:

[Error - 7:07:18 PM] prisma-schema-wasm errored when invoking code_actions. It resulted in a Wasm panic.
RuntimeError: panicked at 'no entry found for key', psl/parser-database/src/walkers/model.rs:104:14
[Trace - 7:07:18 PM] Received request 'window/showMessageRequest - (37)'.
Params: {
    "type": 1,
    "message": "prisma-schema-wasm errored. To get a more detailed output please see Prisma Language Server output. You can do this by going to View, then Output from the toolbar, and then select 'Prisma Language Server' in the drop-down menu.",
    "actions": []
}

From looking at https://github.com/prisma/prisma-engines/blob/4a1c72356ffb884a5752157c21d8bac67afd1720/psl/parser-database/src/walkers/model.rs#L104, somehow this seems to try to iterate over primary keys - which do not exist for this model.

(A stracktrace would be useful in addition to where it crashes...)

@Jolg42
Copy link
Member

Jolg42 commented Jul 26, 2023

I a PR to get the stack trace printed in logs #1474
here it looks like this

[Error - 11:19:41] prisma-schema-wasm errored when invoking code_actions. It resulted in a Wasm panic.
RuntimeError: panicked at 'no entry found for key', psl/parser-database/src/walkers/model.rs:104:14
RuntimeError: panicked at 'no entry found for key', psl/parser-database/src/walkers/model.rs:104:14
    at rust_panic (wasm://wasm/0094251e:wasm-function[2651]:0x17491c)
    at std::panicking::rust_panic_with_hook::h5cee2a9564faeb6d (wasm://wasm/0094251e:wasm-function[1010]:0x147b70)
    at std::panicking::begin_panic_handler::{{closure}}::h38a949ca54e64e6b (wasm://wasm/0094251e:wasm-function[1483]:0x160889)
    at std::sys_common::backtrace::__rust_end_short_backtrace::hc4f3d2fd3090193f (wasm://wasm/0094251e:wasm-function[2594]:0x174760)
    at rust_begin_unwind (wasm://wasm/0094251e:wasm-function[2100]:0x16ff48)
    at core::panicking::panic_fmt::h8a19fa1eb63fbb67 (wasm://wasm/0094251e:wasm-function[2148]:0x170b25)
    at core::option::expect_failed::hd65acbc4e2342087 (wasm://wasm/0094251e:wasm-function[1811]:0x16a4d2)
    at parser_database::walkers::model::<impl parser_database::walkers::Walker<schema_ast::ast::ModelId>>::attributes::ha695952ef445ad30 (wasm://wasm/0094251e:wasm-function[1060]:0x14b139)
    at prisma_fmt::code_actions::h6822d2219700de56 (wasm://wasm/0094251e:wasm-function[8]:0x34d27)
    at code_actions (wasm://wasm/0094251e:wasm-function[1278]:0x157350)
[Trace - 11:19:41] Received request 'window/showMessageRequest - (2)'.
Params: {
    "type": 1,
    "message": "prisma-schema-wasm errored. To get a more detailed output please see Prisma Language Server output. You can do this by going to View, then Output from the toolbar, and then select 'Prisma Language Server' in the drop-down menu.",
    "actions": []
}

@Jolg42
Copy link
Member

Jolg42 commented Jul 26, 2023

@aqrln aqrln self-assigned this Jul 27, 2023
aqrln added a commit to prisma/prisma-engines that referenced this issue Jul 27, 2023
The code action for adding the missing `@map` attribute for MongoDB
relies on the parser database being populated to be able to search for
the primary key of the model. However, the parser database constructor
returns early when there are validation errors during name and types
resolution, so trying to access model attributes later on leads to
runtime errors as those structures are not constructed.

This commit implements populating the model and enum attributes with
stub default values before returning. As no further validation will
happen, and the engines do not work with invalid schemas, this should
not lead to incorrect behaviour. However, the code in the language tools
should take into account that accessing the model or enum attributes in
invalid schemas, although won't lead to crashes anymore, will return
incomplete information.

Fixes: prisma/language-tools#1466
aqrln added a commit to prisma/prisma-engines that referenced this issue Jul 28, 2023
…4087)

The code action for adding the missing `@map` attribute for MongoDB
relies on the parser database being populated to be able to search for
the primary key of the model. However, the parser database constructor
returns early when there are validation errors during name and types
resolution, so trying to access model attributes later on leads to
runtime errors as those structures are not constructed.

This commit implements populating the model and enum attributes with
stub default values before returning. As no further validation will
happen, and the engines do not work with invalid schemas, this should
not lead to incorrect behaviour. However, the code in the language tools
should take into account that accessing the model or enum attributes in
invalid schemas, although won't lead to crashes anymore, will return
incomplete information.

Fixes: prisma/language-tools#1466
@Jolg42 Jolg42 added this to the 5.1.0 milestone Jul 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. tech/engines Issue for tech Engines. topic: mongodb topic: vscode topic: wasm
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants