8

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!";
  }
}
Marat
  • 1,288
  • 2
  • 11
  • 22
Ivan
  • 256
  • 3
  • 13

2 Answers2

12
  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

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
Ivan
  • 256
  • 3
  • 13
  • 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. – mitch2na Nov 06 '20 at 15:15
  • 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/entrypoint/build.dart`. You can copy **Absolute Path** using the Android Studio project tab – Ben Butterworth Apr 07 '22 at 14:30
1

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;
obe
  • 7,378
  • 5
  • 31
  • 40