Skip to content

Commit 83da6fb

Browse files
authored
[tool] Use flutter pub for packages that depend on jni (flutter#13087)
Updates `pub_utils.dart` to use `flutter pub` instead of `dart pub` when a non-Flutter package (or its example) depends on `jni` in `dependencies` or `dev_dependencies`. `package:jni` declares a `flutter` constraint in its `pubspec.yaml` `environment` section, which causes `dart pub` (including `dart pub publish`) to fail version solving when `FLUTTER_ROOT` is not set in the environment—even for non-Flutter Dart packages like `pigeon` that only depend on `jni` in `dev_dependencies` and run unit tests with `dart test`. Scoping this check to `pub_utils.dart` ensures `pub` commands use `flutter pub` while `dart-test` continues to use `dart run test`. If more packages run across this problem, they will need to be added to the list to force the `flutter pub` command. Fixes flutter#192709 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent bd7b7b5 commit 83da6fb

5 files changed

Lines changed: 85 additions & 3 deletions

File tree

‎script/tool/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 0.14.4+4
2+
3+
* Uses `flutter pub` instead of `dart pub` for non-Flutter packages that depend
4+
on `jni`.
5+
16
## 0.14.4+3
27

38
* Adds validation that the root README package table is sorted alphabetically.

‎script/tool/lib/src/common/pub_utils.dart‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ String _pubCommand(
9393
}) {
9494
// Running `dart pub get` on a Flutter package can fail if a non-Flutter Dart
9595
// is first in the path, so use `flutter pub get` for any Flutter package.
96-
bool useFlutter = package.requiresFlutter();
96+
bool useFlutter = _requiresFlutterForPub(package);
9797
if (!useFlutter && recursiveFlutterCheck) {
9898
for (final RepositoryPackage example in package.getExamples()) {
99-
if (example.requiresFlutter()) {
99+
if (_requiresFlutterForPub(example)) {
100100
useFlutter = true;
101101
break;
102102
}
@@ -106,3 +106,16 @@ String _pubCommand(
106106
? (platform.isWindows ? 'flutter.bat' : 'flutter')
107107
: (dartSdkPathOverride ?? 'dart');
108108
}
109+
110+
bool _requiresFlutterForPub(RepositoryPackage package) {
111+
if (package.requiresFlutter()) {
112+
return true;
113+
}
114+
// Some packages declare a `flutter` constraint in their `pubspec.yaml`
115+
// environment section, which causes `dart pub` to fail version solving unless
116+
// run via `flutter pub`, even when depended on by a non-Flutter package.
117+
const flutterRequiringPackages = <String>{'jni'};
118+
final Pubspec pubspec = package.parsePubspec();
119+
return flutterRequiringPackages.any(pubspec.dependencies.containsKey) ||
120+
flutterRequiringPackages.any(pubspec.devDependencies.containsKey);
121+
}

‎script/tool/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: flutter_plugin_tools
22
description: Productivity and CI utils for Flutter team package repositories.
33
repository: https://github.com/flutter/packages/tree/main/script/tool
4-
version: 0.14.4+2
4+
version: 0.14.4+3
55

66
dependencies:
77
args: ^2.1.0

‎script/tool/test/common/pub_utils_test.dart‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,54 @@ void main() {
6161
);
6262
});
6363

64+
test('runs with Flutter for a non-Flutter package that depends on jni', () async {
65+
final RepositoryPackage package = createFakePackage('a_package', packagesDir);
66+
package.pubspecFile.writeAsStringSync('''
67+
name: a_package
68+
version: 0.0.1
69+
70+
environment:
71+
sdk: ^3.0.0
72+
73+
dependencies:
74+
jni: ^1.0.0
75+
''');
76+
final NativePlatform platform = createMockPlatform();
77+
78+
await runPubGet(package, processRunner, platform);
79+
80+
expect(
81+
processRunner.recordedCalls,
82+
orderedEquals(<ProcessCall>[
83+
ProcessCall('flutter', const <String>['pub', 'get'], package.path),
84+
]),
85+
);
86+
});
87+
88+
test('runs with Flutter for a non-Flutter package with a jni dev_dependency', () async {
89+
final RepositoryPackage package = createFakePackage('a_package', packagesDir);
90+
package.pubspecFile.writeAsStringSync('''
91+
name: a_package
92+
version: 0.0.1
93+
94+
environment:
95+
sdk: ^3.0.0
96+
97+
dev_dependencies:
98+
jni: ^1.0.0
99+
''');
100+
final NativePlatform platform = createMockPlatform();
101+
102+
await runPubGet(package, processRunner, platform);
103+
104+
expect(
105+
processRunner.recordedCalls,
106+
orderedEquals(<ProcessCall>[
107+
ProcessCall('flutter', const <String>['pub', 'get'], package.path),
108+
]),
109+
);
110+
});
111+
64112
test('uses the correct Flutter command on Windows', () async {
65113
final RepositoryPackage package = createFakePackage('a_package', packagesDir, isFlutter: true);
66114
final NativePlatform platform = createMockPlatform(isWindows: true);

‎script/tool/test/publish_command_test.dart‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,22 @@ void main() {
256256
);
257257
});
258258

259+
test('uses flutter for non-Flutter packages with a jni dev_dependency', () async {
260+
final RepositoryPackage package = createFakePackage('foo', packagesDir, examples: <String>[]);
261+
package.pubspecFile.writeAsStringSync('''
262+
${package.pubspecFile.readAsStringSync()}
263+
dev_dependencies:
264+
jni: ^1.0.0
265+
''');
266+
267+
await runCapturingPrint(commandRunner, <String>['publish', '--packages=foo']);
268+
269+
expect(
270+
processRunner.recordedCalls,
271+
contains(ProcessCall('flutter', const <String>['pub', 'publish'], package.directory.path)),
272+
);
273+
});
274+
259275
test('forwards --pub-publish-flags to pub publish', () async {
260276
final RepositoryPackage plugin = createFakePlugin('foo', packagesDir, examples: <String>[]);
261277

0 commit comments

Comments
 (0)