Skip to content

Commit b16c6a8

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 44131. Handle .dart_tool/package_config.json file change, re-configure with new Packages.
Bug: #44131 Change-Id: I4c843da4c6cecea5e334ba6b73cffa42be6d80ea Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/171360 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 9711b9a commit b16c6a8

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

pkg/analysis_server/lib/src/context_manager.dart

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,9 @@ class ContextManagerImpl implements ContextManager {
887887
}
888888

889889
void _checkForPackagespecUpdate(String path, ContextInfo info) {
890-
// Check to see if this is the .packages file for this context and if so,
891-
// update the context's source factory.
892-
if (pathContext.basename(path) == PACKAGE_SPEC_NAME) {
890+
// Check to see if this is `.dart_tool/package_config.json` or `.packages`
891+
// file for this context and if so, update the context's source factory.
892+
if (_isPackageConfigJsonFilePath(path) || _isDotPackagesFilePath(path)) {
893893
var driver = info.analysisDriver;
894894
if (driver == null) {
895895
// I suspect that this happens as a result of a race condition: server
@@ -1249,6 +1249,9 @@ class ContextManagerImpl implements ContextManager {
12491249
if (info.hasDependency(path)) {
12501250
_recomputeFolderDisposition(info);
12511251
}
1252+
1253+
_checkForPackagespecUpdate(path, info);
1254+
12521255
// maybe excluded globally
12531256
if (_isExcluded(path) ||
12541257
_isContainedInDotFolder(info.folder.path, path) ||
@@ -1283,7 +1286,7 @@ class ContextManagerImpl implements ContextManager {
12831286
return;
12841287
}
12851288
}
1286-
if (_isPackagespec(path)) {
1289+
if (_isDotPackagesFilePath(path)) {
12871290
// Check for a sibling pubspec.yaml file.
12881291
if (!resourceProvider
12891292
.getFile(pathContext.join(directoryPath, PUBSPEC_NAME))
@@ -1323,7 +1326,7 @@ class ContextManagerImpl implements ContextManager {
13231326
return;
13241327
}
13251328
}
1326-
if (_isPackagespec(path)) {
1329+
if (_isDotPackagesFilePath(path)) {
13271330
// Check for a sibling pubspec.yaml file.
13281331
if (!resourceProvider
13291332
.getFile(pathContext.join(directoryPath, PUBSPEC_NAME))
@@ -1352,7 +1355,6 @@ class ContextManagerImpl implements ContextManager {
13521355
}
13531356
}
13541357
}
1355-
_checkForPackagespecUpdate(path, info);
13561358
_checkForAnalysisOptionsUpdate(path, info);
13571359
_checkForDataFileUpdate(path, info);
13581360
_checkForPubspecUpdate(path, info);
@@ -1388,6 +1390,10 @@ class ContextManagerImpl implements ContextManager {
13881390
/// to specify data-driven fixes.
13891391
bool _isDataFile(String path) => pathContext.basename(path) == dataFileName;
13901392

1393+
bool _isDotPackagesFilePath(String path) {
1394+
return pathContext.basename(path) == PACKAGE_SPEC_NAME;
1395+
}
1396+
13911397
/// Returns `true` if the given [path] is excluded by [excludedPaths].
13921398
bool _isExcluded(String path) => _isExcludedBy(excludedPaths, path);
13931399

@@ -1415,8 +1421,12 @@ class ContextManagerImpl implements ContextManager {
14151421

14161422
bool _isManifest(String path) => pathContext.basename(path) == MANIFEST_NAME;
14171423

1418-
bool _isPackagespec(String path) =>
1419-
pathContext.basename(path) == PACKAGE_SPEC_NAME;
1424+
bool _isPackageConfigJsonFilePath(String path) {
1425+
var components = pathContext.split(path);
1426+
return components.length > 2 &&
1427+
components[components.length - 1] == 'package_config.json' &&
1428+
components[components.length - 2] == '.dart_tool';
1429+
}
14201430

14211431
bool _isPubspec(String path) => pathContext.basename(path) == PUBSPEC_NAME;
14221432

@@ -1477,8 +1487,13 @@ class ContextManagerImpl implements ContextManager {
14771487
var builder = callbacks.createContextBuilder(info.folder);
14781488
var options = builder.getAnalysisOptions(contextRoot,
14791489
contextRoot: driver.contextRoot);
1490+
var packages = builder.createPackageMap(contextRoot);
14801491
var factory = builder.createSourceFactory(contextRoot);
1481-
driver.configure(analysisOptions: options, sourceFactory: factory);
1492+
driver.configure(
1493+
analysisOptions: options,
1494+
packages: packages,
1495+
sourceFactory: factory,
1496+
);
14821497
callbacks.analysisOptionsUpdated(driver);
14831498
}
14841499

pkg/analysis_server/test/context_manager_test.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import 'package:analyzer/src/generated/source.dart';
2323
import 'package:analyzer/src/generated/source_io.dart';
2424
import 'package:analyzer/src/services/lint.dart';
2525
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
26+
import 'package:analyzer/src/test_utilities/package_config_file_builder.dart';
2627
import 'package:analyzer/src/test_utilities/resource_provider_mixin.dart';
2728
import 'package:analyzer/src/util/glob.dart';
2829
import 'package:linter/src/rules.dart';
@@ -1490,6 +1491,34 @@ sky_engine:lib/''');
14901491
});
14911492
}
14921493

1494+
Future<void> test_watch_modifyPackageConfigJson() {
1495+
var packageConfigPath = '$projPath/.dart_tool/package_config.json';
1496+
var filePath = convertPath('$projPath/bin/main.dart');
1497+
1498+
resourceProvider.newFile(packageConfigPath, '');
1499+
resourceProvider.newFile(filePath, 'library main;');
1500+
1501+
manager.setRoots(<String>[projPath], <String>[]);
1502+
1503+
var filePaths = callbacks.currentFilePaths;
1504+
expect(filePaths, hasLength(1));
1505+
expect(filePaths, contains(filePath));
1506+
expect(_currentPackageMap, isEmpty);
1507+
1508+
// update .dart_tool/package_config.json
1509+
callbacks.now++;
1510+
resourceProvider.modifyFile(
1511+
packageConfigPath,
1512+
(PackageConfigFileBuilder()..add(name: 'my', rootPath: '../'))
1513+
.toContent(toUriStr: toUriStr),
1514+
);
1515+
1516+
return pumpEventQueue().then((_) {
1517+
// verify new package info
1518+
expect(_currentPackageMap.keys, unorderedEquals(['my']));
1519+
});
1520+
}
1521+
14931522
Future<void> test_watch_modifyPackagespec() {
14941523
var packagesPath = convertPath('$projPath/.packages');
14951524
var filePath = convertPath('$projPath/bin/main.dart');

0 commit comments

Comments
 (0)