From 13382f41c06acca01bcfd24725ef7d6ef9d368cf Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Fri, 12 Jul 2019 23:24:04 -0700 Subject: [PATCH] [flutter_tool] Catch a yaml parse failure during project creation (#36105) --- packages/flutter_tools/lib/src/project.dart | 12 ++++++++-- packages/flutter_tools/test/project_test.dart | 24 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/packages/flutter_tools/lib/src/project.dart b/packages/flutter_tools/lib/src/project.dart index 00762991956..0a1b5b848de 100644 --- a/packages/flutter_tools/lib/src/project.dart +++ b/packages/flutter_tools/lib/src/project.dart @@ -16,6 +16,7 @@ import 'bundle.dart' as bundle; import 'cache.dart'; import 'desktop.dart'; import 'flutter_manifest.dart'; +import 'globals.dart'; import 'ios/ios_workflow.dart'; import 'ios/plist_utils.dart' as plist; import 'ios/xcodeproj.dart' as xcode; @@ -177,9 +178,16 @@ class FlutterProject { /// Completes with an empty [FlutterManifest], if the file does not exist. /// Completes with a ToolExit on validation error. static FlutterManifest _readManifest(String path) { - final FlutterManifest manifest = FlutterManifest.createFromPath(path); - if (manifest == null) + FlutterManifest manifest; + try { + manifest = FlutterManifest.createFromPath(path); + } on YamlException catch (e) { + printStatus('Error detected in pubspec.yaml:', emphasis: true); + printError('$e'); + } + if (manifest == null) { throwToolExit('Please correct the pubspec.yaml file at $path'); + } return manifest; } diff --git a/packages/flutter_tools/test/project_test.dart b/packages/flutter_tools/test/project_test.dart index c52508480ae..ad984a0a716 100644 --- a/packages/flutter_tools/test/project_test.dart +++ b/packages/flutter_tools/test/project_test.dart @@ -40,7 +40,19 @@ void main() { expect( () => FlutterProject.fromDirectory(directory), - throwsA(isInstanceOf()), + throwsA(isInstanceOf()), + ); + }); + + testInMemory('fails on pubspec.yaml parse failure', () async { + final Directory directory = fs.directory('myproject'); + directory.childFile('pubspec.yaml') + ..createSync(recursive: true) + ..writeAsStringSync(parseErrorPubspec); + + expect( + () => FlutterProject.fromDirectory(directory), + throwsA(isInstanceOf()), ); }); @@ -52,7 +64,7 @@ void main() { expect( () => FlutterProject.fromDirectory(directory), - throwsA(isInstanceOf()), + throwsA(isInstanceOf()), ); }); @@ -575,6 +587,14 @@ flutter: invalid: '''; +String get parseErrorPubspec => ''' +name: hello +# Whitespace is important. +flutter: + something: + something_else: +'''; + String projectFileWithBundleId(String id, {String qualifier}) { return ''' 97C147061CF9000F007C117D /* Debug */ = {