Skip to content

Commit 9ad4ce8

Browse files
authored
Remove computeNode and use new analyzer interfaces for library retrieval (#1851)
* asynchronous PackageGraph construction and precaching * checkpoint * cleanup * checkpoint * Run tools more asynchronously * Enhacements and dropping of Future.wait * dartfmt * Rate-limit the number of tools in flight * checkpoint * Workaround for duplicate ResovledLibraryResult objects * Patch up merge problem and rebuild test package docs * Use maps to avoid having to filter results * dartfmt and review comments * Update test package docs again (quotation marks different in stable branch) * Clear pubspec overrides (this will only work in sdk-analyzer Travis) * Change sdk test branch to master (analyzer-0.33 not integrated yet) * Set correct analyzer version * Eliminate initialization race where analyzer might not find all the files in time for empty packages * Update analyzer requirement * Fix merge error
1 parent ac1cab5 commit 9ad4ce8

File tree

4 files changed

+88
-64
lines changed

4 files changed

+88
-64
lines changed

lib/src/model.dart

Lines changed: 85 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import 'dart:collection' show UnmodifiableListView;
1010
import 'dart:convert';
1111
import 'dart:io';
1212

13+
import 'package:analyzer/dart/analysis/results.dart';
14+
import 'package:analyzer/dart/analysis/session.dart';
1315
import 'package:analyzer/dart/ast/ast.dart'
1416
show
1517
AnnotatedNode,
@@ -437,7 +439,7 @@ class Accessor extends ModelElement implements EnclosedElement {
437439
if (_sourceCode == null) {
438440
if (isSynthetic) {
439441
_sourceCode = packageGraph
440-
._getModelNodeFor((element as PropertyAccessorElement).variable)
442+
._getModelNodeFor((element as PropertyAccessorElement).variable, this.definingLibrary)
441443
.sourceCode;
442444
} else {
443445
_sourceCode = super.sourceCode;
@@ -495,8 +497,8 @@ class Accessor extends ModelElement implements EnclosedElement {
495497
@override
496498
ModelElement get enclosingElement {
497499
if (_accessor.enclosingElement is CompilationUnitElement) {
498-
return packageGraph
499-
.findOrCreateLibraryFor(_accessor.enclosingElement.enclosingElement);
500+
return packageGraph.findButDoNotCreateLibraryFor(
501+
_accessor.enclosingElement.enclosingElement);
500502
}
501503

502504
return new ModelElement.from(
@@ -2059,16 +2061,17 @@ abstract class GetterSetterCombo implements ModelElement {
20592061
}
20602062

20612063
class Library extends ModelElement with Categorization, TopLevelContainer {
2064+
final ResolvedLibraryResult libraryResult;
20622065
List<TopLevelVariable> _variables;
20632066
Namespace _exportedNamespace;
20642067
String _name;
20652068

20662069
factory Library(LibraryElement element, PackageGraph packageGraph) {
2067-
return packageGraph.findOrCreateLibraryFor(element);
2070+
return packageGraph.findButDoNotCreateLibraryFor(element);
20682071
}
20692072

2070-
Library._(LibraryElement element, PackageGraph packageGraph, this._package)
2071-
: super(element, null, packageGraph, null) {
2073+
Library._(this.libraryResult, PackageGraph packageGraph, this._package)
2074+
: super(libraryResult.element, null, packageGraph, null) {
20722075
if (element == null) throw new ArgumentError.notNull('element');
20732076
_exportedNamespace =
20742077
new NamespaceBuilder().createExportNamespaceForLibrary(element);
@@ -2107,9 +2110,12 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
21072110
new ModelElement.fromElement(e.setter.element, packageGraph);
21082111
}
21092112
}
2110-
return new ModelElement.from(e.element,
2111-
packageGraph.findOrCreateLibraryFor(e.element), packageGraph,
2112-
getter: getter, setter: setter)
2113+
return new ModelElement.from(
2114+
e.element,
2115+
packageGraph.findButDoNotCreateLibraryFor(e.element),
2116+
packageGraph,
2117+
getter: getter,
2118+
setter: setter)
21132119
.fullyQualifiedName;
21142120
}).toList();
21152121
}
@@ -2894,7 +2900,7 @@ abstract class ModelElement extends Canonicalization
28942900
this._element, this._library, this._packageGraph, this._originalMember) {}
28952901

28962902
factory ModelElement.fromElement(Element e, PackageGraph p) {
2897-
Library lib = p.findOrCreateLibraryFor(e);
2903+
Library lib = p.findButDoNotCreateLibraryFor(e);
28982904
Accessor getter;
28992905
Accessor setter;
29002906
if (e is PropertyInducingElement) {
@@ -3099,7 +3105,7 @@ abstract class ModelElement extends Canonicalization
30993105
ModelNode _modelNode;
31003106
@override
31013107
ModelNode get modelNode =>
3102-
_modelNode ??= packageGraph._getModelNodeFor(element);
3108+
_modelNode ??= packageGraph._getModelNodeFor(element, definingLibrary);
31033109

31043110
List<String> get annotations => annotationsFromMetadata(element.metadata);
31053111

@@ -3344,7 +3350,8 @@ abstract class ModelElement extends Canonicalization
33443350
documentationFrom.map((e) => e.documentationLocal).join('<p>'));
33453351
}
33463352

3347-
Library get definingLibrary => packageGraph.findOrCreateLibraryFor(element);
3353+
Library get definingLibrary =>
3354+
packageGraph.findButDoNotCreateLibraryFor(element);
33483355

33493356
Library _canonicalLibrary;
33503357
// _canonicalLibrary can be null so we can't check against null to see whether
@@ -4651,31 +4658,28 @@ class PackageGraph {
46514658
// to this graph.
46524659

46534660
PackageGraph._(this.config, this.packageMeta, this._packageWarningOptions,
4654-
this.driver, this.sdk) {}
4661+
this.driver, this.sdk) : session = driver.currentSession {}
46554662

46564663
static Future<PackageGraph> setUpPackageGraph(
4657-
Iterable<LibraryElement> libraryElements,
4658-
Iterable<LibraryElement> specialLibraryElements,
4659-
DartdocOptionContext config,
4660-
PackageMeta packageMeta,
4661-
packageWarningOptions,
4662-
driver,
4663-
sdk,
4664-
) async {
4664+
Iterable<ResolvedLibraryResult> libraryResults,
4665+
Iterable<ResolvedLibraryResult> specialLibraryResults,
4666+
DartdocOptionContext config,
4667+
PackageMeta packageMeta,
4668+
packageWarningOptions,
4669+
driver,
4670+
sdk,) async {
46654671
PackageGraph newGraph =
46664672
PackageGraph._(config, packageMeta, packageWarningOptions, driver, sdk);
46674673
assert(newGraph._allConstructedModelElements.isEmpty);
46684674
assert(newGraph.allLibraries.isEmpty);
46694675
newGraph._packageWarningCounter =
46704676
new PackageWarningCounter(newGraph._packageWarningOptions);
46714677

4672-
// Build [Package] objects.
4673-
libraryElements.forEach((element) {});
4674-
46754678
// Build [Library] objects, and link them to [Package]s.
4676-
libraryElements.forEach((element) {
4679+
libraryResults.forEach((result) {
4680+
LibraryElement element = result.element;
46774681
var packageMeta = new PackageMeta.fromElement(element, config);
4678-
var lib = new Library._(element, newGraph,
4682+
var lib = new Library._(result, newGraph,
46794683
new Package.fromPackageMeta(packageMeta, newGraph));
46804684
newGraph.packageMap[packageMeta.name]._libraries.add(lib);
46814685
newGraph.allLibraries[element] = lib;
@@ -4687,9 +4691,9 @@ class PackageGraph {
46874691
newGraph.allLibrariesAdded = true;
46884692

46894693
// [findOrCreateLibraryFor] already adds to the proper structures.
4690-
specialLibraryElements.forEach((element) {
4691-
newGraph.findOrCreateLibraryFor(element);
4692-
});
4694+
for (ResolvedLibraryResult result in specialLibraryResults) {
4695+
await newGraph.findOrCreateLibraryFor(result.element);
4696+
}
46934697

46944698
// From here on in, we might find special objects. Initialize the
46954699
// specialClasses handler so when we find them, they get added.
@@ -4731,10 +4735,9 @@ class PackageGraph {
47314735
// Many ModelElements have the same ModelNode; don't build/cache this data more
47324736
// than once for them.
47334737
final Map<Element, ModelNode> _modelNodes = Map();
4734-
ModelNode _getModelNodeFor(element) {
4735-
/// TODO(jcollins-g): merge with removal of computeNode.
4738+
ModelNode _getModelNodeFor(element, Library definingLibrary) {
47364739
_modelNodes.putIfAbsent(
4737-
element, () => ModelNode(element?.computeNode(), element));
4740+
element, () => ModelNode(definingLibrary.libraryResult.getElementDeclaration(element)?.node , element));
47384741
return _modelNodes[element];
47394742
}
47404743

@@ -4808,7 +4811,9 @@ class PackageGraph {
48084811
/// Map of package name to Package.
48094812
final Map<String, Package> packageMap = {};
48104813

4814+
/// TODO(brianwilkerson) Replace the driver with the session.
48114815
final AnalysisDriver driver;
4816+
final AnalysisSession session;
48124817
final DartSdk sdk;
48134818

48144819
Map<Source, SdkLibrary> _sdkLibrarySources;
@@ -5089,7 +5094,7 @@ class PackageGraph {
50895094
// The first call to _tagReexportFor should not have a null libraryElement.
50905095
assert(lastExportedElement != null);
50915096
warnOnElement(
5092-
findOrCreateLibraryFor(lastExportedElement.enclosingElement),
5097+
findButDoNotCreateLibraryFor(lastExportedElement.enclosingElement),
50935098
PackageWarning.unresolvedExport,
50945099
message: '"${lastExportedElement.uri}"',
50955100
referredFrom: <Locatable>[topLevelLibrary]);
@@ -5401,7 +5406,18 @@ class PackageGraph {
54015406
/// This is used when we might need a Library object that isn't actually
54025407
/// a documentation entry point (for elements that have no Library within the
54035408
/// set of canonical Libraries).
5404-
Library findOrCreateLibraryFor(Element e) {
5409+
Library findButDoNotCreateLibraryFor(Element e) {
5410+
// This is just a cache to avoid creating lots of libraries over and over.
5411+
if (allLibraries.containsKey(e.library)) {
5412+
return allLibraries[e.library];
5413+
}
5414+
return null;
5415+
}
5416+
5417+
/// This is used when we might need a Library object that isn't actually
5418+
/// a documentation entry point (for elements that have no Library within the
5419+
/// set of canonical Libraries).
5420+
Future<Library> findOrCreateLibraryFor(Element e) async {
54055421
// This is just a cache to avoid creating lots of libraries over and over.
54065422
if (allLibraries.containsKey(e.library)) {
54075423
return allLibraries[e.library];
@@ -5411,8 +5427,10 @@ class PackageGraph {
54115427
return null;
54125428
}
54135429

5430+
ResolvedLibraryResult result =
5431+
await session.getResolvedLibraryByElement(e.library);
54145432
Library foundLibrary = new Library._(
5415-
e.library,
5433+
result,
54165434
this,
54175435
new Package.fromPackageMeta(
54185436
new PackageMeta.fromElement(e.library, config), packageGraph));
@@ -6396,15 +6414,15 @@ class PackageBuilder {
63966414
if (packageMeta.needsPubGet) {
63976415
packageMeta.runPubGet();
63986416
}
6399-
Set<LibraryElement> libraries = new Set();
6400-
Set<LibraryElement> specialLibraries = new Set();
6417+
Map<LibraryElement, ResolvedLibraryResult> libraryResults = new Map();
6418+
Map<LibraryElement, ResolvedLibraryResult> specialLibraryResults = new Map();
64016419
DartSdk findSpecialsSdk = sdk;
64026420
if (embedderSdk != null && embedderSdk.urlMappings.isNotEmpty) {
64036421
findSpecialsSdk = embedderSdk;
64046422
}
6405-
await getLibraries(libraries, specialLibraries, getFiles,
6423+
await getLibraries(libraryResults, specialLibraryResults, getFiles,
64066424
specialLibraryFiles(findSpecialsSdk).toSet());
6407-
return await PackageGraph.setUpPackageGraph(libraries, specialLibraries,
6425+
return await PackageGraph.setUpPackageGraph(libraryResults.values, specialLibraryResults.values,
64086426
config, config.topLevelPackageMeta, getWarningOptions(), driver, sdk);
64096427
}
64106428

@@ -6516,11 +6534,22 @@ class PackageBuilder {
65166534
options);
65176535
driver.results.listen((_) {});
65186536
driver.exceptions.listen((_) {});
6537+
_session = driver.currentSession;
65196538
scheduler.start();
65206539
}
65216540
return _driver;
65226541
}
65236542

6543+
AnalysisSession _session;
6544+
AnalysisSession get session {
6545+
if (_session == null) {
6546+
// The current session is initialized as a side-effect of initializing the
6547+
// driver.
6548+
driver;
6549+
}
6550+
return _session;
6551+
}
6552+
65246553
PackageWarningOptions getWarningOptions() {
65256554
PackageWarningOptions warningOptions =
65266555
new PackageWarningOptions(config.verboseWarnings);
@@ -6554,7 +6583,7 @@ class PackageBuilder {
65546583

65556584
/// Parse a single library at [filePath] using the current analysis driver.
65566585
/// If [filePath] is not a library, returns null.
6557-
Future<LibraryElement> processLibrary(String filePath) async {
6586+
Future<ResolvedLibraryResult> processLibrary(String filePath) async {
65586587
String name = filePath;
65596588

65606589
if (name.startsWith(directoryCurrentPath)) {
@@ -6581,7 +6610,7 @@ class PackageBuilder {
65816610
if (sourceKind == SourceKind.LIBRARY) {
65826611
// Loading libraryElements from part files works, but is painfully slow
65836612
// and creates many duplicates.
6584-
return (await driver.getResult(filePath))?.libraryElement;
6613+
return await session.getResolvedLibrary(source.fullName);
65856614
}
65866615
return null;
65876616
}
@@ -6594,24 +6623,18 @@ class PackageBuilder {
65946623
return metas;
65956624
}
65966625

6597-
Future<List<LibraryElement>> _parseLibraries(Set<String> files) async {
6598-
Iterable<LibraryElement> libraries = new Iterable.empty();
6626+
Future<Map<LibraryElement, ResolvedLibraryResult>> _parseLibraries(Set<String> files) async {
6627+
Map<LibraryElement, ResolvedLibraryResult> libraries = new Map();
65996628
Set<PackageMeta> lastPass = new Set();
66006629
Set<PackageMeta> current;
6601-
Set<String> addedFiles = new Set();
66026630
do {
66036631
lastPass = _packageMetasForFiles(files);
6604-
files.difference(addedFiles).forEach((filename) {
6605-
addedFiles.add(filename);
6606-
});
6607-
libraries = quiverIterables.concat([
6608-
libraries,
6609-
(await Future.wait(files.map((f) => processLibrary(f))))
6610-
.where((LibraryElement l) => l != null)
6611-
]);
6632+
for (ResolvedLibraryResult r in (await Future.wait(files.map((f) => processLibrary(f)))).where((ResolvedLibraryResult l) => l != null)) {
6633+
libraries[r.element] = r;
6634+
}
66126635

6613-
/// We don't care about upstream analysis errors, so save the first
6614-
/// source list.
6636+
// Be sure to give the analyzer enough time to find all the files.
6637+
await driver.discoverAvailableFiles();
66156638
files.addAll(driver.knownFiles);
66166639
files.addAll(_includeExternalsFrom(driver.knownFiles));
66176640
current = _packageMetasForFiles(files);
@@ -6629,7 +6652,7 @@ class PackageBuilder {
66296652
}
66306653
}
66316654
} while (!lastPass.containsAll(current));
6632-
return libraries.toList();
6655+
return libraries;
66336656
}
66346657

66356658
/// Given a package name, explore the directory and pull out all top level
@@ -6719,23 +6742,24 @@ class PackageBuilder {
67196742
}
67206743

67216744
Future<void> getLibraries(
6722-
Set<LibraryElement> libraries,
6723-
Set<LibraryElement> specialLibraries,
6745+
Map<LibraryElement, ResolvedLibraryResult> libraryResults,
6746+
Map<LibraryElement, ResolvedLibraryResult> specialLibraryResults,
67246747
Set<String> files,
67256748
Set<String> specialFiles) async {
6726-
libraries.addAll(await _parseLibraries(files));
6727-
specialLibraries
6749+
libraryResults.addAll(await _parseLibraries(files));
6750+
specialLibraryResults
67286751
.addAll(await _parseLibraries(specialFiles.difference(files)));
67296752
if (config.include.isNotEmpty) {
6730-
Iterable knownLibraryNames = libraries.map((l) => l.name);
6753+
Iterable knownLibraryNames = libraryResults.values.map((l) => l.element.name);
67316754
Set notFound = new Set.from(config.include)
67326755
.difference(new Set.from(knownLibraryNames))
67336756
.difference(new Set.from(config.exclude));
67346757
if (notFound.isNotEmpty) {
67356758
throw 'Did not find: [${notFound.join(', ')}] in '
67366759
'known libraries: [${knownLibraryNames.join(', ')}]';
67376760
}
6738-
libraries.removeWhere((lib) => !config.include.contains(lib.name));
6761+
libraryResults.removeWhere(
6762+
(element, result) => !config.include.contains(result.element.name));
67396763
}
67406764
}
67416765

lib/src/model_utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,4 @@ String stripIndentFromSource(String source) {
131131
line = line.trimRight();
132132
return line.startsWith(indent) ? line.substring(indent.length) : line;
133133
}).join('\n');
134-
}
134+
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ environment:
88
sdk: '>=2.1.0-dev.9.4 <3.0.0'
99

1010
dependencies:
11-
analyzer: ^0.33.3+2
11+
analyzer: ^0.33.6
1212
args: '>=1.4.1 <2.0.0'
1313
collection: ^1.2.0
1414
crypto: ^2.0.6

tool/grind.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ Future<String> createSdkDartdoc() async {
388388
await launcher.runStreamed('git', [
389389
'clone',
390390
'--branch',
391-
'analyzer-0.33',
391+
'master',
392392
'--depth',
393393
'1',
394394
'https://dart.googlesource.com/sdk.git',

0 commit comments

Comments
 (0)