diff --git a/packages/flutter_tools/lib/src/base/os.dart b/packages/flutter_tools/lib/src/base/os.dart index f4d3a9fff5..ff437513f1 100644 --- a/packages/flutter_tools/lib/src/base/os.dart +++ b/packages/flutter_tools/lib/src/base/os.dart @@ -590,15 +590,15 @@ class _WindowsUtils extends OperatingSystemUtils { 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 e3145bfc95..95a4e146fd 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 @@ class BuildAarCommand extends BuildSubCommand { 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 6861f22253..e8b9db01ac 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 @@ void main() { FileSystem: () => fileSystem, }); + testUsingContext('pub get throws error on missing directory', () async { + final PackagesGetCommand command = PackagesGetCommand('get', false); + final CommandRunner commandRunner = createTestCommandRunner(command); + + try { + await commandRunner.run(['get', 'missing_dir']); + fail('expected an exception'); + } on Exception catch (e) { + expect(e.toString(), contains('Expected to find project root in missing_dir')); + } + }, overrides: { + 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 dceac215eb..087e65604c 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 @@ void main() { }); }); + group('throws ToolExit', () { + testUsingContext('main.dart not found', () async { + await expectLater(() async { + await runBuildAarCommand( + 'missing_project', + arguments: ['--no-pub'], + ); + }, throwsToolExit( + message: 'main.dart does not exist', + )); + }); + + testUsingContext('flutter project not valid', () async { + await expectLater(() async { + await runCommandIn( + tempDir.path, + arguments: ['--no-pub'], + ); + }, throwsToolExit( + message: 'is not a valid flutter project', + )); + }); + }); + testUsingContext('support ExtraDartFlagOptions', () async { final String projectPath = await createProject(tempDir, arguments: ['--no-pub', '--template=module']);