diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md index 0540a04a0480..62e48c4bc69b 100644 --- a/packages/pigeon/CHANGELOG.md +++ b/packages/pigeon/CHANGELOG.md @@ -1,3 +1,8 @@ +## 29.0.11 + +* [swift] Warns after FFI code generation if the `objective_c` version constraint in `pubspec.yaml` does not match the generated `ObjCVersionCheck`. +* [kotlin] Warns after JNI code generation if the `jni` version constraint in `pubspec.yaml` does not match the generated `JniVersionCheck`. + ## 29.0.10 * [swift] [kotlin] Allows `sealed` classes in native interop files. diff --git a/packages/pigeon/lib/src/generator_tools.dart b/packages/pigeon/lib/src/generator_tools.dart index c2292b840c77..b6434546bf41 100644 --- a/packages/pigeon/lib/src/generator_tools.dart +++ b/packages/pigeon/lib/src/generator_tools.dart @@ -16,7 +16,7 @@ import 'generator.dart'; /// The current version of pigeon. /// /// This must match the version in pubspec.yaml. -const String pigeonVersion = '29.0.10'; +const String pigeonVersion = '29.0.11'; /// Default plugin package name. const String defaultPluginPackageName = 'dev.flutter.pigeon'; diff --git a/packages/pigeon/lib/src/pigeon_lib.dart b/packages/pigeon/lib/src/pigeon_lib.dart index eca41875c7e9..10b7ac317cc6 100644 --- a/packages/pigeon/lib/src/pigeon_lib.dart +++ b/packages/pigeon/lib/src/pigeon_lib.dart @@ -919,6 +919,10 @@ ${_argParser.usage}'''; return exitCode; } } + final String? objcVersionWarning = checkObjectiveCPubspecVersion(internalOptions); + if (objcVersionWarning != null) { + stderr.writeln('Warning: $objcVersionWarning'); + } } if (useJni) { @@ -947,6 +951,10 @@ ${_argParser.usage}'''; return 1; } } + final String? jniVersionWarning = checkJniPubspecVersion(internalOptions); + if (jniVersionWarning != null) { + stderr.writeln('Warning: $jniVersionWarning'); + } } return 0; diff --git a/packages/pigeon/lib/src/pigeon_lib_internal.dart b/packages/pigeon/lib/src/pigeon_lib_internal.dart index d18b6aa4f9f3..958d5043dcec 100644 --- a/packages/pigeon/lib/src/pigeon_lib_internal.dart +++ b/packages/pigeon/lib/src/pigeon_lib_internal.dart @@ -2668,16 +2668,11 @@ int calculateLineNumber(String contents, int offset) { return result; } -List _validateDependencies({ +String? _findTargetPubspecPath({ required String? appDirectory, required String? dartOutPath, String? basePath, - required String apiName, - required List requiredDeps, - List requiredDevDeps = const [], }) { - final errors = []; - String? pubspecPath; if (dartOutPath != null && dartOutPath.isNotEmpty) { final String fullDartOut = @@ -2701,6 +2696,169 @@ List _validateDependencies({ (dartOutPath == null || dartOutPath.isEmpty)) { pubspecPath = findPubspecPath(Directory.current); } + return pubspecPath; +} + +/// Checks whether the `objective_c` dependency constraint in `pubspec.yaml` +/// matches the `ObjCVersionCheck(major, minor)` emitted by `ffigen` in the +/// generated `.ffi.dart` file. +/// +/// Returns a warning message if the constraint allows incompatible versions, or +/// `null` if the constraint is compatible or cannot be checked. +String? checkObjectiveCPubspecVersion(InternalPigeonOptions options) { + return _checkGeneratedBindingsPubspecVersion( + options: options, + appDirectory: options.swiftOptions?.appDirectory ?? options.appDirectory, + generatedFileSuffix: '.ffi.dart', + versionCheckName: 'ObjCVersionCheck', + packageName: 'objective_c', + bindingTypeLabel: 'FFI', + ); +} + +/// Checks whether the `jni` dependency constraint in `pubspec.yaml` +/// matches the `JniVersionCheck(major, minor)` emitted by `jnigen` in the +/// generated `.jni.dart` file. +/// +/// Returns a warning message if the constraint allows incompatible versions, or +/// `null` if the constraint is compatible or cannot be checked. +String? checkJniPubspecVersion(InternalPigeonOptions options) { + return _checkGeneratedBindingsPubspecVersion( + options: options, + appDirectory: options.kotlinOptions?.appDirectory ?? options.appDirectory, + generatedFileSuffix: '.jni.dart', + versionCheckName: 'JniVersionCheck', + packageName: 'jni', + bindingTypeLabel: 'JNI', + ); +} + +String? _checkGeneratedBindingsPubspecVersion({ + required InternalPigeonOptions options, + required String? appDirectory, + required String generatedFileSuffix, + required String versionCheckName, + required String packageName, + required String bindingTypeLabel, +}) { + final String? dartOutPath = options.dartOptions?.dartOut; + if (dartOutPath == null || dartOutPath.isEmpty) { + return null; + } + final String basePath = options.basePath ?? ''; + final String fullDartOut = (basePath.isNotEmpty && !dartOutPath.startsWith(basePath)) + ? path.join(basePath, dartOutPath) + : dartOutPath; + final bindingsDartFile = File('${path.withoutExtension(fullDartOut)}$generatedFileSuffix'); + if (!bindingsDartFile.existsSync()) { + return null; + } + + final String bindingsContent; + try { + bindingsContent = bindingsDartFile.readAsStringSync(); + } catch (_) { + return null; + } + + final RegExpMatch? match = RegExp( + '$versionCheckName\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\)', + ).firstMatch(bindingsContent); + if (match == null) { + return null; + } + + final int major = int.parse(match.group(1)!); + final int minor = int.parse(match.group(2)!); + final requiredMinVersion = Version(major, minor, 0); + final nextMajorVersion = Version(major + 1, 0, 0); + + final String? pubspecPath = _findTargetPubspecPath( + appDirectory: appDirectory, + dartOutPath: fullDartOut, + basePath: options.basePath, + ); + if (pubspecPath == null) { + return null; + } + + final resolvedPubspec = File(pubspecPath); + if (!resolvedPubspec.existsSync()) { + return null; + } + + try { + final dynamic doc = yaml.loadYaml(resolvedPubspec.readAsStringSync()); + if (doc is! yaml.YamlMap) { + return null; + } + final dependencies = doc['dependencies'] as yaml.YamlMap?; + final dependencyOverrides = doc['dependency_overrides'] as yaml.YamlMap?; + final bool hasOverride = + dependencyOverrides != null && dependencyOverrides.containsKey(packageName); + final bool hasDependency = + hasOverride || (dependencies != null && dependencies.containsKey(packageName)); + if (!hasDependency) { + return null; + } + final Object? rawDependency = hasOverride + ? dependencyOverrides[packageName] + : dependencies![packageName]; + final Object? rawConstraint; + if (rawDependency == null) { + rawConstraint = 'any'; + } else if (rawDependency is Map) { + if (!rawDependency.containsKey('version') && + (rawDependency.containsKey('path') || + rawDependency.containsKey('git') || + rawDependency.containsKey('sdk'))) { + return null; + } + rawConstraint = rawDependency['version'] ?? 'any'; + } else { + rawConstraint = rawDependency; + } + if (rawConstraint is! String) { + return null; + } + + final constraint = VersionConstraint.parse(rawConstraint); + final bool allowsValidVersion = constraint.allowsAny( + VersionRange(min: requiredMinVersion, max: nextMajorVersion, includeMin: true), + ); + final bool allowsLowerVersion = constraint.allowsAny(VersionRange(max: requiredMinVersion)); + final bool allowsHigherMajorVersion = constraint.allowsAny( + VersionRange(min: nextMajorVersion.firstPreRelease, includeMin: true), + ); + + if (!allowsValidVersion || allowsLowerVersion || allowsHigherMajorVersion) { + return 'Generated $bindingTypeLabel bindings in "${path.basename(bindingsDartFile.path)}" require ' + 'package:$packageName ^$major.$minor.0 (from $versionCheckName($major, $minor)), ' + 'but "$pubspecPath" specifies "$packageName: $rawConstraint". ' + 'Consider updating pubspec.yaml to "$packageName: ^$major.$minor.0".'; + } + } catch (_) { + return null; + } + + return null; +} + +List _validateDependencies({ + required String? appDirectory, + required String? dartOutPath, + String? basePath, + required String apiName, + required List requiredDeps, + List requiredDevDeps = const [], +}) { + final errors = []; + + final String? pubspecPath = _findTargetPubspecPath( + appDirectory: appDirectory, + dartOutPath: dartOutPath, + basePath: basePath, + ); if (pubspecPath == null) { errors.add( diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml index b6d3e4861cf8..19ac44dce68f 100644 --- a/packages/pigeon/pubspec.yaml +++ b/packages/pigeon/pubspec.yaml @@ -2,7 +2,7 @@ name: pigeon description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. repository: https://github.com/flutter/packages/tree/main/packages/pigeon issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22 -version: 29.0.10 # This must match the version in lib/src/generator_tools.dart +version: 29.0.11 # This must match the version in lib/src/generator_tools.dart environment: sdk: ^3.11.0 diff --git a/packages/pigeon/test/pigeon_lib_test.dart b/packages/pigeon/test/pigeon_lib_test.dart index bb2a5092f2ae..b92d12e06a1e 100644 --- a/packages/pigeon/test/pigeon_lib_test.dart +++ b/packages/pigeon/test/pigeon_lib_test.dart @@ -2364,6 +2364,252 @@ dev_dependencies: } }); + test('checkObjectiveCPubspecVersion returns null when constraint matches ObjCVersionCheck', () { + final Directory tempDir = Directory.systemTemp.createTempSync('pigeon_objc_version_test_'); + try { + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + ffi: ^2.0.0 + objective_c: ^9.6.2 +'''); + final libDir = Directory('${tempDir.path}/lib')..createSync(recursive: true); + File('${libDir.path}/messages.g.ffi.dart').writeAsStringSync(r''' +const _$objcVersionCheck = objc.ObjCVersionCheck(9, 6); +'''); + final InternalPigeonOptions options = InternalPigeonOptions.fromPigeonOptions( + PigeonOptions( + input: 'foo.dart', + dartOut: '${libDir.path}/messages.g.dart', + swiftOut: '${libDir.path}/messages.g.swift', + swiftOptions: const SwiftOptions(useFfi: true), + dartPackageName: 'my_package', + ), + ); + expect(checkObjectiveCPubspecVersion(options), isNull); + } finally { + tempDir.deleteSync(recursive: true); + } + }); + + test( + 'checkObjectiveCPubspecVersion warns when constraint allows lower minor version than ObjCVersionCheck', + () { + final Directory tempDir = Directory.systemTemp.createTempSync('pigeon_objc_version_test_'); + try { + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + ffi: ^2.0.0 + objective_c: ^9.5.0 +'''); + final libDir = Directory('${tempDir.path}/lib')..createSync(recursive: true); + File('${libDir.path}/messages.g.ffi.dart').writeAsStringSync(r''' +const _$objcVersionCheck = objc.ObjCVersionCheck(9, 6); +'''); + final InternalPigeonOptions options = InternalPigeonOptions.fromPigeonOptions( + PigeonOptions( + input: 'foo.dart', + dartOut: '${libDir.path}/messages.g.dart', + swiftOut: '${libDir.path}/messages.g.swift', + swiftOptions: const SwiftOptions(useFfi: true), + dartPackageName: 'my_package', + ), + ); + final String? warning = checkObjectiveCPubspecVersion(options); + expect(warning, isNotNull); + expect(warning, contains('require package:objective_c ^9.6.0')); + expect(warning, contains('ObjCVersionCheck(9, 6)')); + expect(warning, contains('objective_c: ^9.5.0')); + } finally { + tempDir.deleteSync(recursive: true); + } + }, + ); + + test('checkJniPubspecVersion returns null when constraint matches JniVersionCheck', () { + final Directory tempDir = Directory.systemTemp.createTempSync('pigeon_jni_version_test_'); + try { + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + jni: ^1.1.0 +'''); + final libDir = Directory('${tempDir.path}/lib')..createSync(recursive: true); + File('${libDir.path}/messages.g.jni.dart').writeAsStringSync(r''' +const _$jniVersionCheck = jni$_.JniVersionCheck(1, 1); +'''); + final InternalPigeonOptions options = InternalPigeonOptions.fromPigeonOptions( + PigeonOptions( + input: 'foo.dart', + dartOut: '${libDir.path}/messages.g.dart', + kotlinOut: '${libDir.path}/Messages.g.kt', + kotlinOptions: const KotlinOptions(useJni: true), + dartPackageName: 'my_package', + ), + ); + expect(checkJniPubspecVersion(options), isNull); + } finally { + tempDir.deleteSync(recursive: true); + } + }); + + test( + 'checkJniPubspecVersion warns when constraint allows lower minor version than JniVersionCheck', + () { + final Directory tempDir = Directory.systemTemp.createTempSync('pigeon_jni_version_test_'); + try { + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + jni: ^1.0.0 +'''); + final libDir = Directory('${tempDir.path}/lib')..createSync(recursive: true); + File('${libDir.path}/messages.g.jni.dart').writeAsStringSync(r''' +const _$jniVersionCheck = jni$_.JniVersionCheck(1, 1); +'''); + final InternalPigeonOptions options = InternalPigeonOptions.fromPigeonOptions( + PigeonOptions( + input: 'foo.dart', + dartOut: '${libDir.path}/messages.g.dart', + kotlinOut: '${libDir.path}/Messages.g.kt', + kotlinOptions: const KotlinOptions(useJni: true), + dartPackageName: 'my_package', + ), + ); + final String? warning = checkJniPubspecVersion(options); + expect(warning, isNotNull); + expect(warning, contains('require package:jni ^1.1.0')); + expect(warning, contains('JniVersionCheck(1, 1)')); + expect(warning, contains('jni: ^1.0.0')); + } finally { + tempDir.deleteSync(recursive: true); + } + }, + ); + + test('checkObjectiveCPubspecVersion checks version key when dependency is a map', () { + final Directory tempDir = Directory.systemTemp.createTempSync('pigeon_objc_version_test_'); + try { + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + ffi: ^2.0.0 + objective_c: + hosted: https://some-package-server.com + version: ^9.5.0 +'''); + final libDir = Directory('${tempDir.path}/lib')..createSync(recursive: true); + File('${libDir.path}/messages.g.ffi.dart').writeAsStringSync(r''' +const _$objcVersionCheck = objc.ObjCVersionCheck(9, 6); +'''); + final InternalPigeonOptions options = InternalPigeonOptions.fromPigeonOptions( + PigeonOptions( + input: 'foo.dart', + dartOut: '${libDir.path}/messages.g.dart', + swiftOut: '${libDir.path}/messages.g.swift', + swiftOptions: const SwiftOptions(useFfi: true), + dartPackageName: 'my_package', + ), + ); + final String? warning = checkObjectiveCPubspecVersion(options); + expect(warning, isNotNull); + expect(warning, contains('require package:objective_c ^9.6.0')); + expect(warning, contains('objective_c: ^9.5.0')); + } finally { + tempDir.deleteSync(recursive: true); + } + }); + + test( + 'checkObjectiveCPubspecVersion warns on disjoint major, higher major range, bare, and versionless hosted dependencies, and skips path dependencies', + () { + final Directory tempDir = Directory.systemTemp.createTempSync('pigeon_objc_version_test_'); + try { + final libDir = Directory('${tempDir.path}/lib')..createSync(recursive: true); + File('${libDir.path}/messages.g.ffi.dart').writeAsStringSync(r''' +const _$objcVersionCheck = objc.ObjCVersionCheck(9, 6); +'''); + final InternalPigeonOptions options = InternalPigeonOptions.fromPigeonOptions( + PigeonOptions( + input: 'foo.dart', + dartOut: 'lib/messages.g.dart', + swiftOut: 'lib/messages.g.swift', + swiftOptions: const SwiftOptions(useFfi: true), + dartPackageName: 'my_package', + basePath: tempDir.path, + ), + ); + + // Constraint allowing no valid version (^10.0.0) + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + objective_c: ^10.0.0 +'''); + expect( + checkObjectiveCPubspecVersion(options), + allOf(isNotNull, contains('objective_c: ^10.0.0')), + ); + + // Constraint allowing a higher major (>=9.6.0 <11.0.0) + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + objective_c: ">=9.6.0 <11.0.0" +'''); + expect( + checkObjectiveCPubspecVersion(options), + allOf(isNotNull, contains('objective_c: >=9.6.0 <11.0.0')), + ); + + // Bare dependency (parsed as null -> treated as 'any') + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + objective_c: +'''); + expect( + checkObjectiveCPubspecVersion(options), + allOf(isNotNull, contains('objective_c: any')), + ); + + // Versionless hosted dependency (treated as 'any') + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + objective_c: + hosted: https://some-package-server.com +'''); + expect( + checkObjectiveCPubspecVersion(options), + allOf(isNotNull, contains('objective_c: any')), + ); + + // Path dependency without version is skipped + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + objective_c: + path: ../objective_c +'''); + expect(checkObjectiveCPubspecVersion(options), isNull); + + // dependency_overrides takes precedence over dependencies + File('${tempDir.path}/pubspec.yaml').writeAsStringSync(''' +name: my_package +dependencies: + objective_c: ^9.5.0 +dependency_overrides: + objective_c: ^9.6.0 +'''); + expect(checkObjectiveCPubspecVersion(options), isNull); + } finally { + tempDir.deleteSync(recursive: true); + } + }, + ); + test('KotlinOptions serialization/deserialization with jniClassPaths', () { const options = KotlinOptions(jniClassPaths: ['foo/bar', 'baz.jar']); final Map map = options.toMap();