Use directory exists instead of path.dirname (#112219)
diff --git a/packages/flutter_tools/lib/src/base/os.dart b/packages/flutter_tools/lib/src/base/os.dart
index f4d3a9f..ff43751 100644
--- a/packages/flutter_tools/lib/src/base/os.dart
+++ b/packages/flutter_tools/lib/src/base/os.dart
@@ -590,15 +590,15 @@
String? findProjectRoot(FileSystem fileSystem, [ String? directory ]) {
const String kProjectRootSentinel = 'pubspec.yaml';
directory ??= fileSystem.currentDirectory.path;
+ Directory currentDirectory = fileSystem.directory(directory).absolute;
while (true) {
- if (fileSystem.isFileSync(fileSystem.path.join(directory!, kProjectRootSentinel))) {
- return directory;
+ if (currentDirectory.childFile(kProjectRootSentinel).existsSync()) {
+ return currentDirectory.path;
}
- final String parent = fileSystem.path.dirname(directory);
- if (directory == parent) {
+ if (!currentDirectory.existsSync() || currentDirectory.parent.path == currentDirectory.path) {
return null;
}
- directory = parent;
+ currentDirectory = currentDirectory.parent;
}
}
diff --git a/packages/flutter_tools/lib/src/commands/build_aar.dart b/packages/flutter_tools/lib/src/commands/build_aar.dart
index e3145bf..95a4e14 100644
--- a/packages/flutter_tools/lib/src/commands/build_aar.dart
+++ b/packages/flutter_tools/lib/src/commands/build_aar.dart
@@ -155,6 +155,21 @@
if (remainingArguments.isEmpty) {
return FlutterProject.current();
}
- return FlutterProject.fromDirectory(globals.fs.directory(findProjectRoot(globals.fs, remainingArguments.first)));
+ final File mainFile = globals.fs.file(remainingArguments.first);
+ final String path;
+ if (!mainFile.existsSync()) {
+ final Directory pathProject = globals.fs.directory(remainingArguments.first);
+ if (!pathProject.existsSync()) {
+ throwToolExit('${remainingArguments.first} does not exist');
+ }
+ path = pathProject.path;
+ } else {
+ path = mainFile.parent.path;
+ }
+ final String? projectRoot = findProjectRoot(globals.fs, path);
+ if (projectRoot == null) {
+ throwToolExit('${mainFile.parent.path} is not a valid flutter project');
+ }
+ return FlutterProject.fromDirectory(globals.fs.directory(projectRoot));
}
}
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 6861f22..e8b9db0 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
@@ -114,6 +114,22 @@
FileSystem: () => fileSystem,
});
+ testUsingContext('pub get throws error on missing directory', () async {
+ final PackagesGetCommand command = PackagesGetCommand('get', false);
+ final CommandRunner<void> commandRunner = createTestCommandRunner(command);
+
+ try {
+ await commandRunner.run(<String>['get', 'missing_dir']);
+ fail('expected an exception');
+ } on Exception catch (e) {
+ expect(e.toString(), contains('Expected to find project root in missing_dir'));
+ }
+ }, overrides: <Type, Generator>{
+ Pub: () => pub,
+ ProcessManager: () => FakeProcessManager.any(),
+ FileSystem: () => fileSystem,
+ });
+
testUsingContext('pub get triggers localizations generation when generate: true', () async {
final File pubspecFile = fileSystem.currentDirectory.childFile('pubspec.yaml')
..createSync();
diff --git a/packages/flutter_tools/test/commands.shard/permeable/build_aar_test.dart b/packages/flutter_tools/test/commands.shard/permeable/build_aar_test.dart
index dceac21..087e656 100644
--- a/packages/flutter_tools/test/commands.shard/permeable/build_aar_test.dart
+++ b/packages/flutter_tools/test/commands.shard/permeable/build_aar_test.dart
@@ -230,6 +230,30 @@
});
});
+ group('throws ToolExit', () {
+ testUsingContext('main.dart not found', () async {
+ await expectLater(() async {
+ await runBuildAarCommand(
+ 'missing_project',
+ arguments: <String>['--no-pub'],
+ );
+ }, throwsToolExit(
+ message: 'main.dart does not exist',
+ ));
+ });
+
+ testUsingContext('flutter project not valid', () async {
+ await expectLater(() async {
+ await runCommandIn(
+ tempDir.path,
+ arguments: <String>['--no-pub'],
+ );
+ }, throwsToolExit(
+ message: 'is not a valid flutter project',
+ ));
+ });
+ });
+
testUsingContext('support ExtraDartFlagOptions', () async {
final String projectPath = await createProject(tempDir,
arguments: <String>['--no-pub', '--template=module']);