Is there a way to start AnalysisContextCollection in watch mode?

30 views
Skip to first unread message

dark...@gmail.com

unread,
Jul 31, 2024, 9:46:16 AM7/31/24
to Dart Analyzer Discussion
Hello!

Is it possible to have AnalysisContextCollection listen to file changes?
To:
- Detect new contexts
- Have contexts detect new files and analysisOptions

Because at the moment, the list of collection appears to be fixed. And `context.analysisOptions` never update either to list the new options if one is made.
Same goes for "driver.knownFiles" which doesn't seem to update.

Thanks!

Brian Wilkerson

unread,
Jul 31, 2024, 10:55:42 AM7/31/24
to analyzer...@dartlang.org
Is it possible to have AnalysisContextCollection listen to file changes?

No. The way the code is designed, that's explicitly the responsibility of the code that creates the analysis context collection. At the moment, the code that watches the file system lives in the analysis_server package.

There are a few reasons for this.

1. The applications that we were previously aware of that need to create a collection don't want to listen to file changes. This is the first request we've had for such a feature.

2. When changes occur on the file system, the intended behavior is to discard the existing collection and create a new one. Doing so is a fairly light-weight operation, and it reduces the probability of inconsistencies being introduced while attempting to update the collection in place.

3. In the server we need to also take into account the state of files that have been edited but not saved. If unsaved changes exist in a file that's modified the server needs to continue to analyze the code in the edit buffer, not the code on disk, so it doesn't make sense to put that functionality in code that doesn't know about the client.



--
You received this message because you are subscribed to the Google Groups "Dart Analyzer Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to analyzer-discu...@dartlang.org.
To view this discussion on the web visit https://groups.google.com/a/dartlang.org/d/msgid/analyzer-discuss/127b44a5-dd69-4826-a54f-0a955cf1ab58n%40dartlang.org.

dark...@gmail.com

unread,
Jul 31, 2024, 11:20:02 AM7/31/24
to Dart Analyzer Discussion, brianwilkerson
Thanks.

In that case, when are we supposed to create a new AnalysisContextCollection?
Whenever anything changes in the file system?

I'm aware that there's the topic of "unsaved changes". But I thought this would've been covered by OverlayResourceProvider.

dark...@gmail.com

unread,
Jul 31, 2024, 11:29:37 AM7/31/24
to Dart Analyzer Discussion, dark...@gmail.com, brianwilkerson
Also,  analyzer_server doesn't seem to be public. Is there a publicly available equivalent? Or maybe a way to use analyzer_server that I missed?

Brian Wilkerson

unread,
Jul 31, 2024, 2:07:45 PM7/31/24
to analyzer...@dartlang.org
In that case, when are we supposed to create a new AnalysisContextCollection?

The analysis server creates a new collection whenever a file that could change how the contexts are built is changed. It's not obvious which files are important, but it's basically the `package_confg.json` and `analysis_options.yaml` files (we might include `pubspec.yaml` in that calculus, but I don't think we do and more).

When other files are changed you only need to let the driver know that the file has changed (and I think there's a public API for that now).

I'm aware that there's the topic of "unsaved changes". But I thought this would've been covered by OverlayResourceProvider.

The `OverlayResourceProvider` keeps track of which files have overlays and what the contents are, but it's the outer scope's responsibility to figure out whether to tell the driver of changes when files on disk that have an overlay are changed. (I don't think there's ever any value in doing so.)

Also,  analyzer_server doesn't seem to be public. Is there a publicly available equivalent? Or maybe a way to use analyzer_server that I missed?

No, we don't currently publish the analysis server and we never worry about breaking change processes or notifications when making changes in that package.

In part because we've never had a request for it. If you're willing to say, I'm curious to know what you're building where you're wanting to listen to file changes and use the server this way?

Reply all
Reply to author
Forward
0 new messages