Why is it that AnalysisDriver.results emit an ErrorsResult for non-priority files?

50 views
Skip to first unread message

dark...@gmail.com

unread,
May 24, 2022, 8:53:35 AM5/24/22
to Dart Analyzer Discussion
Hello!

I'm currently working on lint packages and was having some issues understanding the setup of a plugin.
In particular I don't understand the logic of priority files

My understanding is that priority files are supposed to represent the files currently opened by the IDE. As such, we don't want AnalysisDriver.priorityFiles to be the list of all the files in the project (I think).

But I realised that if a file isn't present in priorityFiles, then AnalysisDriver.results will emit an ErrorsResult instead of a ResolvedUnitResult for that file.
I'm also aware that calling driver.getResult for that file will change that.

But what I don't understand is, why is this desired? Because if an analyzer plugin receives an ErrorsResult, then they will be unable to produce lints. Which means that if the plugin wants to treat all files in the projects, they have to either:
- add all files in the project to the list of priority file.
- call driver.getResult on all files.

Neither of these solutions sound logical to me.  And considering the name "ErrorResult", maybe there is something that I'm missing.


TD;DR: How can we analyze all Dart files in a project, while also making sure that files currently opened in the IDE are treated first (when possible)?

Thanks!
Remi

Konstantin Shcheglov

unread,
May 24, 2022, 12:10:33 PM5/24/22
to analyzer...@dartlang.org
On Tue, May 24, 2022 at 5:53 AM dark...@gmail.com <dark...@gmail.com> wrote:
Hello!

I'm currently working on lint packages and was having some issues understanding the setup of a plugin.

  Note, that we have a pending update for plugins, see https://dart-review.googlesource.com/c/sdk/+/243161
  See the motivation.

  Note in the CL usage of `analysisContext.changeFile`, `analysisContext.applyPendingFileChanges()`.
 
In particular I don't understand the logic of priority files

My understanding is that priority files are supposed to represent the files currently opened by the IDE. As such, we don't want AnalysisDriver.priorityFiles to be the list of all the files in the project (I think).

But I realised that if a file isn't present in priorityFiles, then AnalysisDriver.results will emit an ErrorsResult instead of a ResolvedUnitResult for that file.
I'm also aware that calling driver.getResult for that file will change that.

But what I don't understand is, why is this desired?

  We implemented `AnalysisDriver` first and foremost for Analysis Server (DAS), where you want resolved units for priority files to perform semantic highlighting, navigation, etc. For not priority files the only result that you see in the IDE is diagnostics (errors). And it is not unusual that when you open a project, you already have most of its files analyzed, and results stored in `ByteStore` (somewhere on disk). These results are not resolved units, but errors (an index for search). Computing a resolved unit is much more expensive, and so we decided to return a different result kind.

  If you are not DAS, you probably should use `AnalysisContextCollection`, `AnalysisContext`, `AnalysisSession`.
  Then you can decide which result to *pull* from the analyzer instead of waiting until results will be *pushed* into you.

 
Because if an analyzer plugin receives an ErrorsResult, then they will be unable to produce lints. Which means that if the plugin wants to treat all files in the projects, they have to either:
- add all files in the project to the list of priority file.
- call driver.getResult on all files.

Neither of these solutions sound logical to me.  And considering the name "ErrorResult", maybe there is something that I'm missing.

  Yes, please don't add all files into the priority list, this is an unfortunate decision that affects the plugin from the referenced issue.

  OTOH, there is nothing wrong in calling `AnalysisSession.getResolvedUnit()` for files that you want to analyze.
  The trick is to know for which files :-)
  Initially this is actually all files, unless you have a logic that allows you to decide better - and in this case explicitly pulling results is still better.
  And with `analysisContext.applyPendingFileChanges()` you can know which files to re-analyze after changes.
 


TD;DR: How can we analyze all Dart files in a project, while also making sure that files currently opened in the IDE are treated first (when possible)?

  Does the pending CL make it easier for you?
  It actually tries to process priority files files.

--
Konstantin Scheglov
Software Engineer
Google, Inc.

dark...@gmail.com

unread,
May 26, 2022, 5:55:39 PM5/26/22
to Dart Analyzer Discussion, Konstantin Shcheglov
Thanks for the quick reply!

I'll admit that I'm not too familiar with AnalysisContextCollection & co, and don't quite see how they relate to analyzer_plugin the way it currently is (as its API explicitly requests the creation of an AnalysisDriver).
From your PR it seems that this is going to change, although I don't know enough about it to know if that's what I'm looking for.

But if you believe that this is it, then I'm happy with using getResult for now, and update to use the new API once your PR is merged.

Reply all
Reply to author
Forward
0 new messages