10

I am using EasyLocalization, mobx, JsonSerializable etc. To generate *.g.dart files, I am using build_runner watch or the script in scripts/build.sh:

flutter packages pub run build_runner build

It was taking 2 seconds at most, but then suddenly it started to take at least 10 seconds or sometimes 30 seconds. Because of this situation, build_runner watch became useless, I mean it's not working properly since then.

The output on the console when I run my script:

$ sh scripts/build.sh 
[INFO] Generating build script...
[INFO] Generating build script completed, took 611ms

[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 126ms

[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 745ms

[INFO] Running build...
[INFO] 1.2s elapsed, 0/16 actions completed.
[INFO] 2.2s elapsed, 0/16 actions completed.
[INFO] 3.3s elapsed, 0/16 actions completed.
[INFO] 12.5s elapsed, 1/17 actions completed.
[INFO] 13.6s elapsed, 4/20 actions completed.
[INFO] Running build completed, took 14.6s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 57ms

[INFO] Succeeded after 14.6s with 2 outputs (47 actions)
Muhammed Aydogan
  • 570
  • 1
  • 4
  • 22
  • Do you have increased dramatically the amount of dart files in your project during the spike in runner duration? – croxx5f Dec 13 '21 at 16:35
  • No. not much. I just have 13 `*.g.dart` files total, that's all – Muhammed Aydogan Dec 13 '21 at 17:58
  • I meant total files, as build runner has to search each file for annotations. Maybe your project has grown a lot bigger even though your codegen needs don't. – croxx5f Dec 13 '21 at 18:50
  • Yes, I have 167 files under `lib/`. So, you mean, there is no solution but to wait for the flutter team to fix it, to speed it up? – Muhammed Aydogan Dec 13 '21 at 19:12
  • 1
    It should be faster for a project of that size, there are known workarounds like the ones stated [here](https://wilsonwilson.dev/articles/flutter-build-yaml/). tou could mark your files which have codegen and generate for those specific ones. Making the runner analyze way less , its a manual optimization that could ease your issue in the meantime. – croxx5f Dec 13 '21 at 20:28
  • @croxx5f I'm having troubles with `build.yaml`. Can you post a sample `build.yaml` file for `mobx` and `json_serializable`? as an answer? – Muhammed Aydogan Dec 14 '21 at 00:36

2 Answers2

5

In addition to avoiding running builders on unnecessary inputs I manage to make my builds MUCH faster by disabling the mockito mock generator, which I was not using:

build.yaml:

targets:
  $default:
    builders:
      your_builder:
        generate_for:
          - lib/**/*.dart
      mockito:mockBuilder:
        enabled: false

Running a build was causing Mockito to search every one of my test files for @GenerateMocks.

Clavum
  • 171
  • 2
  • 6
  • You sir, are my hero. Know of any other scans running behind the scenes? – JJ Du Plessis Nov 28 '22 at 09:31
  • @JJDuPlessis Nope, and sadly I don't think there's a way to know all the builders active in a project either. I only noticed mockito was active because there were a few times it logged some info when building. – Clavum Nov 28 '22 at 20:25
4

I finally decided to try and solve this myself, I've improved it slightly by adding the following to my pubspec.yaml: E.g.

targets:
  $default:
    builders:
      json_serializable:
        enabled: true
        generate_for:
          include:
            - lib/your_codegen_folder/**.dart
            - lib/**.gen.dart

Hope this speeds it up a little for you. You'll have to add builder configs for your other two generators like the json_serializable one here. I suspect they might be referred to easy_localization and mobx? I don't know, I'm new to this.

JJ Du Plessis
  • 1,047
  • 12
  • 9