medz / prisma-dart

Prisma Client Dart is an auto-generated type-safe ORM. It uses Prisma Engine as the data access layer and is as consistent as possible with the Prisma Client JS/TS APIs.
https://prisma.pub
BSD 3-Clause "New" or "Revised" License
450 stars 30 forks source link

scehma in Windows production source code contains newline characters causing exceptions #344

Closed rifay-next99 closed 6 months ago

rifay-next99 commented 6 months ago

PS D:\Rifay\dart_frog\fitish_bend-Dart_frog> npx prisma generate Environment variables loaded from .env Prisma schema loaded from prisma\schema.prisma Building package executable... (4.5s) Built orm:orm. Error: Could not format because the source could not be parsed:

line 376, column 76 of .: Unterminated string literal. ╷ 376 │ final engine = _i4.BinaryEngine(schema: '// This is your Prisma schema file, │ ^ ╵ line 487, column 33 of .: Unterminated string literal. ╷ 487 │ \n', datasources: datasources, ); │ ^ ╵ line 491, column 51 of .: Expected to find ')'. ╷ 491 │ return createClientWithTransaction(transaction); } │ ^ ╵ line 376, column 41 of .: Expected to find ';'. ╷ 376 │ final engine = _i4.BinaryEngine(schema: '// This is your Prisma schema file, │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ╵ line 377, column 1 of .: Expected an identifier. ╷ 377 │ \n// learn more about it in the docs: https://pris.ly/d/prisma-schema │ ^ ╵ line 377, column 1 of .: Expected to find ';'. ╷ 377 │ \n// learn more about it in the docs: https://pris.ly/d/prisma-schema │ ^ ╵ line 377, column 2 of .: Expected to find ';'. ╷ 377 │ \n// learn more about it in the docs: https://pris.ly/d/prisma-schema │ ^ ╵ line 378, column 1 of .: Expected an identifier. ╷ 378 │ \n │ ^ ╵ line 378, column 1 of .: Expected to find ';'. ╷ 378 │ \n │ ^ ╵ line 378, column 2 of .: Expected to find ';'. ╷ 378 │ \n │ ^ ╵ (404 more errors...)

medz commented 6 months ago

This should be a bug in code_builder (on Windows), the string for this parameter should be one line, containing the \n translation symbol. But it wraps automatically in your code, it could also be a problem with the dart_style package, I don't have a windows device, I'm not sure. But it is normal in Linux and macOS.

AndryHTC commented 6 months ago

I've tried also in Ubuntu (WSL) and gives the same error

medz commented 6 months ago

@AndryHTC @rifay-next99 Can you give me a minimal schema.prisma that causes this error? I can't reproduce it at the moment.

AndryHTC commented 6 months ago

@AndryHTC @rifay-next99 Can you give me a minimal schema.prisma that causes this error? I can't reproduce it at the moment.

It's the same schema I sent you for the other issue (maybe the same issue as well) https://github.com/medz/prisma-dart/issues/340. I removed the schema from the issue after you closed it, but if you need it again I'll do it

medz commented 6 months ago

不,我需要一个更加小型的 schema,我始终无法复现你们提示的这两个问题。

AndryHTC commented 6 months ago

bruh

generator client {
  provider        = "dart run orm"
  output          = "../lib/src/prisma_client/"
  previewFeatures = ["fullTextSearch", "fullTextIndex"]
}

datasource db {
  provider     = "mysql"
  url          = env("DATABASE_URL")
  relationMode = "prisma"
}

model Soggetto {
  uuid                 String                @id @default(uuid())
  ragioneSociale       String
  iban                 String?
  negozioPreferitoUuid String?
  codiceFiscale        String?               @unique
  note                 String?
}
> npx prisma generate

Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Building package executable... (4.8s)
Built orm:orm.
Error: 
Could not format because the source could not be parsed:

line 71, column 59 of .: Unterminated string literal.
   ╷
71 │ final engine = _i4.BinaryEngine(schema: 'generator client {
   │                                                           ^
   ╵
line 75, column 3 of .: Expected to find ')'.
   ╷
75 │ \n}
   │   ^
   ╵
line 90, column 34 of .: Unterminated string literal.
   ╷
90 │ \n}', datasources: datasources, );
   │                                  ^
   ╵
line 71, column 41 of .: Expected to find ';'.
   ╷
71 │ final engine = _i4.BinaryEngine(schema: 'generator client {
   │                                         ^^^^^^^^^^^^^^^^^^^
   ╵
line 72, column 1 of .: Expected an identifier.
   ╷
72 │ \n  provider        = "dart run orm"
   │ ^
   ╵
line 72, column 1 of .: Expected to find ';'.
   ╷
72 │ \n  provider        = "dart run orm"
   │ ^
   ╵
line 72, column 23 of .: Expected to find ';'.
   ╷
72 │ \n  provider        = "dart run orm"
   │                       ^^^^^^^^^^^^^^
   ╵
line 73, column 1 of .: Expected an identifier.
   ╷
73 │ \n  output          = "../lib/src/prisma_client/"
   │ ^
   ╵
line 73, column 1 of .: Expected to find ';'.
   ╷
73 │ \n  output          = "../lib/src/prisma_client/"
   │ ^
   ╵
line 73, column 23 of .: Expected to find ';'.
   ╷
73 │ \n  output          = "../lib/src/prisma_client/"
   │                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   ╵
(63 more errors...)
medz commented 6 months ago

OK, I'll try it!

medz commented 6 months ago

@AndryHTC Still not successful

https://github.com/medz/prisma-dart/commit/2afc7ad2a4ea34b42f73062f3d6a29c697761e54

medz commented 6 months ago

Maybe, it only appears on Windows?

AndryHTC commented 6 months ago

Maybe, it only appears on Windows?

It could.

What could I do to help you in this matter?

kidusdev commented 6 months ago

I encountered the same error days ago and i fixed it with Flutter clean then Flutter pub get

medz commented 6 months ago
image

Unfortunately, I did not successfully reproduce this error.

AndryHTC commented 6 months ago

I've cleaned everything possible, and still do not work.

Prisma client version? Node version? Anything else?

@kidusdev were you on Windows?

AndryHTC commented 6 months ago

@medz I can now confirm that in Mac OS I don't have this problem. Seems to be Windows-specific... But even in that case it's still an important bug to fix

medz commented 6 months ago

@AndryHTC Unfortunately, I don't have a Windows device. In addition, I observed that the error was usually wrongly in the source code. Maybe it's a bug in the code_builder package. I need to take time to borrow a Windows device from a friend to continue to find the reason.

AndryHTC commented 6 months ago

Virtualized Windows on Mac OS is pretty easy to set up.

I hope you can solve this soon, can't wait to use Dart Orm 4. Thank you @medz

medz commented 6 months ago

Sorry, Windows is too complicated. I didn’t even install the Dart environment successfully😫

The code about this bug is at https://github.com/medz/prisma-dart/blob/main/bin/src/generate_client.dart#L88 Can developers whose main device is Windows help me fix it?

medz commented 6 months ago

https://github.com/medz/prisma-dart/blob/3d30cea1fd09f74e8eac202c6b4f6a0bff6d7169/bin/src/generate_client.dart#L88

NeroSong commented 6 months ago

Not successfully reproduce on my Windows 11.

orm : ^4.0.2
prisma : 5.8.1
Node.js : v16.20.0
Dart SDK version: 3.3.2 (stable) (Tue Mar 19 20:44:48 2024 +0000) on "windows_x64"

image

AndryHTC commented 6 months ago

image

Just for reference with the versions you mentioned @NeroSong

NeroSong commented 6 months ago

This is so confusing 🤔

medz commented 6 months ago

@AndryHTC https://pub.dev/packages/orm/versions/4.0.1-issue.344 The error message seems to come from Dart formatter, and I have provided a special version for this issue. It does not format dart code, but constructs the source code and outputs it as is. Would you like to try it? If there is no problem, maybe we can locate the bug causing this problem.

medz commented 6 months ago

It's also possible that the literalString function (which comes from code_builder) mistakenly wraps \r\n instead of translating and outputting the newline symbol. We now need this special build of 4.0.1-issue.344 to confirm it.

medz commented 6 months ago

https://github.com/dart-lang/code_builder/blob/46c0d1052e597d4db913072a059d077a78f850dd/lib/src/specs/expression/literal.dart#L49-L54

I always feel that it is because code_builder's handling of newlines is too crude, leading to the forgetting of some use cases.

AndryHTC commented 6 months ago

@AndryHTC https://pub.dev/packages/orm/versions/4.0.1-issue.344 The error message seems to come from Dart formatter, and I have provided a special version for this issue. It does not format dart code, but constructs the source code and outputs it as is. Would you like to try it? If there is no problem, maybe we can locate the bug causing this problem.

@medz it worked! Is it a definitive fix or have you still to tweak some details?

AndryHTC commented 6 months ago

The client is generated with Dart errors BTW.

final engine = _i4.BinaryEngine(schema: 'generator client {
\n  provider        = "dart run orm"
\n  output          = "../lib/src/prisma_client/"
\n  previewFeatures = ["fullTextSearch", "fullTextIndex"]
\n}
\n
\ndatasource db {
\n  provider     = "mysql"
\n  url          = env("DATABASE_URL")
\n  relationMode = "prisma"
\n}
\n
\nmodel Soggetto {
\n  uuid                 String  @id @default(uuid())
\n  ragioneSociale       String
\n  iban                 String?
\n  negozioPreferitoUuid String?
\n  codiceFiscale        String? @unique
\n  note                 String?
\n}
\n', datasources: datasources, );

Just by adding the triple quotes, the linter goes quiet.

final engine = _i4.BinaryEngine(schema: '''generator client {
\n  provider        = "dart run orm"
\n  output          = "../lib/src/prisma_client/"
\n  previewFeatures = ["fullTextSearch", "fullTextIndex"]
\n}
\n
\ndatasource db {
\n  provider     = "mysql"
\n  url          = env("DATABASE_URL")
\n  relationMode = "prisma"
\n}
\n
\nmodel Soggetto {
\n  uuid                 String  @id @default(uuid())
\n  ragioneSociale       String
\n  iban                 String?
\n  negozioPreferitoUuid String?
\n  codiceFiscale        String? @unique
\n  note                 String?
\n}
\n''', datasources: datasources, );

I don't know if the \n are going to cause issues to the engine

NeroSong commented 6 months ago

Reproduce it when I change my VSCode EOL setting to CRLF:

1711453452109

And set it back to LF is fine.

Looks @medz find the final problem, bingo 😄👍

medz commented 6 months ago

https://github.com/dart-lang/code_builder/issues/452

medz commented 6 months ago

fixed in v4.0.3 version