How run flutter 'packages pub run build_runner build' with debug mode in intellij idea?

1,224

Solution 1

  1. run commad flutter packages pub run build_runner build*

  2. copy build.dart to root folder of project

enter image description here

  1. add new run configuration enter image description here
  2. run debug, now you can debug your code generator!

* the packages is optional, you can just run flutter pub run build_runner build

Solution 2

Ivan's answer worked for me, but every time I changed a file that was using an annotation - the build process outputted:

[SEVERE] Terminating builds due to build script update
[INFO] Terminating. No further builds will be scheduled

and then renamed the build script itself from build.dart to build.dart.cached, and then exit with code 75.

After digging through the build_runner code, I discovered that this behavior can be mitigated by using the following Program Arguments:

serve --skip-build-script-check

(i.e. instead of just serve as Ivan suggested).

There may be some negative consequences; in the build_runner source code, in options.dart, I saw this:

// For testing only, skips the build script updates check.
bool skipBuildScriptCheck;
Share:
1,224
Ivan
Author by

Ivan

Updated on December 15, 2022

Comments

  • Ivan
    Ivan over 1 year

    I want to put break point on my generator code, but I don't know how to run the command on the debug mode.

    I wrote generator using source_gen and build_runner

    class MyGenerator extends GeneratorForAnnotation<Todo> {
      @override
      FutureOr<String> generateForAnnotatedElement(
          Element element, ConstantReader annotation, BuildStep buildStep) {
        return "// Hey! Annotation found!";
      }
    }
    
  • mitch2na
    mitch2na over 3 years
    Thanks for this! Just a side note though, make sure your "Working directory" points to the root path of flutter project where "pubspec.yaml" is located.
  • Ben Butterworth
    Ben Butterworth about 2 years
    You don't actually have to copy the file to the root of the project. Just open the file in Android Studio and place a breakpoint in it. Then debug the project as a Dart Command Line App with Dart file set to the path of the file, eg /opt/flutter/.pub-cache/hosted/pub.dartlang.org/build_runner‌​-2.1.7/lib/src/entry‌​point/build.dart. You can copy Absolute Path using the Android Studio project tab