Skip to content

Commit 0a828c7

Browse files
pqcommit-bot@chromium.org
authored and
commit-bot@chromium.org
committedOct 29, 2019
context workspace caching
Change-Id: Ia0c5a4446e79f48b2cbd76d84e503b03751d5b76 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/123326 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent 780af0f commit 0a828c7

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed
 

‎pkg/analyzer/lib/dart/analysis/analysis_context.dart

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'package:analyzer/dart/analysis/context_root.dart';
66
import 'package:analyzer/dart/analysis/session.dart';
77
import 'package:analyzer/src/generated/engine.dart';
8+
import 'package:analyzer/src/workspace/workspace.dart';
89

910
/// A representation of a body of code and the context in which the code is to
1011
/// be analyzed.
@@ -44,6 +45,9 @@ abstract class AnalysisContext {
4445
@deprecated
4546
List<String> get includedPaths;
4647

48+
/// Return the workspace for containing the context root.
49+
Workspace get workspace;
50+
4751
/// Return the absolute, normalized paths of all of the files that are
4852
/// contained in this context. These are all of the files that are included
4953
/// directly or indirectly by one or more of the [includedPaths] and that are

‎pkg/analyzer/lib/src/dart/analysis/driver_based_analysis_context.dart

+19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import 'package:analyzer/dart/analysis/analysis_context.dart';
66
import 'package:analyzer/dart/analysis/context_root.dart';
77
import 'package:analyzer/dart/analysis/session.dart';
88
import 'package:analyzer/file_system/file_system.dart';
9+
import 'package:analyzer/src/context/builder.dart';
910
import 'package:analyzer/src/dart/analysis/driver.dart' show AnalysisDriver;
1011
import 'package:analyzer/src/generated/engine.dart' show AnalysisOptions;
12+
import 'package:analyzer/src/workspace/workspace.dart';
1113

1214
/**
1315
* An analysis context whose implementation is based on an analysis driver.
@@ -26,6 +28,11 @@ class DriverBasedAnalysisContext implements AnalysisContext {
2628
*/
2729
final AnalysisDriver driver;
2830

31+
/**
32+
* The [Workspace] for this context, `null` if not yet created.
33+
*/
34+
Workspace _workspace;
35+
2936
/**
3037
* Initialize a newly created context that uses the given [resourceProvider]
3138
* to access the file system and that is based on the given analysis [driver].
@@ -49,6 +56,11 @@ class DriverBasedAnalysisContext implements AnalysisContext {
4956
@override
5057
List<String> get includedPaths => contextRoot.includedPaths.toList();
5158

59+
@override
60+
Workspace get workspace {
61+
return _workspace ??= _buildWorkspace();
62+
}
63+
5264
@deprecated
5365
@override
5466
Iterable<String> analyzedFiles() {
@@ -60,4 +72,11 @@ class DriverBasedAnalysisContext implements AnalysisContext {
6072
bool isAnalyzed(String path) {
6173
return contextRoot.isAnalyzed(path);
6274
}
75+
76+
Workspace _buildWorkspace() {
77+
String path = contextRoot.root.path;
78+
ContextBuilder builder = ContextBuilder(
79+
resourceProvider, null /* sdkManager */, null /* contentCache */);
80+
return ContextBuilder.createWorkspace(resourceProvider, path, builder);
81+
}
6382
}

‎pkg/analyzer/lib/src/dart/analysis/library_analyzer.dart

+19-8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import 'package:analyzer/src/lint/linter_visitor.dart';
4242
import 'package:analyzer/src/services/lint.dart';
4343
import 'package:analyzer/src/summary2/linked_element_factory.dart';
4444
import 'package:analyzer/src/task/strong/checker.dart';
45+
import 'package:analyzer/src/workspace/workspace.dart';
4546
import 'package:pub_semver/pub_semver.dart';
4647

4748
var timerLibraryAnalyzer = Stopwatch();
@@ -323,14 +324,7 @@ class LibraryAnalyzer {
323324
var nodeRegistry = new NodeLintRegistry(_analysisOptions.enableTiming);
324325
var visitors = <AstVisitor>[];
325326

326-
// todo (pq): duplicates logic in resolver; de-dup, or...
327-
// todo (pq): get workspace from analysis context
328-
final libraryPath = _library.source.fullName;
329-
final builder = new ContextBuilder(
330-
_resourceProvider, null /* sdkManager */, null /* contentCache */);
331-
final workspace =
332-
ContextBuilder.createWorkspace(_resourceProvider, libraryPath, builder);
333-
final workspacePackage = workspace.findPackageFor(libraryPath);
327+
final workspacePackage = _getPackage(currentUnit.unit);
334328

335329
var context = LinterContextImpl(
336330
allUnits,
@@ -465,6 +459,23 @@ class LibraryAnalyzer {
465459
});
466460
}
467461

462+
WorkspacePackage _getPackage(CompilationUnit unit) {
463+
final libraryPath = _library.source.fullName;
464+
Workspace workspace =
465+
unit.declaredElement.session?.analysisContext?.workspace;
466+
467+
// If there is no driver setup (as in test environments), we need to create
468+
// a workspace ourselves.
469+
// todo (pq): fix tests or otherwise de-dup this logic shared w/ resolver.
470+
if (workspace == null) {
471+
final builder = ContextBuilder(
472+
_resourceProvider, null /* sdkManager */, null /* contentCache */);
473+
workspace = ContextBuilder.createWorkspace(
474+
_resourceProvider, libraryPath, builder);
475+
}
476+
return workspace?.findPackageFor(libraryPath);
477+
}
478+
468479
/**
469480
* Return the name of the library that the given part is declared to be a
470481
* part of, or `null` if the part does not contain a part-of directive.

‎pkg/analyzer/lib/src/generated/resolver.dart

+16-5
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,8 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
118118
new _InvalidAccessVerifier(_errorReporter, _currentLibrary) {
119119
_inDeprecatedMember = _currentLibrary.hasDeprecated;
120120
String libraryPath = _currentLibrary.source.fullName;
121-
ContextBuilder builder = new ContextBuilder(
122-
resourceProvider, null /* sdkManager */, null /* contentCache */);
123-
Workspace workspace =
124-
ContextBuilder.createWorkspace(resourceProvider, libraryPath, builder);
125-
_workspacePackage = workspace.findPackageFor(libraryPath);
121+
_workspacePackage = _getPackage(libraryPath, resourceProvider);
122+
126123
_linterContext = LinterContextImpl(
127124
null /* allUnits */,
128125
new LinterContextUnit(content, unit),
@@ -1335,6 +1332,20 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
13351332
}
13361333
}
13371334

1335+
WorkspacePackage _getPackage(String root, ResourceProvider resourceProvider) {
1336+
Workspace workspace = _currentLibrary.session?.analysisContext?.workspace;
1337+
// If there is no driver setup (as in test environments), we need to create
1338+
// a workspace ourselves.
1339+
// todo (pq): fix tests or otherwise de-dup this logic shared w/ library_analyzer.
1340+
if (workspace == null) {
1341+
final builder = ContextBuilder(
1342+
resourceProvider, null /* sdkManager */, null /* contentCache */);
1343+
workspace =
1344+
ContextBuilder.createWorkspace(resourceProvider, root, builder);
1345+
}
1346+
return workspace?.findPackageFor(root);
1347+
}
1348+
13381349
bool _isLibraryInWorkspacePackage(LibraryElement library) {
13391350
if (_workspacePackage == null || library == null) {
13401351
// Better to not make a big claim that they _are_ in the same package,

0 commit comments

Comments
 (0)