Issue 49951. Fix SearchedFiles to avoid adding unrelated files to known.

Bug: https://github.com/dart-lang/sdk/issues/49951
Change-Id: I66bd8c18c45ae8c0cde163ea094794a56599acc4
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/259504
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
This commit is contained in:
Konstantin Shcheglov 2022-09-16 19:56:00 +00:00 committed by Commit Bot
parent 5fe480b788
commit 8ebb7d70e5
3 changed files with 56 additions and 10 deletions

View file

@ -460,7 +460,7 @@ class FileState {
}
File get resource {
return _fsState._resourceProvider.getFile(path);
return _fsState.resourceProvider.getFile(path);
}
@visibleForTesting
@ -1085,7 +1085,7 @@ class FileStateTestView {
/// Information about known file system state.
class FileSystemState {
final PerformanceLog _logger;
final ResourceProvider _resourceProvider;
final ResourceProvider resourceProvider;
final String contextName;
final ByteStore _byteStore;
final SourceFactory _sourceFactory;
@ -1137,7 +1137,7 @@ class FileSystemState {
FileSystemState(
this._logger,
this._byteStore,
this._resourceProvider,
this.resourceProvider,
this.contextName,
this._sourceFactory,
this._workspace,
@ -1153,7 +1153,7 @@ class FileSystemState {
_testView = FileSystemStateTestView(this);
}
package_path.Context get pathContext => _resourceProvider.pathContext;
package_path.Context get pathContext => resourceProvider.pathContext;
@visibleForTesting
FileSystemStateTestView get test => _testView;
@ -1262,7 +1262,7 @@ class FileSystemState {
}) {
var file = _pathToFile[path];
if (file == null) {
File resource = _resourceProvider.getFile(path);
File resource = resourceProvider.getFile(path);
Uri uri = _sourceFactory.pathToUri(path)!;
file = _newFile(resource, path, uri);
}
@ -1305,7 +1305,7 @@ class FileSystemState {
return Either2.t1(file);
}
File resource = _resourceProvider.getFile(path);
File resource = resourceProvider.getFile(path);
var rewrittenUri = rewriteToCanonicalUri(_sourceFactory, uri);
if (rewrittenUri == null) {
@ -2147,7 +2147,7 @@ class PartOfNameFileKind extends PartFileKind {
@visibleForTesting
void discoverLibraries() {
if (libraries.isEmpty) {
var resourceProvider = file._fsState._resourceProvider;
var resourceProvider = file._fsState.resourceProvider;
var pathContext = resourceProvider.pathContext;
var siblings = <Resource>[];

View file

@ -717,12 +717,18 @@ class SearchedFiles {
final Map<Uri, Search> uriOwners = {};
bool add(String path, Search search) {
var file = search._driver.fsState.getFileForPath(path);
final fsState = search._driver.fsState;
final file = fsState.resourceProvider.getFile(path);
final fileState = fsState.getExisting(file);
if (fileState == null) {
return false;
}
var pathOwner = pathOwners[path];
var uriOwner = uriOwners[file.uri];
var uriOwner = uriOwners[fileState.uri];
if (pathOwner == null && uriOwner == null) {
pathOwners[path] = search;
uriOwners[file.uri] = search;
uriOwners[fileState.uri] = search;
return true;
}
return identical(pathOwner, search) && identical(uriOwner, search);

View file

@ -469,6 +469,46 @@ typedef tf2<T> = int Function<S>(T tp, S sp);
offset: 85, codeOffset: 77, codeLength: 45);
}
test_issue49951_references_dontAddToKnown_unrelated() async {
final myRoot = newFolder('$workspaceRootPath/packages/my');
final myFile = newFile('${myRoot.path}/lib/my.dart', r'''
class A {}
''');
// Configure `package:my`.
writePackageConfig(
getFile('${myRoot.path}/.dart_tool/package_config.json').path,
PackageConfigFileBuilder()..add(name: 'my', rootPath: myRoot.path),
);
final myDriver = driverFor(myFile);
final mySession = contextFor(myFile).currentSession;
final libraryElementResult =
await mySession.getLibraryByUri('package:my/my.dart');
libraryElementResult as LibraryElementResult;
final A = libraryElementResult.element.getClass('A')!;
final searchedFiles = SearchedFiles();
searchedFiles.ownAnalyzed(myDriver.search);
final testDriver = driverFor(testFile);
// No references, but this is not the most important.
final references = await testDriver.search.references(A, searchedFiles);
expect(references, isEmpty);
// We should not add the file to known files. It is not in the
// `package:test` itself, and not in a package from its package config.
// So, it is absolutely unrelated to `package:test`.
for (final knowFile in testDriver.fsState.knownFiles) {
if (knowFile.path == myFile.path) {
fail('The file should not be added.');
}
}
}
test_searchMemberReferences_qualified_resolved() async {
await resolveTestCode('''
class C {