Skip to content

Commit 2251646

Browse files
authored
[native_assets_builder] Make hook environment configurable (#1853)
1 parent 0495080 commit 2251646

File tree

6 files changed

+104
-82
lines changed

6 files changed

+104
-82
lines changed

pkgs/native_assets_builder/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.10.1
2+
3+
- Pass in the environment for hook invocations.
4+
15
## 0.10.0
26

37
- Removed support for dry run (Flutter no long requires it).

pkgs/native_assets_builder/lib/native_assets_builder.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
export 'package:native_assets_builder/src/build_runner/build_runner.dart';
5+
export 'package:native_assets_builder/src/build_runner/build_runner.dart'
6+
show
7+
ApplicationAssetValidator,
8+
BuildConfigValidator,
9+
BuildValidator,
10+
LinkConfigValidator,
11+
LinkValidator,
12+
NativeAssetsBuildRunner;
613
export 'package:native_assets_builder/src/model/build_result.dart';
714
export 'package:native_assets_builder/src/model/kernel_assets.dart';
815
export 'package:native_assets_builder/src/model/link_result.dart';

pkgs/native_assets_builder/lib/src/build_runner/build_runner.dart

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,18 @@ class NativeAssetsBuildRunner {
6868
final Logger logger;
6969
final Uri dartExecutable;
7070
final Duration singleHookTimeout;
71+
final Map<String, String> hookEnvironment;
7172

7273
NativeAssetsBuildRunner({
7374
required this.logger,
7475
required this.dartExecutable,
7576
required FileSystem fileSystem,
7677
Duration? singleHookTimeout,
78+
Map<String, String>? hookEnvironment,
7779
}) : _fileSystem = fileSystem,
78-
singleHookTimeout = singleHookTimeout ?? const Duration(minutes: 5);
80+
singleHookTimeout = singleHookTimeout ?? const Duration(minutes: 5),
81+
hookEnvironment = hookEnvironment ??
82+
filteredEnvironment(hookEnvironmentVariablesFilter);
7983

8084
/// [workingDirectory] is expected to contain `.dart_tool`.
8185
///
@@ -321,7 +325,6 @@ class NativeAssetsBuildRunner {
321325
Uri? resources,
322326
PackageLayout packageLayout,
323327
) async {
324-
final environment = _filteredEnvironment(_environmentVariablesFilter);
325328
final outDir = config.outputDirectory;
326329
return await runUnderDirectoriesLock(
327330
_fileSystem,
@@ -367,7 +370,7 @@ ${e.message}
367370
}
368371

369372
final outdatedDependency =
370-
await dependenciesHashes.findOutdatedDependency(environment);
373+
await dependenciesHashes.findOutdatedDependency(hookEnvironment);
371374
if (outdatedDependency == null) {
372375
logger.info(
373376
'Skipping ${hook.name} for ${config.packageName}'
@@ -393,7 +396,7 @@ ${e.message}
393396
resources,
394397
hookKernelFile,
395398
packageLayout,
396-
environment,
399+
hookEnvironment,
397400
);
398401
if (result == null) {
399402
if (await dependenciesHashes.exists()) {
@@ -409,7 +412,7 @@ ${e.message}
409412
hookKernelFile.uri,
410413
],
411414
lastModifiedCutoffTime,
412-
environment,
415+
hookEnvironment,
413416
);
414417
if (modifiedDuringBuild != null) {
415418
logger.severe('File modified during build. Build must be rerun.');
@@ -420,11 +423,11 @@ ${e.message}
420423
);
421424
}
422425

423-
/// Limit the environment that hook invocations get to see.
424-
///
426+
/// The list of environment variables used if [hookEnvironment] is not passed
427+
/// in.
425428
/// This allowlist lists environment variables needed to run mainstream
426429
/// compilers.
427-
static const _environmentVariablesFilter = {
430+
static const hookEnvironmentVariablesFilter = {
428431
'ANDROID_HOME', // Needed for the NDK.
429432
'HOME', // Needed to find tools in default install locations.
430433
'PATH', // Needed to invoke native tools.
@@ -530,12 +533,6 @@ ${e.message}
530533
}
531534
}
532535

533-
Map<String, String> _filteredEnvironment(Set<String> allowList) => {
534-
for (final entry in Platform.environment.entries)
535-
if (allowList.contains(entry.key.toUpperCase()))
536-
entry.key: entry.value,
537-
};
538-
539536
/// Compiles the hook to kernel and caches the kernel.
540537
///
541538
/// If any of the Dart source files, or the package config changed after
@@ -842,3 +839,8 @@ Future<List<Uri>> _readDepFile(File depFile) async {
842839
final dartSources = parseDepFileInputs(depFileContents);
843840
return dartSources.map(Uri.file).toList();
844841
}
842+
843+
Map<String, String> filteredEnvironment(Set<String> allowList) => {
844+
for (final entry in Platform.environment.entries)
845+
if (allowList.contains(entry.key.toUpperCase())) entry.key: entry.value,
846+
};

pkgs/native_assets_builder/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: native_assets_builder
22
description: >-
33
This package is the backend that invokes build hooks.
4-
version: 0.10.0
4+
version: 0.10.1
55
repository: https://github.com/dart-lang/native/tree/main/pkgs/native_assets_builder
66

77
# publish_to: none

pkgs/native_assets_builder/test/build_runner/build_runner_caching_test.dart

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:convert';
66
import 'dart:io';
77

8+
import 'package:native_assets_builder/src/build_runner/build_runner.dart';
89
import 'package:test/test.dart';
910

1011
import '../helpers.dart';
@@ -221,75 +222,81 @@ void main() async {
221222
},
222223
);
223224

224-
test(
225-
'change environment',
226-
timeout: longTimeout,
227-
() async {
228-
await inTempDir((tempUri) async {
229-
await copyTestProjects(targetUri: tempUri);
230-
final packageUri = tempUri.resolve('native_add/');
225+
for (final modifiedEnvKey in ['PATH', 'CUSTOM_KEY_123']) {
226+
test(
227+
'change environment $modifiedEnvKey',
228+
timeout: longTimeout,
229+
() async {
230+
await inTempDir((tempUri) async {
231+
await copyTestProjects(targetUri: tempUri);
232+
final packageUri = tempUri.resolve('native_add/');
231233

232-
final logMessages = <String>[];
233-
final logger = createCapturingLogger(logMessages);
234+
final logMessages = <String>[];
235+
final logger = createCapturingLogger(logMessages);
234236

235-
await runPubGet(workingDirectory: packageUri, logger: logger);
236-
logMessages.clear();
237+
await runPubGet(workingDirectory: packageUri, logger: logger);
238+
logMessages.clear();
237239

238-
final result = (await build(
239-
packageUri,
240-
logger,
241-
dartExecutable,
242-
buildAssetTypes: [CodeAsset.type],
243-
configValidator: validateCodeAssetBuildConfig,
244-
buildValidator: validateCodeAssetBuildOutput,
245-
applicationAssetValidator: validateCodeAssetInApplication,
246-
))!;
247-
logMessages.clear();
240+
final result = (await build(
241+
packageUri,
242+
logger,
243+
dartExecutable,
244+
buildAssetTypes: [CodeAsset.type],
245+
configValidator: validateCodeAssetBuildConfig,
246+
buildValidator: validateCodeAssetBuildOutput,
247+
applicationAssetValidator: validateCodeAssetInApplication,
248+
hookEnvironment: modifiedEnvKey == 'PATH'
249+
? null
250+
: filteredEnvironment(
251+
NativeAssetsBuildRunner.hookEnvironmentVariablesFilter,
252+
),
253+
))!;
254+
logMessages.clear();
248255

249-
// Simulate that the environment variables changed by augmenting the
250-
// persisted environment from the last invocation.
251-
final dependenciesHashFile = File.fromUri(
252-
CodeAsset.fromEncoded(result.encodedAssets.single)
253-
.file!
254-
.parent
255-
.parent
256-
.resolve('dependencies.dependencies_hash_file.json'),
257-
);
258-
expect(await dependenciesHashFile.exists(), true);
259-
final dependenciesContent =
260-
jsonDecode(await dependenciesHashFile.readAsString())
261-
as Map<Object, Object?>;
262-
const modifiedEnvKey = 'PATH';
263-
(dependenciesContent['environment'] as List<dynamic>).add({
264-
'key': modifiedEnvKey,
265-
'hash': 123456789,
266-
});
267-
await dependenciesHashFile
268-
.writeAsString(jsonEncode(dependenciesContent));
256+
// Simulate that the environment variables changed by augmenting the
257+
// persisted environment from the last invocation.
258+
final dependenciesHashFile = File.fromUri(
259+
CodeAsset.fromEncoded(result.encodedAssets.single)
260+
.file!
261+
.parent
262+
.parent
263+
.resolve('dependencies.dependencies_hash_file.json'),
264+
);
265+
expect(await dependenciesHashFile.exists(), true);
266+
final dependenciesContent =
267+
jsonDecode(await dependenciesHashFile.readAsString())
268+
as Map<Object, Object?>;
269+
(dependenciesContent['environment'] as List<dynamic>).add({
270+
'key': modifiedEnvKey,
271+
'hash': 123456789,
272+
});
273+
await dependenciesHashFile
274+
.writeAsString(jsonEncode(dependenciesContent));
269275

270-
(await build(
271-
packageUri,
272-
logger,
273-
dartExecutable,
274-
buildAssetTypes: [CodeAsset.type],
275-
configValidator: validateCodeAssetBuildConfig,
276-
buildValidator: validateCodeAssetBuildOutput,
277-
applicationAssetValidator: validateCodeAssetInApplication,
278-
))!;
279-
expect(
280-
logMessages.join('\n'),
281-
contains('hook.dill'),
282-
);
283-
expect(
284-
logMessages.join('\n'),
285-
isNot(contains('Skipping build for native_add')),
286-
);
287-
expect(
288-
logMessages.join('\n'),
289-
contains('Environment variable changed: $modifiedEnvKey.'),
290-
);
291-
logMessages.clear();
292-
});
293-
},
294-
);
276+
(await build(
277+
packageUri,
278+
logger,
279+
dartExecutable,
280+
buildAssetTypes: [CodeAsset.type],
281+
configValidator: validateCodeAssetBuildConfig,
282+
buildValidator: validateCodeAssetBuildOutput,
283+
applicationAssetValidator: validateCodeAssetInApplication,
284+
))!;
285+
expect(
286+
logMessages.join('\n'),
287+
contains('hook.dill'),
288+
);
289+
expect(
290+
logMessages.join('\n'),
291+
isNot(contains('Skipping build for native_add')),
292+
);
293+
expect(
294+
logMessages.join('\n'),
295+
contains('Environment variable changed: $modifiedEnvKey.'),
296+
);
297+
logMessages.clear();
298+
});
299+
},
300+
);
301+
}
295302
}

pkgs/native_assets_builder/test/build_runner/helpers.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ Future<BuildResult?> build(
4848
Target? target,
4949
bool linkingEnabled = false,
5050
required List<String> buildAssetTypes,
51+
Map<String, String>? hookEnvironment,
5152
}) async {
5253
final targetOS = target?.os ?? OS.current;
5354
return await runWithLog(capturedLogs, () async {
5455
final result = await NativeAssetsBuildRunner(
5556
logger: logger,
5657
dartExecutable: dartExecutable,
5758
fileSystem: const LocalFileSystem(),
59+
hookEnvironment: hookEnvironment,
5860
).build(
5961
configCreator: () {
6062
final configBuilder = BuildConfigBuilder();

0 commit comments

Comments
 (0)