simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.43k stars 351 forks source link

Issue with generated code: database.g.dart file #3043

Open chani22 opened 1 week ago

chani22 commented 1 week ago

Hello,

I am encountering an issue with the generated code from Drift. When I run the code generation using build_runner, I receive the following error by my ide(vscode):

The argument type 'Object?' can't be assigned to the parameter type 'Expression<int>'.

If I ignore the above error and run flutter run, I encounter the following build-time error.

Error (Xcode): lib/data/database/database.g.dart:7976:25: Error: The argument type 'Object?' can't be assigned
to the parameter type 'Expression<int>'.

Below is the screen of error message reported by VSCode.

스크린샷 2024-06-11 오후 5 59 47

Here is a part of the database.dart code I wrote, defining the BuildingTable.

class BuildingTable extends Table {
  IntColumn get id => integer()();
  IntColumn get gid0 => integer().nullable()();
  IntColumn get gid32 => integer().nullable()();

  TextColumn get buildingName => text().withLength(max: 50)();
  TextColumn get buildingIcon => text().withLength(max: 50)();
  IntColumn get iconColorValue => integer()();
  TextColumn get address => text().withLength(max: 100).nullable()();
  TextColumn get buildingType => text().withLength(max: 70).nullable()();
  TextColumn get housingType => text().withLength(max: 70).nullable()();

  IntColumn get purchaseDate => integer().nullable()();
  IntColumn get purchasePrice => integer().nullable()();
  IntColumn get marketPrice => integer().nullable()();

  DateTimeColumn get createdDateTime => dateTime()();
  DateTimeColumn get lastUpdatedDateTime => dateTime()();
  BoolColumn get isDeleted => boolean().withDefault(const Constant(false))();
}

Below is a part of the yaml file defining the related dependencies.

  drift: ^2.18.0
  sqlite3_flutter_libs: ^0.5.23
  path_provider: ^2.1.3
  path: ^1.9.0
  watcher: ^1.1.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  drift_dev: ^2.18.0
  build_runner: ^2.4.11

How can I resolve this issue?

simolus3 commented 6 days ago

Thanks for the report! Unfortunately I can't reproduce this locally by pasting the table code alone (it generates the same code but I don't get the error about column having an invalid type). I don't think this is an analyzer problem either since the same thing is happening when compiling the app. If you hover over a composableBuilder invocation in VS Code, what does the popup say its type is? It should be something like ColumnFilters<String> Function({required ColumnFilters<String> Function(GeneratedColumn<String>, Set<JoinBuilder>) builder, required GeneratedColumn<String> column}), but I suspect there may be something going wrong here.

As an interim workaround, you can disable generating the code causing this error by creating builder options with this content:

targets:
  $default:
    builders:
      drift_dev:
        options:
          generate_manager: false

But obviously we want to know what's happening here so that you can keep that code. also cc @dickermoshe (not urgent though).

dickermoshe commented 6 days ago

I’ll take a look, but if it not reproducible, there isn’t much I can do…

On Tue, Jun 11, 2024 at 10:42 AM Simon Binder @.***> wrote:

Thanks for the report! Unfortunately I can't reproduce this locally by pasting the table code alone (it generates the same code but I don't get the error about column having an invalid type). I don't think this is an analyzer problem either since the same thing is happening when compiling the app. If you hover over a composableBuilder invocation in VS Code, what does the popup say its type is? It should be something like ColumnFilters Function({required ColumnFilters Function(GeneratedColumn, Set) builder, required GeneratedColumn column}), but I suspect there may be something going wrong here.

As an interim workaround, you can disable generating the code causing this error by creating builder options https://drift.simonbinder.eu/docs/advanced-features/builder_options/ with this content:

targets: $default: builders: drift_dev: options: generate_manager: false

But obviously we want to know what's happening here so that you can keep that code. also cc @dickermoshe https://github.com/dickermoshe (not urgent though).

— Reply to this email directly, view it on GitHub https://github.com/simolus3/drift/issues/3043#issuecomment-2160943504, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASDJ6236LK3XH4LO2RO6E4DZG4EFJAVCNFSM6AAAAABJD4SXIOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNRQHE2DGNJQGQ . You are receiving this because you were mentioned.Message ID: @.***>

chani22 commented 6 days ago

@simolus3 The temporary workaround you provided works effectively. Thank you very much.

And, I agree that we need to find a fundamental solution.

I have attached the popup that appears when hovering over composableBuilder via vscode.

스크린샷 2024-06-12 오전 12 20 22

I am ready to provide any additional information needed to resolve this issue. Please let me know if there is anything else you need.

dickermoshe commented 6 days ago

@chani22 I need the database class too

dickermoshe commented 6 days ago

You've only posted the table class

dickermoshe commented 6 days ago

@chani22 This is still working fine by me.

Do you have any build.yaml file that has some other settings? What's your output of flutter doctor? Did you try building with dart run build_runner build --delete-conflicting-outputs? What about a flutter clean?

chani22 commented 6 days ago

@dickermoshe There was no build.yaml file before simolus3 provided the temporary workaround.

This is output of flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.22.1, on macOS 14.5 23F79 darwin-arm64, locale ko-KR)
[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.3)
[✓] VS Code (version 1.81.1)
[✓] Connected device (5 available)
[✓] Network resources

• No issues found!

I have tried both dart run build_runner build --delete-conflicting-outputs and flutter clean multiple times, and I just tried both methods again, but I still confirmed that the error code exists in the database.g.dart file.

dickermoshe commented 6 days ago

What about dart info?

chani22 commented 6 days ago

Here is output of dart info


If providing this information as part of reporting a bug, please review the information
below to ensure it only contains things you're comfortable posting publicly.

#### General info

- Dart 3.4.1 (stable) (Tue May 21 15:46:25 2024 +0000) on "macos_arm64"
- on macos / Version 14.5 (Build 23F79)
- locale is ko-KR

#### Project info

- sdk constraint: '>=2.17.0 <=3.4.1'
- dependencies: animated_check, awesome_notifications, badges, change_app_package_name, cloud_firestore, conditional_parent_widget, convex_bottom_bar, cupertino_icons, data_table_2, device_info_plus, dotted_border, drift, dropdown_button2, firebase_analytics, firebase_core, firebase_crashlytics, firebase_messaging, firebase_remote_config, flutter, flutter_bounceable, flutter_cupertino_date_picker_fork, flutter_easyloading, flutter_inappwebview, flutter_inset_box_shadow, flutter_local_notifications, flutter_material_pickers, flutter_native_splash, flutter_screen_lock, flutter_sticky_header, flutter_svg, flutter_swiper_null_safety, flutter_toggle_tab, flutter_typeahead, fluttertoast, font_awesome_flutter, get, getwidget, glassmorphism, group_button, hexcolor, http, internet_connection_checker, intl, is_first_run, jwt_decode, kakao_flutter_sdk, kpostal, linked_scroll_controller, logger, lottie, modal_side_sheet, multi_select_flutter, mutex, notification_permissions, package_info_plus, path, path_provider, percent_indicator, rect_getter, shared_preferences, shimmer, show_up_animation, sign_in_with_apple, smooth_page_indicator, sqlite3_flutter_libs, sticky_headers, store_redirect, styled_widget, substring_highlight, table_calendar, transparent_pointer, url_launcher, watcher, webview_flutter, word_break_text
- dev_dependencies: build_runner, drift_dev, flutter_launcher_icons, flutter_lints, flutter_oss_licenses, flutter_test
- elided dependencies: 1

#### Process info

| Memory |  CPU | Elapsed time | Command line                                                                    |
| -----: | ---: | -----------: | ------------------------------------------------------------------------------- |
|   1 MB | 0.0% |  04-01:43:48 | dart devtools --machine --allow-embedding --dtd-uri ws:<path>/fZvSUgQ0B7I3fiaD  |
|   1 MB | 0.0% |  04-01:24:19 | dart devtools --machine --allow-embedding --dtd-uri ws:<path>/pB0ZQGL0dkj0ctw3  |
|   6 MB | 0.0% |  04-01:43:48 | dart language-server --protocol=lsp --client-id=VS-Code --client-version=3.90.0 |
|   6 MB | 0.0% |  04-01:24:19 | dart language-server --protocol=lsp --client-id=VS-Code --client-version=3.90.0 |
|   1 MB | 0.0% |  04-01:43:48 | dart tooling-daemon --machine                                                   |
|   1 MB | 0.0% |  04-01:24:19 | dart tooling-daemon --machine                                                   |
|   4 MB | 0.0% |  04-01:24:19 | flutter_tools.snapshot daemon   
dickermoshe commented 6 days ago

Hmmm, this is really strange. I've got the same with no issues.

What does hovering over $state.table.id reveal? By me: image

dickermoshe commented 6 days ago

Can you post the generated code? You can email it to me privately if you feel more comfortable dickermoshe@gmail.com

chani22 commented 6 days ago

Same type hovering over on $state.table.id

스크린샷 2024-06-12 오전 1 11 49
chani22 commented 6 days ago

@dickermoshe I just sent the full code of the database.g.dart file to the email you provided. Thank you for your consideration.

dickermoshe commented 6 days ago

That is a fairly nasty bug.

The generics on composableBuilder state that the column provided is passed into the builder.

image image

@simolus3 This seems like a dart bug.

dickermoshe commented 6 days ago

@chani22 What about hovering over column in builder: (column, joinBuilders) =>?

dickermoshe commented 6 days ago

@chani22 Your generated code matches mine.

chani22 commented 6 days ago

Here is what appears when hovering over the column.

스크린샷 2024-06-12 오전 1 28 34
dickermoshe commented 6 days ago

This is really odd. If this a bug in dart, the next steps will be long and painful. I want to make sure that we aren't the problem. I'll get back to you

chani22 commented 6 days ago

I sincerely appreciate your prompt response. I will proceed with the development using the temporary workaround you provided. We will address the issue again once the bug is resolved.

Thank you once again.

simolus3 commented 6 days ago

Just to be sure - this also prevents the code from running? I've sometimes seen issues where the analyzer reports phantom type errors but the front end gets it right. That's definitely an analyzer bug, but if the compiler also refuses to run the code with the same reason, chances are we're misusing generics in a subtle way (but then it's weird that it only fails here).

One possible approach to diagnose this further could be to make the generated code more verbose (it could spell out the generics in the composableBuilder invocation or add explicit types to the function expression passed to builder).

dickermoshe commented 6 days ago

@chani22 Try replacing this on BuildingTableTableFilterComposer

ColumnFilters<int> get id => $state.composableBuilder(
      column: $state.table.id,
      builder: (column, joinBuilders) =>
          ColumnFilters(column, joinBuilders: joinBuilders));

with this:

ColumnFilters<int> get id => $state.composableBuilder<ColumnFilters<int>,GeneratedColumn<int>>(
      column: $state.table.id,
      builder: (column, joinBuilders) =>
          ColumnFilters(column, joinBuilders: joinBuilders));

I've added the generics explicitly, maybe that will help?

dickermoshe commented 6 days ago

@simolus3 Yeah, it breaks on flutter run as stated in the initial bug report

If I ignore the above error and run flutter run, I encounter the following build-time error.

Error (Xcode): lib/data/database/database.g.dart:7976:25: Error: The argument type 'Object?' can't be assigned to the parameter type 'Expression'.