Skip to content

Commit 05f0ee3

Browse files
authored
fix: flutter analyze reporting the same issue multiple times (flutter#193279)
This PR fixes `flutter analyze` reporting the same issue multiple times. The analysis server sends the complete list of diagnostics for a file in a `textDocument/publishDiagnostics` notification, and publishes the file again whenever that list changes, for example once for the Dart analysis results and again when an analyzer plugin reports for it. `AnalyzeOnce` appended the errors from every notification, so an issue in a file that was published three times was reported three times. This change keeps only the latest diagnostics for each file, the same way `AnalyzeContinuously` already does, and adds a test that publishes diagnostics for the same file twice. Fixes: flutter#192587 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent bbef902 commit 05f0ee3

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

‎packages/flutter_tools/lib/src/commands/analyze_once.dart‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AnalyzeOnce extends AnalyzeBase {
5050
throwToolExit('Nothing to analyze.', exitCode: 0);
5151
}
5252

53-
final errors = <AnalysisError>[];
53+
final errorsByFile = <String, List<AnalysisError>>{};
5454

5555
final server = AnalysisServer(
5656
sdkPath,
@@ -70,7 +70,7 @@ class AnalyzeOnce extends AnalyzeBase {
7070
Status? progress;
7171
try {
7272
void handleAnalysisErrors(FileAnalysisErrors fileErrors) {
73-
errors.addAll(fileErrors.errors);
73+
errorsByFile[fileErrors.file] = fileErrors.errors;
7474
}
7575

7676
server.onErrors.listen(handleAnalysisErrors);
@@ -113,6 +113,10 @@ class AnalyzeOnce extends AnalyzeBase {
113113
timer?.stop();
114114
}
115115

116+
final List<AnalysisError> errors = errorsByFile.values
117+
.expand((List<AnalysisError> fileErrors) => fileErrors)
118+
.toList();
119+
116120
// emit benchmarks
117121
if (isBenchmarking) {
118122
writeBenchmark(timer, errors.length);

‎packages/flutter_tools/test/commands.shard/hermetic/analyze_test.dart‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,39 @@ void main() {
172172
);
173173
});
174174

175+
testWithoutContext('only the latest diagnostics published for a file are reported', () async {
176+
final process = MockLspServerProcess();
177+
processManager.addCommands(<FakeCommand>[
178+
FakeCommand(
179+
command: const <String>[
180+
'Artifact.engineDartSdkPath/bin/dart',
181+
'language-server',
182+
'--dart-sdk',
183+
'Artifact.engineDartSdkPath',
184+
'--disable-server-feature-completion',
185+
'--disable-server-feature-search',
186+
'--no-with-fine-dependencies',
187+
'--suppress-analytics',
188+
],
189+
process: process,
190+
),
191+
]);
192+
final Uri targetUri = Uri.parse('file:///directoryA/foo');
193+
await process.runSimulatedAnalysis(diagnosticsFor: targetUri);
194+
await process.runSimulatedAnalysis(diagnosticsFor: targetUri, diagnosticsCount: 2);
195+
196+
await expectLater(
197+
runner.run(<String>['analyze']),
198+
throwsA(
199+
isA<ToolExit>().having(
200+
(ToolExit e) => e.message,
201+
'message',
202+
startsWith('2 issues found.'),
203+
),
204+
),
205+
);
206+
});
207+
175208
testWithoutContext('--no-plugins passes --no-plugins to language-server', () async {
176209
processManager.addCommands(<FakeCommand>[
177210
const FakeCommand(

0 commit comments

Comments
 (0)