Skip to content

Commit 3da9eee

Browse files
authored
[flutter_tools] re-use findProjectRoot on flutter command (#104850)
1 parent 7df7e8a commit 3da9eee

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class PackagesCommand extends FlutterCommand {
5858

5959
class PackagesGetCommand extends FlutterCommand {
6060
PackagesGetCommand(this.name, this.upgrade) {
61-
requiresPubspecYaml();
6261
argParser.addFlag('offline',
6362
negatable: false,
6463
help: 'Use cached packages instead of accessing the network.',

packages/flutter_tools/lib/src/runner/flutter_command.dart

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import '../application_package.dart';
1212
import '../base/common.dart';
1313
import '../base/context.dart';
1414
import '../base/io.dart' as io;
15+
import '../base/os.dart';
1516
import '../base/user_messages.dart';
1617
import '../base/utils.dart';
1718
import '../build_info.dart';
@@ -1475,16 +1476,12 @@ abstract class FlutterCommand extends Command<void> {
14751476

14761477
// If there is no pubspec in the current directory, look in the parent
14771478
// until one can be found.
1478-
bool changedDirectory = false;
1479-
while (!globals.fs.isFileSync('pubspec.yaml')) {
1480-
final Directory nextCurrent = globals.fs.currentDirectory.parent;
1481-
if (nextCurrent == null || nextCurrent.path == globals.fs.currentDirectory.path) {
1482-
throw ToolExit(userMessages.flutterNoPubspec);
1483-
}
1484-
globals.fs.currentDirectory = nextCurrent;
1485-
changedDirectory = true;
1479+
final String? path = findProjectRoot(globals.fs, globals.fs.currentDirectory.path);
1480+
if (path == null) {
1481+
throwToolExit(userMessages.flutterNoPubspec);
14861482
}
1487-
if (changedDirectory) {
1483+
if (path != globals.fs.currentDirectory.path) {
1484+
globals.fs.currentDirectory = path;
14881485
globals.printStatus('Changing current working directory to: ${globals.fs.currentDirectory.path}');
14891486
}
14901487
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:flutter_tools/src/base/file_system.dart';
1010
import 'package:flutter_tools/src/cache.dart';
1111
import 'package:flutter_tools/src/commands/packages.dart';
1212
import 'package:flutter_tools/src/dart/pub.dart';
13+
import 'package:flutter_tools/src/project.dart';
1314
import 'package:flutter_tools/src/reporting/reporting.dart';
1415
import 'package:meta/meta.dart';
1516
import 'package:test/fake.dart';
@@ -77,6 +78,24 @@ void main() {
7778
FileSystem: () => fileSystem,
7879
});
7980

81+
testUsingContext('pub get on target directory', () async {
82+
fileSystem.currentDirectory.childDirectory('target').createSync();
83+
final Directory targetDirectory = fileSystem.currentDirectory.childDirectory('target');
84+
targetDirectory.childFile('pubspec.yaml').createSync();
85+
86+
final PackagesGetCommand command = PackagesGetCommand('get', false);
87+
final CommandRunner<void> commandRunner = createTestCommandRunner(command);
88+
89+
await commandRunner.run(<String>['get', targetDirectory.path]);
90+
final FlutterProject rootProject = FlutterProject.fromDirectory(targetDirectory);
91+
expect(rootProject.packageConfigFile.existsSync(), true);
92+
expect(await rootProject.packageConfigFile.readAsString(), '{"configVersion":2,"packages":[]}');
93+
}, overrides: <Type, Generator>{
94+
Pub: () => pub,
95+
ProcessManager: () => FakeProcessManager.any(),
96+
FileSystem: () => fileSystem,
97+
});
98+
8099
testUsingContext("pub get skips example directory if it doesn't contain a pubspec.yaml", () async {
81100
fileSystem.currentDirectory.childFile('pubspec.yaml').createSync();
82101
fileSystem.currentDirectory.childDirectory('example').createSync(recursive: true);
@@ -116,7 +135,7 @@ class FakePub extends Fake implements Pub {
116135
bool shouldSkipThirdPartyGenerator = true,
117136
bool printProgress = true,
118137
}) async {
119-
fileSystem.currentDirectory
138+
fileSystem.directory(directory)
120139
.childDirectory('.dart_tool')
121140
.childFile('package_config.json')
122141
..createSync(recursive: true)

0 commit comments

Comments
 (0)