Reputation: 2782
I am writing a dart source generator that is using json_serializable for some of it models. Initially the source generation was working fine for some time but now when I run dart run build_runner build --delete-conflicting-outputs
I am getting error:
[INFO] Generating build script completed, took 410ms
[INFO] Reading cached asset graph completed, took 62ms
[INFO] Checking for updates since last build completed, took 492ms
[WARNING] Invalidating asset graph due to build script update!
[INFO] Cleaning up outputs from previous builds. completed, took 4ms
[INFO] Generating build script completed, took 79ms
[WARNING] Invalidated precompiled build script due to missing asset graph.
[WARNING] lib/settings/config.dart:3:6: Error: Error when reading 'lib/settings/config.g.dart': No such file or directory
part 'config.g.dart';
^
lib/settings/config.dart:3:6: Error: Can't use 'lib/settings/config.g.dart' as a part, because it has no 'part of' declaration.
part 'config.g.dart';
^
[INFO] Precompiling build script... completed, took 785ms
[SEVERE] Failed to precompile build script .dart_tool/build/entrypoint/build.dart. This is likely caused by a misconfigured builder definition.
Here is the dependencies for my package:
dependencies:
path: ^1.8.1
yaml: ^3.1.0
dotenv: ^3.0.0
recase: ^4.0.0
json_annotation: ^4.4.0
code_builder: ^4.1.0
dart_style: ^2.2.1
dev_dependencies:
lints: ^1.0.0
test: ^1.20.1
build_runner: ^2.1.7
json_serializable: ^6.1.3
And build.yaml looks like this:
builders:
map_gen:
import: "package:map_gen/map_gen.dart"
builder_factories: [ "build" ]
build_extensions: { "$lib$": [ ".map.dart" ] }
auto_apply: dependents
build_to: source
I haven't been able to figure out why this is happening or is there any limitation on using source generator for a package that provides its own generators. Is there any way I can resolve this?
I am using vscode, but I have tried building from terminal and also getting the same error. Even after running flutter clean | dart pub get | dart run build_runner build --delete-conflicting-outputs
same error persists.
Currently I am manually creating .g.dart
file and adding the part of
statement manually, running build_runner
after that results in successful source generation. However if I run build_runner
again after I have linked generated methods to my code, the .g.dart
files are deleted and again I am facing with the same error. I have to repeat this process every time I want source generation.
Upvotes: 2
Views: 2186
Reputation: 11
I faced the exact same issue and got help from the creator of Drift in Dart Build's Gitter chat room. Quoting the solution here:
you can simply create an empty
<project_directory>.build.yaml
file in your package's directory. This special build.yaml file will only be used when a build is started from the directory of your package. To the build system, it will look like your builders simply don't exist (and thus it won't attempt to compile them). When you run a build from other packages that depend on your builder package, the<project_directory>.build.yaml
file is ignored and all your builders are there.
You can refer to how Drift do it here.
Upvotes: 1