Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[script/tool] speed up the pub get portion of the analyze command #3982

Merged
merged 2 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions script/tool/lib/src/analyze_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class AnalyzeCommand extends PluginCommand {
@override
Future<void> run() async {
print('Verifying analysis settings...');

final List<FileSystemEntity> files = packagesDir.listSync(recursive: true);
for (final FileSystemEntity file in files) {
if (file.basename != 'analysis_options.yaml' &&
Expand All @@ -64,6 +65,14 @@ class AnalyzeCommand extends PluginCommand {
}

final List<Directory> packageDirectories = await getPackages().toList();
final Set<String> packagePaths =
packageDirectories.map((Directory dir) => dir.path).toSet();
packageDirectories.removeWhere((Directory directory) {
// We remove the 'example' subdirectories - 'flutter pub get' automatically
// runs 'pub get' there as part of handling the parent directory.
return directory.basename == 'example' &&
packagePaths.contains(directory.parent.path);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was hoping we could just use getPlugins instead of doing the filtering, but it looks like flutter/packages has some other nested constructions.

Copy link
Member Author

@devoncarew devoncarew May 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I would prefer something more semantic here as well. The logic for how these are computed - getPlugins() and getPackages() - seems complicated, and I wasn't comfortable modifying it.

It looks like plugins actually returns packages? And packages returns packages plus their example directories?

I would have assumed that getPlugins() would return the directory containing the n dart packages that make up a plugin - like url_launcher/, url_launcher_platform_interface/, url_launcher_macos/, ...

I'll want a call like that - the top level plugin directory - for a follow up PR, where I'm going to try and speed up the analysis portion of the analyze call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like plugins actually returns packages? And packages returns packages plus there example directories?

I suspect what happened is that this was written for flutter/plugins, then expanded for use in flutter/packages without renaming. Fixing the fact that "plugins" is used to refer to packages in general in the tool (including the --plugins argument) is on my list.

for (final Directory package in packageDirectories) {
await processRunner.runAndStream('flutter', <String>['packages', 'get'],
workingDir: package, exitOnError: true);
Expand All @@ -86,6 +95,7 @@ class AnalyzeCommand extends PluginCommand {
}

print('\n\n');

if (failingPackages.isNotEmpty) {
print('The following packages have analyzer errors (see above):');
for (final String package in failingPackages) {
Expand Down
41 changes: 41 additions & 0 deletions script/tool/test/analyze_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,47 @@ void main() {
]));
});

test('skips flutter pub get for examples', () async {
final Directory plugin1Dir = createFakePlugin('a', withSingleExample: true);

final MockProcess mockProcess = MockProcess();
mockProcess.exitCodeCompleter.complete(0);
processRunner.processToReturn = mockProcess;
await runner.run(<String>['analyze']);

expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall(
'flutter', const <String>['packages', 'get'], plugin1Dir.path),
ProcessCall('dart', const <String>['analyze', '--fatal-infos'],
plugin1Dir.path),
]));
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also add a test of a package with a sub-package that's not example, to make sure nobody makes the "simplification" that I was about to suggest before checking flutter/packages?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have one like that already - the analyzes all packages test - but I can add one where the 2nd package is named 'example' but not contained by the 1st package.

test('don\'t elide a non-contained example package', () async {
final Directory plugin1Dir = createFakePlugin('a');
final Directory plugin2Dir = createFakePlugin('example');

final MockProcess mockProcess = MockProcess();
mockProcess.exitCodeCompleter.complete(0);
processRunner.processToReturn = mockProcess;
await runner.run(<String>['analyze']);

expect(
processRunner.recordedCalls,
orderedEquals(<ProcessCall>[
ProcessCall(
'flutter', const <String>['packages', 'get'], plugin1Dir.path),
ProcessCall(
'flutter', const <String>['packages', 'get'], plugin2Dir.path),
ProcessCall('dart', const <String>['analyze', '--fatal-infos'],
plugin1Dir.path),
ProcessCall('dart', const <String>['analyze', '--fatal-infos'],
plugin2Dir.path),
]));
});

test('uses a separate analysis sdk', () async {
final Directory pluginDir = createFakePlugin('a');

Expand Down