Skip to content

Commit df0f423

Browse files
[tools] Run pub get before format (#8052)
The new Dart formatter needs to know the Dart language version of the code it is formatting, and it reads that from a file in `.dart_tool`, not `pubspec.yaml` directly. To avoid it failing to determine the version and assuming the latest (which will almost always be wrong in this repo): - Adds a step to the `format` repo command to ensure that `pub get` appears to have been run, and runs it if not, and - To avoid `pub get` running in `format` in CI, adds a deps-fetching step to the `repo_checks` task, as we have in other tasks that need to `pub get`. This should unblock the roll.
1 parent d681e4e commit df0f423

File tree

5 files changed

+90
-18
lines changed

5 files changed

+90
-18
lines changed

.ci/targets/repo_checks.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ tasks:
22
- name: prepare tool
33
script: .ci/scripts/prepare_tool.sh
44
infra_step: true # Note infra steps failing prevents "always" from running.
5+
# format requires that 'pub get' has been run to determine the language
6+
# version, so the tool will auto-run it if necessary. Run it manually first
7+
# so that the network requests are in a separate infra step.
8+
- name: download Dart deps
9+
script: .ci/scripts/tool_runner.sh
10+
args: ["fetch-deps"]
11+
infra_step: true
512
- name: tool unit tests
613
script: .ci/scripts/plugin_tools_tests.sh
714
- name: tool format

script/tool/lib/src/format_command.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:meta/meta.dart';
1212
import 'common/core.dart';
1313
import 'common/output_utils.dart';
1414
import 'common/package_looping_command.dart';
15+
import 'common/pub_utils.dart';
1516
import 'common/repository_package.dart';
1617

1718
/// In theory this should be 8191, but in practice that was still resulting in
@@ -120,6 +121,15 @@ class FormatCommand extends PackageLoopingCommand {
120121
relativeTo: package.directory,
121122
);
122123
if (getBoolArg(_dartArg)) {
124+
// Ensure that .dart_tool exists, since without it `dart` doesn't know
125+
// the lanugage version, so the formatter may give different output.
126+
if (!package.directory.childDirectory('.dart_tool').existsSync()) {
127+
if (!await runPubGet(package, processRunner, super.platform)) {
128+
printError('Unable to fetch dependencies.');
129+
return PackageResult.fail(<String>['unable to fetch dependencies']);
130+
}
131+
}
132+
123133
await _formatDart(files, workingDir: package.directory);
124134
}
125135
// Success or failure is determined overall in completeRun, since most code

script/tool/lib/src/license_check_command.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const Set<String> _ignoreBasenameList = <String>{
2929
'flutter_export_environment',
3030
'GeneratedPluginRegistrant',
3131
'generated_plugin_registrant',
32+
'web_plugin_registrant',
3233
};
3334

3435
// File suffixes that otherwise match _codeFileExtensions to ignore.

script/tool/test/format_command_test.dart

Lines changed: 71 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ void main() {
5050
runner.addCommand(analyzeCommand);
5151
});
5252

53+
/// Creates the .dart_tool directory for [package] to simulate (as much as
54+
/// this command requires) `pub get` having been run.
55+
void fakePubGet(RepositoryPackage package) {
56+
package.directory.childDirectory('.dart_tool').createSync();
57+
}
58+
5359
/// Returns a modified version of a list of [relativePaths] that are relative
5460
/// to [package] to instead be relative to [packagesDir].
5561
List<String> getPackagesDirRelativePaths(
@@ -92,6 +98,7 @@ void main() {
9298
packagesDir,
9399
extraFiles: files,
94100
);
101+
fakePubGet(plugin);
95102

96103
await runCapturingPrint(runner, <String>['format']);
97104

@@ -117,6 +124,7 @@ void main() {
117124
unformattedFile,
118125
],
119126
);
127+
fakePubGet(plugin);
120128

121129
final p.Context posixContext = p.posix;
122130
childFileWithSubcomponents(
@@ -140,7 +148,9 @@ void main() {
140148
'lib/src/b.dart',
141149
'lib/src/c.dart',
142150
];
143-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
151+
final RepositoryPackage plugin =
152+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
153+
fakePubGet(plugin);
144154

145155
processRunner.mockProcessesForExecutable['dart'] = <FakeProcessInfo>[
146156
FakeProcessInfo(MockProcess(exitCode: 1), <String>['format'])
@@ -163,7 +173,9 @@ void main() {
163173
const List<String> files = <String>[
164174
'lib/a.dart',
165175
];
166-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
176+
final RepositoryPackage plugin =
177+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
178+
fakePubGet(plugin);
167179

168180
await runCapturingPrint(runner, <String>['format', '--no-dart']);
169181
expect(processRunner.recordedCalls, orderedEquals(<ProcessCall>[]));
@@ -179,6 +191,7 @@ void main() {
179191
packagesDir,
180192
extraFiles: files,
181193
);
194+
fakePubGet(plugin);
182195

183196
await runCapturingPrint(runner, <String>['format']);
184197

@@ -203,7 +216,9 @@ void main() {
203216
'android/src/main/java/io/flutter/plugins/a_plugin/a.java',
204217
'android/src/main/java/io/flutter/plugins/a_plugin/b.java',
205218
];
206-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
219+
final RepositoryPackage plugin =
220+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
221+
fakePubGet(plugin);
207222

208223
processRunner.mockProcessesForExecutable['java'] = <FakeProcessInfo>[
209224
FakeProcessInfo(MockProcess(exitCode: 1), <String>['-version'])
@@ -229,7 +244,9 @@ void main() {
229244
'android/src/main/java/io/flutter/plugins/a_plugin/a.java',
230245
'android/src/main/java/io/flutter/plugins/a_plugin/b.java',
231246
];
232-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
247+
final RepositoryPackage plugin =
248+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
249+
fakePubGet(plugin);
233250

234251
processRunner.mockProcessesForExecutable['java'] = <FakeProcessInfo>[
235252
FakeProcessInfo(
@@ -260,6 +277,7 @@ void main() {
260277
packagesDir,
261278
extraFiles: files,
262279
);
280+
fakePubGet(plugin);
263281

264282
await runCapturingPrint(
265283
runner, <String>['format', '--java-path=/path/to/java']);
@@ -284,7 +302,9 @@ void main() {
284302
const List<String> files = <String>[
285303
'android/src/main/java/io/flutter/plugins/a_plugin/a.java',
286304
];
287-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
305+
final RepositoryPackage plugin =
306+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
307+
fakePubGet(plugin);
288308

289309
await runCapturingPrint(runner, <String>['format', '--no-java']);
290310
expect(processRunner.recordedCalls, orderedEquals(<ProcessCall>[]));
@@ -304,6 +324,7 @@ void main() {
304324
packagesDir,
305325
extraFiles: files,
306326
);
327+
fakePubGet(plugin);
307328

308329
await runCapturingPrint(runner, <String>['format']);
309330

@@ -328,7 +349,9 @@ void main() {
328349
'linux/foo_plugin.cc',
329350
'macos/Classes/Foo.h',
330351
];
331-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
352+
final RepositoryPackage plugin =
353+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
354+
fakePubGet(plugin);
332355

333356
processRunner.mockProcessesForExecutable['clang-format'] =
334357
<FakeProcessInfo>[FakeProcessInfo(MockProcess(exitCode: 1))];
@@ -357,6 +380,7 @@ void main() {
357380
packagesDir,
358381
extraFiles: files,
359382
);
383+
fakePubGet(plugin);
360384

361385
processRunner.mockProcessesForExecutable['clang-format'] =
362386
<FakeProcessInfo>[FakeProcessInfo(MockProcess(exitCode: 1))];
@@ -396,6 +420,7 @@ void main() {
396420
packagesDir,
397421
extraFiles: files,
398422
);
423+
fakePubGet(plugin);
399424

400425
await runCapturingPrint(runner,
401426
<String>['format', '--clang-format-path=/path/to/clang-format']);
@@ -421,7 +446,9 @@ void main() {
421446
'linux/foo_plugin.cc',
422447
'macos/Classes/Foo.h',
423448
];
424-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
449+
final RepositoryPackage plugin =
450+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
451+
fakePubGet(plugin);
425452

426453
processRunner.mockProcessesForExecutable['clang-format'] =
427454
<FakeProcessInfo>[
@@ -448,7 +475,9 @@ void main() {
448475
const List<String> files = <String>[
449476
'linux/foo_plugin.cc',
450477
];
451-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
478+
final RepositoryPackage plugin =
479+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
480+
fakePubGet(plugin);
452481

453482
await runCapturingPrint(runner, <String>['format', '--no-clang-format']);
454483
expect(processRunner.recordedCalls, orderedEquals(<ProcessCall>[]));
@@ -465,6 +494,7 @@ void main() {
465494
packagesDir,
466495
extraFiles: files,
467496
);
497+
fakePubGet(plugin);
468498

469499
await runCapturingPrint(runner, <String>['format']);
470500

@@ -488,7 +518,9 @@ void main() {
488518
'android/src/main/kotlin/io/flutter/plugins/a_plugin/a.kt',
489519
'android/src/main/kotlin/io/flutter/plugins/a_plugin/b.kt',
490520
];
491-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
521+
final RepositoryPackage plugin =
522+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
523+
fakePubGet(plugin);
492524

493525
processRunner.mockProcessesForExecutable['java'] = <FakeProcessInfo>[
494526
FakeProcessInfo(
@@ -513,7 +545,9 @@ void main() {
513545
const List<String> files = <String>[
514546
'android/src/main/kotlin/io/flutter/plugins/a_plugin/a.kt',
515547
];
516-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
548+
final RepositoryPackage plugin =
549+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
550+
fakePubGet(plugin);
517551

518552
await runCapturingPrint(runner, <String>['format', '--no-kotlin']);
519553
expect(processRunner.recordedCalls, orderedEquals(<ProcessCall>[]));
@@ -530,6 +564,7 @@ void main() {
530564
packagesDir,
531565
extraFiles: files,
532566
);
567+
fakePubGet(plugin);
533568

534569
await runCapturingPrint(runner, <String>[
535570
'format',
@@ -567,11 +602,12 @@ void main() {
567602
const List<String> files = <String>[
568603
'macos/foo.swift',
569604
];
570-
createFakePlugin(
605+
final RepositoryPackage plugin = createFakePlugin(
571606
'a_plugin',
572607
packagesDir,
573608
extraFiles: files,
574609
);
610+
fakePubGet(plugin);
575611

576612
await runCapturingPrint(runner, <String>['format', '--no-swift']);
577613

@@ -583,7 +619,9 @@ void main() {
583619
const List<String> files = <String>[
584620
'macos/foo.swift',
585621
];
586-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
622+
final RepositoryPackage plugin =
623+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
624+
fakePubGet(plugin);
587625

588626
processRunner.mockProcessesForExecutable['swift-format'] =
589627
<FakeProcessInfo>[
@@ -609,7 +647,9 @@ void main() {
609647
const List<String> files = <String>[
610648
'macos/foo.swift',
611649
];
612-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
650+
final RepositoryPackage plugin =
651+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
652+
fakePubGet(plugin);
613653

614654
processRunner.mockProcessesForExecutable['swift-format'] =
615655
<FakeProcessInfo>[
@@ -643,7 +683,9 @@ void main() {
643683
const List<String> files = <String>[
644684
'macos/foo.swift',
645685
];
646-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
686+
final RepositoryPackage plugin =
687+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
688+
fakePubGet(plugin);
647689

648690
processRunner.mockProcessesForExecutable['swift-format'] =
649691
<FakeProcessInfo>[
@@ -694,6 +736,7 @@ void main() {
694736
...javaFiles,
695737
],
696738
);
739+
fakePubGet(plugin);
697740

698741
await runCapturingPrint(runner, <String>['format']);
699742

@@ -732,6 +775,7 @@ void main() {
732775
'example/macos/Flutter/GeneratedPluginRegistrant.swift',
733776
],
734777
);
778+
fakePubGet(plugin);
735779

736780
await runCapturingPrint(runner, <String>[
737781
'format',
@@ -773,7 +817,9 @@ void main() {
773817
'linux/foo_plugin.cc',
774818
'macos/Classes/Foo.h',
775819
];
776-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
820+
final RepositoryPackage plugin =
821+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
822+
fakePubGet(plugin);
777823

778824
const String changedFilePath = 'packages/a_plugin/linux/foo_plugin.cc';
779825
processRunner.mockProcessesForExecutable['git'] = <FakeProcessInfo>[
@@ -825,7 +871,9 @@ void main() {
825871
'linux/foo_plugin.cc',
826872
'macos/Classes/Foo.h',
827873
];
828-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
874+
final RepositoryPackage plugin =
875+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
876+
fakePubGet(plugin);
829877

830878
processRunner.mockProcessesForExecutable['git'] = <FakeProcessInfo>[
831879
FakeProcessInfo(MockProcess(exitCode: 1), <String>['ls-files'])
@@ -850,7 +898,9 @@ void main() {
850898
'linux/foo_plugin.cc',
851899
'macos/Classes/Foo.h',
852900
];
853-
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
901+
final RepositoryPackage plugin =
902+
createFakePlugin('a_plugin', packagesDir, extraFiles: files);
903+
fakePubGet(plugin);
854904

855905
const String changedFilePath = 'packages/a_plugin/linux/foo_plugin.cc';
856906
processRunner.mockProcessesForExecutable['git'] = <FakeProcessInfo>[
@@ -892,6 +942,7 @@ void main() {
892942
packagesDir,
893943
extraFiles: <String>[...batch1, extraFile],
894944
);
945+
fakePubGet(package);
895946

896947
await runCapturingPrint(runner, <String>['format']);
897948

@@ -922,11 +973,12 @@ void main() {
922973
// Make the file list one file longer than would fit in a Windows batch.
923974
final List<String> batch = get99CharacterPathExtraFiles(batchSize + 1);
924975

925-
createFakePlugin(
976+
final RepositoryPackage plugin = createFakePlugin(
926977
pluginName,
927978
packagesDir,
928979
extraFiles: batch,
929980
);
981+
fakePubGet(plugin);
930982

931983
await runCapturingPrint(runner, <String>['format']);
932984

@@ -947,6 +999,7 @@ void main() {
947999
packagesDir,
9481000
extraFiles: <String>[...batch1, extraFile],
9491001
);
1002+
fakePubGet(package);
9501003

9511004
await runCapturingPrint(runner, <String>['format']);
9521005

script/tool/test/license_check_command_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ void main() {
115115
'GeneratedPluginRegistrant.m',
116116
'generated_plugin_registrant.cc',
117117
'generated_plugin_registrant.cpp',
118+
'web_plugin_registrant.dart',
118119
// Ignored path suffixes.
119120
'foo.g.dart',
120121
'foo.mocks.dart',

0 commit comments

Comments
 (0)