Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for removal of analysis_options_user.yaml from Flutter #1329

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 20 additions & 59 deletions lib/src/analysis_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,92 +8,53 @@ import 'dart:isolate';

import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:path/path.dart' as p;
import 'package:retry/retry.dart';
import 'package:yaml/yaml.dart' as yaml;

final _logger = Logger('analysis_options');

String? _cachedFlutterOptionsOnGithub;
String? _cachedLintsCoreInResolvedReferences;
String? _cachedLintsCoreOptionsOnGithub;

/// Returns the default analysis options (in yaml format).
Future<String> getDefaultAnalysisOptionsYaml({
required bool usesFlutter,
required String? flutterSdkDir,
}) async {
if (usesFlutter) {
return await _getFlutterAnalysisOptions(flutterSdkDir);
} else {
return await _getLintsCoreAnalysisOptions();
}
}

Future<String> _getFlutterAnalysisOptions(String? flutterSdkDir) async {
// try to load local file
flutterSdkDir ??= Platform.environment['FLUTTER_ROOT'];
if (flutterSdkDir != null &&
flutterSdkDir.isNotEmpty &&
await Directory(flutterSdkDir).exists()) {
final file = File(p.join(flutterSdkDir, 'packages', 'flutter', 'lib',
'analysis_options_user.yaml'));
if (await file.exists()) {
return await file.readAsString();
}
}

// try to load latest from github
if (_cachedFlutterOptionsOnGithub != null) {
return _cachedFlutterOptionsOnGithub!;
}
try {
final rs = await _httpGetWithRetry(Uri.parse(
'https://raw.githubusercontent.com/flutter/flutter/master/packages/flutter/lib/analysis_options_user.yaml'));
if (rs.statusCode == 200) {
_cachedFlutterOptionsOnGithub = rs.body;
return _cachedFlutterOptionsOnGithub!;
}
} catch (_) {
// no-op
}

// fallback empty options
_logger.warning('Unable to load default Flutter analysis options.');
return '';
}
/// The default analysis options configuration (in its raw yaml format).
Future<String> getDefaultAnalysisOptionsYaml() async =>
await _getLintsCoreAnalysisOptions();

Future<String> _getLintsCoreAnalysisOptions() async {
// try to load local lints from the resolved package references
if (_cachedLintsCoreInResolvedReferences != null) {
return _cachedLintsCoreInResolvedReferences!;
// Try to load local lints from the resolved package references.
if (_cachedLintsCoreInResolvedReferences case final cachedCoreLints?) {
parlough marked this conversation as resolved.
Show resolved Hide resolved
return cachedCoreLints;
}

try {
final resource =
await Isolate.resolvePackageUri(Uri.parse('package:lints/core.yaml'));
final file = File.fromUri(resource!);
_cachedLintsCoreInResolvedReferences = await file.readAsString();
return _cachedLintsCoreInResolvedReferences!;
final lintConfigAsString = await file.readAsString();
_cachedLintsCoreInResolvedReferences = lintConfigAsString;
return lintConfigAsString;
} on Exception catch (_) {
// no-op
// Gracefully handle exception to fallback to empty options.
}

// try to load latest from github
if (_cachedLintsCoreOptionsOnGithub != null) {
return _cachedLintsCoreOptionsOnGithub!;
// Try to load latest version of the core lints from GitHub.
if (_cachedLintsCoreOptionsOnGithub case final cachedCoreLints?) {
return cachedCoreLints;
}
try {
final rs = await _httpGetWithRetry(Uri.parse(
'https://raw.githubusercontent.com/dart-lang/lints/main/lib/core.yaml'));
if (rs.statusCode == 200 && rs.body.contains('rules:')) {
_cachedLintsCoreOptionsOnGithub = rs.body;
return _cachedLintsCoreOptionsOnGithub!;
final resultBody = rs.body;
_cachedLintsCoreOptionsOnGithub = resultBody;
return resultBody;
}
} on Exception catch (_) {
// no-op
// Gracefully handle exception to fallback to empty options.
}

// fallback empty options
// If we couldn't load the core lints,
// log a warning and return an empty analysis config.
_logger.warning('Unable to load default analysis options.');
return '';
}
Expand Down
5 changes: 1 addition & 4 deletions lib/src/sdk_env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,7 @@ class ToolEnvironment {
if (await analysisOptionsFile.exists()) {
originalOptions = await analysisOptionsFile.readAsString();
}
final rawOptionsContent = await getDefaultAnalysisOptionsYaml(
usesFlutter: usesFlutter,
flutterSdkDir: _flutterSdk._config.rootPath,
);
final rawOptionsContent = await getDefaultAnalysisOptionsYaml();
final customOptionsContent = updatePassthroughOptions(
original: originalOptions, custom: rawOptionsContent);
try {
Expand Down
18 changes: 1 addition & 17 deletions test/analysis_options_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import 'package:yaml/yaml.dart' as yaml;

void main() {
test('default options', () async {
final content = await getDefaultAnalysisOptionsYaml(
usesFlutter: false, flutterSdkDir: null);
final content = await getDefaultAnalysisOptionsYaml();
expect(content, contains('linter:'));
expect(content, contains('rules:'));
expect(content, contains('avoid_empty_else'));
Expand All @@ -23,21 +22,6 @@ void main() {
});
});

test('default Flutter options', () async {
final content = await getDefaultAnalysisOptionsYaml(
usesFlutter: true, flutterSdkDir: null);
expect(json.decode(json.encode(yaml.loadYaml(content))), {
'analyzer': {
'errors': {
'missing_required_param': 'warning',
},
},
'linter': {
'rules': hasLength(greaterThan(10)),
},
});
});

test('passthrough for empty options', () {
final content = updatePassthroughOptions(original: '', custom: '');
expect(json.decode(content), {});
Expand Down