Skip to content

Commit 618afba

Browse files
authored
[pigeon] add version checking for objective_c and jni dependencies (flutter#13283)
To avoid surprising build errors
1 parent ef6ad7b commit 618afba

6 files changed

Lines changed: 425 additions & 8 deletions

File tree

‎packages/pigeon/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 29.0.11
2+
3+
* [swift] Warns after FFI code generation if the `objective_c` version constraint in `pubspec.yaml` does not match the generated `ObjCVersionCheck`.
4+
* [kotlin] Warns after JNI code generation if the `jni` version constraint in `pubspec.yaml` does not match the generated `JniVersionCheck`.
5+
16
## 29.0.10
27

38
* [swift] [kotlin] Allows `sealed` classes in native interop files.

‎packages/pigeon/lib/src/generator_tools.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import 'generator.dart';
1616
/// The current version of pigeon.
1717
///
1818
/// This must match the version in pubspec.yaml.
19-
const String pigeonVersion = '29.0.10';
19+
const String pigeonVersion = '29.0.11';
2020

2121
/// Default plugin package name.
2222
const String defaultPluginPackageName = 'dev.flutter.pigeon';

‎packages/pigeon/lib/src/pigeon_lib.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,10 @@ ${_argParser.usage}''';
919919
return exitCode;
920920
}
921921
}
922+
final String? objcVersionWarning = checkObjectiveCPubspecVersion(internalOptions);
923+
if (objcVersionWarning != null) {
924+
stderr.writeln('Warning: $objcVersionWarning');
925+
}
922926
}
923927

924928
if (useJni) {
@@ -947,6 +951,10 @@ ${_argParser.usage}''';
947951
return 1;
948952
}
949953
}
954+
final String? jniVersionWarning = checkJniPubspecVersion(internalOptions);
955+
if (jniVersionWarning != null) {
956+
stderr.writeln('Warning: $jniVersionWarning');
957+
}
950958
}
951959

952960
return 0;

‎packages/pigeon/lib/src/pigeon_lib_internal.dart‎

Lines changed: 164 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,16 +2668,11 @@ int calculateLineNumber(String contents, int offset) {
26682668
return result;
26692669
}
26702670

2671-
List<Error> _validateDependencies({
2671+
String? _findTargetPubspecPath({
26722672
required String? appDirectory,
26732673
required String? dartOutPath,
26742674
String? basePath,
2675-
required String apiName,
2676-
required List<String> requiredDeps,
2677-
List<String> requiredDevDeps = const <String>[],
26782675
}) {
2679-
final errors = <Error>[];
2680-
26812676
String? pubspecPath;
26822677
if (dartOutPath != null && dartOutPath.isNotEmpty) {
26832678
final String fullDartOut =
@@ -2701,6 +2696,169 @@ List<Error> _validateDependencies({
27012696
(dartOutPath == null || dartOutPath.isEmpty)) {
27022697
pubspecPath = findPubspecPath(Directory.current);
27032698
}
2699+
return pubspecPath;
2700+
}
2701+
2702+
/// Checks whether the `objective_c` dependency constraint in `pubspec.yaml`
2703+
/// matches the `ObjCVersionCheck(major, minor)` emitted by `ffigen` in the
2704+
/// generated `.ffi.dart` file.
2705+
///
2706+
/// Returns a warning message if the constraint allows incompatible versions, or
2707+
/// `null` if the constraint is compatible or cannot be checked.
2708+
String? checkObjectiveCPubspecVersion(InternalPigeonOptions options) {
2709+
return _checkGeneratedBindingsPubspecVersion(
2710+
options: options,
2711+
appDirectory: options.swiftOptions?.appDirectory ?? options.appDirectory,
2712+
generatedFileSuffix: '.ffi.dart',
2713+
versionCheckName: 'ObjCVersionCheck',
2714+
packageName: 'objective_c',
2715+
bindingTypeLabel: 'FFI',
2716+
);
2717+
}
2718+
2719+
/// Checks whether the `jni` dependency constraint in `pubspec.yaml`
2720+
/// matches the `JniVersionCheck(major, minor)` emitted by `jnigen` in the
2721+
/// generated `.jni.dart` file.
2722+
///
2723+
/// Returns a warning message if the constraint allows incompatible versions, or
2724+
/// `null` if the constraint is compatible or cannot be checked.
2725+
String? checkJniPubspecVersion(InternalPigeonOptions options) {
2726+
return _checkGeneratedBindingsPubspecVersion(
2727+
options: options,
2728+
appDirectory: options.kotlinOptions?.appDirectory ?? options.appDirectory,
2729+
generatedFileSuffix: '.jni.dart',
2730+
versionCheckName: 'JniVersionCheck',
2731+
packageName: 'jni',
2732+
bindingTypeLabel: 'JNI',
2733+
);
2734+
}
2735+
2736+
String? _checkGeneratedBindingsPubspecVersion({
2737+
required InternalPigeonOptions options,
2738+
required String? appDirectory,
2739+
required String generatedFileSuffix,
2740+
required String versionCheckName,
2741+
required String packageName,
2742+
required String bindingTypeLabel,
2743+
}) {
2744+
final String? dartOutPath = options.dartOptions?.dartOut;
2745+
if (dartOutPath == null || dartOutPath.isEmpty) {
2746+
return null;
2747+
}
2748+
final String basePath = options.basePath ?? '';
2749+
final String fullDartOut = (basePath.isNotEmpty && !dartOutPath.startsWith(basePath))
2750+
? path.join(basePath, dartOutPath)
2751+
: dartOutPath;
2752+
final bindingsDartFile = File('${path.withoutExtension(fullDartOut)}$generatedFileSuffix');
2753+
if (!bindingsDartFile.existsSync()) {
2754+
return null;
2755+
}
2756+
2757+
final String bindingsContent;
2758+
try {
2759+
bindingsContent = bindingsDartFile.readAsStringSync();
2760+
} catch (_) {
2761+
return null;
2762+
}
2763+
2764+
final RegExpMatch? match = RegExp(
2765+
'$versionCheckName\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)',
2766+
).firstMatch(bindingsContent);
2767+
if (match == null) {
2768+
return null;
2769+
}
2770+
2771+
final int major = int.parse(match.group(1)!);
2772+
final int minor = int.parse(match.group(2)!);
2773+
final requiredMinVersion = Version(major, minor, 0);
2774+
final nextMajorVersion = Version(major + 1, 0, 0);
2775+
2776+
final String? pubspecPath = _findTargetPubspecPath(
2777+
appDirectory: appDirectory,
2778+
dartOutPath: fullDartOut,
2779+
basePath: options.basePath,
2780+
);
2781+
if (pubspecPath == null) {
2782+
return null;
2783+
}
2784+
2785+
final resolvedPubspec = File(pubspecPath);
2786+
if (!resolvedPubspec.existsSync()) {
2787+
return null;
2788+
}
2789+
2790+
try {
2791+
final dynamic doc = yaml.loadYaml(resolvedPubspec.readAsStringSync());
2792+
if (doc is! yaml.YamlMap) {
2793+
return null;
2794+
}
2795+
final dependencies = doc['dependencies'] as yaml.YamlMap?;
2796+
final dependencyOverrides = doc['dependency_overrides'] as yaml.YamlMap?;
2797+
final bool hasOverride =
2798+
dependencyOverrides != null && dependencyOverrides.containsKey(packageName);
2799+
final bool hasDependency =
2800+
hasOverride || (dependencies != null && dependencies.containsKey(packageName));
2801+
if (!hasDependency) {
2802+
return null;
2803+
}
2804+
final Object? rawDependency = hasOverride
2805+
? dependencyOverrides[packageName]
2806+
: dependencies![packageName];
2807+
final Object? rawConstraint;
2808+
if (rawDependency == null) {
2809+
rawConstraint = 'any';
2810+
} else if (rawDependency is Map) {
2811+
if (!rawDependency.containsKey('version') &&
2812+
(rawDependency.containsKey('path') ||
2813+
rawDependency.containsKey('git') ||
2814+
rawDependency.containsKey('sdk'))) {
2815+
return null;
2816+
}
2817+
rawConstraint = rawDependency['version'] ?? 'any';
2818+
} else {
2819+
rawConstraint = rawDependency;
2820+
}
2821+
if (rawConstraint is! String) {
2822+
return null;
2823+
}
2824+
2825+
final constraint = VersionConstraint.parse(rawConstraint);
2826+
final bool allowsValidVersion = constraint.allowsAny(
2827+
VersionRange(min: requiredMinVersion, max: nextMajorVersion, includeMin: true),
2828+
);
2829+
final bool allowsLowerVersion = constraint.allowsAny(VersionRange(max: requiredMinVersion));
2830+
final bool allowsHigherMajorVersion = constraint.allowsAny(
2831+
VersionRange(min: nextMajorVersion.firstPreRelease, includeMin: true),
2832+
);
2833+
2834+
if (!allowsValidVersion || allowsLowerVersion || allowsHigherMajorVersion) {
2835+
return 'Generated $bindingTypeLabel bindings in "${path.basename(bindingsDartFile.path)}" require '
2836+
'package:$packageName ^$major.$minor.0 (from $versionCheckName($major, $minor)), '
2837+
'but "$pubspecPath" specifies "$packageName: $rawConstraint". '
2838+
'Consider updating pubspec.yaml to "$packageName: ^$major.$minor.0".';
2839+
}
2840+
} catch (_) {
2841+
return null;
2842+
}
2843+
2844+
return null;
2845+
}
2846+
2847+
List<Error> _validateDependencies({
2848+
required String? appDirectory,
2849+
required String? dartOutPath,
2850+
String? basePath,
2851+
required String apiName,
2852+
required List<String> requiredDeps,
2853+
List<String> requiredDevDeps = const <String>[],
2854+
}) {
2855+
final errors = <Error>[];
2856+
2857+
final String? pubspecPath = _findTargetPubspecPath(
2858+
appDirectory: appDirectory,
2859+
dartOutPath: dartOutPath,
2860+
basePath: basePath,
2861+
);
27042862

27052863
if (pubspecPath == null) {
27062864
errors.add(

‎packages/pigeon/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: pigeon
22
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
33
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
5-
version: 29.0.10 # This must match the version in lib/src/generator_tools.dart
5+
version: 29.0.11 # This must match the version in lib/src/generator_tools.dart
66

77
environment:
88
sdk: ^3.11.0

0 commit comments

Comments
 (0)