diff --git a/dev/bots/suite_runners/run_web_tests.dart b/dev/bots/suite_runners/run_web_tests.dart index 81e353da1b..4f52789efe 100644 --- a/dev/bots/suite_runners/run_web_tests.dart +++ b/dev/bots/suite_runners/run_web_tests.dart @@ -224,6 +224,8 @@ class WebTestsSuite { '--dart-define=test.valueB=Value', ] ), + () => _runWebDebugTest('lib/assertion_test.dart'), + () => _runWebReleaseTest('lib/assertion_test.dart'), () => _runWebDebugTest('lib/sound_mode.dart'), () => _runWebReleaseTest('lib/sound_mode.dart'), () => _runFlutterWebTest( diff --git a/dev/integration_tests/web/lib/assertion_test.dart b/dev/integration_tests/web/lib/assertion_test.dart new file mode 100644 index 0000000000..2212131033 --- /dev/null +++ b/dev/integration_tests/web/lib/assertion_test.dart @@ -0,0 +1,32 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:js_interop'; + +import 'package:flutter/foundation.dart'; +import 'package:web/web.dart' as web; + +Future main() async { + bool executedAssert = false; + assert(() { + executedAssert = true; + return true; + }()); + + final StringBuffer output = StringBuffer(); + if (executedAssert == kDebugMode) { + output.write('--- TEST SUCCEEDED ---'); + } else { + output.write('--- TEST FAILED ---'); + } + + await web.window.fetch( + '/test-result'.toJS, + web.RequestInit( + method: 'POST', + body: '$output'.toJS, + ) + ).toDart; + print(output); +} diff --git a/packages/flutter_tools/lib/src/build_system/targets/web.dart b/packages/flutter_tools/lib/src/build_system/targets/web.dart index 13f837f272..01cdca1300 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/web.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/web.dart @@ -177,7 +177,7 @@ class Dart2JSTarget extends Dart2WebTarget { ...decodeCommaSeparated(environment.defines, kExtraFrontEndOptions), if (buildMode == BuildMode.profile) '-Ddart.vm.profile=true' - else + else if (buildMode == BuildMode.release) '-Ddart.vm.product=true', for (final String dartDefine in computeDartDefines(environment)) '-D$dartDefine', @@ -185,7 +185,7 @@ class Dart2JSTarget extends Dart2WebTarget { final List compilationArgs = [ ...sharedCommandOptions, - ...compilerConfig.toSharedCommandOptions(), + ...compilerConfig.toSharedCommandOptions(buildMode), '-o', environment.buildDir.childFile('app.dill').path, '--packages=.dart_tool/package_config.json', @@ -298,7 +298,6 @@ class Dart2WasmTarget extends Dart2WebTarget { final String platformBinariesPath = artifacts.getHostArtifact(HostArtifact.webPlatformKernelFolder).path; final String platformFilePath = environment.fileSystem.path.join(platformBinariesPath, 'dart2wasm_platform.dill'); - assert(buildMode == BuildMode.release || buildMode == BuildMode.profile); final List compilationArgs = [ artifacts.getArtifactPath(Artifact.engineDartBinary, platform: TargetPlatform.web_javascript), 'compile', @@ -313,7 +312,7 @@ class Dart2WasmTarget extends Dart2WebTarget { ], if (buildMode == BuildMode.profile) '-Ddart.vm.profile=true' - else + else if (buildMode == BuildMode.release) '-Ddart.vm.product=true', ...decodeCommaSeparated(environment.defines, kExtraFrontEndOptions), for (final String dartDefine in computeDartDefines(environment)) diff --git a/packages/flutter_tools/lib/src/web/compiler_config.dart b/packages/flutter_tools/lib/src/web/compiler_config.dart index d183de3565..1d767200aa 100644 --- a/packages/flutter_tools/lib/src/web/compiler_config.dart +++ b/packages/flutter_tools/lib/src/web/compiler_config.dart @@ -102,17 +102,18 @@ class JsCompilerConfig extends WebCompilerConfig { CompileTarget get compileTarget => CompileTarget.js; /// Arguments to use in both phases: full JS compile and CFE-only. - List toSharedCommandOptions() => [ + List toSharedCommandOptions(BuildMode buildMode) => [ if (nativeNullAssertions) '--native-null-assertions', if (!sourceMaps) '--no-source-maps', + if (buildMode == BuildMode.debug) '--enable-asserts', ]; /// Arguments to use in the full JS compile, but not CFE-only. /// /// Includes the contents of [toSharedCommandOptions]. List toCommandOptions(BuildMode buildMode) => [ - if (buildMode == BuildMode.profile) '--no-minify', - ...toSharedCommandOptions(), + if (buildMode != BuildMode.release) '--no-minify', + ...toSharedCommandOptions(buildMode), '-O$optimizationLevel', if (dumpInfo) '--dump-info', if (noFrequencyBasedMinification) '--no-frequency-based-minification', @@ -157,6 +158,7 @@ class WasmCompilerConfig extends WebCompilerConfig { '-O$optimizationLevel', '--${stripSymbols ? '' : 'no-'}strip-wasm', if (!sourceMaps) '--extra-compiler-option=--no-source-maps', + if (buildMode == BuildMode.debug) '--extra-compiler-option=--enable-asserts', ]; } diff --git a/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart b/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart index 4acd27e584..aeb17079d9 100644 --- a/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart +++ b/packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart @@ -868,6 +868,54 @@ void main() { ProcessManager: () => processManager, })); + test('Dart2JSTarget calls dart2js with Dart defines in debug mode', () => testbed.run(() async { + environment.defines[kBuildMode] = 'debug'; + environment.defines[kDartDefines] = encodeDartDefines(['FOO=bar', 'BAZ=qux']); + processManager.addCommand(FakeCommand( + command: [ + ..._kDart2jsLinuxArgs, + '-DFOO=bar', + '-DBAZ=qux', + '-DFLUTTER_WEB_AUTO_DETECT=false', + '-DFLUTTER_WEB_USE_SKIA=true', + '-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/', + '--no-source-maps', + '--enable-asserts', + '-o', + environment.buildDir.childFile('app.dill').absolute.path, + '--packages=.dart_tool/package_config.json', + '--cfe-only', + environment.buildDir.childFile('main.dart').absolute.path, + ] + )); + processManager.addCommand(FakeCommand( + command: [ + ..._kDart2jsLinuxArgs, + '-DFOO=bar', + '-DBAZ=qux', + '-DFLUTTER_WEB_AUTO_DETECT=false', + '-DFLUTTER_WEB_USE_SKIA=true', + '-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/', + '--no-minify', + '--no-source-maps', + '--enable-asserts', + '-O4', + '-o', + environment.buildDir.childFile('main.dart.js').absolute.path, + environment.buildDir.childFile('app.dill').absolute.path, + ] + )); + + await Dart2JSTarget( + const JsCompilerConfig( + sourceMaps: false, + ) + ).build(environment); + }, overrides: { + ProcessManager: () => processManager, + })); + + test('Dart2JSTarget calls dart2js with expected args with dump-info', () => testbed.run(() async { environment.defines[kBuildMode] = 'profile'; environment.defines[JsCompilerConfig.kDart2jsDumpInfo] = 'true'; @@ -962,7 +1010,7 @@ void main() { for (int level = 1; level <= 4; level++) { for (final bool strip in [true, false]) { for (final List defines in const >[[], ['FOO=bar', 'BAZ=qux']]) { - for (final String buildMode in const ['profile', 'release']) { + for (final String buildMode in const ['profile', 'release', 'debug']) { for (final bool sourceMaps in const [true, false]) { test('Dart2WasmTarget invokes dart2wasm with renderer=$renderer, -O$level, stripping=$strip, defines=$defines, modeMode=$buildMode sourceMaps=$sourceMaps', () => testbed.run(() async { environment.defines[kBuildMode] = buildMode; @@ -978,7 +1026,8 @@ void main() { '--extra-compiler-option=--import-shared-memory', '--extra-compiler-option=--shared-memory-max-pages=32768', ], - '-Ddart.vm.${buildMode == 'release' ? 'product' : 'profile' }=true', + if (buildMode != 'debug') + '-Ddart.vm.${buildMode == 'release' ? 'product' : 'profile' }=true', ...defines.map((String define) => '-D$define'), if (renderer == WebRendererMode.skwasm) ...[ '-DFLUTTER_WEB_AUTO_DETECT=false', @@ -994,6 +1043,7 @@ void main() { '-O$level', if (strip && buildMode == 'release') '--strip-wasm' else '--no-strip-wasm', if (!sourceMaps) '--extra-compiler-option=--no-source-maps', + if (buildMode == 'debug') '--extra-compiler-option=--enable-asserts', '-o', environment.buildDir.childFile('main.dart.wasm').absolute.path, environment.buildDir.childFile('main.dart').absolute.path,