Skip to content

Implementation of parameter parser#234

Open
Dariaa14 wants to merge 18 commits into
analysis_server_migrationfrom
implementation-of-parameter-parser
Open

Implementation of parameter parser#234
Dariaa14 wants to merge 18 commits into
analysis_server_migrationfrom
implementation-of-parameter-parser

Conversation

@Dariaa14
Copy link
Copy Markdown

Implementation:

Since the analysis_server_plugin currently lacks a direct way to retrieve the context root (noted by the TODO in the official source code), I explored two strategies for resolving analysis_options.yaml:

  1. Plugin Register: I found this too restrictive, as it only functions correctly when 'dart analyze' is executed from the project root.

  2. Rule Registration with Caching (Chosen): By resolving the configuration at the rule level, we can leverage the RuleContext to identify the file path currently under analysis. To prevent the performance overhead of re-parsing for every file, I implemented a caching layer. The YAML is only parsed once per 'dart analyze'; subsequent files retrieve the configuration from the cache.

Research of other packages:

I researched the approach of others:

custom_lint and dart_code_metrics both utilize a custom server architecture built on the legacy analyzer_plugin package.
I found no existing public implementations for dynamic parameter injection for analysis_server_plugin users.

Configuration Structure

Regarding our previous discussion on YAML structure: I tested the older formats used by other analyzers, but they were not recognized by the current server plugin. I have reverted to the structure specified in the official Dart documentation, which I have verified as functional in this implementation.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces the AnalysisOptionsLoader and LintOptions classes to parse and manage lint configurations from analysis_options.yaml. The review identifies critical issues with the current caching strategy, noting that a global cache will cause incorrect behavior in monorepos and lacks a mechanism for invalidation when configuration files change. Further recommendations include renaming variables for clarity and consistently using the analyzer's ResourceProvider API instead of dart:io to ensure better compatibility and testability.

Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated

/// Loads and parses analysis options from a Dart project's YAML file.
class AnalysisOptionsLoader {
Map<String, LintOptions> _rulesCache = {};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The current caching mechanism does not support cache invalidation. In a long-running Analysis Server session (e.g., in an IDE), changes to analysis_options.yaml will not be detected, and the plugin will continue using stale configuration until the server is restarted. Consider checking the file's modification timestamp or implementing a file watcher to refresh the cache when the configuration changes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Dariaa14, have you investigated the problem described here? Looks pretty relevant overall, but I'm not sure if changing analysis_options.yaml would restart the analysis server and reload the configuration.

Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Copy link
Copy Markdown
Collaborator

@solid-vovabeloded solid-vovabeloded left a comment

Choose a reason for hiding this comment

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

Great start, @Dariaa14! Please, take a look at the following suggestions and let me know what you think.


/// Loads and parses analysis options from a Dart project's YAML file.
class AnalysisOptionsLoader {
Map<String, LintOptions> _rulesCache = {};
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@Dariaa14, have you investigated the problem described here? Looks pretty relevant overall, but I'm not sure if changing analysis_options.yaml would restart the analysis server and reload the configuration.

Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/lint_options.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment thread lib/src/common/parameter_parser/analysis_options_loader.dart Outdated
Comment on lines +69 to +71
_analysisLoader.loadRulesFromContext(context);
// To get the options of the rule:
// _analysisLoader.getRuleOptions(context, lintName);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I know this is just a usage example - leaving this comment for the future. We would need to create a separate class for our rules that would encapsulate this logic and just provide an API to get the rule settings.

style: improve variable names
refactor: use root package path instead of library path
refactor: use pattern matching to reduce nesting
refactor: allow mocking resource provider
refactor: extract CachedPackageRules model
refactor: use Map<String, Object?> instead of LintOptions
as the enabled field is implicitly true for all rules that the analyzer processes
remove RuleConfig as it is no longer needed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants