Ensure Flutter for Web debug builds are only accessible through Flutter run (#40783)
This commit is contained in:
@@ -17,7 +17,7 @@ class BuildWebCommand extends BuildSubCommand {
|
||||
BuildWebCommand() {
|
||||
usesTargetOption();
|
||||
usesPubOption();
|
||||
addBuildModeFlags();
|
||||
addBuildModeFlags(excludeDebug: true);
|
||||
argParser.addFlag('web-initialize-platform',
|
||||
defaultsTo: true,
|
||||
negatable: true,
|
||||
@@ -50,6 +50,9 @@ class BuildWebCommand extends BuildSubCommand {
|
||||
final FlutterProject flutterProject = FlutterProject.current();
|
||||
final String target = argResults['target'];
|
||||
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']);
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -126,6 +126,8 @@ abstract class FlutterCommand extends Command<void> {
|
||||
|
||||
bool get shouldUpdateCache => true;
|
||||
|
||||
bool _excludeDebug = false;
|
||||
|
||||
BuildMode _defaultBuildMode;
|
||||
|
||||
void requiresPubspecYaml() {
|
||||
@@ -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.');
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
argParser.addFlag('debug',
|
||||
negatable: false,
|
||||
help: 'Build a debug version of your app${defaultToRelease ? '' : ' (default mode)'}.');
|
||||
if (!excludeDebug) {
|
||||
argParser.addFlag('debug',
|
||||
negatable: false,
|
||||
help: 'Build a debug version of your app${defaultToRelease ? '' : ' (default mode)'}.');
|
||||
}
|
||||
argParser.addFlag('profile',
|
||||
negatable: false,
|
||||
help: 'Build a version of your app specialized for performance profiling.');
|
||||
@@ -312,11 +319,12 @@ abstract class FlutterCommand extends Command<void> {
|
||||
}
|
||||
|
||||
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) {
|
||||
throw UsageException('Only one of --debug, --profile, or --release can be specified.', null);
|
||||
}
|
||||
if (argResults['debug']) {
|
||||
if (debugResult) {
|
||||
return BuildMode.debug;
|
||||
}
|
||||
if (argResults['profile']) {
|
||||
|
||||
@@ -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 {
|
||||
final CommandRunner<void> runner = createTestCommandRunner(BuildCommand());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user