Unverified Commit 2a36ad31 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Ensure Flutter for Web debug builds are only accessible through Flutter run (#40783)

parent a36f809e
...@@ -17,7 +17,7 @@ class BuildWebCommand extends BuildSubCommand { ...@@ -17,7 +17,7 @@ class BuildWebCommand extends BuildSubCommand {
BuildWebCommand() { BuildWebCommand() {
usesTargetOption(); usesTargetOption();
usesPubOption(); usesPubOption();
addBuildModeFlags(); addBuildModeFlags(excludeDebug: true);
argParser.addFlag('web-initialize-platform', argParser.addFlag('web-initialize-platform',
defaultsTo: true, defaultsTo: true,
negatable: true, negatable: true,
...@@ -50,6 +50,9 @@ class BuildWebCommand extends BuildSubCommand { ...@@ -50,6 +50,9 @@ class BuildWebCommand extends BuildSubCommand {
final FlutterProject flutterProject = FlutterProject.current(); final FlutterProject flutterProject = FlutterProject.current();
final String target = argResults['target']; final String target = argResults['target'];
final BuildInfo buildInfo = getBuildInfo(); final BuildInfo buildInfo = getBuildInfo();
if (buildInfo.isDebug) {
throwToolExit('debug builds cannot be built directly for the web. Try using "flutter run"');
}
await buildWeb(flutterProject, target, buildInfo, argResults['web-initialize-platform']); await buildWeb(flutterProject, target, buildInfo, argResults['web-initialize-platform']);
return null; return null;
} }
......
...@@ -126,6 +126,8 @@ abstract class FlutterCommand extends Command<void> { ...@@ -126,6 +126,8 @@ abstract class FlutterCommand extends Command<void> {
bool get shouldUpdateCache => true; bool get shouldUpdateCache => true;
bool _excludeDebug = false;
BuildMode _defaultBuildMode; BuildMode _defaultBuildMode;
void requiresPubspecYaml() { void requiresPubspecYaml() {
...@@ -263,12 +265,17 @@ abstract class FlutterCommand extends Command<void> { ...@@ -263,12 +265,17 @@ abstract class FlutterCommand extends Command<void> {
'Normally there\'s only one, but when adding Flutter to a pre-existing app it\'s possible to create multiple.'); 'Normally there\'s only one, but when adding Flutter to a pre-existing app it\'s possible to create multiple.');
} }
void addBuildModeFlags({ bool defaultToRelease = true, bool verboseHelp = false }) { void addBuildModeFlags({ bool defaultToRelease = true, bool verboseHelp = false, bool excludeDebug = false }) {
// A release build must be the default if a debug build is not possible.
assert(defaultToRelease || !excludeDebug);
_excludeDebug = excludeDebug;
defaultBuildMode = defaultToRelease ? BuildMode.release : BuildMode.debug; defaultBuildMode = defaultToRelease ? BuildMode.release : BuildMode.debug;
argParser.addFlag('debug', if (!excludeDebug) {
negatable: false, argParser.addFlag('debug',
help: 'Build a debug version of your app${defaultToRelease ? '' : ' (default mode)'}.'); negatable: false,
help: 'Build a debug version of your app${defaultToRelease ? '' : ' (default mode)'}.');
}
argParser.addFlag('profile', argParser.addFlag('profile',
negatable: false, negatable: false,
help: 'Build a version of your app specialized for performance profiling.'); help: 'Build a version of your app specialized for performance profiling.');
...@@ -312,11 +319,12 @@ abstract class FlutterCommand extends Command<void> { ...@@ -312,11 +319,12 @@ abstract class FlutterCommand extends Command<void> {
} }
BuildMode getBuildMode() { BuildMode getBuildMode() {
final List<bool> modeFlags = <bool>[argResults['debug'], argResults['profile'], argResults['release']]; final bool debugResult = _excludeDebug ? false : argResults['debug'];
final List<bool> modeFlags = <bool>[debugResult, argResults['profile'], argResults['release']];
if (modeFlags.where((bool flag) => flag).length > 1) { if (modeFlags.where((bool flag) => flag).length > 1) {
throw UsageException('Only one of --debug, --profile, or --release can be specified.', null); throw UsageException('Only one of --debug, --profile, or --release can be specified.', null);
} }
if (argResults['debug']) { if (debugResult) {
return BuildMode.debug; return BuildMode.debug;
} }
if (argResults['profile']) { if (argResults['profile']) {
......
...@@ -92,6 +92,15 @@ void main() { ...@@ -92,6 +92,15 @@ void main() {
); );
})); }));
test('Refuses to build a debug build for web', () => testbed.run(() async {
final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());
expect(() => runner.run(<String>['build', 'web', '--debug']),
throwsA(isInstanceOf<UsageException>()));
}, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
}));
test('Refuses to build for web when feature is disabled', () => testbed.run(() async { test('Refuses to build for web when feature is disabled', () => testbed.run(() async {
final CommandRunner<void> runner = createTestCommandRunner(BuildCommand()); final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment