[flutter_tools] pretty print hot reload rejection error (#66701)
If the vm of an attached device rejects a hot reload, pretty print the reason. Suggest a hot restart so that users are aware that they do not have to detach and rebuild. Also resets the last compilation time, so a subsequent restart would still apply the last change. Adds an integration test for the const field removal. Fixes #64027
This commit is contained in:
@@ -207,6 +207,9 @@ class DevFSException implements Exception {
|
||||
final String message;
|
||||
final dynamic error;
|
||||
final StackTrace stackTrace;
|
||||
|
||||
@override
|
||||
String toString() => 'DevFSException($message, $error, $stackTrace)';
|
||||
}
|
||||
|
||||
/// Interface responsible for syncing asset files to a development device.
|
||||
@@ -399,6 +402,7 @@ class DevFS {
|
||||
|
||||
List<Uri> sources = <Uri>[];
|
||||
DateTime lastCompiled;
|
||||
DateTime _previousCompiled;
|
||||
PackageConfig lastPackageConfig;
|
||||
File _widgetCacheOutputFile;
|
||||
|
||||
@@ -440,6 +444,20 @@ class DevFS {
|
||||
_logger.printTrace('DevFS: Deleted filesystem on the device ($_baseUri)');
|
||||
}
|
||||
|
||||
/// Mark the [lastCompiled] time to the previous successful compile.
|
||||
///
|
||||
/// Sometimes a hot reload will be rejected by the VM due to a change in the
|
||||
/// structure of the code not supporting the hot reload. In these cases,
|
||||
/// the best resolution is a hot restart. However, the resident runner
|
||||
/// will not recognize this file as having been changed since the delta
|
||||
/// will already have been accepted. Instead, reset the compile time so
|
||||
/// that the last updated files are included in subsequent compilations until
|
||||
/// a reload is accepted.
|
||||
void resetLastCompiled() {
|
||||
lastCompiled = _previousCompiled;
|
||||
}
|
||||
|
||||
|
||||
/// If the build method of a single widget was modified, return the widget name.
|
||||
///
|
||||
/// If any other changes were made, or there is an error scanning the file,
|
||||
@@ -520,6 +538,7 @@ class DevFS {
|
||||
return UpdateFSReport(success: false);
|
||||
}
|
||||
// Only update the last compiled time if we successfully compiled.
|
||||
_previousCompiled = lastCompiled;
|
||||
lastCompiled = candidateCompileTime;
|
||||
// list of sources that needs to be monitored are in [compilerOutput.sources]
|
||||
sources = compilerOutput.sources;
|
||||
|
||||
@@ -926,6 +926,11 @@ class WebDevFS implements DevFS {
|
||||
'web',
|
||||
'dart_stack_trace_mapper.js',
|
||||
));
|
||||
|
||||
@override
|
||||
void resetLastCompiled() {
|
||||
// Not used for web compilation.
|
||||
}
|
||||
}
|
||||
|
||||
class ReleaseAssetServer {
|
||||
|
||||
@@ -358,37 +358,6 @@ class FlutterDevice {
|
||||
return devFS.create();
|
||||
}
|
||||
|
||||
Future<List<Future<vm_service.ReloadReport>>> reloadSources(
|
||||
String entryPath, {
|
||||
bool pause = false,
|
||||
}) async {
|
||||
final String deviceEntryUri = devFS.baseUri
|
||||
.resolveUri(globals.fs.path.toUri(entryPath)).toString();
|
||||
final vm_service.VM vm = await vmService.getVM();
|
||||
return <Future<vm_service.ReloadReport>>[
|
||||
for (final vm_service.IsolateRef isolateRef in vm.isolates)
|
||||
vmService.reloadSources(
|
||||
isolateRef.id,
|
||||
pause: pause,
|
||||
rootLibUri: deviceEntryUri,
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
Future<void> resetAssetDirectory() async {
|
||||
final Uri deviceAssetsDirectoryUri = devFS.baseUri.resolveUri(
|
||||
globals.fs.path.toUri(getAssetBuildDirectory()));
|
||||
assert(deviceAssetsDirectoryUri != null);
|
||||
final List<FlutterView> views = await vmService.getFlutterViews();
|
||||
await Future.wait<void>(views.map<Future<void>>(
|
||||
(FlutterView view) => vmService.setAssetDirectory(
|
||||
assetsDirectory: deviceAssetsDirectoryUri,
|
||||
uiIsolateId: view.uiIsolate.id,
|
||||
viewId: view.id,
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
Future<void> debugDumpApp() async {
|
||||
final List<FlutterView> views = await vmService.getFlutterViews();
|
||||
for (final FlutterView view in views) {
|
||||
|
||||
@@ -406,6 +406,12 @@ class HotRunner extends ResidentRunner {
|
||||
return results;
|
||||
}
|
||||
|
||||
void _resetDevFSCompileTime() {
|
||||
for (final FlutterDevice device in flutterDevices) {
|
||||
device.devFS.resetLastCompiled();
|
||||
}
|
||||
}
|
||||
|
||||
void _resetDirtyAssets() {
|
||||
for (final FlutterDevice device in flutterDevices) {
|
||||
device.devFS.assetPathsToEvict.clear();
|
||||
@@ -593,7 +599,7 @@ class HotRunner extends ResidentRunner {
|
||||
/// Returns [true] if the reload was successful.
|
||||
/// Prints errors if [printErrors] is [true].
|
||||
static bool validateReloadReport(
|
||||
Map<String, dynamic> reloadReport, {
|
||||
vm_service.ReloadReport reloadReport, {
|
||||
bool printErrors = true,
|
||||
}) {
|
||||
if (reloadReport == null) {
|
||||
@@ -602,29 +608,12 @@ class HotRunner extends ResidentRunner {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!(reloadReport['type'] == 'ReloadReport' &&
|
||||
(reloadReport['success'] == true ||
|
||||
(reloadReport['success'] == false &&
|
||||
(reloadReport['details'] is Map<String, dynamic> &&
|
||||
reloadReport['details']['notices'] is List<dynamic> &&
|
||||
(reloadReport['details']['notices'] as List<dynamic>).isNotEmpty &&
|
||||
(reloadReport['details']['notices'] as List<dynamic>).every(
|
||||
(dynamic item) => item is Map<String, dynamic> && item['message'] is String
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)) {
|
||||
if (printErrors) {
|
||||
globals.printError('Hot reload received invalid response: $reloadReport');
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (!(reloadReport['success'] as bool)) {
|
||||
final ReloadReportContents contents = ReloadReportContents.fromReloadReport(reloadReport);
|
||||
if (!reloadReport.success) {
|
||||
if (printErrors) {
|
||||
globals.printError('Hot reload was rejected:');
|
||||
for (final Map<String, dynamic> notice in (reloadReport['details']['notices'] as List<dynamic>).cast<Map<String, dynamic>>()) {
|
||||
globals.printError('${notice['message']}');
|
||||
for (final ReasonForCancelling reason in contents.notices) {
|
||||
globals.printError(reason.toString());
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -798,6 +787,38 @@ class HotRunner extends ResidentRunner {
|
||||
return result;
|
||||
}
|
||||
|
||||
Future<List<Future<vm_service.ReloadReport>>> _reloadDeviceSources(
|
||||
FlutterDevice device,
|
||||
String entryPath, {
|
||||
bool pause = false,
|
||||
}) async {
|
||||
final String deviceEntryUri = device.devFS.baseUri
|
||||
.resolveUri(globals.fs.path.toUri(entryPath)).toString();
|
||||
final vm_service.VM vm = await device.vmService.getVM();
|
||||
return <Future<vm_service.ReloadReport>>[
|
||||
for (final vm_service.IsolateRef isolateRef in vm.isolates)
|
||||
device.vmService.reloadSources(
|
||||
isolateRef.id,
|
||||
pause: pause,
|
||||
rootLibUri: deviceEntryUri,
|
||||
)
|
||||
];
|
||||
}
|
||||
|
||||
Future<void> _resetAssetDirectory(FlutterDevice device) async {
|
||||
final Uri deviceAssetsDirectoryUri = device.devFS.baseUri.resolveUri(
|
||||
globals.fs.path.toUri(getAssetBuildDirectory()));
|
||||
assert(deviceAssetsDirectoryUri != null);
|
||||
final List<FlutterView> views = await device.vmService.getFlutterViews();
|
||||
await Future.wait<void>(views.map<Future<void>>(
|
||||
(FlutterView view) => device.vmService.setAssetDirectory(
|
||||
assetsDirectory: deviceAssetsDirectoryUri,
|
||||
uiIsolateId: view.uiIsolate.id,
|
||||
viewId: view.id,
|
||||
)
|
||||
));
|
||||
}
|
||||
|
||||
Future<OperationResult> _reloadSources({
|
||||
String targetPlatform,
|
||||
String sdkName,
|
||||
@@ -835,12 +856,13 @@ class HotRunner extends ResidentRunner {
|
||||
final List<Future<DeviceReloadReport>> allReportsFutures = <Future<DeviceReloadReport>>[];
|
||||
for (final FlutterDevice device in flutterDevices) {
|
||||
if (_shouldResetAssetDirectory) {
|
||||
// Asset directory has to be set only once when we switch from
|
||||
// Asset directory has to be set only once when the engine switches from
|
||||
// running from bundle to uploaded files.
|
||||
await device.resetAssetDirectory();
|
||||
await _resetAssetDirectory(device);
|
||||
_shouldResetAssetDirectory = false;
|
||||
}
|
||||
final List<Future<vm_service.ReloadReport>> reportFutures = await device.reloadSources(
|
||||
final List<Future<vm_service.ReloadReport>> reportFutures = await _reloadDeviceSources(
|
||||
device,
|
||||
entryPath, pause: pause,
|
||||
);
|
||||
allReportsFutures.add(Future.wait(reportFutures).then(
|
||||
@@ -851,7 +873,7 @@ class HotRunner extends ResidentRunner {
|
||||
// Don't print errors because they will be printed further down when
|
||||
// `validateReloadReport` is called again.
|
||||
await device.updateReloadStatus(
|
||||
validateReloadReport(firstReport.json, printErrors: false),
|
||||
validateReloadReport(firstReport, printErrors: false),
|
||||
);
|
||||
return DeviceReloadReport(device, reports);
|
||||
},
|
||||
@@ -860,7 +882,7 @@ class HotRunner extends ResidentRunner {
|
||||
final List<DeviceReloadReport> reports = await Future.wait(allReportsFutures);
|
||||
for (final DeviceReloadReport report in reports) {
|
||||
final vm_service.ReloadReport reloadReport = report.reports[0];
|
||||
if (!validateReloadReport(reloadReport.json)) {
|
||||
if (!validateReloadReport(reloadReport)) {
|
||||
// Reload failed.
|
||||
HotEvent('reload-reject',
|
||||
targetPlatform: targetPlatform,
|
||||
@@ -871,7 +893,11 @@ class HotRunner extends ResidentRunner {
|
||||
nullSafety: usageNullSafety,
|
||||
fastReassemble: null,
|
||||
).send();
|
||||
return OperationResult(1, 'Reload rejected');
|
||||
// Reset devFS lastCompileTime to ensure the file will still be marked
|
||||
// as dirty on subsequent reloads.
|
||||
_resetDevFSCompileTime();
|
||||
final ReloadReportContents contents = ReloadReportContents.fromReloadReport(reloadReport);
|
||||
return OperationResult(1, 'Reload rejected: ${contents.notices.join("\n")}');
|
||||
}
|
||||
// Collect stats only from the first device. If/when run -d all is
|
||||
// refactored, we'll probably need to send one hot reload/restart event
|
||||
@@ -1307,3 +1333,54 @@ class ProjectFileInvalidator {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Additional serialization logic for a hot reload response.
|
||||
class ReloadReportContents {
|
||||
factory ReloadReportContents.fromReloadReport(vm_service.ReloadReport report) {
|
||||
final List<ReasonForCancelling> reasons = <ReasonForCancelling>[];
|
||||
final Object notices = report.json['notices'];
|
||||
if (notices is! List<dynamic>) {
|
||||
return ReloadReportContents._(report.success, reasons, report);
|
||||
}
|
||||
for (final Object obj in notices as List<dynamic>) {
|
||||
if (obj is! Map<String, dynamic>) {
|
||||
continue;
|
||||
}
|
||||
final Map<String, dynamic> notice = obj as Map<String, dynamic>;
|
||||
reasons.add(ReasonForCancelling(
|
||||
message: notice['message'] is String
|
||||
? notice['message'] as String
|
||||
: 'Unknown Error',
|
||||
));
|
||||
}
|
||||
|
||||
return ReloadReportContents._(report.success, reasons, report);
|
||||
}
|
||||
|
||||
ReloadReportContents._(
|
||||
this.success,
|
||||
this.notices,
|
||||
this.report,
|
||||
);
|
||||
|
||||
final bool success;
|
||||
final List<ReasonForCancelling> notices;
|
||||
final vm_service.ReloadReport report;
|
||||
}
|
||||
|
||||
/// A serialization class for hot reload rejection reasons.
|
||||
///
|
||||
/// Injects an additional error message that a hot restart will
|
||||
/// resolve the issue.
|
||||
class ReasonForCancelling {
|
||||
ReasonForCancelling({
|
||||
this.message,
|
||||
});
|
||||
|
||||
final String message;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return '$message.\nTry performing a hot restart instead.';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user