Skip to content

Commit 1033027

Browse files
srawlinsCommit Queue
authored andcommitted
Tidy some legacy plugin code
* Update comments to be modern and reference "package config files" rather than "packages files" * Remove commented out code that we aren't going to uncomment. * Rename PluginFiles.packages. * Simplify `PluginManager._computeFiles` to use less nesting, and use early `throw` statements, rather than storing data in local and choosing late whether an exception should be thrown with the stored data. Change-Id: I7d5da0e299962d8887f2222137478a2fd96671b3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392960 Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 45a32ec commit 1033027

File tree

3 files changed

+68
-112
lines changed

3 files changed

+68
-112
lines changed

pkg/analysis_server/lib/src/plugin/plugin_locator.dart

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,52 +27,21 @@ class PluginLocator {
2727
/// [resourceProvider] to access the file system.
2828
PluginLocator(this.resourceProvider);
2929

30-
/// Given the root directory of a package (the [packageRoot]), return the path
31-
/// to the plugin associated with the package, or `null` if there is no plugin
32-
/// associated with the package.
30+
/// Given the root directory of a package (the [packageRoot]), returns the
31+
/// path to the plugin associated with the package, or `null` if there is no
32+
/// plugin associated with the package.
3333
///
34-
/// This will look first in the `pubspec.yaml` file in the package root for a
35-
/// top-level key (`analysis_plugin`) indicating where the plugin is located.
36-
/// The value associated with the key is expected to be the path of the plugin
37-
/// relative to the package root. If the directory exists, the it is returned.
34+
/// Looks for the directory `tools/analysis_plugin` relative to
35+
/// the package root.
3836
///
39-
/// If the key is not defined in the `pubspec.yaml` file, or if the directory
40-
/// given does not exist, then this method will look for the directory
41-
/// `tools/analysis_plugin` relative to the package root. If the directory
42-
/// exists, then it is returned.
43-
///
44-
/// This method does not validate the content of the plugin directory before
45-
/// returning it.
37+
/// The content of the plugin directory is not validated.
4638
String? findPlugin(String packageRoot) {
4739
return pluginMap.putIfAbsent(packageRoot, () => _findPlugin(packageRoot));
4840
}
4941

5042
/// The implementation of [findPlugin].
5143
String? _findPlugin(String packageRoot) {
5244
var packageFolder = resourceProvider.getFolder(packageRoot);
53-
// TODO(brianwilkerson): Re-enable this after deciding how we want to deal
54-
// with discovery of plugins.
55-
// import 'package:yaml/yaml.dart';
56-
// File pubspecFile = packageFolder.getChildAssumingFile(pubspecFileName);
57-
// if (pubspecFile.exists) {
58-
// try {
59-
// YamlDocument document = loadYamlDocument(pubspecFile.readAsStringSync(),
60-
// sourceUrl: pubspecFile.toUri());
61-
// YamlNode contents = document.contents;
62-
// if (contents is YamlMap) {
63-
// String pluginPath = contents[analyzerPluginKey];
64-
// if (pluginPath != null) {
65-
// Folder pluginFolder =
66-
// packageFolder.getChildAssumingFolder(pluginPath);
67-
// if (pluginFolder.exists) {
68-
// return pluginFolder.path;
69-
// }
70-
// }
71-
// }
72-
// } catch (exception) {
73-
// // If we can't read the file, or if it isn't valid YAML, then ignore it.
74-
// }
75-
// }
7645
var pluginFolder = packageFolder
7746
.getChildAssumingFolder(toolsFolderName)
7847
.getChildAssumingFolder(defaultPluginFolderName);

pkg/analysis_server/lib/src/plugin/plugin_manager.dart

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,15 @@ class PluginException implements Exception {
106106
String toString() => message;
107107
}
108108

109+
/// The necessary files that define an analyzer plugin on disk.
109110
class PluginFiles {
111+
/// The plugin entry point.
110112
final File execution;
111-
final File packages;
112113

113-
PluginFiles(this.execution, this.packages);
114+
/// The plugin package config file.
115+
final File packageConfig;
116+
117+
PluginFiles(this.execution, this.packageConfig);
114118
}
115119

116120
/// Information about a single plugin.
@@ -314,9 +318,11 @@ class PluginManager {
314318
/// Stream emitting an event when known [plugins] change.
315319
Stream<void> get pluginsChanged => _pluginsChanged.stream;
316320

317-
/// Add the plugin with the given [path] to the list of plugins that should be
318-
/// used when analyzing code for the given [contextRoot]. If the plugin had
319-
/// not yet been started, then it will be started by this method.
321+
/// Adds the plugin with the given [path] to the list of plugins that should
322+
/// be used when analyzing code for the given [contextRoot].
323+
///
324+
/// If the plugin had not yet been started, then it will be started by this
325+
/// method.
320326
Future<void> addPluginToContextRoot(
321327
analyzer.ContextRoot contextRoot, String path) async {
322328
var plugin = _pluginMap[path];
@@ -336,7 +342,7 @@ class PluginManager {
336342
plugin = DiscoveredPluginInfo(
337343
path,
338344
pluginFiles.execution.path,
339-
pluginFiles.packages.path,
345+
pluginFiles.packageConfig.path,
340346
notificationManager,
341347
instrumentationService);
342348
_pluginMap[path] = plugin;
@@ -425,8 +431,9 @@ class PluginManager {
425431
return responses;
426432
}
427433

428-
/// Return the files associated with the plugin at the given [pluginPath].
429-
/// Throw a [PluginException] if there is a problem that prevents the plugin
434+
/// Returns the files associated with the plugin at the given [pluginPath].
435+
///
436+
/// Throws a [PluginException] if there is a problem that prevents the plugin
430437
/// from being executing.
431438
@visibleForTesting
432439
PluginFiles filesFor(String pluginPath) {
@@ -443,12 +450,11 @@ class PluginManager {
443450
// there is exactly one version of each package.
444451
return _computeFiles(pluginFolder, workspace: workspace);
445452
}
446-
//
453+
447454
// Copy the plugin directory to a unique subdirectory of the plugin
448455
// manager's state location. The subdirectory's name is selected such that
449-
// it will be invariant across sessions, reducing the number of times the
450-
// plugin will need to be copied and pub will need to be run.
451-
//
456+
// it will be invariant across sessions, reducing the number of times we
457+
// copy the plugin contents, and the number of times we run `pub`.
452458
var stateFolder = resourceProvider.getStateLocation('.plugin_manager');
453459
if (stateFolder == null) {
454460
throw PluginException('No state location, so plugin could not be copied');
@@ -598,56 +604,56 @@ class PluginManager {
598604
}));
599605
}
600606

601-
/// Compute the files to be returned by the enclosing method given that the
602-
/// plugin should exist in the given [pluginFolder].
607+
/// Computes the plugin files, given that the plugin should exist in
608+
/// [pluginFolder].
603609
///
604-
/// Runs pub if [pubCommand] is provided and not null.
610+
/// Runs `pub` if [pubCommand] is not `null`.
605611
PluginFiles _computeFiles(Folder pluginFolder,
606612
{String? pubCommand, Workspace? workspace}) {
607613
var pluginFile = pluginFolder
608614
.getChildAssumingFolder('bin')
609615
.getChildAssumingFile('plugin.dart');
610616
if (!pluginFile.exists) {
611-
throw PluginException('File "${pluginFile.path}" does not exist.');
617+
throw PluginException("File '${pluginFile.path}' does not exist.");
612618
}
613-
String? reason;
614-
File? packagesFile = pluginFolder
619+
File? packageConfigFile = pluginFolder
615620
.getChildAssumingFolder(file_paths.dotDartTool)
616621
.getChildAssumingFile(file_paths.packageConfigJson);
622+
617623
if (pubCommand != null) {
618624
var result = _runPubCommand(pubCommand, pluginFolder);
625+
String? exceptionReason;
619626
if (result.exitCode != 0) {
620627
var buffer = StringBuffer();
621628
buffer.writeln('Failed to run pub $pubCommand');
622629
buffer.writeln(' pluginFolder = ${pluginFolder.path}');
623630
buffer.writeln(' exitCode = ${result.exitCode}');
624631
buffer.writeln(' stdout = ${result.stdout}');
625632
buffer.writeln(' stderr = ${result.stderr}');
626-
reason = buffer.toString();
627-
instrumentationService.logError(reason);
628-
}
629-
if (!packagesFile.exists) {
630-
reason ??= 'File "${packagesFile.path}" does not exist.';
631-
packagesFile = null;
633+
exceptionReason = buffer.toString();
634+
instrumentationService.logError(exceptionReason);
632635
}
633-
} else if (!packagesFile.exists) {
634-
if (workspace != null) {
635-
packagesFile =
636-
_createPackagesFile(pluginFolder, workspace.packageUriResolver);
637-
if (packagesFile == null) {
638-
var name = file_paths.packageConfigJson;
639-
reason = 'Could not create $name file in workspace $workspace.';
640-
}
641-
} else {
642-
reason = 'Could not create "${packagesFile.path}".';
643-
packagesFile = null;
636+
if (!packageConfigFile.exists) {
637+
exceptionReason ??= 'File "${packageConfigFile.path}" does not exist.';
638+
throw PluginException(exceptionReason);
644639
}
640+
return PluginFiles(pluginFile, packageConfigFile);
645641
}
646-
if (packagesFile == null) {
647-
reason ??= 'Could not create packages file for an unknown reason.';
648-
throw PluginException(reason);
642+
643+
if (!packageConfigFile.exists) {
644+
if (workspace == null) {
645+
throw PluginException('Could not create "${packageConfigFile.path}".');
646+
}
647+
648+
packageConfigFile =
649+
_createPackageConfigFile(pluginFolder, workspace.packageUriResolver);
650+
if (packageConfigFile == null) {
651+
throw PluginException(
652+
"Could not create the '${file_paths.packageConfigJson}' file in "
653+
"the workspace at '$workspace'.");
654+
}
649655
}
650-
return PluginFiles(pluginFile, packagesFile);
656+
return PluginFiles(pluginFile, packageConfigFile);
651657
}
652658

653659
WatchEventType _convertChangeType(watcher.ChangeType type) {
@@ -663,17 +669,18 @@ class PluginManager {
663669
return WatchEvent(_convertChangeType(watchEvent.type), watchEvent.path);
664670
}
665671

666-
/// Return a temporary `package_config.json` file that is appropriate for
667-
/// the plugin in the given [pluginFolder]. The [packageUriResolver] is
668-
/// used to determine the location of the packages that need to be included
669-
/// in the packages file.
670-
File? _createPackagesFile(
672+
/// Returns a temporary `package_config.json` file that is appropriate for
673+
/// the plugin in the given [pluginFolder].
674+
///
675+
/// The [packageUriResolver] is used to determine the location of the
676+
/// packages that need to be included in the package config file.
677+
File? _createPackageConfigFile(
671678
Folder pluginFolder, UriResolver packageUriResolver) {
672679
var pluginPath = pluginFolder.path;
673680
var stateFolder = resourceProvider.getStateLocation('.plugin_manager')!;
674681
var stateName = '${_uniqueDirectoryName(pluginPath)}.packages';
675-
var packagesFile = stateFolder.getChildAssumingFile(stateName);
676-
if (!packagesFile.exists) {
682+
var packageConfigFile = stateFolder.getChildAssumingFile(stateName);
683+
if (!packageConfigFile.exists) {
677684
var pluginPubspec =
678685
pluginFolder.getChildAssumingFile(file_paths.pubspecYaml);
679686
if (!pluginPubspec.exists) {
@@ -723,20 +730,20 @@ class PluginManager {
723730
rootPath: package.root.path,
724731
);
725732
}
726-
packagesFile.writeAsStringSync(
733+
packageConfigFile.writeAsStringSync(
727734
packageConfigBuilder.toContent(
728735
toUriStr: (path) {
729736
return resourceProvider.pathContext.toUri(path).toString();
730737
},
731738
),
732739
);
733740
} catch (exception) {
734-
// If we are not able to produce a .packages file, return null so that
735-
// callers will not try to load the plugin.
741+
// If we are not able to produce a package config file, return `null` so
742+
// that callers will not try to load the plugin.
736743
return null;
737744
}
738745
}
739-
return packagesFile;
746+
return packageConfigFile;
740747
}
741748

742749
void _notifyPluginsChanged() => _pluginsChanged.add(null);
@@ -781,7 +788,7 @@ class PluginManager {
781788
return result;
782789
}
783790

784-
/// Return a hex-encoded MD5 signature of the given file [path].
791+
/// Returns a hex-encoded MD5 signature of the given file [path].
785792
String _uniqueDirectoryName(String path) {
786793
var bytes = md5.convert(path.codeUnits).bytes;
787794
return hex.encode(bytes);

pkg/analysis_server/test/src/plugin/plugin_manager_test.dart

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -210,26 +210,6 @@ class PluginManagerFromDiskTest extends PluginTestSupport {
210210
pkg1Dir.deleteSync(recursive: true);
211211
}
212212

213-
@failingTest
214-
Future<void> test_addPluginToContextRoot_pubspec() async {
215-
// We can't successfully run pub until after the analyzer_plugin package has
216-
// been published.
217-
fail('Cannot run pub');
218-
// io.Directory pkg1Dir = io.Directory.systemTemp.createTempSync('pkg1');
219-
// String pkgPath = pkg1Dir.resolveSymbolicLinksSync();
220-
// await withPubspecPlugin(test: (String pluginPath) async {
221-
// ContextRoot contextRoot = _newContextRoot(pkgPath);
222-
// await manager.addPluginToContextRoot(contextRoot, pluginPath);
223-
// String packagesPath =
224-
// resourceProvider.pathContext.join(pluginPath, '.packages');
225-
// File packagesFile = resourceProvider.getFile(packagesPath);
226-
// bool exists = packagesFile.exists;
227-
// await manager.stopAll();
228-
// expect(exists, isTrue, reason: '.packages file was not created');
229-
// });
230-
// pkg1Dir.deleteSync(recursive: true);
231-
}
232-
233213
@SkippedTest(
234214
reason: 'flaky timeouts',
235215
issue: 'https://github.com/dart-lang/sdk/issues/38629')
@@ -504,7 +484,7 @@ class PluginManagerTest with ResourceProviderMixin, _ContextRoot {
504484
//
505485
var files = manager.filesFor(pluginDirPath);
506486
expect(files.execution, pluginFile);
507-
expect(files.packages, packageConfigFile);
487+
expect(files.packageConfig, packageConfigFile);
508488
}
509489

510490
void test_pathsFor_withPubspec_inBlazeWorkspace() {
@@ -540,10 +520,10 @@ class PluginManagerTest with ResourceProviderMixin, _ContextRoot {
540520
//
541521
var files = manager.filesFor(pluginDirPath);
542522
expect(files.execution, pluginFile);
543-
var packagesFile = files.packages;
544-
expect(packagesFile.exists, isTrue);
523+
var packageConfigFile = files.packageConfig;
524+
expect(packageConfigFile.exists, isTrue);
545525

546-
var content = packagesFile.readAsStringSync();
526+
var content = packageConfigFile.readAsStringSync();
547527
expect(content, '''
548528
{
549529
"configVersion": 2,

0 commit comments

Comments
 (0)