Skip to content

Commit 39ca9f5

Browse files
scheglovCommit Queue
authored andcommitted
Fine. Update LinkedElementFactory.libraryOfUri() to return null for missing library.
We use this method to check requirements, and it is possible that some libraries are not available anymore. Specifically, this happens when conditional imports used, and environment variables changed. There are no new tests, because test failures happened with existing summary tests when `withFineDependencies` is `true`. Change-Id: I460948a759be7ed8c5b36f76b61ab4e1d1522097 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/444520 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Johnni Winther <[email protected]>
1 parent 15805da commit 39ca9f5

File tree

1 file changed

+43
-37
lines changed

1 file changed

+43
-37
lines changed

pkg/analyzer/lib/src/summary2/linked_element_factory.dart

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -96,37 +96,6 @@ class LinkedElementFactory {
9696
return Namespace(exportedNames);
9797
}
9898

99-
LibraryElementImpl createLibraryElementForReading(Uri uri) {
100-
var sourceFactory = analysisContext.sourceFactory;
101-
var librarySource = sourceFactory.forUri2(uri)!;
102-
103-
var reader = _libraryReaders[uri];
104-
if (reader == null) {
105-
var rootChildren = rootReference.children.map((e) => e.name).toList();
106-
if (rootChildren.length > 50) {
107-
rootChildren = [
108-
...rootChildren.take(50),
109-
'... (${rootChildren.length} total)',
110-
];
111-
}
112-
var readers = _libraryReaders.keys.map((uri) => uri.toString()).toList();
113-
if (readers.length > 50) {
114-
readers = [...readers.take(50), '... (${readers.length} total)'];
115-
}
116-
throw ArgumentError(
117-
'Missing library: $uri\n'
118-
'Libraries: $uriListWithLibraryElements\n'
119-
'Root children: $rootChildren\n'
120-
'Readers: $readers\n'
121-
'Log: ${_logRing.join('\n')}\n',
122-
);
123-
}
124-
125-
var libraryElement = reader.readElement(librarySource: librarySource);
126-
setLibraryTypeSystem(libraryElement);
127-
return libraryElement;
128-
}
129-
13099
void createTypeProviders(
131100
LibraryElementImpl dartCore,
132101
LibraryElementImpl dartAsync,
@@ -165,7 +134,9 @@ class LinkedElementFactory {
165134

166135
if (reference.isLibrary) {
167136
var uri = uriCache.parse(reference.name);
168-
return createLibraryElementForReading(uri);
137+
var result = _createLibraryElementForReading(uri);
138+
result ?? _reportMissingLibrary(uri);
139+
return result;
169140
}
170141

171142
var parentRef = reference.parentNotContainer;
@@ -191,15 +162,12 @@ class LinkedElementFactory {
191162
if (reference.element case LibraryElementImpl element) {
192163
return element;
193164
}
194-
return createLibraryElementForReading(uri);
165+
return _createLibraryElementForReading(uri);
195166
}
196167

197168
LibraryElementImpl libraryOfUri2(Uri uri) {
198169
var element = libraryOfUri(uri);
199-
if (element == null) {
200-
libraryOfUri(uri);
201-
throw StateError('No library: $uri');
202-
}
170+
element ?? _reportMissingLibrary(uri);
203171
return element;
204172
}
205173

@@ -269,5 +237,43 @@ class LinkedElementFactory {
269237
libraryElement.hasTypeProviderSystemSet = true;
270238
}
271239

240+
LibraryElementImpl? _createLibraryElementForReading(Uri uri) {
241+
var sourceFactory = analysisContext.sourceFactory;
242+
var librarySource = sourceFactory.forUri2(uri);
243+
if (librarySource == null) {
244+
return null;
245+
}
246+
247+
var reader = _libraryReaders[uri];
248+
if (reader == null) {
249+
return null;
250+
}
251+
252+
var libraryElement = reader.readElement(librarySource: librarySource);
253+
setLibraryTypeSystem(libraryElement);
254+
return libraryElement;
255+
}
256+
272257
void _disposeLibrary(ElementImpl? libraryElement) {}
258+
259+
Never _reportMissingLibrary(Uri uri) {
260+
var rootChildren = rootReference.children.map((e) => e.name).toList();
261+
if (rootChildren.length > 50) {
262+
rootChildren = [
263+
...rootChildren.take(50),
264+
'... (${rootChildren.length} total)',
265+
];
266+
}
267+
var readers = _libraryReaders.keys.map((uri) => uri.toString()).toList();
268+
if (readers.length > 50) {
269+
readers = [...readers.take(50), '... (${readers.length} total)'];
270+
}
271+
throw ArgumentError(
272+
'Missing library: $uri\n'
273+
'Libraries: $uriListWithLibraryElements\n'
274+
'Root children: $rootChildren\n'
275+
'Readers: $readers\n'
276+
'Log: ${_logRing.join('\n')}\n',
277+
);
278+
}
273279
}

0 commit comments

Comments
 (0)