Skip to content

Commit cd7a810

Browse files
[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.
1 parent 030dd4e commit cd7a810

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

script/tool/lib/src/common/package_command.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -662,22 +662,30 @@ abstract class PackageCommand extends Command<void> {
662662
}
663663

664664
String? _getCurrentDirectoryPackageName() {
665-
// Ensure that the current directory is within the packages directory.
666-
final Directory absolutePackagesDir = packagesDir.absolute;
665+
final Set<Directory> absolutePackagesDirs = <Directory>{
666+
packagesDir.absolute,
667+
thirdPartyPackagesDir.absolute,
668+
};
669+
bool isATopLevelPackagesDir(Directory directory) =>
670+
absolutePackagesDirs.any((Directory d) => d.path == directory.path);
671+
667672
Directory currentDir = packagesDir.fileSystem.currentDirectory.absolute;
668-
if (!currentDir.path.startsWith(absolutePackagesDir.path) ||
669-
currentDir.path == packagesDir.path) {
673+
// Ensure that the current directory is within one of the top-level packages
674+
// directories.
675+
if (isATopLevelPackagesDir(currentDir) ||
676+
!absolutePackagesDirs
677+
.any((Directory d) => currentDir.path.startsWith(d.path))) {
670678
return null;
671679
}
672-
// If the current directory is a direct subdirectory of the packages
680+
// If the current directory is a direct subdirectory of a packages
673681
// directory, then that's the target.
674-
if (currentDir.parent.path == absolutePackagesDir.path) {
682+
if (isATopLevelPackagesDir(currentDir.parent)) {
675683
return currentDir.basename;
676684
}
677685
// Otherwise, walk up until a package is found...
678686
while (!isPackage(currentDir)) {
679687
currentDir = currentDir.parent;
680-
if (currentDir.path == absolutePackagesDir.path) {
688+
if (isATopLevelPackagesDir(currentDir)) {
681689
return null;
682690
}
683691
}

script/tool/test/common/package_command_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,19 @@ packages/plugin1/plugin1/plugin1.dart
455455
expect(command.plugins, unorderedEquals(<String>[package.path]));
456456
});
457457

458+
test('runs on a package when run from the third_party/packages directory',
459+
() async {
460+
final RepositoryPackage package =
461+
createFakePlugin('a_package', thirdPartyPackagesDir);
462+
createFakePlugin('another_package', thirdPartyPackagesDir);
463+
fileSystem.currentDirectory = package.directory;
464+
465+
await runCapturingPrint(
466+
runner, <String>['sample', '--current-package']);
467+
468+
expect(command.plugins, unorderedEquals(<String>[package.path]));
469+
});
470+
458471
test('runs only app-facing package of a federated plugin', () async {
459472
const String pluginName = 'foo';
460473
final Directory groupDir = packagesDir.childDirectory(pluginName);

0 commit comments

Comments
 (0)