mirror of
https://github.com/dart-lang/sdk
synced 2024-10-04 19:49:47 +00:00
[analyzer]/[analysis_server] Handle parse errors in analysis_options.yaml better + tweak unhandled exception text
Fixes the main issues at https://github.com/dart-lang/sdk/issues/55987 Change-Id: I5b86f7c0df4017c02b96e67d8d8d03b71933b318 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/371861 Reviewed-by: Sam Rawlins <srawlins@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
parent
3a91bcb39e
commit
d4c2b91a14
|
@ -135,6 +135,26 @@ include: package:pedantic/analysis_options.yaml
|
|||
// expect(updatedDiagnostics, hasLength(0));
|
||||
}
|
||||
|
||||
/// Ensure the server can initialize correctly and send diagnostics when the
|
||||
/// analysis_options file throws errors during parsing.
|
||||
///
|
||||
/// https://github.com/dart-lang/sdk/issues/55987
|
||||
Future<void> test_analysisOptionsFile_parseError() async {
|
||||
newFile(analysisOptionsPath, '''
|
||||
include: package:lints/recommended.yaml
|
||||
f
|
||||
|
||||
''');
|
||||
|
||||
var firstDiagnosticsUpdate = waitForDiagnostics(analysisOptionsUri);
|
||||
await initialize();
|
||||
var initialDiagnostics = await firstDiagnosticsUpdate;
|
||||
var diagnostic = initialDiagnostics!.first;
|
||||
expect(diagnostic.severity, DiagnosticSeverity.Error);
|
||||
expect(diagnostic.code, 'parse_error');
|
||||
expect(diagnostic.message, "Expected ':'.");
|
||||
}
|
||||
|
||||
Future<void> test_contextMessage() async {
|
||||
newFile(mainFilePath, '''
|
||||
void f() {
|
||||
|
|
|
@ -24,7 +24,8 @@ class AnalysisOptionsProvider {
|
|||
/// [root]/[file_paths.analysisOptionsYaml].
|
||||
/// Recursively merge options referenced by an include directive
|
||||
/// and remove the include directive from the resulting options map.
|
||||
/// Return an empty options map if the file does not exist.
|
||||
/// Return an empty options map if the file does not exist or cannot be
|
||||
/// parsed.
|
||||
YamlMap getOptions(Folder root) {
|
||||
File? optionsFile = getOptionsFile(root);
|
||||
if (optionsFile == null) {
|
||||
|
@ -51,7 +52,8 @@ class AnalysisOptionsProvider {
|
|||
/// Provide the options found in [file].
|
||||
/// Recursively merge options referenced by an include directive
|
||||
/// and remove the include directive from the resulting options map.
|
||||
/// Return an empty options map if the file does not exist.
|
||||
/// Return an empty options map if the file does not exist or cannot be
|
||||
/// parsed.
|
||||
YamlMap getOptionsFromFile(File file) {
|
||||
return getOptionsFromSource(FileSource(file));
|
||||
}
|
||||
|
@ -60,21 +62,25 @@ class AnalysisOptionsProvider {
|
|||
///
|
||||
/// Recursively merge options referenced by an `include` directive and remove
|
||||
/// the `include` directive from the resulting options map. Return an empty
|
||||
/// options map if the file does not exist.
|
||||
/// options map if the file does not exist or cannot be parsed.
|
||||
YamlMap getOptionsFromSource(Source source) {
|
||||
YamlMap options = getOptionsFromString(_readAnalysisOptions(source));
|
||||
var node = options.valueAt(AnalyzerOptions.include);
|
||||
var sourceFactory = this.sourceFactory;
|
||||
if (sourceFactory != null && node is YamlScalar) {
|
||||
var path = node.value;
|
||||
if (path is String) {
|
||||
var parent = sourceFactory.resolveUri(source, path);
|
||||
if (parent != null) {
|
||||
options = merge(getOptionsFromSource(parent), options);
|
||||
try {
|
||||
YamlMap options = getOptionsFromString(_readAnalysisOptions(source));
|
||||
var node = options.valueAt(AnalyzerOptions.include);
|
||||
var sourceFactory = this.sourceFactory;
|
||||
if (sourceFactory != null && node is YamlScalar) {
|
||||
var path = node.value;
|
||||
if (path is String) {
|
||||
var parent = sourceFactory.resolveUri(source, path);
|
||||
if (parent != null) {
|
||||
options = merge(getOptionsFromSource(parent), options);
|
||||
}
|
||||
}
|
||||
}
|
||||
return options;
|
||||
} on OptionsFormatException {
|
||||
return YamlMap();
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
/// Provide the options found in [content].
|
||||
|
|
|
@ -295,8 +295,12 @@ class AnalysisServer {
|
|||
void _handleServerError(Map<String, dynamic>? error) {
|
||||
_serverErrorReceived = true;
|
||||
final err = error!;
|
||||
log.stderr('An unexpected error was encountered by the Analysis Server.');
|
||||
log.stderr('Please file an issue at '
|
||||
'https://github.com/dart-lang/sdk/issues/new/choose with the following '
|
||||
'details:\n');
|
||||
// Fields are 'isFatal', 'message', and 'stackTrace'.
|
||||
log.stderr('Error from the analysis server: ${err['message']}');
|
||||
log.stderr(err['message']);
|
||||
if (err['stackTrace'] != null) {
|
||||
log.stderr(err['stackTrace'] as String);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue