flutterdartbuilt-valuedart-build

Trying to create a custom builder results in a non unique output error


I'm trying to create custom builder for my Flutter project. I want to achieve something similar to built_value code generation.

I've started by creating build.yaml in the root folder of my project

targets:
  $default:
    builders:
      app|db_builder:
        enabled: true

builders:
  db_builder:
    target: ":db_builder"
    import: "package:app/db/build/builder.dart"
    builder_factories: ["dbBuilder"]
    build_extensions: {".dart": [".db.g.part"]}
    auto_apply: dependents
    build_to: cache
    applies_builders: ["source_gen|combining_builder"]

Then implemented dbBuilder factory at app/db/build/builder.dart

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

import 'db_generator.dart';

Builder dbBuilder(BuilderOptions _) =>
    SharedPartBuilder([DBAccessGenerator()], 'built_value');

DBAccessGenerator is an empty class inherited from Generator

My project also contains built_value_generator.

After the above mentioned steps, when running flutter pub run build_runner build I've got the following error:

Both app:db_builder and built_value_generator:built_value may output
test/widget_test.built_value.g.part. Potential outputs must be unique across all builders.

Why this happens? I've seen the source code of built_value_generator and it declares build extensions as build_extensions: {".dart": [".built_value.g.part"]} but my declaration differs, I'm using another target extension.


Solution

  • The reason is inside the app/db/build/builder.dart file.

    When creating SharedPartBuilder, the second argument is a partId and it is the real output extension of a generated file, not a one specified in a build.yaml file.

    So, setting another name here fixes the problem.

    More here: https://github.com/dart-lang/build/issues/2670