Skip to content

Commit c76fc43

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Add WorkspacePackage to each FileState.
We need this so that we can look if the file is NullSafe by asking the package that contains it. Bug: #42594 Change-Id: I11c71faf7bddd53b458a66f0786454ecd7453b5e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/153521 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 4210268 commit c76fc43

14 files changed

+139
-64
lines changed

pkg/analyzer/lib/src/context/builder.dart

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,12 @@ class ContextBuilder {
133133
includedPaths: [contextRoot.root],
134134
excludedPaths: contextRoot.exclude,
135135
);
136-
driver.analysisContext = api.DriverBasedAnalysisContext(
137-
resourceProvider,
138-
apiContextRoots.first,
139-
driver,
136+
driver.configure(
137+
analysisContext: api.DriverBasedAnalysisContext(
138+
resourceProvider,
139+
apiContextRoots.first,
140+
driver,
141+
),
140142
);
141143

142144
// temporary plugin support:

pkg/analyzer/lib/src/dart/analysis/driver.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,14 @@ class AnalysisDriver implements AnalysisDriverGeneric {
488488
/// At least one of the optional parameters should be provided, but only those
489489
/// that represent state that has actually changed need be provided.
490490
void configure({
491+
api.AnalysisContext analysisContext,
491492
AnalysisOptions analysisOptions,
492493
Packages packages,
493494
SourceFactory sourceFactory,
494495
}) {
496+
if (analysisContext != null) {
497+
this.analysisContext = analysisContext;
498+
}
495499
if (analysisOptions != null) {
496500
_analysisOptions = analysisOptions;
497501
}
@@ -1286,7 +1290,6 @@ class AnalysisDriver implements AnalysisDriverGeneric {
12861290
libraryContext.elementFactory,
12871291
libraryContext.analysisSession.inheritanceManager,
12881292
library,
1289-
_resourceProvider,
12901293
testingData: testingData);
12911294
Map<FileState, UnitAnalysisResult> results = analyzer.analyze();
12921295

@@ -1363,7 +1366,6 @@ class AnalysisDriver implements AnalysisDriverGeneric {
13631366
libraryContext.elementFactory,
13641367
libraryContext.analysisSession.inheritanceManager,
13651368
library,
1366-
_resourceProvider,
13671369
testingData: testingData);
13681370
Map<FileState, UnitAnalysisResult> unitResults = analyzer.analyze();
13691371
var resolvedUnits = <ResolvedUnitResult>[];
@@ -1465,6 +1467,7 @@ class AnalysisDriver implements AnalysisDriverGeneric {
14651467
_resourceProvider,
14661468
name,
14671469
sourceFactory,
1470+
analysisContext?.workspace,
14681471
analysisOptions,
14691472
declaredVariables,
14701473
_saltForUnlinked,

pkg/analyzer/lib/src/dart/analysis/file_state.dart

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import 'package:analyzer/src/summary/format.dart';
3838
import 'package:analyzer/src/summary/idl.dart';
3939
import 'package:analyzer/src/summary/package_bundle_reader.dart';
4040
import 'package:analyzer/src/summary2/informative_data.dart';
41+
import 'package:analyzer/src/workspace/workspace.dart';
4142
import 'package:convert/convert.dart';
4243
import 'package:crypto/crypto.dart';
4344
import 'package:meta/meta.dart';
@@ -91,6 +92,11 @@ class FileState {
9192
/// The [Source] of the file with the [uri].
9293
final Source source;
9394

95+
/// The [WorkspacePackage] that contains this file.
96+
///
97+
/// It might be `null` if the file is outside of the workspace.
98+
final WorkspacePackage workspacePackage;
99+
94100
/// Return `true` if this file is a stub created for a file in the provided
95101
/// external summary store. The values of most properties are not the same
96102
/// as they would be if the file were actually read from the file system.
@@ -144,6 +150,7 @@ class FileState {
144150
this.path,
145151
this.uri,
146152
this.source,
153+
this.workspacePackage,
147154
this._contextFeatureSet,
148155
this._packageLanguageVersion,
149156
) : isInExternalSummaries = false;
@@ -152,6 +159,7 @@ class FileState {
152159
: isInExternalSummaries = true,
153160
path = null,
154161
source = null,
162+
workspacePackage = null,
155163
_exists = true,
156164
_contextFeatureSet = null,
157165
_packageLanguageVersion = null {
@@ -734,6 +742,7 @@ class FileSystemState {
734742
final ByteStore _byteStore;
735743
final FileContentOverlay _contentOverlay;
736744
final SourceFactory _sourceFactory;
745+
final Workspace _workspace;
737746
final AnalysisOptions _analysisOptions;
738747
final DeclaredVariables _declaredVariables;
739748
final Uint32List _saltForUnlinked;
@@ -793,6 +802,7 @@ class FileSystemState {
793802
this._resourceProvider,
794803
this.contextName,
795804
this._sourceFactory,
805+
this._workspace,
796806
this._analysisOptions,
797807
this._declaredVariables,
798808
this._saltForUnlinked,
@@ -814,8 +824,8 @@ class FileSystemState {
814824
FileState get unresolvedFile {
815825
if (_unresolvedFile == null) {
816826
var featureSet = FeatureSet.fromEnableFlags([]);
817-
_unresolvedFile = FileState._(
818-
this, null, null, null, featureSet, ExperimentStatus.currentVersion);
827+
_unresolvedFile = FileState._(this, null, null, null, null, featureSet,
828+
ExperimentStatus.currentVersion);
819829
_unresolvedFile.refresh();
820830
}
821831
return _unresolvedFile;
@@ -841,11 +851,12 @@ class FileSystemState {
841851
}
842852
// Create a new file.
843853
FileSource uriSource = FileSource(resource, uri);
854+
WorkspacePackage workspacePackage = _workspace?.findPackageFor(path);
844855
FeatureSet featureSet = featureSetProvider.getFeatureSet(path, uri);
845856
Version packageLanguageVersion =
846857
featureSetProvider.getLanguageVersion(path, uri);
847-
file = FileState._(
848-
this, path, uri, uriSource, featureSet, packageLanguageVersion);
858+
file = FileState._(this, path, uri, uriSource, workspacePackage,
859+
featureSet, packageLanguageVersion);
849860
_uriToFile[uri] = file;
850861
_addFileWithPath(path, file);
851862
_pathToCanonicalFile[path] = file;
@@ -884,11 +895,12 @@ class FileSystemState {
884895
String path = uriSource.fullName;
885896
File resource = _resourceProvider.getFile(path);
886897
FileSource source = FileSource(resource, uri);
898+
WorkspacePackage workspacePackage = _workspace?.findPackageFor(path);
887899
FeatureSet featureSet = featureSetProvider.getFeatureSet(path, uri);
888900
Version packageLanguageVersion =
889901
featureSetProvider.getLanguageVersion(path, uri);
890-
file = FileState._(
891-
this, path, uri, source, featureSet, packageLanguageVersion);
902+
file = FileState._(this, path, uri, source, workspacePackage, featureSet,
903+
packageLanguageVersion);
892904
_uriToFile[uri] = file;
893905
_addFileWithPath(path, file);
894906
file.refresh(allowCached: true);

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

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/element/element.dart';
99
import 'package:analyzer/error/error.dart';
1010
import 'package:analyzer/error/listener.dart';
11-
import 'package:analyzer/file_system/file_system.dart';
12-
import 'package:analyzer/src/context/builder.dart';
1311
import 'package:analyzer/src/dart/analysis/file_state.dart';
1412
import 'package:analyzer/src/dart/analysis/testing_data.dart';
1513
import 'package:analyzer/src/dart/ast/ast.dart';
@@ -50,7 +48,6 @@ import 'package:analyzer/src/lint/linter_visitor.dart';
5048
import 'package:analyzer/src/services/lint.dart';
5149
import 'package:analyzer/src/summary2/linked_element_factory.dart';
5250
import 'package:analyzer/src/task/strong/checker.dart';
53-
import 'package:analyzer/src/workspace/workspace.dart';
5451
import 'package:pub_semver/pub_semver.dart';
5552

5653
var timerLibraryAnalyzer = Stopwatch();
@@ -70,7 +67,6 @@ class LibraryAnalyzer {
7067
final DeclaredVariables _declaredVariables;
7168
final SourceFactory _sourceFactory;
7269
final FileState _library;
73-
final ResourceProvider _resourceProvider;
7470

7571
final InheritanceManager3 _inheritance;
7672
final bool Function(Uri) _isLibraryUri;
@@ -100,7 +96,6 @@ class LibraryAnalyzer {
10096
this._elementFactory,
10197
this._inheritance,
10298
this._library,
103-
this._resourceProvider,
10499
{TestingData testingData})
105100
: _testingData = testingData;
106101

@@ -251,8 +246,8 @@ class LibraryAnalyzer {
251246
declaredVariables: _declaredVariables,
252247
typeSystem: _typeSystem,
253248
inheritanceManager: _inheritance,
254-
resourceProvider: _resourceProvider,
255249
analysisOptions: _context.analysisOptions,
250+
workspacePackage: _library.workspacePackage,
256251
),
257252
);
258253

@@ -309,8 +304,6 @@ class LibraryAnalyzer {
309304
var nodeRegistry = NodeLintRegistry(_analysisOptions.enableTiming);
310305
var visitors = <AstVisitor>[];
311306

312-
final workspacePackage = _getPackage(currentUnit.unit);
313-
314307
var context = LinterContextImpl(
315308
allUnits,
316309
currentUnit,
@@ -319,7 +312,7 @@ class LibraryAnalyzer {
319312
_typeSystem,
320313
_inheritance,
321314
_analysisOptions,
322-
workspacePackage,
315+
file.workspacePackage,
323316
);
324317
for (Linter linter in _analysisOptions.lintRules) {
325318
linter.reporter = errorReporter;
@@ -479,23 +472,6 @@ class LibraryAnalyzer {
479472
});
480473
}
481474

482-
WorkspacePackage _getPackage(CompilationUnit unit) {
483-
final libraryPath = _library.source.fullName;
484-
Workspace workspace =
485-
unit.declaredElement.session?.analysisContext?.workspace;
486-
487-
// If there is no driver setup (as in test environments), we need to create
488-
// a workspace ourselves.
489-
// todo (pq): fix tests or otherwise de-dup this logic shared w/ resolver.
490-
if (workspace == null) {
491-
final builder = ContextBuilder(
492-
_resourceProvider, null /* sdkManager */, null /* contentCache */);
493-
workspace = ContextBuilder.createWorkspace(
494-
_resourceProvider, libraryPath, builder);
495-
}
496-
return workspace?.findPackageFor(libraryPath);
497-
}
498-
499475
/// Return the name of the library that the given part is declared to be a
500476
/// part of, or `null` if the part does not contain a part-of directive.
501477
_NameOrSource _getPartLibraryNameOrUri(Source partSource,

pkg/analyzer/lib/src/dart/micro/library_analyzer.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ class LibraryAnalyzer {
279279
declaredVariables: _declaredVariables,
280280
typeSystem: _typeSystem,
281281
inheritanceManager: _inheritance,
282-
resourceProvider: _resourceProvider,
283282
analysisOptions: _context.analysisOptions,
283+
workspacePackage: null, // TODO(scheglov) implement it
284284
),
285285
);
286286

pkg/analyzer/lib/src/error/best_practices_verifier.dart

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import 'package:analyzer/dart/element/nullability_suffix.dart';
1313
import 'package:analyzer/dart/element/type.dart';
1414
import 'package:analyzer/dart/element/type_provider.dart';
1515
import 'package:analyzer/error/listener.dart';
16-
import 'package:analyzer/file_system/file_system.dart';
17-
import 'package:analyzer/src/context/builder.dart';
1816
import 'package:analyzer/src/dart/element/element.dart';
1917
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
2018
import 'package:analyzer/src/dart/element/type.dart';
@@ -65,7 +63,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
6563
final _InvalidAccessVerifier _invalidAccessVerifier;
6664

6765
/// The [WorkspacePackage] in which [_currentLibrary] is declared.
68-
WorkspacePackage _workspacePackage;
66+
final WorkspacePackage _workspacePackage;
6967

7068
/// The [LinterContext] used for possible const calculations.
7169
LinterContext _linterContext;
@@ -87,20 +85,19 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
8785
String content, {
8886
@required TypeSystemImpl typeSystem,
8987
@required InheritanceManager3 inheritanceManager,
90-
@required ResourceProvider resourceProvider,
9188
@required DeclaredVariables declaredVariables,
9289
@required AnalysisOptions analysisOptions,
90+
@required WorkspacePackage workspacePackage,
9391
}) : _nullType = typeProvider.nullType,
9492
_typeSystem = typeSystem,
9593
_isNonNullableByDefault = typeSystem.isNonNullableByDefault,
9694
_strictInference =
9795
(analysisOptions as AnalysisOptionsImpl).strictInference,
9896
_inheritanceManager = inheritanceManager,
9997
_invalidAccessVerifier =
100-
_InvalidAccessVerifier(_errorReporter, _currentLibrary) {
98+
_InvalidAccessVerifier(_errorReporter, _currentLibrary),
99+
_workspacePackage = workspacePackage {
101100
_inDeprecatedMember = _currentLibrary.hasDeprecated;
102-
String libraryPath = _currentLibrary.source.fullName;
103-
_workspacePackage = _getPackage(libraryPath, resourceProvider);
104101

105102
_linterContext = LinterContextImpl(
106103
null /* allUnits */,
@@ -1356,20 +1353,6 @@ class BestPracticesVerifier extends RecursiveAstVisitor<void> {
13561353
}
13571354
}
13581355

1359-
WorkspacePackage _getPackage(String root, ResourceProvider resourceProvider) {
1360-
Workspace workspace = _currentLibrary.session?.analysisContext?.workspace;
1361-
// If there is no driver setup (as in test environments), we need to create
1362-
// a workspace ourselves.
1363-
// todo (pq): fix tests or otherwise de-dup this logic shared w/ library_analyzer.
1364-
if (workspace == null) {
1365-
final builder = ContextBuilder(
1366-
resourceProvider, null /* sdkManager */, null /* contentCache */);
1367-
workspace =
1368-
ContextBuilder.createWorkspace(resourceProvider, root, builder);
1369-
}
1370-
return workspace?.findPackageFor(root);
1371-
}
1372-
13731356
bool _isLibraryInWorkspacePackage(LibraryElement library) {
13741357
if (_workspacePackage == null || library == null) {
13751358
// Better to not make a big claim that they _are_ in the same package,

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import 'package:analyzer/src/generated/source.dart';
2121
import 'package:analyzer/src/source/package_map_resolver.dart';
2222
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
2323
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
24+
import 'package:analyzer/src/workspace/basic.dart';
2425
import 'package:convert/convert.dart';
2526
import 'package:crypto/crypto.dart';
2627
import 'package:test/test.dart';
@@ -50,15 +51,25 @@ class FileSystemStateTest with ResourceProviderMixin {
5051
void setUp() {
5152
logger = PerformanceLog(logBuffer);
5253
sdk = MockSdk(resourceProvider: resourceProvider);
54+
55+
var packageMap = <String, List<Folder>>{
56+
'aaa': [getFolder('/aaa/lib')],
57+
'bbb': [getFolder('/bbb/lib')],
58+
};
59+
60+
var workspace = BasicWorkspace.find(
61+
resourceProvider,
62+
packageMap,
63+
convertPath('/test'),
64+
);
65+
5366
sourceFactory = SourceFactory([
5467
DartUriResolver(sdk),
5568
generatedUriResolver,
56-
PackageMapUriResolver(resourceProvider, <String, List<Folder>>{
57-
'aaa': [getFolder('/aaa/lib')],
58-
'bbb': [getFolder('/bbb/lib')],
59-
}),
69+
PackageMapUriResolver(resourceProvider, packageMap),
6070
ResourceUriResolver(resourceProvider)
6171
]);
72+
6273
AnalysisOptions analysisOptions = AnalysisOptionsImpl();
6374
var featureSetProvider = FeatureSetProvider.build(
6475
sourceFactory: sourceFactory,
@@ -73,6 +84,7 @@ class FileSystemStateTest with ResourceProviderMixin {
7384
resourceProvider,
7485
'contextName',
7586
sourceFactory,
87+
workspace,
7688
analysisOptions,
7789
DeclaredVariables(),
7890
Uint32List(0),

0 commit comments

Comments
 (0)