Skip to content

Commit 5e7cebe

Browse files
FMorschelCommit Queue
authored andcommitted
[analyzer] Fixes incompatible_lint diagnostics for package includes
Fixes: #61497 Change-Id: I55cb128f4e671750f46151c9469871082eb0a755 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/449370 Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 46eae16 commit 5e7cebe

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

pkg/analysis_server/test/services/linter/linter_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import 'package:analyzer/error/listener.dart';
88
import 'package:analyzer/source/source.dart';
99
import 'package:analyzer/src/analysis_options/analysis_options_provider.dart';
1010
import 'package:analyzer/src/analysis_options/error/option_codes.dart';
11+
import 'package:analyzer/src/file_system/file_system.dart';
12+
import 'package:analyzer/src/generated/source.dart';
1113
import 'package:analyzer/src/lint/options_rule_validator.dart';
1214
import 'package:analyzer_testing/resource_provider_mixin.dart';
1315
import 'package:linter/src/rules.dart';
@@ -23,18 +25,20 @@ void main() {
2325
@reflectiveTest
2426
class LinterRuleOptionsValidatorTest with ResourceProviderMixin {
2527
late RecordingDiagnosticListener recorder;
26-
28+
late SourceFactory sourceFactory;
2729
late DiagnosticReporter reporter;
2830

2931
List<Diagnostic> get diagnostics => recorder.diagnostics;
3032

3133
LinterRuleOptionsValidator get validator => LinterRuleOptionsValidator(
3234
optionsProvider: AnalysisOptionsProvider(),
3335
resourceProvider: resourceProvider,
36+
sourceFactory: sourceFactory,
3437
);
3538

3639
void setUp() {
3740
registerLintRules();
41+
sourceFactory = SourceFactory([ResourceUriResolver(resourceProvider)]);
3842
recorder = RecordingDiagnosticListener();
3943
reporter = DiagnosticReporter(recorder, _TestSource());
4044
}

pkg/analyzer/lib/src/analysis_options/options_file_validator.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ List<Diagnostic> analyzeAnalysisOptions(
8484
sdkVersionConstraint: sdkVersionConstraint,
8585
isPrimarySource: isSourcePrimary,
8686
optionsProvider: optionsProvider,
87+
sourceFactory: sourceFactory,
8788
resourceProvider: resourceProvider,
8889
).validate(options);
8990
addDirectErrorOrIncludedError(
@@ -299,6 +300,7 @@ class OptionsFileValidator {
299300
required bool isPrimarySource,
300301
required AnalysisOptionsProvider optionsProvider,
301302
required ResourceProvider resourceProvider,
303+
required SourceFactory sourceFactory,
302304
}) : _validators = [
303305
AnalyzerOptionsValidator(),
304306
_CodeStyleOptionsValidator(),
@@ -307,6 +309,7 @@ class OptionsFileValidator {
307309
LinterRuleOptionsValidator(
308310
resourceProvider: resourceProvider,
309311
optionsProvider: optionsProvider,
312+
sourceFactory: sourceFactory,
310313
sdkVersionConstraint: sdkVersionConstraint,
311314
isPrimarySource: isPrimarySource,
312315
),

pkg/analyzer/lib/src/lint/options_rule_validator.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:analyzer/src/analysis_options/error/option_codes.dart';
1111
import 'package:analyzer/src/analysis_options/options_validator.dart';
1212
import 'package:analyzer/src/analysis_rule/rule_context.dart';
1313
import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
14+
import 'package:analyzer/src/generated/source.dart';
1415
import 'package:analyzer/src/lint/registry.dart';
1516
import 'package:analyzer/src/util/yaml.dart';
1617
import 'package:analyzer/src/utilities/extensions/string.dart';
@@ -54,10 +55,12 @@ class LinterRuleOptionsValidator extends OptionsValidator {
5455

5556
final AnalysisOptionsProvider optionsProvider;
5657
final ResourceProvider resourceProvider;
58+
final SourceFactory sourceFactory;
5759

5860
LinterRuleOptionsValidator({
5961
required this.resourceProvider,
6062
required this.optionsProvider,
63+
required this.sourceFactory,
6164
this.sdkVersionConstraint,
6265
this.isPrimarySource = true,
6366
});
@@ -105,6 +108,14 @@ class LinterRuleOptionsValidator extends OptionsValidator {
105108

106109
if (includePath.isEmpty) return null;
107110

111+
if (sourceUri != null) {
112+
var source = FileSource(resourceProvider.getFile(sourceUri.toFilePath()));
113+
var resolved = sourceFactory.resolveUri(source, includePath);
114+
if (resolved is FileSource) {
115+
return resolved.file.toUri();
116+
}
117+
}
118+
108119
var uri = Uri.parse(includePath);
109120
if (uri == sourceUri) {
110121
// The URI is the same as the source URI, so we don't need to resolve it.

pkg/analyzer/test/src/diagnostics/analysis_options/analysis_options_test_support.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:analyzer/src/analysis_options/options_file_validator.dart';
88
import 'package:analyzer/src/context/source.dart';
99
import 'package:analyzer/src/file_system/file_system.dart';
1010
import 'package:analyzer/src/generated/source.dart';
11+
import 'package:analyzer/src/source/package_map_resolver.dart';
1112
import 'package:analyzer/src/test_utilities/lint_registration_mixin.dart';
1213
import 'package:analyzer_testing/resource_provider_mixin.dart';
1314
import 'package:meta/meta.dart';
@@ -18,6 +19,7 @@ import '../../../generated/test_support.dart';
1819
abstract class AbstractAnalysisOptionsTest
1920
with ResourceProviderMixin, LintRegistrationMixin {
2021
late SourceFactory sourceFactory;
22+
Map<String, String>? dependencies;
2123

2224
late File analysisOptionsFile = newFile(analysisOptionsPath, '');
2325
late String analysisOptionsPath = convertPath('/analysis_options.yaml');
@@ -64,7 +66,14 @@ abstract class AbstractAnalysisOptionsTest
6466
);
6567

6668
void setUp() {
67-
var resolvers = [ResourceUriResolver(resourceProvider)];
69+
var resolvers = [
70+
ResourceUriResolver(resourceProvider),
71+
if (dependencies != null)
72+
PackageMapUriResolver(resourceProvider, {
73+
for (var entry in dependencies!.entries)
74+
entry.key: [getFolder(convertPath(entry.value))],
75+
}),
76+
];
6877
sourceFactory = SourceFactoryImpl(resolvers);
6978
}
7079

pkg/analyzer/test/src/options/options_file_validator_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class OptionsFileValidatorTest
8080
isPrimarySource: true,
8181
optionsProvider: optionsProvider,
8282
resourceProvider: resourceProvider,
83+
sourceFactory: SourceFactory([ResourceUriResolver(resourceProvider)]),
8384
);
8485
final AnalysisOptionsProvider optionsProvider = AnalysisOptionsProvider();
8586

pkg/analyzer/test/src/options/options_rule_validator_test.dart

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class DeprecatedSince3Lint extends TestLintRule {
5050
@reflectiveTest
5151
class OptionsRuleValidatorIncludedFileTest extends AbstractAnalysisOptionsTest
5252
with OptionsRuleValidatorTestMixin {
53+
static const otherLib = '/other/lib';
54+
55+
@override
56+
get dependencies => {'other': otherLib};
57+
5358
void test_compatible_multiple_include() {
5459
newFile('/included1.yaml', '''
5560
linter:
@@ -260,6 +265,26 @@ linter:
260265
);
261266
}
262267

268+
void test_package_import() {
269+
newFile('$otherLib/analysis_options.yaml', '''
270+
linter:
271+
rules:
272+
rule_pos: true
273+
''');
274+
testProjectPath = '/test';
275+
assertErrors(
276+
'''
277+
include:
278+
- package:other/analysis_options.yaml
279+
280+
linter:
281+
rules:
282+
rule_neg: true
283+
''',
284+
[AnalysisOptionsWarningCode.incompatibleLintFiles],
285+
);
286+
}
287+
263288
Future<void> test_removed_rule_inInclude_ok() async {
264289
newFile('/included.yaml', '''
265290
linter:
@@ -490,6 +515,8 @@ linter:
490515
}
491516

492517
mixin OptionsRuleValidatorTestMixin on AbstractAnalysisOptionsTest {
518+
String? testProjectPath;
519+
493520
/// Assert that when the validator is used on the given [content] the
494521
/// [expectedCodes] are produced.
495522
void assertErrors(
@@ -498,15 +525,17 @@ mixin OptionsRuleValidatorTestMixin on AbstractAnalysisOptionsTest {
498525
VersionConstraint? sdk,
499526
}) {
500527
GatheringDiagnosticListener listener = GatheringDiagnosticListener();
501-
var reporter = DiagnosticReporter(
502-
listener,
503-
StringSource(content, 'analysis_options.yaml'),
504-
);
505-
var source = StringSource(content, 'analysis_options.yaml');
528+
String filePath = 'analysis_options.yaml';
529+
if (testProjectPath != null) {
530+
filePath = resourceProvider.pathContext.join(testProjectPath!, filePath);
531+
}
532+
var source = StringSource(content, filePath);
533+
var reporter = DiagnosticReporter(listener, source);
506534
var validator = LinterRuleOptionsValidator(
507535
optionsProvider: AnalysisOptionsProvider(sourceFactory),
508-
sdkVersionConstraint: sdk,
509536
resourceProvider: resourceProvider,
537+
sourceFactory: sourceFactory,
538+
sdkVersionConstraint: sdk,
510539
);
511540
validator.validate(
512541
reporter,

0 commit comments

Comments
 (0)