Skip to content

Commit c1b93ec

Browse files
authored
Fix running build once per output. (#4045)
1 parent 70ab8bb commit c1b93ec

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

build_runner_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 9.0.1-wip
22

33
- Don't log stack traces for subclasses of `Exception`.
4+
- Bug fix: don't run builders with multiple outputs once per output.
45

56
## 9.0.0
67

build_runner_core/lib/src/generate/build.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ class Build {
310310
// If `primaryInputs` is empty, the phase will only run lazily,
311311
// and might not run at all; so don't log it to start with.
312312
if (primaryInputs.isNotEmpty) {
313-
primaryInputs.sort();
314313
primaryInputsByPhase[phase] = primaryInputs;
315314
primaryInputCountsByPhase[phase] = primaryInputs.length;
316315
}
@@ -382,7 +381,8 @@ class Build {
382381
String package,
383382
int phaseNumber,
384383
) async {
385-
var ids = <AssetId>[];
384+
// Accumulate in a `Set` because inputs are found once per output.
385+
var ids = <AssetId>{};
386386
var phase = buildPhases[phaseNumber];
387387
var packageNode = options.packageGraph[package]!;
388388

@@ -406,7 +406,7 @@ class Build {
406406

407407
ids.add(node.generatedNodeConfiguration!.primaryInput);
408408
}
409-
return ids;
409+
return ids.toList()..sort();
410410
}
411411

412412
/// If [id] is a generated asset, ensures that it has been built.

build_runner_core/test/generate/build_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,25 @@ void main() {
210210
);
211211
});
212212

213+
test('runs once per input not once per output', () async {
214+
var runs = 0;
215+
final testBuilder = TestBuilder(
216+
buildExtensions: {
217+
'.txt': ['.txt.1', '.txt.2'],
218+
},
219+
extraWork: (_, _) {
220+
++runs;
221+
},
222+
);
223+
224+
await testPhases(
225+
[applyToRoot(testBuilder)],
226+
{'a|web/a.txt': ''},
227+
outputs: {'a|web/a.txt.1': '', 'a|web/a.txt.2': ''},
228+
);
229+
expect(runs, 1);
230+
});
231+
213232
test('with a PostProcessBuilder', () async {
214233
await testPhases(
215234
[requiresPostProcessBuilderApplication, postCopyABuilderApplication],

0 commit comments

Comments
 (0)