Skip to content

Commit b3d8af0

Browse files
authored
Clean up build.yaml warnings. (#4207)
1 parent 280eab1 commit b3d8af0

File tree

5 files changed

+47
-124
lines changed

5 files changed

+47
-124
lines changed

build_runner/CHANGELOG.md

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

3+
- Improved warnings when an option is specified for an unknown builder.
34
- Bug fix: require `args` 2.5.0.
45

56
## 2.8.0

build_runner/lib/src/bootstrap/build_script_generate.dart

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'dart:async';
66

7-
import 'package:build/build.dart' show AssetId, BuilderOptions;
7+
import 'package:build/build.dart';
88
import 'package:build_config/build_config.dart';
99
import 'package:code_builder/code_builder.dart';
1010
import 'package:dart_style/dart_style.dart';
@@ -110,37 +110,20 @@ Future<BuildScriptInfo> findBuildScriptOptions() async {
110110
}
111111
}
112112

113-
// TODO: Remove the dynamic calls here. We don't have a shared interface
114-
// today and rely on dynamic calls instead which is bad.
115-
bool isValidDefinition(dynamic definition) {
116-
// Filter out builderDefinitions with relative imports that aren't
117-
// from the root package, because they will never work.
113+
bool isPackageImportOrForRoot(dynamic definition) {
118114
// ignore: avoid_dynamic_calls
119115
final import = definition.import as String;
120-
if (import.startsWith('package:')) {
121-
// Make sure package is known in packageGraph (when present),
122-
// otherwise PackageNotFoundException will be thrown down the road
123-
final pkg = AssetId.resolve(Uri.parse(import)).package;
124-
if (packageGraph.allPackages.containsKey(pkg)) {
125-
return true;
126-
}
127-
buildLog.warning(
128-
'Could not load imported package "$pkg" for definition '
129-
// ignore: avoid_dynamic_calls
130-
'"${definition.key}".',
131-
);
132-
return false;
133-
}
134116
// ignore: avoid_dynamic_calls
135-
return definition.package == packageGraph.root.name;
117+
final package = definition.package as String;
118+
return import.startsWith('package:') || package == packageGraph.root.name;
136119
}
137120

138121
final orderedConfigs = await Future.wait(
139122
orderedPackages.map(packageBuildConfig),
140123
);
141124
final builderDefinitions = orderedConfigs
142125
.expand((c) => c.builderDefinitions.values)
143-
.where(isValidDefinition);
126+
.where(isPackageImportOrForRoot);
144127

145128
final rootBuildConfig = orderedConfigs.last;
146129
final orderedBuilders =
@@ -151,25 +134,7 @@ Future<BuildScriptInfo> findBuildScriptOptions() async {
151134

152135
final postProcessBuilderDefinitions = orderedConfigs
153136
.expand((c) => c.postProcessBuilderDefinitions.values)
154-
.where(isValidDefinition);
155-
156-
// Validate the builder keys in the global builder config, these should always
157-
// refer to actual builders.
158-
final allBuilderKeys = {
159-
for (final definition in orderedBuilders) definition.key,
160-
};
161-
for (final globalBuilderConfig in rootBuildConfig.globalOptions.entries) {
162-
void checkBuilderKey(String builderKey) {
163-
if (allBuilderKeys.contains(builderKey)) return;
164-
buildLog.warning(
165-
'Invalid builder key `$builderKey` found in global_options config of '
166-
'build.yaml. This configuration will have no effect.',
167-
);
168-
}
169-
170-
checkBuilderKey(globalBuilderConfig.key);
171-
globalBuilderConfig.value.runsBefore.forEach(checkBuilderKey);
172-
}
137+
.where(isPackageImportOrForRoot);
173138

174139
final applications = [
175140
for (final builder in orderedBuilders) _applyBuilder(builder),
@@ -320,14 +285,9 @@ Expression _rawStringList(List<String> strings) =>
320285
/// Returns the actual import to put in the generated script based on an import
321286
/// found in the build.yaml.
322287
String _buildScriptImport(String import) {
323-
if (import.startsWith('package:')) {
324-
return import;
325-
} else if (import.startsWith('../') || import.startsWith('/')) {
326-
buildLog.warning(
327-
'The `../` import syntax in build.yaml is now deprecated, '
328-
'instead do a normal relative import as if it was from the root of '
329-
'the package. Found `$import` in your `build.yaml` file.',
330-
);
288+
if (import.startsWith('package:') ||
289+
import.startsWith('../') ||
290+
import.startsWith('/')) {
331291
return import;
332292
} else {
333293
return p.url.relative(import, from: p.url.dirname(scriptLocation));

build_runner/lib/src/build_plan/build_phases.dart

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Future<BuildPhases> createBuildPhases(
137137
BuiltMap<String, BuiltMap<String, dynamic>> builderConfigOverrides,
138138
bool isReleaseMode,
139139
) async {
140-
validateBuilderConfig(
140+
warnForUnknownBuilders(
141141
builderApplications,
142142
targetGraph.rootPackageConfig,
143143
builderConfigOverrides,
@@ -312,8 +312,8 @@ Map<String, List<BuilderApplication>> _applyWith(
312312
BuilderOptions _options(Map<String, dynamic>? options) =>
313313
options?.isEmpty ?? true ? BuilderOptions.empty : BuilderOptions(options!);
314314

315-
/// Checks that all configuration is for valid builder keys.
316-
void validateBuilderConfig(
315+
/// Warns about configuration related to unknown builders.
316+
void warnForUnknownBuilders(
317317
Iterable<BuilderApplication> builders,
318318
BuildConfig rootPackageConfig,
319319
BuiltMap<String, BuiltMap<String, dynamic>> builderConfigOverrides,
@@ -322,27 +322,34 @@ void validateBuilderConfig(
322322
for (final key in builderConfigOverrides.keys) {
323323
if (!builderKeys.contains(key)) {
324324
buildLog.warning(
325-
'Overriding configuration for `$key` but this is not a '
326-
'known Builder',
325+
'Ignoring options overrides for '
326+
'unknown builder `$key`.',
327327
);
328328
}
329329
}
330330
for (final target in rootPackageConfig.buildTargets.values) {
331331
for (final key in target.builders.keys) {
332332
if (!builderKeys.contains(key)) {
333333
buildLog.warning(
334-
'Configuring `$key` in target `${target.key}` but this '
335-
'is not a known Builder.',
334+
'Ignoring options for unknown builder `$key` '
335+
'in target `${target.key}`.',
336336
);
337337
}
338338
}
339339
}
340340
for (final key in rootPackageConfig.globalOptions.keys) {
341341
if (!builderKeys.contains(key)) {
342-
buildLog.warning(
343-
'Configuring `$key` in global options but this is not a '
344-
'known Builder.',
345-
);
342+
buildLog.warning('Ignoring `global_options` for unknown builder `$key`.');
343+
}
344+
}
345+
for (final value in rootPackageConfig.globalOptions.values) {
346+
for (final key in value.runsBefore) {
347+
if (!builderKeys.contains(key)) {
348+
buildLog.warning(
349+
'Ignoring `runs_before` in `global_options` '
350+
'referencing unknown builder `$key`.',
351+
);
352+
}
346353
}
347354
}
348355
}

build_runner/test/bootstrap/build_script_generate_test.dart

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,6 @@ void main() {
2929
});
3030

3131
group('of builder imports', () {
32-
test('warn about deprecated ../ style imports', () async {
33-
await d.dir('a', [
34-
d.file('build.yaml', '''
35-
builders:
36-
fake:
37-
import: "../../../tool/builder.dart"
38-
builder_factories: ["myFactory"]
39-
build_extensions: {"foo": ["bar"]}
40-
'''),
41-
]).create();
42-
43-
final result = await runPub(
44-
'a',
45-
'run',
46-
args: ['build_runner', 'build'],
47-
);
48-
expect(result.stderr, isEmpty);
49-
expect(
50-
result.stdout,
51-
contains('The `../` import syntax in build.yaml is now deprecated'),
52-
);
53-
});
54-
5532
test('support package relative imports', () async {
5633
await d.dir('a', [
5734
d.file('build.yaml', '''
@@ -69,13 +46,6 @@ builders:
6946
args: ['build_runner', 'build'],
7047
);
7148
expect(result.stderr, isEmpty);
72-
expect(
73-
result.stdout,
74-
isNot(
75-
contains('The `../` import syntax in build.yaml is now deprecated'),
76-
),
77-
);
78-
7949
await d.dir('a', [
8050
d.dir('.dart_tool', [
8151
d.dir('build', [
@@ -108,31 +78,6 @@ builders:
10878
expect(result.stderr, isEmpty);
10979
expect(result.stdout, contains('could not be parsed'));
11080
});
111-
112-
test('warn when import not present in packageGraph', () async {
113-
await d.dir('a', [
114-
d.file('build.yaml', '''
115-
builders:
116-
fake:
117-
import: "package:unknown_package/import.dart"
118-
builder_factories: ["myFactory"]
119-
build_extensions: {"foo": ["bar"]}
120-
'''),
121-
]).create();
122-
final result = await runPub(
123-
'a',
124-
'run',
125-
args: ['build_runner', 'build'],
126-
);
127-
expect(result.stderr, isEmpty);
128-
expect(
129-
result.stdout,
130-
contains(
131-
'Could not load imported package "unknown_package" '
132-
'for definition "a:fake".',
133-
),
134-
);
135-
});
13681
});
13782

13883
test('checks builder keys in global_options', () async {
@@ -150,13 +95,10 @@ global_options:
15095
expect(
15196
result.stdout,
15297
allOf(
98+
contains('Ignoring `global_options` for unknown builder `a:a`.'),
15399
contains(
154-
'Invalid builder key `a:a` found in global_options config of '
155-
'build.yaml. This configuration will have no effect.',
156-
),
157-
contains(
158-
'Invalid builder key `b:b` found in global_options config of '
159-
'build.yaml. This configuration will have no effect.',
100+
'Ignoring `runs_before` in `global_options` '
101+
'referencing unknown builder `b:b`.',
160102
),
161103
),
162104
);

build_runner/test/integration_tests/build_integration_test.dart

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,12 @@ targets:
579579

580580
final result = await runBuild();
581581

582-
expect(result, contains('not a known Builder'));
582+
expect(
583+
result,
584+
contains(
585+
'Ignoring options for unknown builder `bad:builder` in target `a:a`.',
586+
),
587+
);
583588
});
584589

585590
test('warns on invalid builder key in global options', () async {
@@ -608,7 +613,12 @@ global_options:
608613

609614
final result = await runBuild();
610615

611-
expect(result, contains('not a known Builder'));
616+
expect(
617+
result,
618+
contains(
619+
'Ignoring `global_options` for unknown builder `bad:builder`.',
620+
),
621+
);
612622
});
613623

614624
test('warns on invalid builder key --define', () async {
@@ -632,7 +642,10 @@ global_options:
632642

633643
final result = await runBuild(extraArgs: ['--define=bad:key=foo=bar']);
634644

635-
expect(result, contains('not a known Builder'));
645+
expect(
646+
result,
647+
contains('Ignoring options overrides for unknown builder `bad:key`.'),
648+
);
636649
});
637650
});
638651

0 commit comments

Comments
 (0)