1

When using an aggregate builder, I notice that the generate_for option on the TargetBuilderConfig in the build.yaml changes behaviour. The builder will only output if the target builder is set to default where as for normal builders setting the generate_for does work as expected.

This is my folder structure

lib
- entity_a.dart
- entity_b.dart
protos

where using the default generate_for of ** seems to work and outputs one proto file with aggregated content in the protos folder. But upon setting the generate_for to lib/**.dart it will stop outputting the proto file. But why?

This is my target build.yaml

targets:
  $default:
    builders:
      package_name|builder_name:
        enabled: true
        generate_for:
          - lib/**.dart

Which only works when removing the generate_for. And this is my build.yaml in my generator package.

builders:
  builder_name:
    import: "package:package_name/builders.dart"
    builder_factories: ["build"]
    build_extensions: {'$package$': ['protos/model.proto']}
    build_to: source
    auto_apply: root_package

And this is my builder

import 'package:build/build.dart';
import 'package:glob/glob.dart';

Builder build(BuilderOptions options) => ProtoBuilder();

class ProtoBuilder implements Builder {
  static final _allFilesInLib = Glob('**.dart');

  static AssetId _allDartFiles(BuildStep buildStep) {
    return AssetId(
      buildStep.inputId.package,
      'protos/model.proto',
    );
  }

  @override
  Map<String, List<String>> get buildExtensions {
    return const {
      r'$package$': ['protos/model.proto'],
    };
  }

  @override
  Future<void> build(BuildStep buildStep) async {
    final files = <String>[];
    await for (final input in buildStep.findAssets(_allFilesInLib)) {
      files.add(input.path);
    }
    final output = _allDartFiles(buildStep);
    return buildStep.writeAsString(output, files.join('\n'));
  }
}

1 Answer 1

1

I can't answer your question, but I can reproduce this problem and it appears to be related to the use of the synthetic input $package$, see also builders using synthetic input. Maybe file an issue with the maintainers of build?

As a work-around I would suggest setting sources rather than generate_for:

targets:
  $default:
    builders:
      package_name|builder_name:
        enabled: true
        #generate_for:
          #- lib/*.dart
    sources:
      - lib/src/input_folder/**
      - $package$

With the configuration above and e.g. an additional folder lib/src/input_folder containing the files input1.dart and input2.dart the following output is generated and written to protos/model.proto:

lib/src/input_folder/input1.dart
lib/src/input_folder/input2.dart
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.