Skip to content

Commit 86e3305

Browse files
DanTupCommit Queue
authored andcommitted
Reland "[analysis_server] Analyze fix data in fix_data folder"
This is a reland of commit bbacf39 Original change's description: > [analysis_server] Analyze fix data in fix_data folder > > Fixes part of #52126. > > Change-Id: Ib4bd7830a2f644eacedccd375c7c8dc60f040d33 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/296801 > Commit-Queue: Brian Wilkerson <[email protected]> > Reviewed-by: Brian Wilkerson <[email protected]> Change-Id: I571c1e4f87fdf0095d003d496f3c5d88e5cf0ff8 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/297940 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent b508533 commit 86e3305

File tree

4 files changed

+112
-12
lines changed

4 files changed

+112
-12
lines changed

pkg/analysis_server/lib/src/context_manager.dart

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,14 +344,28 @@ class ContextManagerImpl implements ContextManager {
344344
callbacks.recordAnalysisErrors(path, convertedErrors);
345345
}
346346

347+
/// Use the given analysis [driver] to analyze the content of yaml files
348+
/// inside [folder].
349+
void _analyzeFixDataFolder(
350+
AnalysisDriver driver, Folder folder, String packageName) {
351+
for (var resource in folder.getChildren()) {
352+
if (resource is File) {
353+
if (resource.shortName.endsWith('.yaml')) {
354+
_analyzeFixDataYaml(driver, resource, packageName);
355+
}
356+
} else if (resource is Folder) {
357+
_analyzeFixDataFolder(driver, resource, packageName);
358+
}
359+
}
360+
}
361+
347362
/// Use the given analysis [driver] to analyze the content of the
348-
/// data file at the given [path].
349-
void _analyzeFixDataYaml(AnalysisDriver driver, String path) {
363+
/// given [File].
364+
void _analyzeFixDataYaml(
365+
AnalysisDriver driver, File file, String packageName) {
350366
var convertedErrors = const <protocol.AnalysisError>[];
351367
try {
352-
var file = resourceProvider.getFile(path);
353-
var packageName = file.parent.parent.shortName;
354-
var content = _readFile(path);
368+
var content = file.readAsStringSync();
355369
var errorListener = RecordingErrorListener();
356370
var errorReporter = ErrorReporter(
357371
errorListener,
@@ -368,7 +382,7 @@ class ContextManagerImpl implements ContextManager {
368382
// If the file cannot be analyzed, fall through to clear any previous
369383
// errors.
370384
}
371-
callbacks.recordAnalysisErrors(path, convertedErrors);
385+
callbacks.recordAnalysisErrors(file.path, convertedErrors);
372386
}
373387

374388
/// Use the given analysis [driver] to analyze the content of the pubspec file
@@ -436,10 +450,31 @@ class ContextManagerImpl implements ContextManager {
436450
}
437451

438452
void _checkForFixDataYamlUpdate(String path) {
453+
String? extractPackageNameFromPath(String path) {
454+
String? packageName;
455+
var pathSegments = pathContext.split(path);
456+
if (pathContext.basename(path) == file_paths.fixDataYaml &&
457+
pathSegments.length >= 3) {
458+
// packageName/lib/fix_data.yaml
459+
packageName = pathSegments[pathSegments.length - 3];
460+
} else {
461+
var fixDataIndex = pathSegments.indexOf('fix_data');
462+
if (fixDataIndex >= 2) {
463+
// packageName/lib/fix_data/foo/bar/fix.yaml
464+
packageName = pathSegments[fixDataIndex - 2];
465+
}
466+
}
467+
return packageName;
468+
}
469+
439470
if (file_paths.isFixDataYaml(pathContext, path)) {
440471
var driver = getDriverFor(path);
441472
if (driver != null) {
442-
_analyzeFixDataYaml(driver, path);
473+
String? packageName = extractPackageNameFromPath(path);
474+
if (packageName != null) {
475+
var file = resourceProvider.getFile(path);
476+
_analyzeFixDataYaml(driver, file, packageName);
477+
}
443478
}
444479
}
445480
}
@@ -529,11 +564,19 @@ class ContextManagerImpl implements ContextManager {
529564
_analyzeAnalysisOptionsYaml(driver, optionsFile.path);
530565
}
531566

567+
var packageName = rootFolder.shortName;
532568
var fixDataYamlFile = rootFolder
533569
.getChildAssumingFolder('lib')
534570
.getChildAssumingFile(file_paths.fixDataYaml);
535571
if (fixDataYamlFile.exists) {
536-
_analyzeFixDataYaml(driver, fixDataYamlFile.path);
572+
_analyzeFixDataYaml(driver, fixDataYamlFile, packageName);
573+
}
574+
575+
var fixDataFolder = rootFolder
576+
.getChildAssumingFolder('lib')
577+
.getChildAssumingFolder('fix_data');
578+
if (fixDataFolder.exists) {
579+
_analyzeFixDataFolder(driver, fixDataFolder, packageName);
537580
}
538581

539582
var pubspecFile =

pkg/analysis_server/lib/src/lsp/server_capabilities_computer.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ class ServerCapabilitiesComputer {
164164
final analysisOptionsFile = TextDocumentFilterWithScheme(
165165
language: 'yaml', scheme: 'file', pattern: '**/analysis_options.yaml');
166166
final fixDataFile = TextDocumentFilterWithScheme(
167-
language: 'yaml', scheme: 'file', pattern: '**/lib/fix_data.yaml');
167+
language: 'yaml',
168+
scheme: 'file',
169+
pattern: '**/lib/{fix_data.yaml,fix_data/**.yaml}');
168170

169171
ServerCapabilitiesComputer(this._server);
170172
ServerCapabilities computeServerCapabilities(

pkg/analysis_server/test/domain_analysis_test.dart

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,28 @@ class A {}
238238
assertNoErrors(b_path);
239239
}
240240

241+
Future<void> test_fileSystem_addFile_fixDataFolderYaml() async {
242+
var path = '$testPackageLibPath/fix_data/foo.yaml';
243+
244+
newFile('$testPackageLibPath/a.dart', '');
245+
246+
await setRoots(included: [workspaceRootPath], excluded: []);
247+
248+
// No `fix_data.yaml` to analyze yet.
249+
assertNoErrorsNotification(path);
250+
251+
// Create it, will be analyzed.
252+
newFile(path, '0: 1');
253+
await pumpEventQueue();
254+
await server.onAnalysisComplete;
255+
256+
// And it has errors.
257+
assertHasErrors(path);
258+
259+
// We don't recreate analysis contexts.
260+
_assertFlushedResults([]);
261+
}
262+
241263
Future<void> test_fileSystem_addFile_fixDataYaml() async {
242264
var path = '$testPackageLibPath/fix_data.yaml';
243265

@@ -531,6 +553,34 @@ class A {}
531553
assertNoErrors(b_path);
532554
}
533555

556+
Future<void> test_fileSystem_changeFile_fixDataFolderYaml() async {
557+
var path = '$testPackageLibPath/fix_data/foo.yaml';
558+
559+
newFile('$testPackageLibPath/a.dart', '');
560+
561+
// This file has an error.
562+
newFile(path, '0: 1');
563+
564+
await setRoots(included: [workspaceRootPath], excluded: []);
565+
566+
// The file was analyzed.
567+
assertHasErrors(path);
568+
569+
// Replace with the content that does not have errors.
570+
newFile(path, r'''
571+
version: 1
572+
transforms: []
573+
''');
574+
await pumpEventQueue();
575+
await server.onAnalysisComplete;
576+
577+
// And it has errors.
578+
assertNoErrors(path);
579+
580+
// We don't recreate analysis contexts.
581+
_assertFlushedResults([]);
582+
}
583+
534584
Future<void> test_fileSystem_changeFile_fixDataYaml() async {
535585
var path = '$testPackageLibPath/fix_data.yaml';
536586

@@ -544,7 +594,7 @@ class A {}
544594
// The file was analyzed.
545595
assertHasErrors(path);
546596

547-
// Replace with the context that does not have errors.
597+
// Replace with the content that does not have errors.
548598
newFile(path, r'''
549599
version: 1
550600
transforms: []

pkg/analyzer/lib/src/util/file_paths.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const String dotDartTool = '.dart_tool';
3131
/// The name of the data file used to specify data-driven fixes.
3232
const String fixDataYaml = 'fix_data.yaml';
3333

34+
/// The name of the data folder used to specify data-driven fixes.
35+
const String fixDataYamlFolder = 'fix_data';
36+
3437
/// The name of the package config files.
3538
const String packageConfigJson = 'package_config.json';
3639

@@ -65,10 +68,12 @@ bool isDart(p.Context pathContext, String path) {
6568
return pathContext.extension(path) == '.dart';
6669
}
6770

68-
/// Return `true` if the [path] is a `fix_data.yaml` file.
71+
/// Return `true` if the [path] is a `fix_data.yaml` or `fix_data/**.yaml` file.
6972
/// Such files specify data-driven fixes.
7073
bool isFixDataYaml(p.Context pathContext, String path) {
71-
return pathContext.basename(path) == fixDataYaml;
74+
return pathContext.basename(path) == fixDataYaml ||
75+
(pathContext.split(path).contains('fix_data') &&
76+
pathContext.extension(path) == '.yaml');
7277
}
7378

7479
/// Return `true` if the given [path] refers to a file that is assumed to be

0 commit comments

Comments
 (0)