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

[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 this issue Oct 16, 2023 · 13 comments

Comments

@GhaithMalas99
Copy link

GhaithMalas99 commented Oct 16, 2023

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.

@GhaithMalas99 GhaithMalas99 changed the title Build runner throws invalid type for VoidCallback type in freezed classes or mobx stores Build runner throws invalid type for VoidCallback type in freezed classes or mobx stores also for Color type in mobx stores Oct 16, 2023
@GhaithMalas99 GhaithMalas99 changed the title Build runner throws invalid type for VoidCallback type in freezed classes or mobx stores also for Color type in mobx stores [build_runner] Reports 'Invalid Type' for VoidCallback in Freezed Classes and MobX Stores, as well as for Color Type in MobX Stores Oct 16, 2023
@jakemac53
Copy link
Contributor

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

@jakemac53 jakemac53 added the needs-info Additional information needed from the issue author label Oct 17, 2023
@jakemac53
Copy link
Contributor

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).

@github-actions github-actions bot removed the needs-info Additional information needed from the issue author label Oct 18, 2023
@jakemac53
Copy link
Contributor

@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
Copy link
Contributor

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

@mrRedSun
Copy link

@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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link

@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
Copy link

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

@mrRedSun
Copy link

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

@nekear
Copy link

nekear commented May 14, 2024

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: mobxjs/mobx.dart#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:

  • outputDirectory: /lib/__generated/graphql - Custom folder for generated files, doesn't affect the flow;
  • builders - Official MobX builder configuration;
  • required_inputs: ['.graphql.dart'] - This solves the issue by ensuring GraphQL files are generated before others.

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: #2701

@daksh-gargas
Copy link

Any updates on this?

@therohansanap
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants