Skip to content

Commit 999ddf1

Browse files
authored
[native_assets_builder] Fail early on too old native_assets_cli (#1923)
1 parent 42298b2 commit 999ddf1

File tree

9 files changed

+148
-0
lines changed

9 files changed

+148
-0
lines changed

.github/workflows/native.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ jobs:
7272
- run: dart pub get -C test_data/native_add_version_skew/
7373
if: ${{ matrix.package == 'native_assets_builder' }}
7474

75+
- run: dart pub get -C test_data/native_add_version_skew_2/
76+
if: ${{ matrix.package == 'native_assets_builder' }}
77+
7578
- run: dart pub get -C test_data/native_subtract/
7679
if: ${{ matrix.package == 'native_assets_builder' }}
7780

@@ -105,6 +108,9 @@ jobs:
105108
- run: dart pub get -C test_data/native_dynamic_linking/
106109
if: ${{ matrix.package == 'native_assets_builder' }}
107110

111+
- run: dart pub get -C test_data/no_hook/
112+
if: ${{ matrix.package == 'native_assets_builder' }}
113+
108114
- run: dart pub get -C example/build/download_asset/
109115
if: ${{ matrix.package == 'native_assets_cli' }}
110116

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:logging/logging.dart';
1111
import 'package:meta/meta.dart';
1212
import 'package:native_assets_cli/native_assets_cli_internal.dart';
1313
import 'package:package_config/package_config.dart';
14+
import 'package:pub_semver/pub_semver.dart';
1415

1516
import '../dependencies_hash_file/dependencies_hash_file.dart';
1617
import '../locking/locking.dart';
@@ -116,6 +117,10 @@ class NativeAssetsBuildRunner {
116117
);
117118
if (buildPlan == null) return null;
118119

120+
if (!await _ensureNativeAssetsCliProtocolVersion()) {
121+
return null;
122+
}
123+
119124
var hookResult = HookResult();
120125
final globalMetadata = <String, Metadata>{};
121126
for (final package in buildPlan) {
@@ -806,6 +811,58 @@ ${compileResult.stdout}
806811
? BuildOutput(hookOutputJson)
807812
: LinkOutput(hookOutputJson);
808813
}
814+
815+
Future<bool> _ensureNativeAssetsCliProtocolVersion() async {
816+
final package = packageLayout.packageConfig['native_assets_cli'] ??
817+
packageLayout.packageConfig['hook']; // Anticipate rename.
818+
if (package == null) {
819+
// No dependencies with a hook or using a different protocol helper
820+
// package.
821+
return true;
822+
}
823+
final packageRoot = package.root.normalizePath();
824+
final hookVersion = await _nativeAssetsCliProtocolVersion(packageRoot);
825+
if (hookVersion == null) {
826+
logger.fine('Could not determine the protocol version of '
827+
'${packageRoot.toFilePath()}.');
828+
// This is most likely due to a newer version of the package.
829+
return true;
830+
}
831+
if (latestParsableVersion > hookVersion) {
832+
// The hook is too old.
833+
logger.shout(
834+
'The protocol version of ${packageRoot.toFilePath()} is '
835+
'$hookVersion, which is no longer supported. Please update your '
836+
'dependencies.',
837+
);
838+
return false;
839+
}
840+
return true;
841+
}
842+
843+
Future<Version?> _nativeAssetsCliProtocolVersion(Uri packageRoot) async {
844+
const files = [
845+
'lib/src/config.dart',
846+
'lib/src/model/hook_config.dart',
847+
];
848+
for (final fileName in files) {
849+
final file = _fileSystem.file(packageRoot.resolve(fileName));
850+
if (!await file.exists()) {
851+
continue;
852+
}
853+
final contents = await file.readAsString();
854+
final regex = RegExp(r'latestVersion = Version\((\d+), (\d+), (\d+)\);');
855+
final match = regex.firstMatch(contents);
856+
if (match == null) {
857+
continue;
858+
}
859+
final major = int.parse(match.group(1)!);
860+
final minor = int.parse(match.group(2)!);
861+
final patch = int.parse(match.group(3)!);
862+
return Version(major, minor, patch);
863+
}
864+
return null;
865+
}
809866
}
810867

811868
/// Parses depfile contents.

pkgs/native_assets_builder/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ dependencies:
2020
native_assets_cli:
2121
path: ../native_assets_cli/
2222
package_config: ^2.1.0
23+
pub_semver: ^2.1.5
2324
yaml: ^3.1.2
2425
yaml_edit: ^2.1.0
2526

pkgs/native_assets_builder/test/build_runner/version_skew_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,33 @@ void main() async {
3636
}
3737
});
3838
});
39+
40+
test('Test hook using a too old version of native_assets_cli',
41+
timeout: longTimeout, () async {
42+
await inTempDir((tempUri) async {
43+
await copyTestProjects(targetUri: tempUri);
44+
final packageUri = tempUri.resolve('native_add_version_skew_2/');
45+
46+
// First, run `pub get`, we need pub to resolve our dependencies.
47+
await runPubGet(
48+
workingDirectory: packageUri,
49+
logger: logger,
50+
);
51+
52+
final logMessages = <String>[];
53+
final result = await buildCodeAssets(
54+
packageUri,
55+
capturedLogs: logMessages,
56+
);
57+
expect(result, isNull);
58+
expect(
59+
logMessages.join('\n'),
60+
stringContainsInOrder([
61+
'The protocol version of ',
62+
'native_assets_cli',
63+
' is 1.3.0, which is no longer supported.',
64+
'Please update your dependencies.'
65+
]));
66+
});
67+
});
3968
}

pkgs/native_assets_builder/test_data/manifest.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
- native_add_version_skew/src/native_add.c
7070
- native_add_version_skew/src/native_add.h
7171
- native_add_version_skew/test/native_add_test.dart
72+
- native_add_version_skew_2/hook/build.dart
73+
- native_add_version_skew_2/pubspec.yaml
7274
- native_add/ffigen.yaml
7375
- native_add/hook/build.dart
7476
- native_add/lib/native_add.dart
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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:native_assets_cli/native_assets_cli.dart';
7+
import 'package:native_toolchain_c/native_toolchain_c.dart';
8+
9+
void main(List<String> arguments) async {
10+
await build(arguments, (config, output) async {
11+
final packageName = config.packageName;
12+
final cbuilder = CBuilder.library(
13+
name: packageName,
14+
assetName: 'src/native_add_bindings_generated.dart',
15+
sources: [
16+
'src/native_add.c',
17+
],
18+
dartBuildFiles: [],
19+
);
20+
await cbuilder.run(
21+
config: config,
22+
output: output,
23+
logger: Logger('')
24+
..level = Level.ALL
25+
..onRecord.listen((record) {
26+
print('${record.level.name}: ${record.time}: ${record.message}');
27+
}),
28+
);
29+
});
30+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- hook/build.dart
2+
- pubspec.yaml
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: native_add_version_skew_2
2+
description: This package should no longer work due to being too old.
3+
version: 0.1.0
4+
5+
publish_to: none
6+
7+
environment:
8+
sdk: '>=3.3.0 <4.0.0'
9+
10+
dependencies:
11+
logging: ^1.1.1
12+
native_assets_cli: ^0.6.0
13+
native_toolchain_c: ^0.5.0
14+
15+
dev_dependencies:
16+
ffigen: ^8.0.2
17+
lints: ^3.0.0
18+
some_dev_dep:
19+
path: ../some_dev_dep/
20+
test: ^1.23.1

pkgs/native_assets_cli/lib/native_assets_cli_internal.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
library;
77

88
export 'native_assets_cli_builder.dart';
9+
export 'src/config.dart' show latestParsableVersion;
910
export 'src/model/hook.dart';

0 commit comments

Comments
 (0)