Skip to content

Commit 93b1fcb

Browse files
[beta] [flutter_tools] Resolve includes recursively in AnalysisOptionsMigration (#191531)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: #191056 ### Impact Description: `AnalysisOptionsMigration` ignored `include:` directives in `analysis_options.yaml` files, causing `flutter pub get` and build commands to repeatedly rewrite `analysis_options.yaml` files that already inherited the required analyzer `exclude:` patterns (such as `build/**`, `android/**`, `ios/**`) via relative or `package:` includes. ### Changelog Description: [flutter/191056] Recursively resolve includes in AnalysisOptionsMigration to prevent redundant rewrites of analysis_options.yaml. ### Workaround: Explicitly duplicate exclude patterns in the top-level `analysis_options.yaml` file instead of relying on included configs. ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: 1. Create a Flutter project whose `analysis_options.yaml` includes another options file defining platform exclusions. 2. Run `flutter pub get`. 3. Confirm that `analysis_options.yaml` is not unnecessarily modified. 4. Run `packages/flutter_tools/test/general.shard/analysis_options_migration_test.dart`.
1 parent 3816f7b commit 93b1fcb

3 files changed

Lines changed: 375 additions & 24 deletions

File tree

‎packages/flutter_tools/lib/src/migrations/analysis_options_migration.dart‎

Lines changed: 126 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:package_config/package_config.dart';
56
import 'package:yaml/yaml.dart';
67
import 'package:yaml_edit/yaml_edit.dart';
78

89
import '../base/file_system.dart';
910
import '../base/project_migrator.dart';
11+
import '../dart/package_map.dart';
1012
import '../project.dart';
1113

1214
/// Migrates analysis_options.yaml to exclude build and platform directories.
1315
class AnalysisOptionsMigration extends ProjectMigrator {
14-
AnalysisOptionsMigration(FlutterProject project, super.logger)
15-
: _analysisOptionsFile = project.directory.childFile('analysis_options.yaml');
16+
AnalysisOptionsMigration(FlutterProject project, super.logger, {PackageConfig? packageConfig})
17+
: _project = project,
18+
_analysisOptionsFile = project.directory.childFile('analysis_options.yaml'),
19+
_packageConfig = packageConfig;
1620

21+
final FlutterProject _project;
1722
final File _analysisOptionsFile;
23+
final PackageConfig? _packageConfig;
24+
PackageConfig? _loadedPackageConfig;
1825

1926
@override
2027
Future<void> migrate() async {
@@ -46,25 +53,15 @@ class AnalysisOptionsMigration extends ProjectMigrator {
4653
'linux/**',
4754
];
4855

49-
var needsMigration = false;
50-
final Object? analyzer = root['analyzer'];
51-
if (analyzer is! YamlMap) {
52-
needsMigration = true;
53-
} else {
54-
final exclude = analyzer['exclude'] as Object?;
55-
if (exclude is! YamlList) {
56-
needsMigration = true;
57-
} else {
58-
for (final requiredExclude in excludesToExclude) {
59-
if (!exclude.contains(requiredExclude)) {
60-
needsMigration = true;
61-
break;
62-
}
63-
}
56+
final Set<String> activeExcludes = await _collectExcludes(_analysisOptionsFile);
57+
final missingExcludes = <String>[];
58+
for (final requiredExclude in excludesToExclude) {
59+
if (!activeExcludes.contains(requiredExclude)) {
60+
missingExcludes.add(requiredExclude);
6461
}
6562
}
6663

67-
if (!needsMigration) {
64+
if (missingExcludes.isEmpty) {
6865
return;
6966
}
7067

@@ -73,18 +70,19 @@ class AnalysisOptionsMigration extends ProjectMigrator {
7370
);
7471

7572
final editor = YamlEditor(originalContent);
73+
final Object? analyzer = root['analyzer'];
7674

7775
try {
7876
if (analyzer is! YamlMap) {
79-
editor.update(<String>['analyzer'], <String, Object>{'exclude': excludesToExclude});
77+
editor.update(<String>['analyzer'], <String, Object>{'exclude': missingExcludes});
8078
} else {
8179
final exclude = analyzer['exclude'] as Object?;
8280
if (exclude is! YamlList) {
83-
editor.update(<String>['analyzer', 'exclude'], excludesToExclude);
81+
editor.update(<String>['analyzer', 'exclude'], missingExcludes);
8482
} else {
85-
for (final requiredExclude in excludesToExclude) {
86-
if (!exclude.contains(requiredExclude)) {
87-
editor.appendToList(<String>['analyzer', 'exclude'], requiredExclude);
83+
for (final missingExclude in missingExcludes) {
84+
if (!exclude.contains(missingExclude)) {
85+
editor.appendToList(<String>['analyzer', 'exclude'], missingExclude);
8886
}
8987
}
9088
}
@@ -94,4 +92,109 @@ class AnalysisOptionsMigration extends ProjectMigrator {
9492
logger.printError('Failed to migrate analysis_options.yaml: $e');
9593
}
9694
}
95+
96+
/// Recursively collects analyzer `exclude:` patterns from [file] and any
97+
/// configuration files referenced via `include:` directives.
98+
///
99+
/// Supports both relative paths and `package:` URIs, handling single string
100+
/// or list-based include directives while tracking [visited] canonical paths
101+
/// to prevent infinite recursion on cyclic includes.
102+
Future<Set<String>> _collectExcludes(File file, {Set<String> visited = const <String>{}}) async {
103+
final String canonicalPath = file.fileSystem.path.canonicalize(file.path);
104+
if (visited.contains(canonicalPath)) {
105+
return <String>{}; // Avoid cycles
106+
}
107+
if (!file.existsSync()) {
108+
return <String>{};
109+
}
110+
final String content;
111+
try {
112+
content = file.readAsStringSync();
113+
} on FileSystemException {
114+
return <String>{};
115+
}
116+
final YamlNode root;
117+
try {
118+
root = loadYamlNode(content);
119+
} on YamlException {
120+
return <String>{};
121+
}
122+
if (root is! YamlMap) {
123+
return <String>{};
124+
}
125+
126+
final excludes = <String>{};
127+
final Object? analyzer = root['analyzer'];
128+
if (analyzer is YamlMap) {
129+
final Object? exclude = analyzer['exclude'];
130+
if (exclude is YamlList) {
131+
excludes.addAll(exclude.whereType<String>());
132+
}
133+
}
134+
135+
final Object? include = root['include'];
136+
final includes = <String>[];
137+
if (include is String) {
138+
includes.add(include);
139+
} else if (include is YamlList) {
140+
includes.addAll(include.whereType<String>());
141+
}
142+
143+
for (final includeUri in includes) {
144+
final File? includedFile = await _resolveInclude(includeUri, file);
145+
if (includedFile != null) {
146+
excludes.addAll(
147+
await _collectExcludes(includedFile, visited: <String>{...visited, canonicalPath}),
148+
);
149+
}
150+
}
151+
152+
return excludes;
153+
}
154+
155+
Future<File?> _resolveInclude(String includeUri, File referringFile) async {
156+
if (includeUri.startsWith('package:')) {
157+
final PackageConfig? packageConfig = await _getPackageConfig();
158+
if (packageConfig == null) {
159+
return null;
160+
}
161+
try {
162+
final Uri? resolvedUri = packageConfig.resolve(Uri.parse(includeUri));
163+
if (resolvedUri != null && resolvedUri.scheme == 'file') {
164+
return referringFile.fileSystem.file(resolvedUri);
165+
}
166+
} on FormatException {
167+
// Invalid URI
168+
}
169+
return null;
170+
} else {
171+
// Relative path
172+
final String path = referringFile.fileSystem.path.join(referringFile.parent.path, includeUri);
173+
return referringFile.fileSystem.file(path);
174+
}
175+
}
176+
177+
Future<PackageConfig?> _getPackageConfig() async {
178+
if (_packageConfig != null) {
179+
return _packageConfig;
180+
}
181+
if (_loadedPackageConfig != null) {
182+
return _loadedPackageConfig;
183+
}
184+
final File? configFile = findPackageConfigFile(_project.directory);
185+
if (configFile == null) {
186+
return null;
187+
}
188+
try {
189+
_loadedPackageConfig = await loadPackageConfigWithLogging(
190+
configFile,
191+
logger: logger,
192+
throwOnError: false,
193+
);
194+
return _loadedPackageConfig;
195+
} on Exception {
196+
// Ignore errors loading package config during migration checks.
197+
return null;
198+
}
199+
}
97200
}

‎packages/flutter_tools/lib/src/project.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ class FlutterProject {
426426
}
427427

428428
final migration = ProjectMigration(<ProjectMigrator>[
429-
AnalysisOptionsMigration(this, globals.logger),
429+
AnalysisOptionsMigration(this, globals.logger, packageConfig: packageConfig),
430430
]);
431431
await migration.run();
432432

0 commit comments

Comments
 (0)