[tool] Support third_party for --current-package (#7967)
Fixes `--current-package` so that when run on a package in third_party/packages/ in works as expected, rather than failing with an error message saying that it must be run from inside a package.
diff --git a/script/tool/lib/src/common/package_command.dart b/script/tool/lib/src/common/package_command.dart
index 7af85f1..8b3b026 100644
--- a/script/tool/lib/src/common/package_command.dart
+++ b/script/tool/lib/src/common/package_command.dart
@@ -662,22 +662,30 @@
}
String? _getCurrentDirectoryPackageName() {
- // Ensure that the current directory is within the packages directory.
- final Directory absolutePackagesDir = packagesDir.absolute;
+ final Set<Directory> absolutePackagesDirs = <Directory>{
+ packagesDir.absolute,
+ thirdPartyPackagesDir.absolute,
+ };
+ bool isATopLevelPackagesDir(Directory directory) =>
+ absolutePackagesDirs.any((Directory d) => d.path == directory.path);
+
Directory currentDir = packagesDir.fileSystem.currentDirectory.absolute;
- if (!currentDir.path.startsWith(absolutePackagesDir.path) ||
- currentDir.path == packagesDir.path) {
+ // Ensure that the current directory is within one of the top-level packages
+ // directories.
+ if (isATopLevelPackagesDir(currentDir) ||
+ !absolutePackagesDirs
+ .any((Directory d) => currentDir.path.startsWith(d.path))) {
return null;
}
- // If the current directory is a direct subdirectory of the packages
+ // If the current directory is a direct subdirectory of a packages
// directory, then that's the target.
- if (currentDir.parent.path == absolutePackagesDir.path) {
+ if (isATopLevelPackagesDir(currentDir.parent)) {
return currentDir.basename;
}
// Otherwise, walk up until a package is found...
while (!isPackage(currentDir)) {
currentDir = currentDir.parent;
- if (currentDir.path == absolutePackagesDir.path) {
+ if (isATopLevelPackagesDir(currentDir)) {
return null;
}
}
diff --git a/script/tool/test/common/package_command_test.dart b/script/tool/test/common/package_command_test.dart
index f80db41..1123886 100644
--- a/script/tool/test/common/package_command_test.dart
+++ b/script/tool/test/common/package_command_test.dart
@@ -455,6 +455,19 @@
expect(command.plugins, unorderedEquals(<String>[package.path]));
});
+ test('runs on a package when run from the third_party/packages directory',
+ () async {
+ final RepositoryPackage package =
+ createFakePlugin('a_package', thirdPartyPackagesDir);
+ createFakePlugin('another_package', thirdPartyPackagesDir);
+ fileSystem.currentDirectory = package.directory;
+
+ await runCapturingPrint(
+ runner, <String>['sample', '--current-package']);
+
+ expect(command.plugins, unorderedEquals(<String>[package.path]));
+ });
+
test('runs only app-facing package of a federated plugin', () async {
const String pluginName = 'foo';
final Directory groupDir = packagesDir.childDirectory(pluginName);