Skip to content

Commit 104d468

Browse files
authored
Warn about configuring builders with --config (#2968)
Closes #2942 This also cleans up some `dart:io` imports to use the asset reader interface instead, which was a requirement for unit testing this but is a good general cleanup.
1 parent b6cab77 commit 104d468

File tree

9 files changed

+146
-38
lines changed

9 files changed

+146
-38
lines changed

build_runner/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.10.12-dev
2+
3+
- Add a warning if a `builders` section is found in when parsing an overriden
4+
build.yaml file via the `--config` flag.
5+
16
## 1.10.11
27

38
- Fix handling of `build.yaml` `generateFor` default config for values including

build_runner/lib/src/build_script_generate/build_script_generate.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ Future<Iterable<Expression>> _findBuilderApplications() async {
6666
equals: (a, b) => a.name == b.name,
6767
hashCode: (n) => n.name.hashCode,
6868
).expand((c) => c);
69-
final buildConfigOverrides =
70-
await findBuildConfigOverrides(packageGraph, null);
69+
final buildConfigOverrides = await findBuildConfigOverrides(
70+
packageGraph, null, FileBasedAssetReader(packageGraph));
7171
Future<BuildConfig> _packageBuildConfig(PackageNode package) async {
7272
if (buildConfigOverrides.containsKey(package.name)) {
7373
return buildConfigOverrides[package.name];

build_runner/lib/src/daemon/daemon_builder.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ class BuildRunnerDaemonBuilder implements DaemonBuilder {
191191
var logSubscription =
192192
LogSubscription(environment, verbose: daemonOptions.verbose);
193193

194-
var overrideBuildConfig =
195-
await findBuildConfigOverrides(packageGraph, daemonOptions.configKey);
194+
var overrideBuildConfig = await findBuildConfigOverrides(
195+
packageGraph, daemonOptions.configKey, daemonEnvironment.reader);
196196

197197
var buildOptions = await BuildOptions.create(
198198
logSubscription,

build_runner/lib/src/entrypoint/doctor.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class DoctorCommand extends BuildRunnerCommand {
5151

5252
Future<Map<String, BuilderDefinition>> _loadBuilderDefinitions() async {
5353
final packageGraph = await PackageGraph.forThisPackage();
54-
final buildConfigOverrides =
55-
await findBuildConfigOverrides(packageGraph, null);
54+
final buildConfigOverrides = await findBuildConfigOverrides(
55+
packageGraph, null, FileBasedAssetReader(packageGraph));
5656
Future<BuildConfig> _packageBuildConfig(PackageNode package) async {
5757
if (buildConfigOverrides.containsKey(package.name)) {
5858
return buildConfigOverrides[package.name];

build_runner/lib/src/generate/build.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ Future<BuildResult> build(List<BuilderApplication> builders,
8787
deleteFilesByDefault: deleteFilesByDefault,
8888
packageGraph: packageGraph,
8989
skipBuildScriptCheck: skipBuildScriptCheck,
90-
overrideBuildConfig:
91-
await findBuildConfigOverrides(packageGraph, configKey),
90+
overrideBuildConfig: await findBuildConfigOverrides(
91+
packageGraph, configKey, environment.reader),
9292
enableLowResourcesMode: enableLowResourcesMode,
9393
trackPerformance: trackPerformance,
9494
logPerformanceDir: logPerformanceDir,

build_runner/lib/src/generate/watch_impl.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ Future<ServeHandler> watch(
6666
onLog: onLog ?? stdIOLogListener(assumeTty: assumeTty, verbose: verbose));
6767
var logSubscription =
6868
LogSubscription(environment, verbose: verbose, logLevel: logLevel);
69-
overrideBuildConfig ??=
70-
await findBuildConfigOverrides(packageGraph, configKey);
69+
overrideBuildConfig ??= await findBuildConfigOverrides(
70+
packageGraph, configKey, environment.reader);
7171
var options = await BuildOptions.create(
7272
logSubscription,
7373
deleteFilesByDefault: deleteFilesByDefault,

build_runner/lib/src/package_graph/build_config_overrides.dart

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6-
import 'dart:io';
76

7+
import 'package:build/build.dart';
88
import 'package:build_config/build_config.dart';
99
import 'package:build_runner_core/build_runner_core.dart';
1010
import 'package:glob/glob.dart';
@@ -14,43 +14,49 @@ import 'package:path/path.dart' as p;
1414
final _log = Logger('BuildConfigOverrides');
1515

1616
Future<Map<String, BuildConfig>> findBuildConfigOverrides(
17-
PackageGraph packageGraph, String configKey) async {
17+
PackageGraph packageGraph,
18+
String configKey,
19+
RunnerAssetReader reader) async {
1820
final configs = <String, BuildConfig>{};
19-
final configFiles = Glob('*.build.yaml').list();
20-
await for (final file in configFiles) {
21-
if (file is File) {
22-
final packageName = p.basename(file.path).split('.').first;
23-
final packageNode = packageGraph.allPackages[packageName];
24-
if (packageNode == null) {
25-
_log.warning('A build config override is provided for $packageName but '
26-
'that package does not exist. '
27-
'Remove the ${p.basename(file.path)} override or add a dependency '
28-
'on $packageName.');
29-
continue;
30-
}
31-
final yaml = file.readAsStringSync();
32-
final config = BuildConfig.parse(
33-
packageName,
34-
packageNode.dependencies.map((n) => n.name),
35-
yaml,
36-
configYamlPath: file.path,
37-
);
38-
configs[packageName] = config;
21+
final configFiles =
22+
reader.findAssets(Glob('*.build.yaml'), package: packageGraph.root.name);
23+
await for (final id in configFiles) {
24+
final packageName = p.basename(id.path).split('.').first;
25+
final packageNode = packageGraph.allPackages[packageName];
26+
if (packageNode == null) {
27+
_log.warning('A build config override is provided for $packageName but '
28+
'that package does not exist. '
29+
'Remove the ${p.basename(id.path)} override or add a dependency '
30+
'on $packageName.');
31+
continue;
3932
}
33+
final yaml = await reader.readAsString(id);
34+
final config = BuildConfig.parse(
35+
packageName,
36+
packageNode.dependencies.map((n) => n.name),
37+
yaml,
38+
configYamlPath: id.path,
39+
);
40+
configs[packageName] = config;
4041
}
4142
if (configKey != null) {
42-
final file = File('build.$configKey.yaml');
43-
if (!file.existsSync()) {
44-
_log.warning('Cannot find build.$configKey.yaml for specified config.');
43+
final id = AssetId(packageGraph.root.name, 'build.$configKey.yaml');
44+
if (!await reader.canRead(id)) {
45+
_log.warning('Cannot find ${id.path} for specified config.');
4546
throw CannotBuildException();
4647
}
47-
final yaml = file.readAsStringSync();
48+
final yaml = await reader.readAsString(id);
4849
final config = BuildConfig.parse(
4950
packageGraph.root.name,
5051
packageGraph.root.dependencies.map((n) => n.name),
5152
yaml,
52-
configYamlPath: file.path,
53+
configYamlPath: id.path,
5354
);
55+
if (config.builderDefinitions.isNotEmpty) {
56+
_log.warning(
57+
'Ignoring `builders` configuration in `build.$configKey.yaml` - '
58+
'overriding builder configuration is not supported.');
59+
}
5460
configs[packageGraph.root.name] = config;
5561
}
5662
return configs;

build_runner/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: build_runner
2-
version: 1.10.11
2+
version: 1.10.12-dev
33
description: A build system for Dart code generation and modular compilation.
44
repository: https://github.com/dart-lang/build/tree/master/build_runner
55

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:async';
6+
import 'dart:convert';
7+
8+
import 'package:logging/logging.dart';
9+
import 'package:path/path.dart' as path;
10+
import 'package:test/test.dart';
11+
12+
import 'package:build_runner_core/build_runner_core.dart';
13+
import 'package:build_runner/src/generate/build.dart' as build_impl;
14+
import 'package:build_test/build_test.dart';
15+
16+
import 'package:_test_common/common.dart';
17+
import 'package:_test_common/package_graphs.dart';
18+
19+
void main() {
20+
// Basic phases/phase groups which get used in many tests
21+
final copyABuildApplication = applyToRoot(
22+
TestBuilder(buildExtensions: appendExtension('.copy', from: '.txt')));
23+
final packageConfigId = makeAssetId('a|.dart_tool/package_config.json');
24+
InMemoryRunnerAssetWriter writer;
25+
26+
setUp(() async {
27+
writer = InMemoryRunnerAssetWriter();
28+
await writer.writeAsString(makeAssetId('a|.packages'), '''
29+
# Fake packages file
30+
a:file://fake/pkg/path
31+
''');
32+
await writer.writeAsString(packageConfigId, jsonEncode(_packageConfig));
33+
});
34+
35+
group('--config', () {
36+
test('warns override config defines builders', () async {
37+
var logs = <LogRecord>[];
38+
final packageGraph = buildPackageGraph({
39+
rootPackage('a', path: path.absolute('a')): [],
40+
});
41+
var result = await _doBuild([
42+
copyABuildApplication
43+
], {
44+
'a|build.yaml': '',
45+
'a|build.cool.yaml': '''
46+
builders:
47+
fake:
48+
import: "a.dart"
49+
builder_factories: ["myFactory"]
50+
build_extensions: {"a": ["b"]}
51+
'''
52+
}, writer,
53+
configKey: 'cool',
54+
logLevel: Level.WARNING,
55+
onLog: logs.add,
56+
packageGraph: packageGraph);
57+
expect(result.status, BuildStatus.success);
58+
expect(
59+
logs.first.message,
60+
contains('Ignoring `builders` configuration in `build.cool.yaml` - '
61+
'overriding builder configuration is not supported.'));
62+
});
63+
});
64+
}
65+
66+
Future<BuildResult> _doBuild(List<BuilderApplication> builders,
67+
Map<String, String> inputs, InMemoryRunnerAssetWriter writer,
68+
{PackageGraph packageGraph,
69+
void Function(LogRecord) onLog,
70+
Level logLevel,
71+
String configKey}) async {
72+
onLog ??= (_) {};
73+
inputs.forEach((serializedId, contents) {
74+
writer.writeAsString(makeAssetId(serializedId), contents);
75+
});
76+
packageGraph ??=
77+
buildPackageGraph({rootPackage('a', path: path.absolute('a')): []});
78+
final reader = InMemoryRunnerAssetReader.shareAssetCache(writer.assets,
79+
rootPackage: packageGraph.root.name);
80+
81+
return await build_impl.build(builders,
82+
configKey: configKey,
83+
deleteFilesByDefault: true,
84+
reader: reader,
85+
writer: writer,
86+
packageGraph: packageGraph,
87+
logLevel: logLevel,
88+
onLog: onLog,
89+
skipBuildScriptCheck: true);
90+
}
91+
92+
const _packageConfig = {
93+
'configVersion': 2,
94+
'packages': [
95+
{'name': 'a', 'rootUri': 'file://fake/pkg/path', 'packageUri': 'lib/'},
96+
],
97+
};

0 commit comments

Comments
 (0)