diff --git a/packages/flutter_tools/lib/src/commands/packages.dart b/packages/flutter_tools/lib/src/commands/packages.dart index 4ff6e332b9a..88689f44893 100644 --- a/packages/flutter_tools/lib/src/commands/packages.dart +++ b/packages/flutter_tools/lib/src/commands/packages.dart @@ -58,7 +58,6 @@ class PackagesCommand extends FlutterCommand { class PackagesGetCommand extends FlutterCommand { PackagesGetCommand(this.name, this.upgrade) { - requiresPubspecYaml(); argParser.addFlag('offline', negatable: false, help: 'Use cached packages instead of accessing the network.', diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index 10f6749e00a..d3716fc0117 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -12,6 +12,7 @@ import '../application_package.dart'; import '../base/common.dart'; import '../base/context.dart'; import '../base/io.dart' as io; +import '../base/os.dart'; import '../base/user_messages.dart'; import '../base/utils.dart'; import '../build_info.dart'; @@ -1475,16 +1476,12 @@ abstract class FlutterCommand extends Command { // If there is no pubspec in the current directory, look in the parent // until one can be found. - bool changedDirectory = false; - while (!globals.fs.isFileSync('pubspec.yaml')) { - final Directory nextCurrent = globals.fs.currentDirectory.parent; - if (nextCurrent == null || nextCurrent.path == globals.fs.currentDirectory.path) { - throw ToolExit(userMessages.flutterNoPubspec); - } - globals.fs.currentDirectory = nextCurrent; - changedDirectory = true; + final String? path = findProjectRoot(globals.fs, globals.fs.currentDirectory.path); + if (path == null) { + throwToolExit(userMessages.flutterNoPubspec); } - if (changedDirectory) { + if (path != globals.fs.currentDirectory.path) { + globals.fs.currentDirectory = path; globals.printStatus('Changing current working directory to: ${globals.fs.currentDirectory.path}'); } } diff --git a/packages/flutter_tools/test/commands.shard/hermetic/pub_get_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/pub_get_test.dart index e8bf4537227..9a68bcec6f3 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/pub_get_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/pub_get_test.dart @@ -10,6 +10,7 @@ import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/commands/packages.dart'; import 'package:flutter_tools/src/dart/pub.dart'; +import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:meta/meta.dart'; import 'package:test/fake.dart'; @@ -77,6 +78,24 @@ void main() { FileSystem: () => fileSystem, }); + testUsingContext('pub get on target directory', () async { + fileSystem.currentDirectory.childDirectory('target').createSync(); + final Directory targetDirectory = fileSystem.currentDirectory.childDirectory('target'); + targetDirectory.childFile('pubspec.yaml').createSync(); + + final PackagesGetCommand command = PackagesGetCommand('get', false); + final CommandRunner commandRunner = createTestCommandRunner(command); + + await commandRunner.run(['get', targetDirectory.path]); + final FlutterProject rootProject = FlutterProject.fromDirectory(targetDirectory); + expect(rootProject.packageConfigFile.existsSync(), true); + expect(await rootProject.packageConfigFile.readAsString(), '{"configVersion":2,"packages":[]}'); + }, overrides: { + Pub: () => pub, + ProcessManager: () => FakeProcessManager.any(), + FileSystem: () => fileSystem, + }); + testUsingContext("pub get skips example directory if it doesn't contain a pubspec.yaml", () async { fileSystem.currentDirectory.childFile('pubspec.yaml').createSync(); fileSystem.currentDirectory.childDirectory('example').createSync(recursive: true); @@ -116,7 +135,7 @@ class FakePub extends Fake implements Pub { bool shouldSkipThirdPartyGenerator = true, bool printProgress = true, }) async { - fileSystem.currentDirectory + fileSystem.directory(directory) .childDirectory('.dart_tool') .childFile('package_config.json') ..createSync(recursive: true)