[flutter_tools] do not crash if target file cannot be read for language version (#73879)

This commit is contained in:
Jonah Williams
2021-01-13 14:24:04 -08:00
committed by GitHub
parent 7ec0e39800
commit d4f0c08261
2 changed files with 31 additions and 1 deletions

View File

@@ -24,7 +24,17 @@ final LanguageVersion nullSafeVersion = LanguageVersion(2, 12);
/// https://github.com/dart-lang/language/blob/master/accepted/future-releases/language-versioning/feature-specification.md#individual-library-language-version-override
LanguageVersion determineLanguageVersion(File file, Package package) {
int blockCommentDepth = 0;
for (final String line in file.readAsLinesSync()) {
// If reading the file fails, default to a null-safe version. The
// command will likely fail later in the process with a better error
// message.
List<String> lines;
try {
lines = file.readAsLinesSync();
} on FileSystemException {
return nullSafeVersion;
}
for (final String line in lines) {
final String trimmedLine = line.trim();
if (trimmedLine.isEmpty) {
continue;

View File

@@ -4,8 +4,10 @@
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/dart/language_version.dart';
import 'package:package_config/package_config.dart';
import 'package:test/fake.dart';
import '../../src/common.dart';
@@ -249,4 +251,22 @@ library funstuff;
expect(determineLanguageVersion(file, package), LanguageVersion(2, 7));
});
testWithoutContext('Returns null safe error if reading the file throws a FileSystemException', () {
final Package package = Package(
'foo',
Uri.parse('file://foo/'),
languageVersion: LanguageVersion(2, 7),
);
expect(determineLanguageVersion(FakeFile(), package), nullSafeVersion);
});
}
class FakeFile extends Fake implements File {
@override
List<String> readAsLinesSync({ Encoding encoding = utf8ForTesting }) {
throw const FileSystemException();
}
}