From cc5612e322674fe96edf65bf7a693a1253022ca7 Mon Sep 17 00:00:00 2001 From: Rohit Date: Tue, 21 Jan 2025 20:34:40 -0800 Subject: [PATCH] Fix: False Positive "include_file_not_found" See https://github.com/dart-lang/sdk/issues/59888 for an explanation of the bug this commit fixes. Incidentally, the bug prevents a "recursive_include_file" error from being reported when a file includes itself with a quoted "include" field value. This fix happens to also correct that so it is no longer the case. Introduce code to handle quotations delimiting an "include" field value. The code removes the quotes before the URI resolution is attempted. This will prevent quotations from being interpreted as a part of the URI to resolve. Also, add tests to verify these code changes fix the bug. R=brianwilkerson@google.com, keertip@google.com, leafp@google.com Bug: #59888 Change-Id: Idc921652bf356889142c6e38b0cdec5e66825d36 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/405180 Auto-Submit: Rohit Saily Commit-Queue: Keerti Parthasarathy Reviewed-by: Brian Wilkerson Reviewed-by: Keerti Parthasarathy --- pkg/analyzer/lib/src/task/options.dart | 8 +++ .../include_file_not_found_test.dart | 56 ++++++++++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) diff --git a/pkg/analyzer/lib/src/task/options.dart b/pkg/analyzer/lib/src/task/options.dart index d9c609ac3bab..0975d14cfe4b 100644 --- a/pkg/analyzer/lib/src/task/options.dart +++ b/pkg/analyzer/lib/src/task/options.dart @@ -94,6 +94,14 @@ List analyzeAnalysisOptions( var includeSpan = includeNode.span; initialIncludeSpan ??= includeSpan; var includeUri = includeSpan.text; + var (first, last) = ( + includeUri.codeUnits.firstOrNull, + includeUri.codeUnits.lastOrNull); + if ((first == 0x0022 || first == 0x0027) && first == last) { + // The URI begins and ends with either a double quote or single quote + // i.e. the value of the "include" field is quoted. + includeUri = includeUri.substring(1, includeUri.length - 1); + } var includedSource = sourceFactory.resolveUri(source, includeUri); if (includedSource == initialSource) { diff --git a/pkg/analyzer/test/src/diagnostics/analysis_options/include_file_not_found_test.dart b/pkg/analyzer/test/src/diagnostics/analysis_options/include_file_not_found_test.dart index 9ab6264882da..0952569db0e6 100644 --- a/pkg/analyzer/test/src/diagnostics/analysis_options/include_file_not_found_test.dart +++ b/pkg/analyzer/test/src/diagnostics/analysis_options/include_file_not_found_test.dart @@ -15,7 +15,46 @@ main() { @reflectiveTest class IncludeFileNotFoundTest extends AbstractAnalysisOptionsTest { - void test_notFound() { + void test_notFound_existent_doubleQuoted() { + assertErrorsInCode(''' +include: "./analysis_options.yaml" +''', [ + error(AnalysisOptionsWarningCode.RECURSIVE_INCLUDE_FILE, 9, 25) + ]); + } + + void test_notFound_existent_notQuoted() { + assertErrorsInCode(''' +include: ./analysis_options.yaml +''', [ + error(AnalysisOptionsWarningCode.RECURSIVE_INCLUDE_FILE, 9, 23) + ]); + } + + void test_notFound_existent_singleQuoted() { + assertErrorsInCode(''' +include: './analysis_options.yaml' +''', [ + error(AnalysisOptionsWarningCode.RECURSIVE_INCLUDE_FILE, 9, 25) + ]); + } + + void test_notFound_nonexistent_doubleQuoted() { + assertErrorsInCode(''' +# We don't depend on pedantic, but we should consider adding it. +include: "package:pedantic/analysis_options.yaml" +''', [ + error( + AnalysisOptionsWarningCode.INCLUDE_FILE_NOT_FOUND, + 74, + 40, + text: "The include file 'package:pedantic/analysis_options.yaml'" + " in '${convertPath('/analysis_options.yaml')}' can't be found when analyzing '/'.", + ) + ]); + } + + void test_notFound_nonexistent_notQuoted() { assertErrorsInCode(''' # We don't depend on pedantic, but we should consider adding it. include: package:pedantic/analysis_options.yaml @@ -29,4 +68,19 @@ include: package:pedantic/analysis_options.yaml ) ]); } + + void test_notFound_nonexistent_singleQuoted() { + assertErrorsInCode(''' +# We don't depend on pedantic, but we should consider adding it. +include: 'package:pedantic/analysis_options.yaml' +''', [ + error( + AnalysisOptionsWarningCode.INCLUDE_FILE_NOT_FOUND, + 74, + 40, + text: "The include file 'package:pedantic/analysis_options.yaml'" + " in '${convertPath('/analysis_options.yaml')}' can't be found when analyzing '/'.", + ) + ]); + } }