Skip to content

Commit 1391b39

Browse files
authored
[hooks_runner] Pass proxy environment variables to hook (#2821)
1 parent 613aa5f commit 1391b39

File tree

11 files changed

+120
-7
lines changed

11 files changed

+120
-7
lines changed

pkgs/hooks/example/build/download_asset/lib/src/hook_helpers/download.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ Future<File> downloadAsset(
2727
createTargetName(targetOS.name, targetArchitecture.name, iOSSdk?.type),
2828
);
2929
final uri = downloadUri(targetName);
30-
final request = await HttpClient().getUrl(uri);
30+
final client = HttpClient()
31+
// Respect the http(s)_proxy environment variables.
32+
..findProxy = HttpClient.findProxyFromEnvironment;
33+
final request = await client.getUrl(uri);
3134
final response = await request.close();
3235
if (response.statusCode != 200) {
3336
throw ArgumentError('The request to $uri failed.');

pkgs/hooks_runner/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.2-wip
2+
3+
- Pass `HTTP(S)_PROXY` and related environment variables to hooks.
4+
15
## 1.0.1
26

37
- Ensure build fails if a build hook does not produce an output file.

pkgs/hooks_runner/lib/src/build_runner/build_runner.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import 'dart:async';
66
import 'dart:convert';
77
import 'dart:developer';
8-
import 'dart:io' show Platform;
8+
import 'dart:io' show HttpClient, Platform;
99

1010
import 'package:collection/collection.dart';
1111
import 'package:file/file.dart';
@@ -538,6 +538,19 @@ class NativeAssetsBuildRunner {
538538
),
539539
);
540540

541+
/// Environment variables respected by [HttpClient.findProxyFromEnvironment].
542+
///
543+
/// We forward them to allow hooks to make HTTP requests in environments where
544+
/// a proxy is required.
545+
static const _httpProxyEnvironmentVariables = {
546+
'http_proxy',
547+
'https_proxy',
548+
'no_proxy',
549+
'HTTP_PROXY',
550+
'HTTPS_PROXY',
551+
'NO_PROXY',
552+
};
553+
541554
/// Determines whether to allow an environment variable through
542555
/// if [hookEnvironment] is not passed in.
543556
///
@@ -556,6 +569,7 @@ class NativeAssetsBuildRunner {
556569
'TMPDIR', // Needed for temp dirs in Dart process.
557570
'USERPROFILE', // Needed to find tools in default install locations.
558571
'WINDIR', // Needed for CMake.
572+
..._httpProxyEnvironmentVariables,
559573
};
560574
const variablePrefixesFilter = {
561575
'NIX_', // Needed for Nix-installed toolchains.

pkgs/hooks_runner/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: hooks_runner
22
description: >-
33
This package is the backend that invokes build hooks.
44
5-
version: 1.0.1
5+
version: 1.0.2-wip
66

77
repository: https://github.com/dart-lang/native/tree/main/pkgs/hooks_runner
88

pkgs/hooks_runner/test/build_runner/concurrency_shared_test_helper.dart renamed to pkgs/hooks_runner/test/build_runner/build_process_helper.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import 'package:logging/logging.dart';
1010

1111
import '../helpers.dart';
1212

13-
// Is invoked concurrently multiple times in separate processes.
13+
/// Runs the build process for a test package.
14+
///
15+
/// Extracted into a separate file so that this can be invoked as an independent
16+
/// process to patch environment variables or to test concurrency.
1417
void main(List<String> args) async {
1518
final packageUri = Uri.directory(args[0]);
1619
final packageName = packageUri.pathSegments.lastWhere((e) => e.isNotEmpty);

pkgs/hooks_runner/test/build_runner/concurrency_shared_test.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ void main() async {
3131
executable: dartExecutable,
3232
arguments: [
3333
pkgNativeAssetsBuilderUri
34-
.resolve(
35-
'test/build_runner/concurrency_shared_test_helper.dart',
36-
)
34+
.resolve('test/build_runner/build_process_helper.dart')
3735
.toFilePath(),
3836
packageUri.toFilePath(),
3937
target.toString(),
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2025, 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:io';
6+
7+
import 'package:hooks_runner/hooks_runner.dart';
8+
import 'package:test/test.dart';
9+
10+
import '../helpers.dart';
11+
import 'helpers.dart';
12+
13+
void main() {
14+
test('hooks have access to http proxy variable', () async {
15+
final server = await HttpServer.bind(InternetAddress.anyIPv4, 0);
16+
server.listen((request) async {
17+
expect(request.headers.host, 'testing_proxy.dart.dev');
18+
expect(request.uri.path, '/test_asset');
19+
20+
final response = request.response;
21+
response.statusCode = 200;
22+
response.writeln('test body response');
23+
await response.close();
24+
});
25+
26+
addTearDown(server.close);
27+
final port = server.port;
28+
29+
await inTempDir((tempUri) async {
30+
await copyTestProjects(targetUri: tempUri);
31+
final packageUri = tempUri.resolve('download_assets/');
32+
await runPubGet(workingDirectory: packageUri, logger: logger);
33+
34+
final result = await runProcess(
35+
executable: dartExecutable,
36+
arguments: [
37+
pkgNativeAssetsBuilderUri
38+
.resolve('test/build_runner/build_process_helper.dart')
39+
.toFilePath(),
40+
packageUri.toFilePath(),
41+
Target.current.toString(),
42+
],
43+
workingDirectory: packageUri,
44+
logger: logger,
45+
environment: {'HTTP_PROXY': 'localhost:$port'},
46+
);
47+
expect(result.exitCode, 0);
48+
});
49+
});
50+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import 'dart:io';
2+
3+
import 'package:hooks/hooks.dart';
4+
5+
void main(List<String> args) async {
6+
await build(args, (input, output) async {
7+
final file = File.fromUri(input.outputDirectory.resolve('testfile'));
8+
9+
final client = HttpClient()
10+
..findProxy = HttpClient.findProxyFromEnvironment;
11+
// This request should fail outside of proxies.
12+
final request = await client.get(
13+
'testing_proxy.dart.dev',
14+
80,
15+
'test_asset',
16+
);
17+
final response = await request.close();
18+
19+
if (response.statusCode != 200) {
20+
throw Exception('Could not fetch asset');
21+
}
22+
23+
await response.pipe(file.openWrite());
24+
});
25+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: download_assets
2+
description: Has a build hook downloading a code asset, used to verify proxy env variables working.
3+
version: 0.1.0
4+
5+
publish_to: none
6+
7+
resolution: workspace
8+
9+
environment:
10+
sdk: '>=3.9.0 <4.0.0'
11+
12+
dependencies:
13+
hooks: any

pkgs/hooks_runner/test_data/manifest.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
- depend_on_fail_build_app/pubspec.yaml
4141
- dev_dependency_with_hook/pubspec.yaml
4242
- dev_dependency_with_hook/test/my_test.dart
43+
- download_assets/hook/build.dart
44+
- download_assets/pubspec.yaml
4345
- drop_dylib_link/bin/drop_dylib_link.dart
4446
- drop_dylib_link/hook/build.dart
4547
- drop_dylib_link/hook/link.dart

0 commit comments

Comments
 (0)