Skip to content

Commit b8a7a92

Browse files
[CP-stable][ Widget Preview ] Throw ToolExit if Flutter Web is not enabled (#178534)
This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request) Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request. ### Issue Link: What is the link to the issue this cherry-pick is addressing? #178486 ### Impact Description: What is the impact (ex. visual jank on Samsung phones, app crash, cannot ship an iOS app)? Does it impact development (ex. flutter doctor crashes when Android Studio is installed), or the shipping of production apps (the app crashes on launch). This information is for domain experts and release engineers to understand the consequences of saying yes or no to the cherry pick. Running `flutter widget-preview start` with Flutter web disabled will result in an error message stating that either Chrome couldn't be found or a `StateError` is thrown (for `--web-server`). ### Changelog Description: Explain this cherry pick: * In one line that is accessible to most Flutter developers. * That describes the state prior to the fix. * That includes which platforms are impacted. See [best practices](https://github.com/flutter/flutter/blob/main/docs/releases/Hotfix-Documentation-Best-Practices.md) for examples. [flutter/178486] When running `flutter widget-preview start` with Flutter Web disabled, an exception is thrown and the widget previewer fails to start. ### Workaround: Is there a workaround for this issue? Run `flutter config --enable-web`, but there's currently no indication that this needs to be done. ### Risk: What is the risk level of this cherry-pick? ### Test Coverage: Are you confident that your fix is well-tested by automated tests? ### Validation Steps: What are the steps to validate that this fix works? 1. Run `flutter config --no-enable-web` 2. Run `flutter widget-preview start` in a Flutter project 3. Verify an error message with instructions to enable Flutter web is output
1 parent 95324c1 commit b8a7a92

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

‎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)