Skip to content

Commit 8f0e776

Browse files
Set dart defines properly while in debug mode. (flutter#152262)
Turns out just supporting the right value for `kDebugMode` was a lot simpler than I thought. Debug builds used to never go through the build system code path when using `flutter run`, but now that we have wasm this can occur with the run command. This should address flutter#148850
1 parent e08d263 commit 8f0e776

File tree

5 files changed

+94
-9
lines changed

5 files changed

+94
-9
lines changed

dev/bots/suite_runners/run_web_tests.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ class WebTestsSuite {
224224
'--dart-define=test.valueB=Value',
225225
]
226226
),
227+
() => _runWebDebugTest('lib/assertion_test.dart'),
228+
() => _runWebReleaseTest('lib/assertion_test.dart'),
227229
() => _runWebDebugTest('lib/sound_mode.dart'),
228230
() => _runWebReleaseTest('lib/sound_mode.dart'),
229231
() => _runFlutterWebTest(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:js_interop';
6+
7+
import 'package:flutter/foundation.dart';
8+
import 'package:web/web.dart' as web;
9+
10+
Future<void> main() async {
11+
bool executedAssert = false;
12+
assert(() {
13+
executedAssert = true;
14+
return true;
15+
}());
16+
17+
final StringBuffer output = StringBuffer();
18+
if (executedAssert == kDebugMode) {
19+
output.write('--- TEST SUCCEEDED ---');
20+
} else {
21+
output.write('--- TEST FAILED ---');
22+
}
23+
24+
await web.window.fetch(
25+
'/test-result'.toJS,
26+
web.RequestInit(
27+
method: 'POST',
28+
body: '$output'.toJS,
29+
)
30+
).toDart;
31+
print(output);
32+
}

packages/flutter_tools/lib/src/build_system/targets/web.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ class Dart2JSTarget extends Dart2WebTarget {
177177
...decodeCommaSeparated(environment.defines, kExtraFrontEndOptions),
178178
if (buildMode == BuildMode.profile)
179179
'-Ddart.vm.profile=true'
180-
else
180+
else if (buildMode == BuildMode.release)
181181
'-Ddart.vm.product=true',
182182
for (final String dartDefine in computeDartDefines(environment))
183183
'-D$dartDefine',
184184
];
185185

186186
final List<String> compilationArgs = <String>[
187187
...sharedCommandOptions,
188-
...compilerConfig.toSharedCommandOptions(),
188+
...compilerConfig.toSharedCommandOptions(buildMode),
189189
'-o',
190190
environment.buildDir.childFile('app.dill').path,
191191
'--packages=.dart_tool/package_config.json',
@@ -298,7 +298,6 @@ class Dart2WasmTarget extends Dart2WebTarget {
298298
final String platformBinariesPath = artifacts.getHostArtifact(HostArtifact.webPlatformKernelFolder).path;
299299
final String platformFilePath = environment.fileSystem.path.join(platformBinariesPath, 'dart2wasm_platform.dill');
300300

301-
assert(buildMode == BuildMode.release || buildMode == BuildMode.profile);
302301
final List<String> compilationArgs = <String>[
303302
artifacts.getArtifactPath(Artifact.engineDartBinary, platform: TargetPlatform.web_javascript),
304303
'compile',
@@ -313,7 +312,7 @@ class Dart2WasmTarget extends Dart2WebTarget {
313312
],
314313
if (buildMode == BuildMode.profile)
315314
'-Ddart.vm.profile=true'
316-
else
315+
else if (buildMode == BuildMode.release)
317316
'-Ddart.vm.product=true',
318317
...decodeCommaSeparated(environment.defines, kExtraFrontEndOptions),
319318
for (final String dartDefine in computeDartDefines(environment))

packages/flutter_tools/lib/src/web/compiler_config.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,18 @@ class JsCompilerConfig extends WebCompilerConfig {
102102
CompileTarget get compileTarget => CompileTarget.js;
103103

104104
/// Arguments to use in both phases: full JS compile and CFE-only.
105-
List<String> toSharedCommandOptions() => <String>[
105+
List<String> toSharedCommandOptions(BuildMode buildMode) => <String>[
106106
if (nativeNullAssertions) '--native-null-assertions',
107107
if (!sourceMaps) '--no-source-maps',
108+
if (buildMode == BuildMode.debug) '--enable-asserts',
108109
];
109110

110111
/// Arguments to use in the full JS compile, but not CFE-only.
111112
///
112113
/// Includes the contents of [toSharedCommandOptions].
113114
List<String> toCommandOptions(BuildMode buildMode) => <String>[
114-
if (buildMode == BuildMode.profile) '--no-minify',
115-
...toSharedCommandOptions(),
115+
if (buildMode != BuildMode.release) '--no-minify',
116+
...toSharedCommandOptions(buildMode),
116117
'-O$optimizationLevel',
117118
if (dumpInfo) '--dump-info',
118119
if (noFrequencyBasedMinification) '--no-frequency-based-minification',
@@ -157,6 +158,7 @@ class WasmCompilerConfig extends WebCompilerConfig {
157158
'-O$optimizationLevel',
158159
'--${stripSymbols ? '' : 'no-'}strip-wasm',
159160
if (!sourceMaps) '--extra-compiler-option=--no-source-maps',
161+
if (buildMode == BuildMode.debug) '--extra-compiler-option=--enable-asserts',
160162
];
161163
}
162164

packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,54 @@ void main() {
868868
ProcessManager: () => processManager,
869869
}));
870870

871+
test('Dart2JSTarget calls dart2js with Dart defines in debug mode', () => testbed.run(() async {
872+
environment.defines[kBuildMode] = 'debug';
873+
environment.defines[kDartDefines] = encodeDartDefines(<String>['FOO=bar', 'BAZ=qux']);
874+
processManager.addCommand(FakeCommand(
875+
command: <String>[
876+
..._kDart2jsLinuxArgs,
877+
'-DFOO=bar',
878+
'-DBAZ=qux',
879+
'-DFLUTTER_WEB_AUTO_DETECT=false',
880+
'-DFLUTTER_WEB_USE_SKIA=true',
881+
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
882+
'--no-source-maps',
883+
'--enable-asserts',
884+
'-o',
885+
environment.buildDir.childFile('app.dill').absolute.path,
886+
'--packages=.dart_tool/package_config.json',
887+
'--cfe-only',
888+
environment.buildDir.childFile('main.dart').absolute.path,
889+
]
890+
));
891+
processManager.addCommand(FakeCommand(
892+
command: <String>[
893+
..._kDart2jsLinuxArgs,
894+
'-DFOO=bar',
895+
'-DBAZ=qux',
896+
'-DFLUTTER_WEB_AUTO_DETECT=false',
897+
'-DFLUTTER_WEB_USE_SKIA=true',
898+
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
899+
'--no-minify',
900+
'--no-source-maps',
901+
'--enable-asserts',
902+
'-O4',
903+
'-o',
904+
environment.buildDir.childFile('main.dart.js').absolute.path,
905+
environment.buildDir.childFile('app.dill').absolute.path,
906+
]
907+
));
908+
909+
await Dart2JSTarget(
910+
const JsCompilerConfig(
911+
sourceMaps: false,
912+
)
913+
).build(environment);
914+
}, overrides: <Type, Generator>{
915+
ProcessManager: () => processManager,
916+
}));
917+
918+
871919
test('Dart2JSTarget calls dart2js with expected args with dump-info', () => testbed.run(() async {
872920
environment.defines[kBuildMode] = 'profile';
873921
environment.defines[JsCompilerConfig.kDart2jsDumpInfo] = 'true';
@@ -962,7 +1010,7 @@ void main() {
9621010
for (int level = 1; level <= 4; level++) {
9631011
for (final bool strip in <bool>[true, false]) {
9641012
for (final List<String> defines in const <List<String>>[<String>[], <String>['FOO=bar', 'BAZ=qux']]) {
965-
for (final String buildMode in const <String>['profile', 'release']) {
1013+
for (final String buildMode in const <String>['profile', 'release', 'debug']) {
9661014
for (final bool sourceMaps in const <bool>[true, false]) {
9671015
test('Dart2WasmTarget invokes dart2wasm with renderer=$renderer, -O$level, stripping=$strip, defines=$defines, modeMode=$buildMode sourceMaps=$sourceMaps', () => testbed.run(() async {
9681016
environment.defines[kBuildMode] = buildMode;
@@ -978,7 +1026,8 @@ void main() {
9781026
'--extra-compiler-option=--import-shared-memory',
9791027
'--extra-compiler-option=--shared-memory-max-pages=32768',
9801028
],
981-
'-Ddart.vm.${buildMode == 'release' ? 'product' : 'profile' }=true',
1029+
if (buildMode != 'debug')
1030+
'-Ddart.vm.${buildMode == 'release' ? 'product' : 'profile' }=true',
9821031
...defines.map((String define) => '-D$define'),
9831032
if (renderer == WebRendererMode.skwasm) ...<String>[
9841033
'-DFLUTTER_WEB_AUTO_DETECT=false',
@@ -994,6 +1043,7 @@ void main() {
9941043
'-O$level',
9951044
if (strip && buildMode == 'release') '--strip-wasm' else '--no-strip-wasm',
9961045
if (!sourceMaps) '--extra-compiler-option=--no-source-maps',
1046+
if (buildMode == 'debug') '--extra-compiler-option=--enable-asserts',
9971047
'-o',
9981048
environment.buildDir.childFile('main.dart.wasm').absolute.path,
9991049
environment.buildDir.childFile('main.dart').absolute.path,

0 commit comments

Comments
 (0)