diff --git a/packages/flutter_tools/lib/src/commands/packages.dart b/packages/flutter_tools/lib/src/commands/packages.dart index 9ef6a47808..0303db9a26 100644 --- a/packages/flutter_tools/lib/src/commands/packages.dart +++ b/packages/flutter_tools/lib/src/commands/packages.dart @@ -234,7 +234,7 @@ class PackagesGetCommand extends FlutterCommand { @override Future runCommand() async { - List rest = argResults!.rest; + final List rest = argResults!.rest; bool isHelp = false; bool example = true; bool exampleWasParsed = false; @@ -254,34 +254,12 @@ class PackagesGetCommand extends FlutterCommand { FlutterProject? rootProject; if (!isHelp) { - if (directoryOption == null && - rest.length == 1 && - // Anything that looks like an argument should not be interpreted as - // a directory. - !rest.single.startsWith('-') && - ((rest.single.contains('/') || rest.single.contains(r'\')) || - name == 'get')) { - // For historical reasons, if there is one argument to the command and it contains - // a multiple-component path (i.e. contains a slash) then we use that to determine - // to which project we're applying the command. - target = findProjectRoot(globals.fs, rest.single); - - globals.printWarning(''' - Using a naked argument for directory is deprecated and will stop working in a future Flutter release. - - Use --directory instead.'''); - if (target == null) { - throwToolExit('Expected to find project root in ${rest.single}.'); - } - rest = []; - } else { - target = findProjectRoot(globals.fs, directoryOption); - if (target == null) { - if (directoryOption == null) { - throwToolExit('Expected to find project root in current working directory.'); - } else { - throwToolExit('Expected to find project root in $directoryOption.'); - } + target = findProjectRoot(globals.fs, directoryOption); + if (target == null) { + if (directoryOption == null) { + throwToolExit('Expected to find project root in current working directory.'); + } else { + throwToolExit('Expected to find project root in $directoryOption.'); } } diff --git a/packages/flutter_tools/test/commands.shard/hermetic/pub_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/pub_test.dart index 885ef48a1f..d5199b94db 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/pub_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/pub_test.dart @@ -114,7 +114,7 @@ void main() { final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet); final CommandRunner commandRunner = createTestCommandRunner(command); - await commandRunner.run(['get', targetDirectory.path]); + await commandRunner.run(['get', '--directory=${targetDirectory.path}']); final FlutterProject rootProject = FlutterProject.fromDirectory(targetDirectory); final File packageConfigFile = rootProject.dartTool.childFile('package_config.json'); expect(packageConfigFile.existsSync(), true); @@ -138,7 +138,7 @@ void main() { FileSystem: () => fileSystem, }); - testUsingContext("pub get doesn't treat -v as directory", () async { + testUsingContext("pub get doesn't treat -v as directory", () async { fileSystem.currentDirectory.childDirectory('target').createSync(); fileSystem.currentDirectory.childFile('pubspec.yaml').createSync(); final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet); @@ -151,6 +151,34 @@ void main() { FileSystem: () => fileSystem, }); + // Regression test for https://github.com/flutter/flutter/issues/144898 + // Regression test for https://github.com/flutter/flutter/issues/160145 + testUsingContext("pub add doesn't treat dependency syntax as directory", () async { + fileSystem.currentDirectory.childDirectory('target').createSync(); + fileSystem.currentDirectory.childFile('pubspec.yaml').createSync(); + fileSystem.currentDirectory.childDirectory('example').createSync(recursive: true); + fileSystem.currentDirectory.childDirectory('android').childFile('AndroidManifest.xml') + ..createSync(recursive: true) + ..writeAsStringSync(minimalV2EmbeddingManifest); + + final PackagesGetCommand command = PackagesGetCommand('add', '', PubContext.pubAdd); + final CommandRunner commandRunner = createTestCommandRunner(command); + const List availableSyntax = [ + 'foo:{"path":"../foo"}', + 'foo:{"hosted":"my-pub.dev"}', + 'foo:{"sdk":"flutter"}', + 'foo:{"git":"https://github.com/foo/foo"}', + ]; + for (final String syntax in availableSyntax) { + pub.expectedArguments = ['add', syntax, '--example', '--directory', '.']; + await commandRunner.run(['add', syntax]); + } + }, 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); @@ -179,7 +207,7 @@ void main() { final CommandRunner commandRunner = createTestCommandRunner(command); try { - await commandRunner.run(['get', 'missing_dir']); + await commandRunner.run(['get', '--directory=missing_dir']); fail('expected an exception'); } on Exception catch (e) { expect(e.toString(), contains('Expected to find project root in missing_dir'));