build
build copied to clipboard
Bug: build_runner fails to cleanup outdated outputs
I am investigating this build_runner error when using Jaspr. After some digging I found a reproducible setup and think its a general build_runner bug. It boils down to the following:
I have two builders:
- Builder A reads in
.aand generates.b(e.g.main.a->main.b) - Builder B reads in
.band generates.c(e.g.main.b->main.c)
Builder A has some logic that it only conditionally generates .b files and that condition might change while watching the files (dart run build_runner watch).
When that happens, Builder A no longer outputs main.b. At that point I expect build_runner to also cleanup main.c since Builder B is also no longer executed since it doesn't have any input. However main.c still persists with now outdated contents.
To reproduce do the following steps:
- Create a blank dart project.
- Add
buildas a dep andbuild_runneras a dev dep. - Add the following files:
build.yaml
builders:
a:
import: "package:buildtest/builders.dart"
builder_factories:
- aBuilder
build_extensions:
.a:
- .b
auto_apply: all_packages
build_to: source
required_inputs: [".a"]
b:
import: "package:buildtest/builders.dart"
builder_factories:
- bBuilder
build_extensions:
.b:
- .c
auto_apply: all_packages
build_to: source
required_inputs: [".b"]
lib/builders.dart
import 'dart:async';
import 'package:build/build.dart';
Builder aBuilder(BuilderOptions options) => ABuilder();
Builder bBuilder(BuilderOptions options) => BBuilder();
class ABuilder extends Builder {
@override
FutureOr<void> build(BuildStep buildStep) async {
var content = await buildStep.readAsString(buildStep.inputId);
if (content.contains('on')) {
await buildStep.writeAsString(buildStep.inputId.changeExtension('.b'), content);
}
}
@override
Map<String, List<String>> get buildExtensions => {
'.a': ['.b'],
};
}
class BBuilder extends Builder {
@override
FutureOr<void> build(BuildStep buildStep) async {
var content = await buildStep.readAsString(buildStep.inputId);
await buildStep.writeAsString(buildStep.inputId.changeExtension('.c'), content);
}
@override
Map<String, List<String>> get buildExtensions => {
'.b': ['.c'],
};
}
lib/test.a
test on
- Run
dart run build_runner watch -v - See
lib/test.bandlib/test.cbeing generated. - Change
lib/test.atotest off - See
lib/test.bcorrectly being removed, butlib/test.cwrongly being still there.
In the real-world case I experienced, BuilderA is a custom builder by Jaspr that generates web/<filename>.dart files that should be compiled to js. BuilderB is ddc_modules from package:build_web_compilers that outputs .ddc.module files for each .dart file. The error occurs in the ddc builder that for each .ddc.module tries to read the respective .dart file, which doesn't exist for an outdated module.
You can reproduce the error by creating a new Jaspr project (jaspr create testapp), starting build_runner watch and removing the @client annotation from e.g. lib/pages/about.dart.
dart --version: Dart SDK version: 3.7.0 (stable) (Wed Feb 5 04:53:58 2025 -0800) on "macos_arm64"
build_runner: ^2.4.15 (latest)
build: ^2.4.2 (latest)
Thanks!
Do you think this is a regression, or an old issue?
If I broke something recently I should try to figure out why; if it's an older issue it'll need to wait a little longer until the current refactoring is done.
@davidmorgan I can try to reproduce it with an older build_runner version if that helps. What would be the right version to do that with?
That would be helpful, yes please.
There are a lot of packages :) maybe try
dart pub downgrade build build_config build_modules build_resolvers build_runner build_runner_core build_web_compilers build_test
and see if it picks something sensible?
It appears to be a longer standing bug.
I could reproduce with Dart 3.5 + build_runner 2.4.8 and with Dart 3.4 + build_runner 2.4.2. (I also downgraded the other build packages and tried around with lots of different version combinations, but that lead to nothing.)
Fwiw, the test for this is here https://github.com/dart-lang/build/blob/master/build_runner/test/generate/watch_test.dart#L911.
Yes but not really.
If I simply delete the first file by hand, the rest is removed fine. But this bug is that the first builder stops generating a file. Then it doesn't propagate.
👍 I can reproduce the issue, and yeah it seems specific to whether the output is created based on some condition in the builder, interesting.
Thanks Kilian, thanks Jake :) will see if it's possible to get a fix into the next release, probably a week or two away since a pile of things are mid refactor.
Hi @davidmorgan, whats the current status of this?
@schultek I think I found the place where it needs fixing; once the performance refactoring release is out I'll try it.
"A week or two away" turned out to be very optimistic :) but now I think the next release really is just a week or two away.
Finally made it back to this one, the fix will be in the next release.
Thanks for your patience! :)