Skip to content

Commit 93039d7

Browse files
authored
[hooks_runner] Don't time out on missiong output.json (#2823)
1 parent 59a519f commit 93039d7

File tree

8 files changed

+82
-3
lines changed

8 files changed

+82
-3
lines changed

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.1
2+
3+
- Ensure build fails if a build hook does not produce an output file.
4+
15
## 1.0.0
26

37
- Stable release.

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,17 @@ ${e.message}''');
961961
String packageName,
962962
) async {
963963
final file = hookOutputFile;
964-
final fileContents = await file.readAsString();
964+
final String fileContents;
965+
try {
966+
fileContents = await file.readAsString();
967+
} on FileSystemException catch (e) {
968+
logger.severe('''
969+
Building assets for package:$packageName failed.
970+
Error reading ${hookOutputFile.uri.toFilePath()}.
971+
972+
${e.message}''');
973+
return const Failure(HooksRunnerFailure.hookRun);
974+
}
965975
logger.info('output.json contents:\n$fileContents');
966976
final Map<String, Object?> hookOutputJson;
967977
try {

pkgs/hooks_runner/lib/src/locking/locking.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ Future<T> _runUnderFileLock<T>(
8787
if (!await file.exists()) await file.create(recursive: true);
8888
final randomAccessFile = await file.open(mode: FileMode.write);
8989
var printed = false;
90+
var errorFromCallback = false;
9091
final stopwatch = Stopwatch()..start();
9192
while (timeout == null || stopwatch.elapsed < timeout) {
9293
try {
@@ -96,11 +97,19 @@ Future<T> _runUnderFileLock<T>(
9697
'Last acquired by ${Platform.resolvedExecutable} '
9798
'(pid $pid) running ${Platform.script} on ${DateTime.now()}.',
9899
);
99-
return await callback();
100+
try {
101+
return await callback();
102+
} on FileSystemException {
103+
errorFromCallback = true;
104+
rethrow;
105+
}
100106
} finally {
101107
await randomAccessFile.unlock();
102108
}
103109
} on FileSystemException {
110+
if (errorFromCallback) {
111+
rethrow;
112+
}
104113
if (!printed) {
105114
logger?.finer(
106115
'Waiting to be able to obtain lock of directory: ${file.path}.',

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.0
5+
version: 1.0.1
66

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 'package:logging/logging.dart';
6+
import 'package:test/test.dart';
7+
8+
import '../helpers.dart';
9+
import 'helpers.dart';
10+
11+
const Timeout longTimeout = Timeout(Duration(minutes: 5));
12+
13+
void main() async {
14+
test('no build output', timeout: longTimeout, () async {
15+
await inTempDir((tempUri) async {
16+
await copyTestProjects(targetUri: tempUri);
17+
final packageUri = tempUri.resolve('no_build_output/');
18+
19+
await runPubGet(workingDirectory: packageUri, logger: logger);
20+
21+
final logMessages = <String>[];
22+
final result = await build(
23+
packageUri,
24+
createCapturingLogger(logMessages, level: Level.SEVERE),
25+
dartExecutable,
26+
buildAssetTypes: [],
27+
);
28+
expect(result.isFailure, isTrue);
29+
final fullLog = logMessages.join('\n');
30+
expect(fullLog, contains('Error reading'));
31+
expect(fullLog, contains('output.json'));
32+
});
33+
});
34+
}

pkgs/hooks_runner/test_data/manifest.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@
139139
- native_subtract/src/native_subtract.h
140140
- no_asset_for_link/hook/link.dart
141141
- no_asset_for_link/pubspec.yaml
142+
- no_build_output/hook/build.dart
143+
- no_build_output/pubspec.yaml
142144
- no_hook/lib/no_hook.dart
143145
- no_hook/pubspec.yaml
144146
- package_reading_metadata/hook/build.dart
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
void main(List<String> arguments) {
6+
// This hook does nothing, and doesn't write an output file.
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: no_build_output
2+
description: A package with a build hook that does not produce any output.
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

0 commit comments

Comments
 (0)