Search code examples
dartbuildcode-generationbuild-runner

I can't get build runner to generate anything


I have this build.yaml in my package called temp

builders:
  my_package:
    import: "package: my_package/my_package.dart"
    builder_factories: ["myBuilder"]
    build_extensions: {'.dart': ['.g.dart']}
    auto_apply: none
    build_to: source

I confirm my_package is correctly imported in the pubspec.yaml. Then I have this builder in the package my_package, and export it. The file is located at lib/my_package.dart in my_package

library my_package;

import 'package:build/build.dart';

Builder myBuilder(BuilderOptions options) {
  return SimpleBuilder();
}

class SimpleBuilder extends Builder {
  SimpleBuilder();

  @override
  Map<String, List<String>> get buildExtensions => {
        '.dart': ['.g.dart']
      };

  @override
  Future<void> build(BuildStep buildStep) async {
    log.info('SimpleBuilder: Processing ${buildStep.inputId.path}');

    final outputId = buildStep.inputId.changeExtension('.g.dart');
    final content = 'Generated content for ${buildStep.inputId.path}';

    log.info('SimpleBuilder: Writing to ${outputId.path}');

    await buildStep.writeAsString(outputId, content);
  }
}

And in the package temp that contains the build.yaml, I have one dart file at

lib/filename.dart

I run

dart run build_runner build --verbose

And I would expect a file to appear at

lib/filename.g.dart

But nothing happens why? It outputs this

dart run build_runner build --verbose
Resolving dependencies in `/my_drive/temp`... 
Downloading packages... 
Got dependencies in `/my_drive/temp`.
Building package executable... 
Built build_runner:build_runner.
[INFO] Entrypoint:Generating build script...
[INFO] Entrypoint:Generating build script completed, took 157ms

[INFO] Bootstrap:Precompiling build script......
[INFO] Bootstrap:Precompiling build script... completed, took 2.0s

[FINE] Bootstrap:Core package locations file does not exist
[INFO] BuildDefinition:Initializing inputs
[INFO] BuildDefinition:Building new asset graph...
[INFO] BuildDefinition:Building new asset graph completed, took 513ms

[INFO] BuildDefinition:Checking for unexpected pre-existing outputs....
[INFO] BuildDefinition:Checking for unexpected pre-existing outputs. completed, took 0ms

[INFO] Build:Running build...
[INFO] Build:Running build completed, took 3ms

[INFO] Build:Caching finalized dependency graph...
[INFO] Build:Caching finalized dependency graph completed, took 34ms

[INFO] Build:Succeeded after 41ms with 0 outputs (0 actions)

What am I doing wrong? The logs are also not showing up.

When I change either the import or build factories, it does throw an error when I build that either the package or build factories don't exist, which leads me to believe the import and build factories are correctly configured.


Solution

  • The file build.yaml that defines the builder has be in the root directory of the package that contains the source code of the builder. You would have to move build.yaml to the root directory of my_package.

    It is common to export builders in a file called builder.dart located directly in lib. Your file lib/my_package.dart will then become lib/builder.dart.

    Then build.yaml becomes:

    builders:
      my_builder:
        import: "package:my_package/builder.dart"
        builder_factories: ["myBuilder"]
        build_extensions: {'.dart': ['.g.dart']}
        auto_apply: none
        build_to: source
    

    With these changes, the build of package temp proceeds as expected. For example, file filename.g.dart has content:

    Generated content for lib/filename.dart
    

    Some confusion might arise from the fact, that you can have an optional build.yaml file in your package temp. For example:

    targets:
      $default:
        builders:
          # Configure the builder `pkg_name|builder_name`
          my_package|my_builder:
            enabled: true
            # generate_for:
            #   - lib/*.dart
            # options:
            
    

    Here you can enable/disable or configure the builder. Note that I named your builder: my_builder and it is accessed using the syntax: my_package|my_builder.