Skip to content

Commit 383b618

Browse files
Revert: [flutter_tools] Intercept prohibited hot reloads in widget preview and trigger hot restart (flutter#193968)
Reverts: [[flutter_tools] Intercept prohibited hot reloads in widget preview and trigger hot restart](flutter#193447) Initiated by: @bkonyi Reason for reverting: conflicting changes with flutter#192831 are causing failures due to concurrent merge Original PR Author: @bkonyi Reviewed By: @srawlins The original PR description is provided below: When hot reload fails due to unsupported changes (such as class hierarchy modifications, global initializer changes, or file additions/deletions) in `flutter widget-preview`, automatically fall back to a hot restart instead of leaving the previewer in a failed reload state. ### Changes - **`ResidentWebRunner`**: Attach `updateFSReport` to the failed `OperationResult` when `_updateDevFS` fails so callers can inspect `UpdateFSReport.hotReloadRejected`. - **`WidgetPreviewStartCommand`**: Introduce `handleReload()` (invoked inside `_triggerReload` for `.hotReload` requests so the fallback remains serialized with other reload and restart requests, and add a `@visibleForTesting` `widgetPreviewApp` setter), which checks if `restart()` returned an `OperationResult` with `UpdateFSReport(hotReloadRejected: true)`. When rejected, log a status message (`kHotReloadRejectedMessage`) and invoke `restart(fullRestart: true)`. Fixes flutter#192697 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent ec3293c commit 383b618

4 files changed

Lines changed: 7 additions & 151 deletions

File tree

‎packages/flutter_tools/lib/src/commands/widget_preview.dart‎

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import '../cache.dart';
2727
import '../context/tool_context.dart';
2828
import '../convert.dart';
2929
import '../dart/analysis.dart';
30-
import '../devfs.dart';
3130
import '../device.dart';
3231
import '../features.dart';
3332
import '../isolated/resident_web_runner.dart';
@@ -141,8 +140,7 @@ abstract base class WidgetPreviewSubCommandBase extends FlutterCommand {
141140
/// The type of reload operation queued to run once [WidgetPreviewStartCommand._reloadMutex]
142141
/// is available.
143142
enum _PendingReload {
144-
/// Perform a hot reload via [WidgetPreviewStartCommand.handleReload], which
145-
/// falls back to a hot restart if the reload is rejected.
143+
/// Perform a hot reload (`restart(fullRestart: false)`).
146144
hotReload,
147145

148146
/// Perform a full hot restart (`restart(fullRestart: true)`).
@@ -221,10 +219,6 @@ final class WidgetPreviewStartCommand extends WidgetPreviewSubCommandBase with C
221219
'Failed to locate browser. Make sure you are using an up-to-date Chrome or Edge. '
222220
'Otherwise, consider running with --$kWebServer instead.';
223221

224-
@visibleForTesting
225-
static const kHotReloadRejectedMessage =
226-
'Hot reload rejected due to unsupported changes. Performing hot restart instead.';
227-
228222
@override
229223
Future<Set<DevelopmentArtifact>> get requiredArtifacts async => const <DevelopmentArtifact>{
230224
// Ensure the Flutter Web SDK is installed.
@@ -328,9 +322,6 @@ final class WidgetPreviewStartCommand extends WidgetPreviewSubCommandBase with C
328322
@visibleForTesting
329323
ResidentRunner? get widgetPreviewApp => _widgetPreviewApp;
330324

331-
@visibleForTesting
332-
set widgetPreviewApp(ResidentRunner? value) => _widgetPreviewApp = value;
333-
334325
/// Serializes initial preview scaffold startup and subsequent hot reload / hot
335326
/// restart operations so reloads cannot execute before the debug connection is
336327
/// established or overlap with an in-flight reload.
@@ -513,18 +504,6 @@ final class WidgetPreviewStartCommand extends WidgetPreviewSubCommandBase with C
513504
}
514505
}
515506

516-
/// Hot reloads the previewer, falling back to a hot restart if the reload is
517-
/// rejected due to unsupported changes.
518-
@visibleForTesting
519-
Future<OperationResult?> handleReload() async {
520-
final OperationResult? result = await _widgetPreviewApp?.restart();
521-
if (result case OperationResult(updateFSReport: UpdateFSReport(hotReloadRejected: true))) {
522-
logger.printStatus(kHotReloadRejectedMessage);
523-
return _widgetPreviewApp?.restart(fullRestart: true);
524-
}
525-
return result;
526-
}
527-
528507
Future<void> _triggerReload(_PendingReload requested) async {
529508
// Record the requested reload type. If a task is already queued waiting for
530509
// [_reloadMutex], coalesce this request into the pending one (upgrading a
@@ -547,12 +526,7 @@ final class WidgetPreviewStartCommand extends WidgetPreviewSubCommandBase with C
547526
return;
548527
}
549528
try {
550-
switch (pending) {
551-
case .hotReload:
552-
await handleReload();
553-
case .hotRestart:
554-
await _widgetPreviewApp?.restart(fullRestart: true);
555-
}
529+
await _widgetPreviewApp?.restart(fullRestart: pending == .hotRestart);
556530
} on Object catch (e, st) {
557531
logger.printTrace('Error during widget preview reload: $e\n$st');
558532
}

‎packages/flutter_tools/lib/src/isolated/resident_web_runner.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ class ResidentWebRunner extends ResidentRunner {
493493
),
494494
);
495495
}
496-
return OperationResult(1, 'Failed to recompile application.', updateFSReport: report);
496+
return OperationResult(1, 'Failed to recompile application.');
497497
}
498498
} else {
499499
report = null;

‎packages/flutter_tools/test/commands.shard/permeable/widget_preview/widget_preview_test.dart‎

Lines changed: 4 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,8 @@ class FakeCustomBrowserDevice extends Fake implements ChromiumDevice {
159159
}
160160

161161
class FakeResidentRunner extends Fake implements ResidentRunner {
162-
FakeResidentRunner({this.onRestart, this.waitForAppToFinishCompleter});
162+
FakeResidentRunner({this.waitForAppToFinishCompleter});
163163

164-
final OperationResult Function(bool fullRestart)? onRestart;
165164
final Completer<int>? waitForAppToFinishCompleter;
166165
int restartCount = 0;
167166
int concurrentRestarts = 0;
@@ -200,7 +199,7 @@ class FakeResidentRunner extends Fake implements ResidentRunner {
200199
await currentRestartCompleter!.future;
201200
}
202201
concurrentRestarts--;
203-
return onRestart?.call(fullRestart) ?? OperationResult.ok;
202+
return OperationResult.ok;
204203
}
205204

206205
@override
@@ -1140,19 +1139,7 @@ List<_i1.WidgetPreview> previews() => [
11401139
'serializes, coalesces, and gates reloads across previewer lifecycle',
11411140
() async {
11421141
final Directory rootProject = await createRootProject();
1143-
var rejectNextHotReload = false;
11441142
final fakeResidentRunner = FakeResidentRunner(
1145-
onRestart: (bool fullRestart) {
1146-
if (!fullRestart && rejectNextHotReload) {
1147-
rejectNextHotReload = false;
1148-
return OperationResult(
1149-
1,
1150-
'Failed to recompile application.',
1151-
updateFSReport: UpdateFSReport(hotReloadRejected: true),
1152-
);
1153-
}
1154-
return OperationResult.ok;
1155-
},
11561143
waitForAppToFinishCompleter: Completer<int>(),
11571144
);
11581145

@@ -1256,35 +1243,13 @@ List<_i1.WidgetPreview> previews() => [
12561243
expect(fakeResidentRunner.maxConcurrentRestarts, 1);
12571244
expect(fakeResidentRunner.fullRestartRequests, <bool>[false, false, false, false, true]);
12581245

1259-
// 4. A rejected hot reload falls back to a hot restart under the mutex,
1260-
// before any reload queued while the rejected reload was in flight.
1261-
rejectNextHotReload = true;
1262-
final rejectedReloadCompleter = Completer<OperationResult>();
1263-
fakeResidentRunner.currentRestartCompleter = rejectedReloadCompleter;
1264-
1265-
triggerChange();
1266-
await pumpEventQueue();
1267-
triggerChange();
1268-
1269-
fakeResidentRunner.currentRestartCompleter = null;
1270-
rejectedReloadCompleter.complete(OperationResult.ok);
1271-
await pumpEventQueue();
1272-
1273-
expect(fakeResidentRunner.restartCount, 8);
1274-
expect(fakeResidentRunner.maxConcurrentRestarts, 1);
1275-
expect(fakeResidentRunner.fullRestartRequests.skip(5), <bool>[false, true, false]);
1276-
expect(
1277-
asLogger<BufferLogger>(logger).statusText,
1278-
contains(WidgetPreviewStartCommand.kHotReloadRejectedMessage),
1279-
);
1280-
1281-
// 5. Ignore reload requests after the previewer has finished.
1246+
// 4. Ignore reload requests after the previewer has finished.
12821247
fakeResidentRunner.waitForAppToFinishCompleter!.complete(0);
12831248
await runFuture;
12841249

12851250
triggerChange();
12861251
await pumpEventQueue();
1287-
expect(fakeResidentRunner.restartCount, 8);
1252+
expect(fakeResidentRunner.restartCount, 5);
12881253
},
12891254
overrides: <Type, Generator>{
12901255
Analytics: () => fakeAnalytics,
@@ -1301,87 +1266,5 @@ List<_i1.WidgetPreview> previews() => [
13011266
),
13021267
},
13031268
);
1304-
1305-
WidgetPreviewStartCommand createStartCommand(FakeResidentRunner runner) {
1306-
final command = WidgetPreviewCommand(
1307-
toolContext: FakeToolContext(
1308-
fs: fs,
1309-
logger: logger,
1310-
processManager: loggingProcessManager,
1311-
platform: platform,
1312-
artifacts: Artifacts.test(),
1313-
),
1314-
dtdServicesOverride: fakeDtdServices,
1315-
);
1316-
return (command.subcommands['start']! as WidgetPreviewStartCommand)
1317-
..widgetPreviewApp = runner;
1318-
}
1319-
1320-
testWithoutContext('triggers hot restart if hot reload is rejected', () async {
1321-
final fakeResidentRunner = FakeResidentRunner(
1322-
onRestart: (bool fullRestart) {
1323-
if (!fullRestart) {
1324-
return OperationResult(
1325-
1,
1326-
'Failed to recompile application.',
1327-
updateFSReport: UpdateFSReport(hotReloadRejected: true),
1328-
);
1329-
}
1330-
return OperationResult.ok;
1331-
},
1332-
);
1333-
final WidgetPreviewStartCommand startCommand = createStartCommand(fakeResidentRunner);
1334-
1335-
final OperationResult? result = await startCommand.handleReload();
1336-
1337-
final BufferLogger bufferLogger = asLogger<BufferLogger>(logger);
1338-
expect(fakeResidentRunner.fullRestartRequests, <bool>[false, true]);
1339-
expect(result?.isOk, isTrue);
1340-
expect(
1341-
bufferLogger.statusText,
1342-
contains(WidgetPreviewStartCommand.kHotReloadRejectedMessage),
1343-
);
1344-
});
1345-
1346-
testWithoutContext(
1347-
'does not trigger hot restart if hot reload fails for other reasons',
1348-
() async {
1349-
final fakeResidentRunner = FakeResidentRunner(
1350-
onRestart: (bool fullRestart) {
1351-
return OperationResult(
1352-
1,
1353-
'Failed to recompile application.',
1354-
updateFSReport: UpdateFSReport(),
1355-
);
1356-
},
1357-
);
1358-
final WidgetPreviewStartCommand startCommand = createStartCommand(fakeResidentRunner);
1359-
1360-
final OperationResult? result = await startCommand.handleReload();
1361-
1362-
final BufferLogger bufferLogger = asLogger<BufferLogger>(logger);
1363-
expect(fakeResidentRunner.fullRestartRequests, <bool>[false]);
1364-
expect(result?.isOk, isFalse);
1365-
expect(
1366-
bufferLogger.statusText,
1367-
isNot(contains(WidgetPreviewStartCommand.kHotReloadRejectedMessage)),
1368-
);
1369-
},
1370-
);
1371-
1372-
testWithoutContext('does not trigger hot restart if hot reload succeeds', () async {
1373-
final fakeResidentRunner = FakeResidentRunner();
1374-
final WidgetPreviewStartCommand startCommand = createStartCommand(fakeResidentRunner);
1375-
1376-
final OperationResult? result = await startCommand.handleReload();
1377-
1378-
final BufferLogger bufferLogger = asLogger<BufferLogger>(logger);
1379-
expect(fakeResidentRunner.fullRestartRequests, <bool>[false]);
1380-
expect(result?.isOk, isTrue);
1381-
expect(
1382-
bufferLogger.statusText,
1383-
isNot(contains(WidgetPreviewStartCommand.kHotReloadRejectedMessage)),
1384-
);
1385-
});
13861269
});
13871270
}

‎packages/flutter_tools/test/general.shard/resident_web_runner_test.dart‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,6 @@ name: my_app
11111111
final OperationResult result = await residentWebRunner.restart();
11121112

11131113
expect(result.code, 1);
1114-
expect(result.updateFSReport?.hotReloadRejected, isTrue);
11151114
expect(webDevFS.mainUri.toString(), contains('entrypoint.dart'));
11161115

11171116
expect(

0 commit comments

Comments
 (0)