Skip to content

Commit aaba96a

Browse files
jensjohaCommit Queue
authored andcommitted
[analyzer] getLibraryByUri returns early again
Back in 2023 an early return was added to `getLibraryByUri` (https://dart-review.googlesource.com/c/sdk/+/304540). This was then migrated in January (https://dart-review.googlesource.com/c/sdk/+/403705), which unfortunately left the early return in a non-working state. This CL sets libraryElement on `Reference.element2` too, which makes the early return in `getLibraryByUri` work again and adds a test so any future regression will hopefully be caught sooner. On most of the getFixes requests I test in http://b/407797012 this seems to be a 2x-3x improvement. Change-Id: I91f2122b9ade101df78428ddb3ed5c64ad29134f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421220 Commit-Queue: Jens Johansen <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 50a3bf6 commit aaba96a

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ class LibraryReader {
633633
_libraryElement = LibraryElementImpl(
634634
analysisContext, analysisSession, name, -1, 0, featureSet);
635635
_reference.element = _libraryElement;
636+
_reference.element2 = _libraryElement;
636637
_libraryElement.reference = _reference;
637638

638639
// Read the rest of non-resolution data for the library.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ class LibraryBuilder {
806806
libraryElement.isSynthetic = !libraryFile.exists;
807807
libraryElement.languageVersion = libraryUnitNode.languageVersion;
808808
_bindReference(libraryReference, libraryElement);
809+
libraryReference.element2 = libraryElement;
809810

810811
var unitContainerRef = libraryReference.getChild('@fragment');
811812

pkg/analyzer/test/src/dart/analysis/driver_test.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1950,6 +1950,51 @@ part of 'b.dart';
19501950
''');
19511951
}
19521952

1953+
test_getLibraryByUri_subsequentCallsDoesNoWork() async {
1954+
var aUriStr = 'package:test/a.dart';
1955+
var bUriStr = 'package:test/b.dart';
1956+
1957+
newFile('$testPackageLibPath/a.dart', r'''
1958+
part 'b.dart';
1959+
1960+
class A {}
1961+
''');
1962+
1963+
newFile('$testPackageLibPath/b.dart', r'''
1964+
part of 'a.dart';
1965+
1966+
class B {}
1967+
''');
1968+
1969+
for (var run = 0; run < 5; run++) {
1970+
var driver = driverFor(testFile);
1971+
var collector = DriverEventCollector(driver);
1972+
1973+
var result = await driver.getLibraryByUri(aUriStr);
1974+
result as LibraryElementResult;
1975+
expect(result.element2.getClass2('A'), isNotNull);
1976+
expect(result.element2.getClass2('B'), isNotNull);
1977+
1978+
// It is an error to ask for a library when we know that it is a part.
1979+
expect(
1980+
await driver.getLibraryByUri(bUriStr),
1981+
isA<NotLibraryButPartResult>(),
1982+
);
1983+
1984+
if (run == 0) {
1985+
// First `getLibraryByUri` call does actual work.
1986+
await assertEventsText(collector, r'''
1987+
[status] working
1988+
[status] idle
1989+
''');
1990+
} else {
1991+
// Subsequent `getLibraryByUri` just grabs the result via rootReference
1992+
// and thus does no actual work.
1993+
await assertEventsText(collector, '');
1994+
}
1995+
}
1996+
}
1997+
19531998
test_getLibraryByUri_unresolvedUri() async {
19541999
var driver = driverFor(testFile);
19552000
var collector = DriverEventCollector(driver);

0 commit comments

Comments
 (0)