Skip to content

Commit 1f69497

Browse files
authored
[ Widget Preview ] Throw ToolExit if Flutter Web is not enabled (flutter#178500)
If Flutter Web is not enabled, the widget previewer will crash when trying to find a web device to launch with. This change explicitly checks for this case and throws `ToolExit` with instructions to enable Flutter Web if it's not enabled. Fixes flutter#178486
1 parent 35b8aca commit 1f69497

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import '../bundle.dart' as bundle;
2424
import '../cache.dart';
2525
import '../convert.dart';
2626
import '../device.dart';
27+
import '../features.dart';
2728
import '../globals.dart' as globals;
2829
import '../isolated/resident_web_runner.dart';
2930
import '../project.dart';
@@ -389,6 +390,14 @@ final class WidgetPreviewStartCommand extends WidgetPreviewSubCommandBase with C
389390

390391
Future<int> runPreviewEnvironment({required FlutterProject widgetPreviewScaffoldProject}) async {
391392
try {
393+
// In the rare case that Flutter Web is disabled, the device manager will not return any web
394+
// devices which will cause us to crash.
395+
if (!featureFlags.isWebEnabled) {
396+
throwToolExit(
397+
'Widget Previews requires Flutter Web to be enabled. Please run '
398+
"'flutter config --enable-web' to enable Flutter Web and try again.",
399+
);
400+
}
392401
final Device device;
393402
if (boolArg(kWebServer)) {
394403
final List<Device> devices;

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import 'package:flutter_tools/src/cache.dart';
1919
import 'package:flutter_tools/src/commands/widget_preview.dart';
2020
import 'package:flutter_tools/src/dart/pub.dart';
2121
import 'package:flutter_tools/src/device.dart';
22+
import 'package:flutter_tools/src/features.dart';
2223
import 'package:flutter_tools/src/globals.dart' as globals;
2324
import 'package:flutter_tools/src/project.dart';
2425
import 'package:flutter_tools/src/web/web_device.dart';
@@ -265,6 +266,37 @@ void main() {
265266
}
266267
expectNoPreviewLaunchTimingEvents();
267268
});
269+
270+
testUsingContext(
271+
'Flutter Web is disabled',
272+
() async {
273+
try {
274+
await startWidgetPreview(rootProject: await createRootProject());
275+
fail('Successfully executed with Flutter Web disabled.');
276+
} on ToolExit catch (e) {
277+
expect(
278+
e.message,
279+
'Error: Widget Previews requires Flutter Web to be enabled. Please run '
280+
"'flutter config --enable-web' to enable Flutter Web and try again.",
281+
);
282+
}
283+
expectNoPreviewLaunchTimingEvents();
284+
},
285+
overrides: {
286+
FeatureFlags: () => TestFeatureFlags(
287+
// ignore: avoid_redundant_argument_values, readability
288+
isWebEnabled: false,
289+
),
290+
Pub: () => Pub.test(
291+
fileSystem: fs,
292+
logger: logger,
293+
processManager: loggingProcessManager,
294+
botDetector: botDetector,
295+
platform: platform,
296+
stdio: mockStdio,
297+
),
298+
},
299+
);
268300
});
269301

270302
testUsingContext(

0 commit comments

Comments
 (0)