-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't crash in ModelWalker if name or type resolution fails
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
- Loading branch information
Showing
9 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
prisma-fmt/tests/code_actions/scenarios/mongodb_at_map_with_validation_errors/result.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
24 changes: 24 additions & 0 deletions
24
prisma-fmt/tests/code_actions/scenarios/mongodb_at_map_with_validation_errors/schema.prisma
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "mongodb" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model Kattbjorn { | ||
// As of now, the code action won't appear here until the other validation | ||
// error is fixed. The reason is the implementation of this code action | ||
// relies on the parser database to be fully populated and all attributes | ||
// to be resolved, however when there are name and type resolution errors, | ||
// the attributes are not currently resolved and only an empty stub for | ||
// model attributes is constructed. This may change in the future, and this | ||
// test expectation would need to be updated correspondingly then. | ||
// | ||
// Note that the validation error itself does not appear here too until the | ||
// error on the next line is fixed (due to these being separate validation | ||
// passes), so the behaviour is consistent. | ||
id String @id | ||
bar | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod regressions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#[test] | ||
fn code_actions_should_not_crash_on_validation_errors_with_mongodb() { | ||
let schema = r#" | ||
datasource db { | ||
provider = "mongodb" | ||
url = env("DATABASE_URL") | ||
} | ||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
model A { | ||
1bar Bar[] | ||
} | ||
"#; | ||
|
||
let params = lsp_types::CodeActionParams { | ||
text_document: lsp_types::TextDocumentIdentifier { | ||
uri: "file:/path/to/schema.prisma".parse().unwrap(), | ||
}, | ||
range: lsp_types::Range::default(), | ||
context: lsp_types::CodeActionContext::default(), | ||
work_done_progress_params: lsp_types::WorkDoneProgressParams { work_done_token: None }, | ||
partial_result_params: lsp_types::PartialResultParams { | ||
partial_result_token: None, | ||
}, | ||
}; | ||
|
||
prisma_fmt::code_actions(schema.to_owned(), &serde_json::to_string_pretty(¶ms).unwrap()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod language_tools_1466; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters