dartbuildyamlcode-generationbuild-runner

Why does the PostProcessBuilder only run once?


I have the following PostProcessBuilder

class MoveBuilder extends PostProcessBuilder {

  @override
  Iterable<String> get inputExtensions => ['.g.dart'];

  MoveModelBuilder() ;

  @override
  Future<void> build(PostProcessBuildStep buildStep) async {
    log.info('MOVE THE FILE');
  }
}

And the following top level function

PostProcessBuilder moveSomething(BuilderOptions options) {
  return MoveModelBuilder();
}

With the following build config in my generator package

builders:
  my_builder:
    import: "package:my_package/src/builders/builders.dart"
    builder_factories: ["buildSomething"]
    build_extensions: {'.dart': ['.g.dart']}
    auto_apply: none
    build_to: source
    applies_builders:
      - my_package|move_builder

post_process_builders:
  move_builder:
    import: "package:my_package/src/builders/builders.dart"
    builder_factory: moveSomething

And in my external package where I use this generator I have the following build yaml

targets:
  $default:
    builders:
      my_package|my_builder:
        enabled: true
        generate_for:
          - lib/*/input_folder/*.dart
      my_package|move_builder:
        enabled: true
        generate_for:
          - lib/*/relative_output_folder/*.g.dart

But only the first time I run the build runner command the MoveBuilder gets triggered. It seems to only get triggered after this warning

[WARNING] BuildDefinition: Throwing away cached asset graph because the build phases have changed. This most commonly would happen as a result of adding a new dependency or updating your dependencies.

So after the cached asset graph gets reinitialised.

What can I do to make it run every time after my main builder has ran?

EDIT ----------------------------------------------------

For anyone wanting to move files with a PostProcessBuilder, don't. Use capture groups instead.

https://github.com/dart-lang/build/blob/master/docs/writing_a_builder.md#configuring-output

And here is an example at the bottom

https://github.com/dart-lang/build/blob/master/example/lib/example.dart


Solution

  • Not sure what you expected, but to me the build setup above seems to do what it is supposed to do:

    1. Inital run: dart run build_runner build --delete-conflicting-outputs --verbose the main builder generates .g.dart files and the post-process builder runs on .g.dart files.

    2. Subsequent build runs will generate output or take actions if for example:

      • Additional target files are detected.
      • The generated files were deleted.
      • A build.yaml file changed.
      • The file containing a builder changed.
      • The user deletes the build cache by executing: dart run build_runner clean

    Other than that I would expect that building the project a second time should produce: 0 outputs (0 actions).