Re: [analyzer-discuss] How to get the dart files which refers current dart file with analyzer:6.2.0 and analyzer_plugin: 0.11.3 ?

75 views
Skip to first unread message

Brian Wilkerson

unread,
Nov 16, 2023, 1:52:33 PM11/16/23
to analyzer...@dartlang.org
Sorry for the long delay. This got lost in my email. :-(

I'm developing a dart analyzer plugin ...

I'll start with the standard disclaimer that the current plugin implementation is only a proof of concept and isn't formally supported. It has some issues, not least of which is that because of poor performance characteristics we've had to limit users to running at most one plugin.

We are actively working to see whether we can support a performant and usable plugin implementation. If we can we'll support it, but we don't have any plans that I'm aware of to make it easy to port any work you do now to any future replacement.

That said, I'll point you to our newly created Discord channel as a potentially better place to reach us: https://discord.com/channels/608014603317936148/1171510601655275612.

Is there any way to get all dart files which refer current dart file quickly?

Not that I can think of, but I'm curious to understand why you need to access that information. If a `.dart` file is modified, then you can use the method `AnalysisContext.changeFile` to cause the driver to reanalyze that file and anything that depends on it. If the `package_config.json` file changes then you should recreate the `AnalysisContextCollection` through which analysis is being performed.

On Mon, Nov 6, 2023 at 9:01 AM 2333 heel <heel...@gmail.com> wrote:
I'm developing a dart analyzer plugin which will analyze the dart files by the configuration json format file. When the configuration json format file changes, I need to find all dart files which refer the dart files of current directory and analyze them agagin.

But with analyzer:6.2.0 and analyzer_plugin: 0.11.3, I can not find AnalyzeDriver.getFilesReferencingName api in ServerPlugin. I could only cache all dart files in ServerPlugin.afterNewContextCollection and find the relationships between them. It cost s a lot of time to analyze every dart file and find all dart files referring it.

Is there any way to get all dart files which refer current dart file quickly?

--
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/12cb5ab6-0b84-4213-8637-cef4851602e1n%40dartlang.org.

2333 heel

unread,
Nov 18, 2023, 6:05:41 AM11/18/23
to Dart Analyzer Discussion, brianwilkerson
Thank you for your response.

Regarding the Analyzer Plugin API, it is currently considered to be in an immature state with suboptimal performance. In fact, the plugin I am currently developing exhibits poor performance. Enabling the plugin in the project causes the analyzer to take several minutes to start functioning and often appears to be malfunctioning.

The plugin I am developing aims to identify all symbols with names starting with '$' and designate them as protected variables. I  use a single visitor for this purpose. However, I am concerned about the potential performance impact of retrieving the absolute path of the source library of the protected symbol. Could this operation cost a lot?

Brian Wilkerson

unread,
Nov 18, 2023, 12:18:00 PM11/18/23
to 2333 heel, Dart Analyzer Discussion
The plugin I am developing aims to identify all symbols with names starting with '$' and designate them as protected variables. I  use a single visitor for this purpose. However, I am concerned about the potential performance impact of retrieving the absolute path of the source library of the protected symbol. Could this operation cost a lot?

It shouldn't. Assuming that you're working with a resolved AST, these identifiers (`SimpleIdentifier` everywhere except at declaration sites) should have a non-null `staticElement`. You can ask that element for its `library` (a `LibraryElement`). All of that information is already computed; you're just accessing data stored in fields.

You might find it helpful to look at how the `BestPracticesVerifier` supports the `protected` annotation (from `package:meta`).

2333 heel

unread,
Nov 19, 2023, 9:54:16 AM11/19/23
to Brian Wilkerson, Dart Analyzer Discussion
Thanks for replying.

I have looked into the @protected annotation, but what I want is more complex. The protected symbols, which could be classes, functions, const vars, etc., cannot be accessed in other directories. They should only be granted access within the current directory, similar to protected classes in Java.

Currently, the cost of searching the path is cheap. However, I encountered another problem, as shown in the pictures below.

I don't allow importing the protected Dart file that starts with '_' and using the symbols within it. Then, I return the correct position to the analyzer server, which I have verified in the unit test. However, in Visual Studio Code and Android Studio, the positions are not the same.

In Visual Studio Code, the error is import 'self_pkg/_protected.dart', but the error line is shown as line 1 column 35. The correct position should be (2, 35).
image.png

In android studio. Only the import error is correct...
image.png


What I should do to fix it? 

Additionally, besides using the Dart analyzer plugin (which is still an immature solution), are there any other ways for me to achieve this requirement? I hope to have stricter control over symbols to achieve better software maintenance.

I appreciate your assistance once again.

Brian Wilkerson <brianwi...@google.com> 于2023年11月19日周日 01:17写道:

Brian Wilkerson

unread,
Nov 20, 2023, 5:12:02 PM11/20/23
to 2333 heel, Dart Analyzer Discussion
I have looked into the @protected annotation, but what I want is more complex.

I wasn't suggesting that you use the annotation, just that the implementation was likely to be similar in some ways, so it might be a good example of the kind of checks you're trying to implement.

(Though it does sound like the annotation is very close to what you're building, just replacing the Dart notion of a library for the Java notion of a directory. The different semantics of Dart might mean that the semantics of the annotation really are what you want. Just a thought.)

In Visual Studio Code, the error is import 'self_pkg/_protected.dart', but the error line is shown as line 1 column 35. The correct position should be (2, 35).

That's likely to be a bug in the analysis server, though you could verify that by enabling the server's instrumentation log (using the `protocol-traffic-log` command-line argument) and check the values sent from the server to VSCode similar to the way you checked the values being sent from your plugin to the server.

In android studio. Only the import error is correct...

The protocol used to communicate with IntelliJ and Android Studio is a different protocol than LSP, and it uses the offset/length that the plugin sends back. It makes sense that the import diagnostic is correct, but I don't know why the other diagnostics would be wrong. (I'm assuming that you've checked the offsets being sent from the plugin and that they're correct.)

What I should do to fix it?

It seems likely that the bugs are in the analysis server. We aren't likely to try to fix bugs in an unsupported feature, but if you want to make changes to the analysis server to fix the issue we'd likely accept a bug fix. You might want to ask on the Discord channel to see whether other plugin authors have seen similar behavior and if so whether they have fixes. Even new it appears to have more active community participation than this mailing list (which was our goal for creating the channel, so Yea!).

Additionally, besides using the Dart analyzer plugin (which is still an immature solution), are there any other ways for me to achieve this requirement?

Unfortunately, no. The only thing I can think of is to write your own IDE plugins, but that's a prohibitively expensive option. We do hope to have reasonable support for analyzer plugins at some point, but I don't know when that will be. I only know that we're actively designing the support now in order to determine whether the project is one we can commit to.

2333 heel

unread,
Nov 21, 2023, 8:29:36 AM11/21/23
to Brian Wilkerson, Dart Analyzer Discussion
Thanks for the advice. 

Brian Wilkerson <brianwi...@google.com> 于2023年11月21日周二 06:12写道:
Reply all
Reply to author
Forward
0 new messages