sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Analyzer plugin only scaned and reported the opened files in IDE

Open taibaiyinxing opened this issue 3 years ago • 5 comments
trafficstars

I just learned to add a custom analyze plugin to my test project, but after I added configurations to the analysis_options.yaml, and written some code that against a rule from my custom plugin, then I switched to another file which didn't have any proplem, and finaly I restart my VS Code, and found that the warning info only show when I switch back to the one with proplem. My question is 'How to make the analyzer plugin analyze the full project when I open the IDE or project?', thanks.

taibaiyinxing avatar Jul 28 '22 14:07 taibaiyinxing

@incendial Thanks for the pointer to the extra context.

How to make the analyzer plugin analyze the full project when I open the IDE or project?

You shouldn't need to do anything, that should be the default behavior. I'm not sure why it isn't working that way, so we'll need more information. Could you please send us the instrumentation log from either VS Code or Android Studio? (There will be file paths and possibly other information that you might consider confidential, so please look at the file before sending it.)

The file isn't normally written, so you'll need to enable logging based on the IDE you're using. For VS Code, follow the instructions at https://dartcode.org/docs/logging/. For this purpose we only need the analysis server log files. For Android Studio, follow the instructions at https://intellij-support.jetbrains.com/hc/en-us/community/posts/4407710497682-Dart-analysis-server-log.

bwilkerson avatar Jul 29 '22 18:07 bwilkerson

@bwilkerson Thanks for your attention. I've tried to get some logs following the steps from the link, and found that the reason maybe was the ".flutterw" folder in my project root directory. Because our company made some custom change based on the official flutter sdk, and the ".flutterw" folder contains aliases (Mac OS) pointed to a folder contains whole flutter framework code. From the log file I found too much instrumentation logs about files from this folder. I guess maybe the reason is this folder contains too many files so that the analyzer cann't analyze them in a short time? (I don't know whether this guess is reasonable, do you have any suggestion?)Then I deleted this folder and restarted the analyze server, and found that the analyzer could analyze the files in the "lib/" directory! But in actual my project cann't work without the ".flutterw" folder, so I restored that folder and tried to add it to the [exclude] node int the "analysis_options.yaml" file as below:

analyzer:
  exclude:
    - .flutterw/**
  plugins:
     - dart_code_metrics

linter:
  rules:
    - avoid_print

dart_code_metrics:
  rules:
    - avoid-returning-widgets
    - prefer-first

But I found this not work, the files in this folder still be analyzed and the problem as described at begin still exists. So how can I ignore or exclude this ".flutterw" folder? Thank you.

taibaiyinxing avatar Aug 01 '22 03:08 taibaiyinxing

as_instrumentation_logs.txt @bwilkerson Hi, this is the log file I got after the Android Studio started in the project.This is the case that the only new opened files can show the analyzed info in the Dart Analysis tool window(After the project was opened, Even though some files seems like opened in the tab list, their analyze info can only be show after I switch to them).If the log file is not enough, please let me know, Thanks.

taibaiyinxing avatar Aug 02 '22 12:08 taibaiyinxing

The log file doesn't tell me why it isn't working as expected, but does confirm that there was no crash.

At the top of the log file there's a line containing "analysis.setAnalysisRoots". That line contains a list of excluded file paths. If you see anything in that list that shouldn't have been excluded from analysis, that might indicate the reason for the failure.

Based on the line containing "method"::"analysis.setPriorityFiles", it appears that there was only one file open. Is that the case?

If you search for lines containing "analysis.errors" you'll see which files had diagnostics generated for them (the path follows "params"::{"file"::. If you see any diagnostics reported for files and those diagnostics didn't show up in Android Studio, please let us know. (The lines containing Noti are diagnostics coming from the analysis server, the lines containing PluginNoti are diagnostics coming from the plugin.

If you see any files in the list of files for which diagnostics were reported that weren't open at the time the log was captured, that suggests that some files are being analyzed even when they aren't open, which would be useful information to have.

Can you confirm that there are files for which you would have expected to see diagnostics but for which there is no corresponding lines containing "analysis.errors"?

bwilkerson avatar Aug 02 '22 20:08 bwilkerson

@bwilkerson Thanks for your guide to how to analyze a log file. Today I repeated many times to get and analyze the log files following your guide, and founded something I think useful. First I answer the questions for you: 1.

Based on the line containing "method"::"analysis.setPriorityFiles", it appears that there was only one file open. Is that the case?

Yes, the project had only one file opened when the project was opend, the file was "analysis_options.yaml". Though I had some other files in the tab list of the editor window, I didn't switch to them, so I don't think they were opened.

If you search for lines containing "analysis.errors" you'll see which files had diagnostics generated for them (the path follows "params"::{"file"::. If you see any diagnostics reported for files and those diagnostics didn't show up in Android Studio, please let us know. (The lines containing Noti are diagnostics coming from the analysis server, the lines containing PluginNoti are diagnostics coming from the plugin.

I didn't find the situation that having diagnostics but not show up in Android Studio.

Can you confirm that there are files for which you would have expected to see diagnostics but for which there is no corresponding lines containing "analysis.errors"?

Yes, I think the most important discovery is that: When I have the .flutterw folder in the root of my project, I opened the project and displayed the logs in the terminal window, and found that there were logs one after another contains "analysis.errors" for files in ".flutterw" even after 10 minuts, at this moment I didn't find any expected diagnostics for files in the "lib" directory, maybe they would be analyzed after all the files in ".flutterw" folder being analyzed, but it has too many files and I don't know how long it will take to finish.So I think the root reason is there are too many files in the ".flutterw" folder.

My Solution

With the conclution above, I think we can find a way to avoid analyze the ".flutterw" folder, but putting it in the [exclude] node in the "analysis_options.yaml" seems not work, so I tried to exclude it in pragram code like this:

//  class AnalyzerPlugin extends ServerPlugin
 @override
  AnalysisDriverGeneric createAnalysisDriver(plugin.ContextRoot contextRoot) {
    contextRoot.exclude.add('${contextRoot.root}/.flutterw');
    ...
}

And then I restarted the analyze server, and found that it solved my problem, so do you have any suggestions for my solution? Thanks.

taibaiyinxing avatar Aug 03 '22 10:08 taibaiyinxing

Marking P2 as I don't think this is affecting many users.

srawlins avatar Aug 10 '22 20:08 srawlins

Sorry for the delay, I missed seeing your response.

... putting it in the [exclude] node in the "analysis_options.yaml" seems not work ...

I would expect that you don't need to exclude the directory because I thought we were already excluding all folders whose name started with a period, but that appears to not be the case.

Can you tell me what pattern you used in the analysis options file?

bwilkerson avatar Aug 10 '22 20:08 bwilkerson

@bwilkerson I'm sorry that in fact I don't quiet understand what

"Can you tell me what pattern you used in the analysis options file?"

means.So I try to offer some info about my project. Firstly, the structure of my project like this: | .flutterw/ | android/ | ios/ | lib/ | analysis_options.yaml | pubspec.yaml ...

Secondly, the content in "analysis_options.yaml" like this:

analyzer:
  exclude:
    - .flutterw/**
  plugins:
     - dart_code_metrics

linter:
  rules:
    - avoid_print

dart_code_metrics:
  rules:
    - avoid-returning-widgets
    - prefer-first

That's all.If you need more info, please let me know. Thanks.

taibaiyinxing avatar Aug 11 '22 12:08 taibaiyinxing

I was specifically asking about the glob pattern .flutterw/** used to exclude the directory.

I've been unsuccessful so far in reproducing the problem you're seeing.

I created a directory with an analysis options file containing

analyzer:
  exclude:
    - .flutterw/**

a directory named bin with a myApp.dart file in it, and a .flutterw directory with a file named not_analyzed.dart in it containing

class C {
  ink f; // Note the invalid type and the lack of an initializer.
}

The analyzer does not report any diagnostics, either in the IDE or from the command line.

If I remove the analysis options file I get the same result: no diagnostics. The analyzer should ignore files and directories starting with . by default, and it appears to be doing so. If I copy that file to the lib directory, then I get the expected diagnostics.

I'm at a loss to explain the behavior you're seeing.

bwilkerson avatar Aug 11 '22 18:08 bwilkerson

@bwilkerson Maybe "does not report any dianostics" is not equal to "not analyze them"? I means maybe the analyzer or plugins has tried to analyze them and then excluded them from the reported diagnostics. There are too many dart files in my ".flutterw" folder (at least the whole flutter framwork code of one version). If my guess is right, the workload was too heavy. Can you get some infos about this form the log I have uploaded( as_instrumentation_logs.txt) several days ago?

Another fact is that when I tried to get the excluded paths from my server plugin like this:

class AnalyzerPlugin extends ServerPlugin {
...

    @override
    AnalysisDriverGeneric createAnalysisDriver(ContextRoot contextRoot){
        ...
        log.info(contextRoot.exclude);  // print the excluded paths to a local file
        ...
    }

...
}

and the result didn't contain the ".flutterw" which was excluded in the analyze_options.yaml:

[/Users/bytedance/test/test_lint/.dart_tool, /Users/bytedance/test/test_lint/.idea]

taibaiyinxing avatar Aug 12 '22 03:08 taibaiyinxing

Maybe "does not report any dianostics" is not equal to "not analyze them"? I means maybe the analyzer or plugins has tried to analyze them and then excluded them from the reported diagnostics.

The intended behavior is that the analyzer will not analyze any files that have been excluded, with one exception, If code that is being analyzed (not excluded code) refers to an excluded file, either directly or indirectly, then it's necessary for the excluded file to be analyzed in order to analyze the included file, so it will be analyzed. When that happens, yes, the analyzer suppresses any diagnostics that would have been reported for the excluded file.

So generally excluded means "not analyzed", but sometimes it means "does not report any diagnostics".

The question is: are there references to libraries in .flutterw from non-excluded files?

Can you get some infos about this form the log I have uploaded( as_instrumentation_logs.txt) several days ago?

Not really, no. The log contains the information sent between the client (Android Studio in this case) and the analysis server (in both directions). The information about which context roots were created from the analysis roots isn't included in that traffic.

I suspect that you're right, that the problem is that the .flutterw directory isn't being excluded. I'm just not sure how to track down why that's happening.

@scheglov Do you have any suggestions for how to debug this situation?

bwilkerson avatar Aug 12 '22 16:08 bwilkerson

First of all, switch to using analyzer_plugin 0.11.1, so that it uses AnalysisContextCollection, just like the main analysis server does. Don't use AnalysisDriverGeneric and createAnalysisDriver(). And the new ServerPlugin class has methods analyzeFiles() and analyzeFile() that might be useful point to add some custom logging (I usually log into my tmp directory).

scheglov avatar Aug 12 '22 17:08 scheglov

@bwilkerson

The question is: are there references to libraries in .flutterw from non-excluded files?

I think the answer is YES. Because the .flutterw folder contains the whole Flutter framework code such as "StatelessWidget" and "StatefulWidget", and the code in my lib folder refer to them. This maybe the reason why they still be analyzed even I excluded them in the analysis_options.yaml file.

So I'm a little worry about what I did as I said to solve this problem:

contextRoot.exclude.add('${contextRoot.root}/.flutterw');

Do you think it has any side effects when I exclude this folder in program?

taibaiyinxing avatar Aug 15 '22 09:08 taibaiyinxing

@scheglov Thanks and I'll try to update the analyzer_plugin to 0.11.1 in the future.But for the current time we use the flutter 2.10 and it's not campatible with 0.11.1.

taibaiyinxing avatar Aug 15 '22 09:08 taibaiyinxing

Do you think it has any side effects when I exclude this folder in program?

Assuming that the code is executed before the context root is used, there shouldn't be any side-effects. That code ought to be equivalent to having excluded that directory in the analysis options file. The analyzer will still analyze any files in the directory that are required in order to analyze code outside the directory.

I still don't understand why that folder wasn't being excluded without the need to the code change.

bwilkerson avatar Aug 15 '22 15:08 bwilkerson