Skip to content

Commit a0f2424

Browse files
authored
🐛 [tool] Do not handle directory arguments implicitly for pub commands (#160223)
- Resolves flutter/flutter#144898 - Resolves flutter/flutter#160145 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 2bda0cc commit a0f2424

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

packages/flutter_tools/lib/src/commands/packages.dart

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ class PackagesGetCommand extends FlutterCommand {
234234

235235
@override
236236
Future<FlutterCommandResult> runCommand() async {
237-
List<String> rest = argResults!.rest;
237+
final List<String> rest = argResults!.rest;
238238
bool isHelp = false;
239239
bool example = true;
240240
bool exampleWasParsed = false;
@@ -254,34 +254,12 @@ class PackagesGetCommand extends FlutterCommand {
254254
FlutterProject? rootProject;
255255

256256
if (!isHelp) {
257-
if (directoryOption == null &&
258-
rest.length == 1 &&
259-
// Anything that looks like an argument should not be interpreted as
260-
// a directory.
261-
!rest.single.startsWith('-') &&
262-
((rest.single.contains('/') || rest.single.contains(r'\')) ||
263-
name == 'get')) {
264-
// For historical reasons, if there is one argument to the command and it contains
265-
// a multiple-component path (i.e. contains a slash) then we use that to determine
266-
// to which project we're applying the command.
267-
target = findProjectRoot(globals.fs, rest.single);
268-
269-
globals.printWarning('''
270-
Using a naked argument for directory is deprecated and will stop working in a future Flutter release.
271-
272-
Use --directory instead.''');
273-
if (target == null) {
274-
throwToolExit('Expected to find project root in ${rest.single}.');
275-
}
276-
rest = <String>[];
277-
} else {
278-
target = findProjectRoot(globals.fs, directoryOption);
279-
if (target == null) {
280-
if (directoryOption == null) {
281-
throwToolExit('Expected to find project root in current working directory.');
282-
} else {
283-
throwToolExit('Expected to find project root in $directoryOption.');
284-
}
257+
target = findProjectRoot(globals.fs, directoryOption);
258+
if (target == null) {
259+
if (directoryOption == null) {
260+
throwToolExit('Expected to find project root in current working directory.');
261+
} else {
262+
throwToolExit('Expected to find project root in $directoryOption.');
285263
}
286264
}
287265

packages/flutter_tools/test/commands.shard/hermetic/pub_test.dart

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ void main() {
114114
final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet);
115115
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
116116

117-
await commandRunner.run(<String>['get', targetDirectory.path]);
117+
await commandRunner.run(<String>['get', '--directory=${targetDirectory.path}']);
118118
final FlutterProject rootProject = FlutterProject.fromDirectory(targetDirectory);
119119
final File packageConfigFile = rootProject.dartTool.childFile('package_config.json');
120120
expect(packageConfigFile.existsSync(), true);
@@ -138,7 +138,7 @@ void main() {
138138
FileSystem: () => fileSystem,
139139
});
140140

141-
testUsingContext("pub get doesn't treat -v as directory", () async {
141+
testUsingContext("pub get doesn't treat -v as directory", () async {
142142
fileSystem.currentDirectory.childDirectory('target').createSync();
143143
fileSystem.currentDirectory.childFile('pubspec.yaml').createSync();
144144
final PackagesGetCommand command = PackagesGetCommand('get', '', PubContext.pubGet);
@@ -151,6 +151,34 @@ void main() {
151151
FileSystem: () => fileSystem,
152152
});
153153

154+
// Regression test for https://github.com/flutter/flutter/issues/144898
155+
// Regression test for https://github.com/flutter/flutter/issues/160145
156+
testUsingContext("pub add doesn't treat dependency syntax as directory", () async {
157+
fileSystem.currentDirectory.childDirectory('target').createSync();
158+
fileSystem.currentDirectory.childFile('pubspec.yaml').createSync();
159+
fileSystem.currentDirectory.childDirectory('example').createSync(recursive: true);
160+
fileSystem.currentDirectory.childDirectory('android').childFile('AndroidManifest.xml')
161+
..createSync(recursive: true)
162+
..writeAsStringSync(minimalV2EmbeddingManifest);
163+
164+
final PackagesGetCommand command = PackagesGetCommand('add', '', PubContext.pubAdd);
165+
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
166+
const List<String> availableSyntax = <String>[
167+
'foo:{"path":"../foo"}',
168+
'foo:{"hosted":"my-pub.dev"}',
169+
'foo:{"sdk":"flutter"}',
170+
'foo:{"git":"https://github.com/foo/foo"}',
171+
];
172+
for (final String syntax in availableSyntax) {
173+
pub.expectedArguments = <String>['add', syntax, '--example', '--directory', '.'];
174+
await commandRunner.run(<String>['add', syntax]);
175+
}
176+
}, overrides: <Type, Generator>{
177+
Pub: () => pub,
178+
ProcessManager: () => FakeProcessManager.any(),
179+
FileSystem: () => fileSystem,
180+
});
181+
154182
testUsingContext("pub get skips example directory if it doesn't contain a pubspec.yaml", () async {
155183
fileSystem.currentDirectory.childFile('pubspec.yaml').createSync();
156184
fileSystem.currentDirectory.childDirectory('example').createSync(recursive: true);
@@ -179,7 +207,7 @@ void main() {
179207
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
180208

181209
try {
182-
await commandRunner.run(<String>['get', 'missing_dir']);
210+
await commandRunner.run(<String>['get', '--directory=missing_dir']);
183211
fail('expected an exception');
184212
} on Exception catch (e) {
185213
expect(e.toString(), contains('Expected to find project root in missing_dir'));

0 commit comments

Comments
 (0)