From 3f29cf4b7d1631750680aa9d6d0eefee4a86c9a4 Mon Sep 17 00:00:00 2001 From: Paul Berry Date: Thu, 6 Nov 2025 14:39:20 -0800 Subject: [PATCH] [analyzer_testing] Propagate linter exceptions by default. Changes the behavior of the `analysisOptionsContent` function so that unless `propagateLinterExceptions: false` is passed to it, it creates an analysis options file that specifies a `true` value for `propagate-linter-exceptions`. This ensures that when tests that use `package:analyzer_testing` are run, exceptions that occur while processing lint rules will cause the test to fail. Previously, such exceptions would be silently swallowed by the analyzer, and the test would pass. Change-Id: I6a6a69643d76cf15d801eab5c4ce6a77c7dbac96 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/460220 Reviewed-by: Brian Wilkerson Reviewed-by: Konstantin Shcheglov Commit-Queue: Paul Berry --- pkg/analysis_server/test/abstract_context.dart | 2 ++ .../correction/fix/ignore_diagnostic_test.dart | 11 +++++++++-- .../test/src/dart/analysis/context_builder_test.dart | 5 ++++- pkg/analyzer_testing/CHANGELOG.md | 7 +++++++ pkg/analyzer_testing/api.txt | 2 +- pkg/analyzer_testing/lib/utilities/utilities.dart | 11 ++++++++++- pkg/analyzer_testing/pubspec.yaml | 2 +- 7 files changed, 34 insertions(+), 6 deletions(-) diff --git a/pkg/analysis_server/test/abstract_context.dart b/pkg/analysis_server/test/abstract_context.dart index d83eff2852d..808497917e8 100644 --- a/pkg/analysis_server/test/abstract_context.dart +++ b/pkg/analysis_server/test/abstract_context.dart @@ -125,6 +125,7 @@ class AbstractContextTest List cannotIgnore = const [], List lints = const [], Map errors = const {}, + bool propagateLinterExceptions = true, bool strictCasts = false, bool strictInference = false, bool strictRawTypes = false, @@ -134,6 +135,7 @@ class AbstractContextTest includes: includes, experiments: experiments, legacyPlugins: legacyPlugins, + propagateLinterExceptions: propagateLinterExceptions, rules: lints, errors: errors, unignorableNames: cannotIgnore, diff --git a/pkg/analysis_server/test/src/services/correction/fix/ignore_diagnostic_test.dart b/pkg/analysis_server/test/src/services/correction/fix/ignore_diagnostic_test.dart index e70f8bba9bd..2ac47f8beb7 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/ignore_diagnostic_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/ignore_diagnostic_test.dart @@ -23,7 +23,10 @@ class IgnoreDiagnosticAnalysisOptionFileTest extends FixProcessorTest { FixKind get kind => ignoreErrorAnalysisFileKind; Future test_addFixToExistingErrorMap() async { - createAnalysisOptionsFile(errors: {'unused_label': 'ignore'}); + createAnalysisOptionsFile( + errors: {'unused_label': 'ignore'}, + propagateLinterExceptions: false, + ); await resolveTestCode(''' void f() { @@ -107,6 +110,7 @@ analyzer: // To create a valid `analyzer` label, we add a `cannot-ignore` label. // This also implicitly tests when unrelated label is in `cannot-ignore` cannotIgnore: ['unused_label'], + propagateLinterExceptions: false, ); await resolveTestCode(''' @@ -139,7 +143,10 @@ void f() { // Having a newline is important because yaml_edit copies existing // newlines and we want to test the current platforms EOLs. // The content is normalized in newFile(). - createAnalysisOptionsFile(includes: ['package:lints/recommended.yaml']); + createAnalysisOptionsFile( + includes: ['package:lints/recommended.yaml'], + propagateLinterExceptions: false, + ); await resolveTestCode(''' void f() { diff --git a/pkg/analyzer/test/src/dart/analysis/context_builder_test.dart b/pkg/analyzer/test/src/dart/analysis/context_builder_test.dart index cda434a7256..062d43eb40e 100644 --- a/pkg/analyzer/test/src/dart/analysis/context_builder_test.dart +++ b/pkg/analyzer/test/src/dart/analysis/context_builder_test.dart @@ -75,7 +75,10 @@ class ContextBuilderImplTest with ResourceProviderMixin { var projectPath = convertPath('/home/test'); var optionsFile = newAnalysisOptionsYamlFile( projectPath, - analysisOptionsContent(strictRawTypes: true), + analysisOptionsContent( + strictRawTypes: true, + propagateLinterExceptions: false, + ), ); var analysisContext = _createSingleAnalysisContext(projectPath); diff --git a/pkg/analyzer_testing/CHANGELOG.md b/pkg/analyzer_testing/CHANGELOG.md index ed5b8b6da19..fa6a0cf8435 100644 --- a/pkg/analyzer_testing/CHANGELOG.md +++ b/pkg/analyzer_testing/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.1.8-dev + +- Change the behavior of `analysisOptionsContent` so that by default, the + analysis options file used in testing specifies a `true` value for + `propagate-linter-exceptions`. This ensures that when tests are run, + exceptions that occur while processing lint rules will cause the test to fail. + ## 0.1.7 - Deprecate `AnalysisRuleTest.analysisRule`; instead of implementing this diff --git a/pkg/analyzer_testing/api.txt b/pkg/analyzer_testing/api.txt index 65bae4fb5d6..5073509d644 100644 --- a/pkg/analyzer_testing/api.txt +++ b/pkg/analyzer_testing/api.txt @@ -70,7 +70,7 @@ package:analyzer_testing/utilities/extensions/resource_provider.dart: ResourceProviderExtension (extension on ResourceProvider): convertPath (method: String Function(String)) package:analyzer_testing/utilities/utilities.dart: - analysisOptionsContent (function: String Function({Map errors, List experiments, List includes, List legacyPlugins, List rules, bool strictCasts, bool strictInference, bool strictRawTypes, List unignorableNames})) + analysisOptionsContent (function: String Function({Map errors, List experiments, List includes, List legacyPlugins, bool propagateLinterExceptions, List rules, bool strictCasts, bool strictInference, bool strictRawTypes, List unignorableNames})) pubspecYamlContent (function: String Function({List dependencies, String? name, String? sdkVersion})) dart:async: Future (referenced) diff --git a/pkg/analyzer_testing/lib/utilities/utilities.dart b/pkg/analyzer_testing/lib/utilities/utilities.dart index 1f58df486ee..8f16cdc1de8 100644 --- a/pkg/analyzer_testing/lib/utilities/utilities.dart +++ b/pkg/analyzer_testing/lib/utilities/utilities.dart @@ -8,6 +8,7 @@ String analysisOptionsContent({ List includes = const [], List experiments = const [], List legacyPlugins = const [], + bool propagateLinterExceptions = true, List rules = const [], Map errors = const {}, bool strictCasts = false, @@ -30,7 +31,8 @@ String analysisOptionsContent({ errors.isNotEmpty || unignorableNames.isNotEmpty || legacyPlugins.isNotEmpty || - experiments.isNotEmpty) { + experiments.isNotEmpty || + propagateLinterExceptions) { buffer.writeln('analyzer:'); } if (experiments.isNotEmpty) { @@ -74,6 +76,13 @@ String analysisOptionsContent({ } } + if (propagateLinterExceptions) { + buffer.writeln(' optional-checks:'); + buffer.writeln( + ' propagate-linter-exceptions: $propagateLinterExceptions', + ); + } + if (rules.isNotEmpty) { buffer.writeln('linter:'); buffer.writeln(' rules:'); diff --git a/pkg/analyzer_testing/pubspec.yaml b/pkg/analyzer_testing/pubspec.yaml index 9e201920f86..e6535cee6a1 100644 --- a/pkg/analyzer_testing/pubspec.yaml +++ b/pkg/analyzer_testing/pubspec.yaml @@ -1,6 +1,6 @@ name: analyzer_testing description: Testing utilities related to the analyzer and analysis_server_plugin packages. -version: 0.1.7 +version: 0.1.8-dev repository: https://github.com/dart-lang/sdk/tree/main/pkg/analyzer_testing environment: