diff --git a/packages/flutter_tools/lib/src/commands/create_base.dart b/packages/flutter_tools/lib/src/commands/create_base.dart index dcb7db856af..63b66e69411 100644 --- a/packages/flutter_tools/lib/src/commands/create_base.dart +++ b/packages/flutter_tools/lib/src/commands/create_base.dart @@ -249,10 +249,16 @@ abstract class CreateBase extends FlutterCommand { @protected void validateProjectDir({bool overwrite = false}) { if (globals.fs.path.isWithin(flutterRoot, projectDirPath)) { - throwToolExit( - 'Cannot create a project within the Flutter SDK. ' - "Target directory '$projectDirPath' is within the Flutter SDK at '$flutterRoot'.", - exitCode: 2); + // Make exception for dev and examples to facilitate example project development. + final String examplesDirectory = globals.fs.path.join(flutterRoot, 'examples'); + final String devDirectory = globals.fs.path.join(flutterRoot, 'dev'); + if (!globals.fs.path.isWithin(examplesDirectory, projectDirPath) && + !globals.fs.path.isWithin(devDirectory, projectDirPath)) { + throwToolExit( + 'Cannot create a project within the Flutter SDK. ' + "Target directory '$projectDirPath' is within the Flutter SDK at '$flutterRoot'.", + exitCode: 2); + } } // If the destination directory is actually a file, then we refuse to diff --git a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart index 293b7f050e6..4db25b08325 100755 --- a/packages/flutter_tools/test/commands.shard/permeable/create_test.dart +++ b/packages/flutter_tools/test/commands.shard/permeable/create_test.dart @@ -186,6 +186,31 @@ void main() { ...noColorTerminalOverride, }); + testUsingContext('cannot create a project in flutter root', () async { + Cache.flutterRoot = '../..'; + final String flutterBin = globals.fs.path.join(getFlutterRoot(), 'bin', globals.platform.isWindows ? 'flutter.bat' : 'flutter'); + final ProcessResult exec = await Process.run( + flutterBin, + [ + 'create', + 'flutter_project', + ], + workingDirectory: Cache.flutterRoot, + ); + expect(exec.exitCode, 2); + expect(exec.stderr, contains('Cannot create a project within the Flutter SDK')); + }, overrides: { + Pub: () => Pub( + fileSystem: globals.fs, + logger: globals.logger, + processManager: globals.processManager, + usage: globals.flutterUsage, + botDetector: globals.botDetector, + platform: globals.platform, + ), + ...noColorTerminalOverride, + }); + testUsingContext('Will create an app project if non-empty non-project directory exists without .metadata', () async { await projectDir.absolute.childDirectory('blag').create(recursive: true); await projectDir.absolute.childDirectory('.idea').create(recursive: true);