From 762d9f7f4e2099c8fa1abcf7686046387dd69f1d Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 5 Aug 2020 15:11:15 -0700 Subject: [PATCH] [flutter_tools] warn when main library is relocated (#62930) --- packages/flutter_tools/lib/src/run_hot.dart | 11 +++- .../general.shard/resident_runner_test.dart | 61 +++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/packages/flutter_tools/lib/src/run_hot.dart b/packages/flutter_tools/lib/src/run_hot.dart index 6d4aed4064..77a7867eab 100644 --- a/packages/flutter_tools/lib/src/run_hot.dart +++ b/packages/flutter_tools/lib/src/run_hot.dart @@ -376,10 +376,19 @@ class HotRunner extends ResidentRunner { asyncScanning: hotRunnerConfig.asyncScanning, packageConfig: flutterDevices[0].devFS.lastPackageConfig, ); + final File entrypointFile = globals.fs.file(mainPath); + if (!entrypointFile.existsSync()) { + globals.printError( + 'The entrypoint file (i.e. the file with main()) ${entrypointFile.path} ' + 'cannot be found. Moving or renaming this file will prevent changes to ' + 'its contents from being discovered during hot reload/restart until ' + 'flutter is restarted or the file is restored.' + ); + } final UpdateFSReport results = UpdateFSReport(success: true); for (final FlutterDevice device in flutterDevices) { results.incorporateResults(await device.updateDevFS( - mainUri: globals.fs.file(mainPath).absolute.uri, + mainUri: entrypointFile.absolute.uri, target: target, bundle: assetBundle, firstBuildTime: firstBuildTime, diff --git a/packages/flutter_tools/test/general.shard/resident_runner_test.dart b/packages/flutter_tools/test/general.shard/resident_runner_test.dart index 4d6ba6552c..93a05f7f37 100644 --- a/packages/flutter_tools/test/general.shard/resident_runner_test.dart +++ b/packages/flutter_tools/test/general.shard/resident_runner_test.dart @@ -609,6 +609,67 @@ void main() { Usage: () => MockUsage(), })); + testUsingContext('ResidentRunner reports error with missing entrypoint file', () => testbed.run(() async { + fakeVmServiceHost = FakeVmServiceHost(requests: [ + listViews, + listViews, + listViews, + listViews, + FakeVmServiceRequest( + method: 'getIsolate', + args: { + 'isolateId': '1', + }, + jsonResponse: fakeUnpausedIsolate.toJson(), + ), + FakeVmServiceRequest( + method: 'ext.flutter.reassemble', + args: { + 'isolateId': fakeUnpausedIsolate.id, + }, + ), + ]); + when(mockDevice.sdkNameAndVersion).thenAnswer((Invocation invocation) async { + return 'Example'; + }); + when(mockDevice.targetPlatform).thenAnswer((Invocation invocation) async { + return TargetPlatform.android_arm; + }); + when(mockDevice.isLocalEmulator).thenAnswer((Invocation invocation) async { + return false; + }); + final Completer onConnectionInfo = Completer.sync(); + final Completer onAppStart = Completer.sync(); + unawaited(residentRunner.attach( + appStartedCompleter: onAppStart, + connectionInfoCompleter: onConnectionInfo, + )); + await onAppStart.future; + when(mockFlutterDevice.updateDevFS( + mainUri: anyNamed('mainUri'), + target: anyNamed('target'), + bundle: anyNamed('bundle'), + firstBuildTime: anyNamed('firstBuildTime'), + bundleFirstUpload: anyNamed('bundleFirstUpload'), + bundleDirty: anyNamed('bundleDirty'), + fullRestart: anyNamed('fullRestart'), + projectRootPath: anyNamed('projectRootPath'), + pathToReload: anyNamed('pathToReload'), + invalidatedFiles: anyNamed('invalidatedFiles'), + dillOutputPath: anyNamed('dillOutputPath'), + packageConfig: anyNamed('packageConfig'), + )).thenAnswer((Invocation invocation) async { + return UpdateFSReport(success: true); + }); + + final OperationResult result = await residentRunner.restart(fullRestart: false); + + expect(globals.fs.file(globals.fs.path.join('lib', 'main.dart')), isNot(exists)); + expect(testLogger.errorText, contains('The entrypoint file (i.e. the file with main())')); + expect(result.fatal, false); + expect(result.code, 0); + })); + testUsingContext('ResidentRunner can send target platform to analytics from hot reload', () => testbed.run(() async { fakeVmServiceHost = FakeVmServiceHost(requests: [