Skip to content

Commit 193db9b

Browse files
natebiggsCommit Queue
authored and
Commit Queue
committed
[dart2js] More lenient source code handling for diagnostic reporting.
Should avoid exceptions as thrown in flutter/flutter#138326 Only register source code if there is content in the byte array so we know when we should try to read from file. And when trying to read contents of a file for diagnostics, if we fail to read the file then fail gracefully by using position information. Change-Id: Ic7e2bdcba0b093a48735aa26670509354f953861 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/336480 Reviewed-by: Mayank Patke <[email protected]> Commit-Queue: Nate Biggs <[email protected]>
1 parent e6b02b5 commit 193db9b

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

pkg/compiler/lib/src/source_file_provider.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ abstract class SourceFileProvider implements api.CompilerInput {
6363
}
6464

6565
registerUri(resourceUri);
66-
if (!disableByteCache) {
66+
67+
// Source bytes can be empty when the dill has source content erased. In
68+
// that case we should read the file contents from disk if we need them.
69+
if (!disableByteCache && source.isNotEmpty) {
6770
_byteCache[resourceUri] = source;
6871
}
6972
sourceBytesFromDill += source.length;
@@ -95,6 +98,15 @@ abstract class SourceFileProvider implements api.CompilerInput {
9598
return _sourceToFile(Uri.parse(relativizeUri(uri)), source, inputKind);
9699
}
97100

101+
api.Input<List<int>>? _readFromFileSyncOrNull(
102+
Uri uri, api.InputKind inputKind) {
103+
try {
104+
return _readFromFileSync(uri, inputKind);
105+
} catch (_) {
106+
return null;
107+
}
108+
}
109+
98110
/// Read [resourceUri] directly as a UTF-8 file. If reading fails, `null` is
99111
/// returned.
100112
api.Input<List<int>>? readUtf8FromFileSyncForTesting(Uri resourceUri) {
@@ -129,7 +141,7 @@ abstract class SourceFileProvider implements api.CompilerInput {
129141
resourceUri, _byteCache[resourceUri]!, api.InputKind.UTF8);
130142
}
131143
return resourceUri.isScheme('file')
132-
? _readFromFileSync(resourceUri, api.InputKind.UTF8)
144+
? _readFromFileSyncOrNull(resourceUri, api.InputKind.UTF8)
133145
: null;
134146
}
135147

0 commit comments

Comments
 (0)