dart-lang / build

A build system for Dart written in Dart
https://pub.dev/packages/build
BSD 3-Clause "New" or "Revised" License
791 stars 210 forks source link

[build_runner] Reports 'Invalid Type' for VoidCallback in Freezed Classes and MobX Stores, as well as for Color Type in MobX Stores #3596

Open GhaithMalas99 opened 1 year ago

GhaithMalas99 commented 1 year ago

When using [Freezed/MobX] to generate code for my Dart classes in a Flutter project, I encountered an issue where the build runner reports "invalid type" for certain fields or properties in my classes.

Affected Types:

VoidCallback Type: In my Flutter project, I have classes that include fields or properties of type VoidCallback, which represent functions that take no arguments and return no value. The build runner is encountering difficulties when dealing with these types.

Color Type: I'm also experiencing issues with fields or properties of the Color type from the dart:ui library in my MobX stores or models. The build runner is flagging them as "invalid types."

Both VoidCallback types and Color types in MobX stores and Freezed classes result in 'InvalidType' errors after running build_runner. Additionally, when utilizing some Freezed-generated classes within MobX stores, the same 'InvalidType' issues persist.

Expected Behavior:

I expect the build runner to handle these types correctly and generate code without reporting "invalid type" errors.

Environment:

Flutter Version: 3.13.7 • channel stable Dart Version: 3.1.3 freezed:2.4.5 mobx_codegen: ^2.4.0 build_runner: ^2.4.6

Please let me know if additional information or code samples are needed to investigate and address this issue. Any guidance on how to work around this problem temporarily would also be appreciated.

jakemac53 commented 1 year ago

@GhaithMalas99 can you run with --verbose and provide a stack trace for one of these errors?

jakemac53 commented 1 year ago

Note also that when running on a flutter package, you must use the dart executable provided by flutter. This enables us to find the flutter specific types (from dart:ui).

You should be able to either do flutter pub run build_runner build or set up the dart on your path to be the one from your flutter install (which lives at the path bin/cache/dart-sdk/bin/dart under your flutter repo).

jakemac53 commented 12 months ago

@mrRedSun it looks like that build succeeded because it didn't do anything at all :)

I still can't proceed with diagnosing this issue until I get a stack trace of the error

jakemac53 commented 12 months ago

@mrRedSun Can you link similar logs but for one of the failed builds?

mrRedSun commented 12 months ago

@mrRedSun Can you link similar logs but for one of the failed builds?

That's the thing, those are the logs I get when my types turn to InvaidType inside generated files

jakemac53 commented 12 months ago

That's the thing, those are the logs I get when my types turn to InvaidType inside generated files

Oh see, so it is running fine but producing bad code. Does this reproduce with a basic flutter project using freezed?

jakemac53 commented 12 months ago

I tried just creating a minimal project using freezed and that doesn't repro it. Would it be possible for you to try and create a minimal repro, possibly using mobx and freezed together in such a way that triggers this?

mrRedSun commented 12 months ago

@jakemac53 I'll try tomorrow, in meantime please check if you are running build on top of already generated files without clean, that's the part that's not working it seems

mrRedSun commented 12 months ago

@jakemac53 I was unable to create a minimal reproduction project, unfortunately, even with the same pubspec it seems to be working

mrRedSun commented 12 months ago

@GhaithMalas99 in case you need a workaround flutter packages pub run build_runner clean before running codegen resolves the issue

nekear commented 5 months ago

Brief

I encountered the same issue, which turned out to be caused by MobX files being generated before the classes they depended on, leading to "InvalidType" replacements (in my case).


I'm using mobx with graphql_flutter. I provided two build.yaml configurations, which solve the problem. Both produce the same result.

Solution №1

targets:
  $default:
    sources:
      - lib/**
      - pubspec.*
      - $package$
      - graphql/**
    builders:
      json_serializable:
        options:
          explicit_to_json: true
      graphql_codegen:
        options:
          outputDirectory: /lib/__generated/graphql
          assetsPath: graphql/**
          clients:
            - graphql_flutter
global_options:  # <-- solution
  graphql_codegen:
    runs_before:
      - mobx_codegen:mobx_generator

Thanks to this issue: https://github.com/mobxjs/mobx.dart/issues/926.

Solution №2

targets:
  $default:
    sources:
      - lib/**
      - pubspec.*
      - $package$
      - graphql/**
    builders:
      json_serializable:
        options:
          explicit_to_json: true
      graphql_codegen:
        options:
          outputDirectory: /lib/__generated/graphql
          assetsPath: graphql/**
          clients:
            - graphql_flutter
builders:
  mobx_generator:
    target: ':store_generator'
    import: 'package:mobx_codegen/builder.dart'
    builder_factories: [ 'storeGenerator' ]
    build_extensions: { '.dart': [ '.store.g.part' ] }
    auto_apply: dependents
    build_to: cache
    applies_builders: [ 'source_gen|combining_builder' ]
    required_inputs: [ '.graphql.dart' ] # <-- solution

Key points:

Note: Solution with adding builders.mobx_generator.runs_before to GraphQL config did not work for me, the reason is unknown.

Here is a similar issue discussed on GitHub, which suggested a described solution with required_inputs: https://github.com/dart-lang/build/issues/2701

daksh-gargas commented 5 months ago

Any updates on this?

therohansanap commented 1 month ago

Adding following to my build.yaml file fixed the issue for me:

global_options:
  graphql_codegen:
    runs_before:
      - freezed
  freezed:
    runs_before:
      - mobx_codegen:mobx_generator