Skip to content

Commit e64bf7a

Browse files
authored
Use restricted analysis_options file also when formatting (#1449)
1 parent 2ca35c2 commit e64bf7a

File tree

2 files changed

+73
-62
lines changed

2 files changed

+73
-62
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.22.19
22

33
- Extended recognized image extensions in URL resolver.
4+
- Preserve formatter:page-width when doing `dart format`.
45

56
## 0.22.18
67

lib/src/sdk_env.dart

Lines changed: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,8 @@ class ToolEnvironment {
172172
return toolEnv;
173173
}
174174

175-
Future<String> runAnalyzer(
176-
String packageDir,
177-
String dir,
178-
bool usesFlutter, {
179-
required InspectOptions inspectOptions,
180-
}) async {
181-
final command =
182-
usesFlutter ? _flutterSdk.dartAnalyzeCmd : _dartSdk.dartAnalyzeCmd;
183-
175+
Future<T> withRestrictedAnalysisOptions<T>(
176+
String packageDir, Future<T> Function() fn) async {
184177
final analysisOptionsFile =
185178
File(p.join(packageDir, 'analysis_options.yaml'));
186179
String? originalOptions;
@@ -192,6 +185,26 @@ class ToolEnvironment {
192185
original: originalOptions, custom: rawOptionsContent);
193186
try {
194187
await analysisOptionsFile.writeAsString(customOptionsContent);
188+
return await fn();
189+
} finally {
190+
if (originalOptions == null) {
191+
await analysisOptionsFile.delete();
192+
} else {
193+
await analysisOptionsFile.writeAsString(originalOptions);
194+
}
195+
}
196+
}
197+
198+
Future<String> runAnalyzer(
199+
String packageDir,
200+
String dir,
201+
bool usesFlutter, {
202+
required InspectOptions inspectOptions,
203+
}) async {
204+
final command =
205+
usesFlutter ? _flutterSdk.dartAnalyzeCmd : _dartSdk.dartAnalyzeCmd;
206+
207+
return await withRestrictedAnalysisOptions(packageDir, () async {
195208
final proc = await runConstrained(
196209
[...command, '--format', 'machine', dir],
197210
environment:
@@ -218,13 +231,7 @@ class ToolEnvironment {
218231
throw ToolException('dart analyze exception: $errorMessage', proc);
219232
}
220233
return output;
221-
} finally {
222-
if (originalOptions == null) {
223-
await analysisOptionsFile.delete();
224-
} else {
225-
await analysisOptionsFile.writeAsString(originalOptions);
226-
}
227-
}
234+
});
228235
}
229236

230237
Future<List<String>> filesNeedingFormat(String packageDir, bool usesFlutter,
@@ -233,56 +240,59 @@ class ToolEnvironment {
233240
if (dirs.isEmpty) {
234241
return const [];
235242
}
236-
final files = <String>{};
237-
for (final dir in dirs) {
238-
final fullPath = p.join(packageDir, dir);
239-
240-
final params = <String>[
241-
'format',
242-
'--output=none',
243-
'--set-exit-if-changed',
244-
];
245-
if (lineLength != null && lineLength > 0) {
246-
params.addAll(<String>['--line-length', lineLength.toString()]);
247-
}
248-
params.add(fullPath);
243+
return withRestrictedAnalysisOptions(packageDir, () async {
244+
final files = <String>{};
245+
for (final dir in dirs) {
246+
final fullPath = p.join(packageDir, dir);
247+
248+
final params = <String>[
249+
'format',
250+
'--output=none',
251+
'--set-exit-if-changed',
252+
];
253+
if (lineLength != null && lineLength > 0) {
254+
params.addAll(<String>['--line-length', lineLength.toString()]);
255+
}
256+
params.add(fullPath);
249257

250-
final result = await runConstrained(
251-
[..._dartSdk.dartCmd, ...params],
252-
environment:
253-
usesFlutter ? _flutterSdk.environment : _dartSdk.environment,
254-
timeout: _dartFormatTimeout,
255-
);
256-
if (result.exitCode == 0) {
257-
continue;
258-
}
258+
final result = await runConstrained(
259+
[..._dartSdk.dartCmd, ...params],
260+
environment:
261+
usesFlutter ? _flutterSdk.environment : _dartSdk.environment,
262+
timeout: _dartFormatTimeout,
263+
);
264+
if (result.exitCode == 0) {
265+
continue;
266+
}
259267

260-
final dirPrefix = packageDir.endsWith('/') ? packageDir : '$packageDir/';
261-
final output = result.asJoinedOutput;
262-
final lines = LineSplitter.split(result.asJoinedOutput)
263-
.where((l) => l.startsWith('Changed'))
264-
.map((l) => l.substring(8).replaceAll(dirPrefix, '').trim())
265-
.toList();
266-
267-
// `dart format` exits with code = 1
268-
if (result.exitCode == 1) {
269-
assert(lines.isNotEmpty);
270-
files.addAll(lines);
271-
continue;
272-
}
268+
final dirPrefix =
269+
packageDir.endsWith('/') ? packageDir : '$packageDir/';
270+
final output = result.asJoinedOutput;
271+
final lines = LineSplitter.split(result.asJoinedOutput)
272+
.where((l) => l.startsWith('Changed'))
273+
.map((l) => l.substring(8).replaceAll(dirPrefix, '').trim())
274+
.toList();
275+
276+
// `dart format` exits with code = 1
277+
if (result.exitCode == 1) {
278+
assert(lines.isNotEmpty);
279+
files.addAll(lines);
280+
continue;
281+
}
273282

274-
final errorMsg = LineSplitter.split(output).take(10).join('\n');
275-
final isUserProblem = output.contains(
276-
'Could not format because the source could not be parsed') ||
277-
output.contains('The formatter produced unexpected output.');
278-
if (!isUserProblem) {
279-
throw Exception(
280-
'dart format on $dir/ failed with exit code ${result.exitCode}\n$output',
281-
);
283+
final errorMsg = LineSplitter.split(output).take(10).join('\n');
284+
final isUserProblem = output.contains(
285+
'Could not format because the source could not be parsed') ||
286+
output.contains('The formatter produced unexpected output.');
287+
if (!isUserProblem) {
288+
throw Exception(
289+
'dart format on $dir/ failed with exit code ${result.exitCode}\n$output',
290+
);
291+
}
292+
throw ToolException(errorMsg, result);
282293
}
283-
throw ToolException(errorMsg, result);
284-
}
285-
return files.toList()..sort();
294+
return files.toList()..sort();
295+
});
286296
}
287297

288298
Future<Map<String, dynamic>> _getFlutterVersion() async {

0 commit comments

Comments
 (0)