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

feat(psl): Recommend type in error message if schema validation fails because of case #4137

Merged
merged 5 commits into from
Apr 5, 2024

Conversation

pranayat
Copy link
Contributor

@pranayat pranayat commented Aug 13, 2023

Hi!
This PR closes prisma/prisma#15174
For now I've fixed this issue for 'ScalarType' types and titled the PR 'WIP' so you can let me know if I'm doing something absolutely inane. I've added a test as well.
Thanks!

@pranayat pranayat requested a review from a team as a code owner August 13, 2023 19:17
@CLAassistant
Copy link

CLAassistant commented Aug 13, 2023

CLA assistant check
All committers have signed the CLA.

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 21, 2023

CodSpeed Performance Report

Merging #4137 will not alter performance

Comparing pranayat:feat/ignore-case-type-error (8398e80) with main (fc73af6)

Summary

✅ 11 untouched benchmarks

@pranayat pranayat force-pushed the feat/ignore-case-type-error branch from e0985a5 to 666ce3a Compare February 20, 2024 14:09
@pranayat pranayat requested a review from a team as a code owner February 20, 2024 14:09
@pranayat pranayat requested review from laplab and Druue and removed request for a team February 20, 2024 14:09
@pranayat pranayat changed the title feat(psl): [WIP] Recommend datamodel type in error message if validation fails because of case feat(psl): Recommend datamodel type in error message if validation fails because of case Feb 20, 2024
@pranayat
Copy link
Contributor Author

pranayat commented Feb 20, 2024

(Force)pushed an update that tries to do a case insensitive match of a model's field type against scalar types as well as names of all top level entries in the schema definition.

@pranayat pranayat changed the title feat(psl): Recommend datamodel type in error message if validation fails because of case feat(psl): Recommend type in error message if schema validation fails because of case Feb 23, 2024
@pranayat
Copy link
Contributor Author

pranayat commented Feb 23, 2024

I should probably also exclude Source and Generator top level entries when looking for suggestions, i.e. only look for suggestions in Enum, CompositeType and Model entry names, since only these types can be referenced as model fields.

Could it make sense to add a function similar to iter_tops() in ast.rs that returns only these top level entries ?

@pranayat
Copy link
Contributor Author

pranayat commented Mar 7, 2024

No cigar ? 😅

@SevInf SevInf added the PR: Improvement A PR that improves existing functionality label Mar 20, 2024
@Druue Druue self-assigned this Apr 2, 2024
@Druue
Copy link
Contributor

Druue commented Apr 3, 2024

Hey, @pranayat

Thanks for submitting! ✨

Screen.Recording.2024-04-03.at.16.07.52.mov

Could it make sense to add a function similar to iter_tops() in ast.rs that returns only these top level entries ?

I thought about it, and I think I prefer the explicitness of a filter_map, so something like this should be fine :)

let top_names: Vec<_> =
    ctx
      .ast
      .iter_tops()
      .filter_map(|(_, top)| match top {
          ast::Top::Source(_) | ast::Top::Generator(_) => None,
          _ => Some(&top.identifier().name),
      })
      .collect();

@Druue Druue added this to the 5.13.0 milestone Apr 3, 2024
@pranayat
Copy link
Contributor Author

pranayat commented Apr 3, 2024

@Druue Sounds good 😄
I've made the change and updated the test.

Copy link
Contributor

@Druue Druue left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

psl/diagnostics/src/error.rs Outdated Show resolved Hide resolved
psl/psl/tests/base/basic.rs Outdated Show resolved Hide resolved
@Druue Druue merged commit e66d30d into prisma:main Apr 5, 2024
204 checks passed
jkomyno added a commit that referenced this pull request Apr 5, 2024
jkomyno added a commit that referenced this pull request Apr 8, 2024
* Implement multi-file schema handling in PSL

This commit implements multi-file schema handling in the Prisma Schema Language.

At a high level, instead of accepting a single string, `psl::validate_multi_file()` is an alternative to `psl::validate()` that accepts something morally equivalent to:

```json
{
  "./prisma/schema/a.prisma": "datasource db { ... }",
  "./prisma/schema/nested/b.prisma": "model Test { ... }"
}
```

There are tests for PSL validation with multiple schema files, but most of the rest of engines still consumes the single file version of `psl::validate()`. The implementation and the return type are shared between `psl::validate_multi_file()` and `psl::validate()`, so the change is completely transparent, other than the expectation of passing in a list of (file_name, file_contents) instead of a single string. The `psl::validate()` entry point should behave exactly the same as `psl::multi_schema()` with a single file named `schema.prisma`. In particular, it has the exact same return type.

Implementation
==============

This is achieved by extending `Span` to contain, in addition to a start and end offset, a `FileId`. The `FileId` is a unique identifier for a file and its parsed `SchemaAst` inside `ParserDatabase`. The identifier types for AST items in `ParserDatabase` are also extended to contain the `FileId`, so that they can be uniquely referred to in the context of the (multi-file) schema. After the analysis phase (the `parser_database` crate), consumers of the analyzed schema become multi-file aware completely transparently, no change is necessary in the other engines.

The only changes that will be required at scattered points across the codebase are the `psl::validate()` call sites that will need to receive a `Vec<Box<Path>, SourceFile>` instead of a single `SourceFile`. This PR does _not_ deal with that, but it makes where these call sites are obvious by what entry points they use: `psl::validate()`, `psl::parse_schema()` and the various `*_assert_single()` methods on `ParserDatabase`.

The PR contains tests confirming that schema analysis, validation and displaying diagnostics across multiple files works as expected.

Status of this PR
=================

This is going to be directly mergeable after review, and it will not affect the current schema handling behaviour when dealing with a single schema file.

Next steps
==========

- Replace all calls to `psl::validate()` with calls to `psl::validate_multi_file()`.
- The `*_assert_single()` calls should be progressively replaced with their multi-file counterparts across engines.
- The language server should start sending multiple files to prisma-schema-wasm in all calls. This is not in the spirit of the language server spec, but that is the most immediate solution. We'll have to make `range_to_span()` in `prisma-fmt` multi-schema aware by taking a FileId param.

Links
=====

Relevant issue: prisma/prisma#2377

Also see the [internal design doc](https://www.notion.so/prismaio/Multi-file-Schema-24d68fe8664048ad86252fe446caac24?d=68ef128f25974e619671a9855f65f44d#2889a038e68c4fe1ac9afe3cd34978bd).

* chore(prisma-fmt): fix typo

* chore(prisma-fmt): add comment

* chore(prisma-fmt): fix compilation after #4137

---------

Co-authored-by: Alberto Schiabel <jkomyno@users.noreply.github.com>
Co-authored-by: jkomyno <skiabo97@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Improvement A PR that improves existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nicer Datamodel Type validation
5 participants