Set dart defines properly while in debug mode. (#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 https://github.com/flutter/flutter/issues/148850
This commit is contained in:
Jackson Gardner 2024-07-25 15:00:50 -07:00 committed by GitHub
parent e08d26319c
commit 8f0e77696f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 94 additions and 9 deletions

View File

@ -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(

View File

@ -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<void> 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);
}

View File

@ -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<String> compilationArgs = <String>[
...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<String> compilationArgs = <String>[
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))

View File

@ -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<String> toSharedCommandOptions() => <String>[
List<String> toSharedCommandOptions(BuildMode buildMode) => <String>[
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<String> toCommandOptions(BuildMode buildMode) => <String>[
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',
];
}

View File

@ -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(<String>['FOO=bar', 'BAZ=qux']);
processManager.addCommand(FakeCommand(
command: <String>[
..._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: <String>[
..._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: <Type, Generator>{
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 <bool>[true, false]) {
for (final List<String> defines in const <List<String>>[<String>[], <String>['FOO=bar', 'BAZ=qux']]) {
for (final String buildMode in const <String>['profile', 'release']) {
for (final String buildMode in const <String>['profile', 'release', 'debug']) {
for (final bool sourceMaps in const <bool>[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) ...<String>[
'-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,