More analyzer workarounds (#959)

This commit is contained in:
Simon Binder 2020-12-07 21:18:27 +01:00
parent cf8215ecfe
commit 47b8989318
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 29 additions and 14 deletions

View File

@ -78,26 +78,39 @@ class BuildBackendTask extends BackendTask {
} else {
_currentAnalysisSession ??= element.session;
// Transform element to new session if necessary, also updating the
// session if it was invalidated by another builder
var library = element.library;
if (library.session != _currentAnalysisSession) {
for (var retries = 0; retries < _maxSessionRetries; retries++) {
try {
library = await _currentAnalysisSession
.getLibraryByUri(assetId.uri.toString());
} on InconsistentAnalysisException {
library = await step.resolver.libraryFor(assetId);
_currentAnalysisSession = library.session;
}
}
final library =
await _libraryInCurrentSession(element.library, assetId);
final result =
await _currentAnalysisSession.getResolvedLibraryByElement(library);
final result = await _currentAnalysisSession
.getResolvedLibraryByElement(library);
_cachedResults[assetId] = result;
// Note: getElementDeclaration works by comparing source offsets, so
// element.session != session is not a problem in this case.
return result.getElementDeclaration(element);
} on InconsistentAnalysisException {
final isLastTry = retries == _maxSessionRetries - 1;
if (isLastTry) rethrow;
}
}
}
}
Future<LibraryElement> _libraryInCurrentSession(
LibraryElement library, AssetId definingAsset) async {
if (library.session == _currentAnalysisSession) return library;
try {
// This is safe: If this build step knows the library, it has already read
// the originating asset. We can bypass the build asset reader!
return await _currentAnalysisSession
.getLibraryByUri(definingAsset.uri.toString());
} on InconsistentAnalysisException {
final library = await step.resolver.libraryFor(definingAsset);
_currentAnalysisSession = library.session;
return library;
}
}
@ -127,4 +140,6 @@ class BuildBackendTask extends BackendTask {
return typeDeserializer
.deserialize(SerializedType.fromJson(serializedType));
}
static const int _maxSessionRetries = 5;
}