Skip to content

Conversation

@danrubel
Copy link
Contributor

Now that dartanalyzer uses the default Flutter analysis options (package:flutter/analysis_options_user.yaml) to analyze a flutter project,
and a Dart SDK containing this functionality has rolled into Flutter...

Update flutter analyze to use dartanalyzer for everything except analyzing the flutter repository.

@Hixie

// Use dartanalyzer directly when possible
if (!flutterRepo) {
final String dartanalyzer = fs.path.join(
Cache.flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', 'dartanalyzer');
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: newline before ) since there was a newline after the (

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


if (cmd.length < 2 || currentPackage) {
final Directory projDir = await projectDirFor(fs.currentDirectory.absolute);
if (projDir != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: no braces around one-line statements

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

if (cmd.length < 2 || currentPackage) {
final Directory projDir = await projectDirFor(fs.currentDirectory.absolute);
if (projDir != null) {
cmd.add((projDir).path);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: i think the inner parentheses are redundant here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Artifact of refactoring tools. Removed.

if (!flutterRepo) {
final String dartanalyzer = fs.path.join(
Cache.flutterRoot, 'bin', 'cache', 'dart-sdk', 'bin', 'dartanalyzer');
final List<String> cmd = <String>[dartanalyzer];
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: please don't use abbreviations for variable names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed cmd to arguments. Hopefully that's more clear.

final List<String> cmd = <String>[dartanalyzer];
cmd.addAll(dartFiles.map((FileSystemEntity f) => f.path));

if (cmd.length < 2 || currentPackage) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I got a bit confused reading this, in part because I was reading "cmd" as "our command line" as opposed to "the command line we are building", and in part because it took me a while to realise that "< 2" was equivalent to "nothing got added in the previous step, and so there's one entry, the analyzer itself".

I suggest adding the path to the analyzer after all this work of adding the files to be analyzed (inserting at index 0). That would let you change this from length < 2 to isEmpty which I think would be clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I like that approach. Done.

cmd.addAll(dartFiles.map((FileSystemEntity f) => f.path));

if (cmd.length < 2 || currentPackage) {
final Directory projDir = await projectDirFor(fs.currentDirectory.absolute);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: no abbreviations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

}

final bool currentDirectory = argResults['current-directory'] && (argResults.wasParsed('current-directory') || dartFiles.isEmpty);
final bool currentPackage = argResults['current-package'] && (argResults.wasParsed('current-package') || dartFiles.isEmpty);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should remove --current-directory in this patch. This would simplify the logic here and is consistent with the long term plan in #7274.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Done.

if (projDir != null) {
cmd.add((projDir).path);
}
} else if (currentDirectory) {
Copy link
Contributor

Choose a reason for hiding this comment

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

imho we should remove this here and just let people use . if they want this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

}
} else if (currentDirectory) {
cmd.add('.');
} else if (!cmd[1].startsWith(fs.currentDirectory.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 don't understand what this is doing. Why does the path of the first command line argument matter?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also is path guaranteed to end in a separator?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. If the files being analyzed are outside of the current directory hierarchy then dartanalyzer does not yet know how to find the .packages file. I'll add a comment to that effect.

  2. Hmm... not sure. Modified the code to ensure that it does.

stopwatch.stop();
final String elapsed = (stopwatch.elapsedMilliseconds / 1000.0).toStringAsFixed(1);
if (exitCode != 0)
throwToolExit('(Ran in ${elapsed}s)');
Copy link
Contributor

Choose a reason for hiding this comment

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

propagate the exit code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. Done.

if (isDartFile(entry)) {
dartFiles.add(entry);
foundOne = true;
// Use dartanalyzer directly when possible
Copy link
Contributor

Choose a reason for hiding this comment

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

this comment should elaborate on why it's not possible if flutterRepo is true.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Done.

}
}

Future<Directory> projectDirFor(FileSystemEntity entity) async {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: no abbreviations (several cases in this method including the method name)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed to projectDirectoryContaining.

else
throwToolExit('(Ran in ${elapsed}s)');
}
if (argResults['congratulate']) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the docs to mention that congratulate will be ignored if you're not using either --watch or --flutter-repo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Help text updated.

// Use dartanalyzer directly except when analyzing the Flutter repository.
// Analyzing the repository requires a more complex report than dartanalyzer
// currently supports (e.g. missing member dartdoc summary).
// TODO(danrubel) enhance dartanalyzer to provide this type of summary
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: missing color after TODO(danrubel)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

final List<String> arguments = <String>[];
arguments.addAll(dartFiles.map((FileSystemEntity f) => f.path));

if (arguments.length < 1 || currentPackage) {
Copy link
Contributor

Choose a reason for hiding this comment

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

arguments.length < 1 is better written arguments.isEmpty
Amusingly this is a creative-enough way to write isEmpty that the lint that looks for this kind of thing didn't catch it. :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

final Directory projDir = await projectDirectoryContaining(target);
if (projDir != null) {
final String packagesPath = fs.path.join(projDir.path, '.packages');
if (packagesPath != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

how can it be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Heh. Left over from refactoring. Removed null check.

final String targetPath = arguments[0];
final FileSystemEntity target = await fs.isDirectory(targetPath)
? fs.directory(targetPath) : fs.file(targetPath);
final Directory projDir = await projectDirectoryContaining(target);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: projDir is two abbreviations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Missed that one when renaming. Fixed.

// In this situation, use the first file as a starting point
// to search for a "pubspec.yaml" and ".packages" file.
// TODO(danrubel): fix dartanalyzer to find the .packages file
if (!arguments[0].startsWith(currentDirectoryPath)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should explicitly check that all the files would find the same .packages (or all would find no .packages?), and exit with a message saying that we don't yet support checking multiple packages at once if you're not also checking the flutter repo. I've definitely run into the case where I've tried to use flutter analyze to analyze two packages at once (an app I was working on and the support library I was writing for the app), and it would be massively confusing if that did something different depending on the order in which the paths were listed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I simplified the code to always pass in the .packages file path and report an error as you suggested.

toolExit: false,
);
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nice test!

'[lint] Only throw instances of classes extending either Exception or Error',
'1 warning and 1 lint found.',
],
// TODO(danrubel) fix dartanalyzer to have non-zero exit code
Copy link
Contributor

Choose a reason for hiding this comment

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

we should make sure to fix this asap because anyone depending on flutter analyze in their build bots will be skewered by this regression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated this functionality to work around this issue so that flutter analyze will return a non-zero exit code even though dartanalyzer never does.

// Not used by analyze --watch
argParser.addFlag('congratulate', help: 'Show output even when there are no errors, warnings, hints, or lints.', defaultsTo: true);
argParser.addFlag('congratulate', help: 'When analyzing the flutter repository, show output even when there are no errors, warnings, hints, or lints.', defaultsTo: true);
argParser.addFlag('preamble', help: 'Display the number of files that will be analyzed.', defaultsTo: true);
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like this also only works with --flutter-repo now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Help text updated.

await createTestCommandRunner(command).run(arguments);
expect(toolExit, isFalse, reason: 'Expected ToolExit exception');
} on ToolExit catch (e) {
} on ToolExit catch (_) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can just remove the "catch" clause entirely (just } on ToolExit {)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

class AnalyzeCommand extends FlutterCommand {
AnalyzeCommand({bool verboseHelp: false}) {
/// The working directory for testing analysis using dartanalyzer.
final Directory workingDirectory;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: constructor first

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

/// The working directory for testing analysis using dartanalyzer
final Directory workingDirectory;

AnalyzeOnce(ArgResults argResults, this.repoPackages, { this.workingDirectory }) : super(argResults);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: constructors first please

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry. Fixed.

jakobr-google and others added 21 commits March 27, 2017 12:11
* update platform_channel package name

* lowercase name
* adds border radius to ink widgets. sets default ink border radius for material buttons with no background colors

* tidying up code

* add ink test stub

* remove unused import
…e of FragmentActivity in FlutterActivity (flutter#9036)"" (flutter#9046)

Increased the time limit slightly to allow the microbenchmark test time to finish.
…allow use of FragmentActivity in FlutterActivity (flutter#9036)"" (flutter#9046)" (flutter#9047)

Test is still failing with increased timeout.

This reverts commit 864b3c3.
We now use modern scrolling machinery and patterns. The API should also be
easier to maintain over time.

Fixes flutter#6166
Fixes flutter#2591
Fixes flutter#3123
…el/flutter into flutter-analyze-call-dartanalyzer
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@danrubel
Copy link
Contributor Author

So.... this is what happens when I rebase and push.
Note to self... don't use rebase in this situation. :-)

danrubel pushed a commit to danrubel/flutter that referenced this pull request Mar 28, 2017
@danrubel
Copy link
Contributor Author

Closing this in favor of the identical PR... #9049

@danrubel danrubel closed this Mar 28, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider upping severity of missing required hints to warnings in analysis options.