When creating a Generator or Builder implementation, depending on macro-generated code crashes the builder (e.g. if the builder uses a data model class annotated with package:json@JsonCodable()).
Console output:
❯ dart --enable-experiment=macros run build_runner watch -d --enable-experiment=macros
Building package executable...
Built build_runner:build_runner.
[INFO] Generating build script completed, took 536ms
[WARNING] ../lib/builders_interop/generator_using_macro.dart:23:2: Error: This macro application didn't apply correctly.
@JsonCodable()
^
../lib/builders_interop/generator_using_macro.dart:25:16: Error: Final field 'value' is not initialized.
Try to initialize the field in the declaration or in every constructor.
final String value;
^^^^^
[INFO] Precompiling build script... completed, took 921ms
[SEVERE] Failed to precompile build script .dart_tool/build/entrypoint/build.dart.
This is likely caused by a misconfigured builder definition.
Example Builder Code
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:json/json.dart';
import 'package:source_gen/source_gen.dart';
class HelloWorld {
const HelloWorld();
}
class GeneratorUsingMacro extends GeneratorForAnnotation<HelloWorld> {
@override
String generateForAnnotatedElement(
Element element,
ConstantReader annotation,
BuildStep buildStep,
) {
// Comment the below line in/out to see build_runner crash.
// SomeDataClass;
return '/// Hello, world!';
}
}
@JsonCodable()
class SomeDataClass {
final String value;
}
Description
When creating a Generator or Builder implementation, depending on macro-generated code crashes the builder (e.g. if the builder uses a data model class annotated with
package:json
@JsonCodable()
).Console output:
Example Builder Code
Builder Declaration
Additional Info
Dart SDK version: 3.6.0-149.0.dev (dev) (Wed Aug 14 11:51:04 2024 -0700) on "macos_x64"
build
- v2.4.1build_runner
- v2.4.12build_runner_core
- v7.3.2