diff --git a/script/tool/lib/src/common/package_command.dart b/script/tool/lib/src/common/package_command.dart index 7af85f1d50da..8b3b02626fe6 100644 --- a/script/tool/lib/src/common/package_command.dart +++ b/script/tool/lib/src/common/package_command.dart @@ -662,22 +662,30 @@ abstract class PackageCommand extends Command { } String? _getCurrentDirectoryPackageName() { - // Ensure that the current directory is within the packages directory. - final Directory absolutePackagesDir = packagesDir.absolute; + final Set absolutePackagesDirs = { + 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 f80db4181d34..112388642f90 100644 --- a/script/tool/test/common/package_command_test.dart +++ b/script/tool/test/common/package_command_test.dart @@ -455,6 +455,19 @@ packages/plugin1/plugin1/plugin1.dart expect(command.plugins, unorderedEquals([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, ['sample', '--current-package']); + + expect(command.plugins, unorderedEquals([package.path])); + }); + test('runs only app-facing package of a federated plugin', () async { const String pluginName = 'foo'; final Directory groupDir = packagesDir.childDirectory(pluginName);