Skip to content

Commit b5b3636

Browse files
biggs0125natebiggs
andauthored
Add support for running dart2wasm in dry run mode on js compilations (#171682)
Example message with findings:
 ```
 Wasm dry run findings: Found incompatibilities with WebAssembly. package:counter1/main.dart 3:1 - dart:html unsupported (0) Consider addressing these issues to enable wasm builds. See docs for more info: https://docs.flutter.dev/platform-integration/web/wasm Compiling lib/main.dart for the Web... 10.9s ✓ Built build/web ``` Example message without findings:
 ```
 Wasm dry run succeeded. Consider building and testing your application with the `--wasm` flag. See docs for more info: https://docs.flutter.dev/platform-integration/web/wasm Compiling lib/main.dart for the Web... 10.8s ✓ Built build/web ``` --------- Co-authored-by: Nate Biggs <natebiggs@google.com>
1 parent 968b657 commit b5b3636

File tree

6 files changed

+116
-33
lines changed

6 files changed

+116
-33
lines changed

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

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,13 @@ class Dart2WasmTarget extends Dart2WebTarget {
351351
processManager: environment.processManager,
352352
);
353353

354-
await processUtils.run(throwOnError: true, compilationArgs);
354+
final RunResult runResult = await processUtils.run(
355+
throwOnError: !compilerConfig.dryRun,
356+
compilationArgs,
357+
);
358+
if (compilerConfig.dryRun) {
359+
_handleDryRunResult(environment, runResult);
360+
}
355361
}
356362

357363
@override
@@ -361,31 +367,68 @@ class Dart2WasmTarget extends Dart2WebTarget {
361367
List<String> get depfiles => const <String>['dart2wasm.d'];
362368

363369
@override
364-
Map<String, Object?> get buildConfig => <String, Object?>{
365-
'compileTarget': 'dart2wasm',
366-
'renderer': compilerConfig.renderer.name,
367-
'mainWasmPath': 'main.dart.wasm',
368-
'jsSupportRuntimePath': 'main.dart.mjs',
369-
};
370-
371-
@override
372-
Iterable<File> buildFiles(Environment environment) => environment.buildDir
373-
.listSync(recursive: true)
374-
.whereType<File>()
375-
.where(
376-
(File file) => switch (file.basename) {
377-
'main.dart.wasm' || 'main.dart.mjs' => true,
378-
'main.dart.wasm.map' => compilerConfig.sourceMaps,
379-
_ => false,
380-
},
370+
Map<String, Object?> get buildConfig => compilerConfig.dryRun
371+
? const <String, Object?>{}
372+
: <String, Object?>{
373+
'compileTarget': 'dart2wasm',
374+
'renderer': compilerConfig.renderer.name,
375+
'mainWasmPath': 'main.dart.wasm',
376+
'jsSupportRuntimePath': 'main.dart.mjs',
377+
};
378+
379+
@override
380+
Iterable<File> buildFiles(Environment environment) => compilerConfig.dryRun
381+
? const <File>[]
382+
: environment.buildDir
383+
.listSync(recursive: true)
384+
.whereType<File>()
385+
.where(
386+
(File file) => switch (file.basename) {
387+
'main.dart.wasm' || 'main.dart.mjs' => true,
388+
'main.dart.wasm.map' => compilerConfig.sourceMaps,
389+
_ => false,
390+
},
391+
);
392+
393+
@override
394+
Iterable<String> get buildPatternStems => compilerConfig.dryRun
395+
? const <String>[]
396+
: <String>[
397+
'main.dart.wasm',
398+
'main.dart.mjs',
399+
if (compilerConfig.sourceMaps) 'main.dart.wasm.map',
400+
];
401+
402+
void _handleDryRunResult(Environment environment, RunResult runResult) {
403+
final int exitCode = runResult.exitCode;
404+
final String stdout = runResult.stdout;
405+
final String stderr = runResult.stderr;
406+
if (exitCode != 0 && exitCode != 254) {
407+
environment.logger.printWarning('Unexpected wasm dry run failure ($exitCode):');
408+
if (stderr.isNotEmpty) {
409+
environment.logger.printWarning(stdout);
410+
environment.logger.printWarning(stderr);
411+
}
412+
} else if (exitCode == 0) {
413+
environment.logger.printWarning(
414+
'Wasm dry run succeeded. Consider building and testing your application with the '
415+
'`--wasm` flag. See docs for more info: '
416+
'https://docs.flutter.dev/platform-integration/web/wasm',
381417
);
382-
383-
@override
384-
Iterable<String> get buildPatternStems => <String>[
385-
'main.dart.wasm',
386-
'main.dart.mjs',
387-
if (compilerConfig.sourceMaps) 'main.dart.wasm.map',
388-
];
418+
} else if (stderr.isNotEmpty) {
419+
environment.logger.printWarning('Wasm dry run failed:');
420+
environment.logger.printWarning(stdout);
421+
environment.logger.printWarning(stderr);
422+
} else if (stdout.isNotEmpty) {
423+
environment.logger.printWarning('Wasm dry run findings:');
424+
environment.logger.printWarning(stdout);
425+
environment.logger.printWarning(
426+
'Consider addressing these issues to enable wasm builds. See docs for more info: '
427+
'https://docs.flutter.dev/platform-integration/web/wasm\n',
428+
);
429+
}
430+
environment.logger.printWarning('Use --no-wasm-dry-run to disable these warnings.');
431+
}
389432
}
390433

391434
/// Unpacks the dart2js or dart2wasm compilation and resources to a given
@@ -760,7 +803,7 @@ class WebServiceWorker extends Target {
760803
final String serviceWorker = generateServiceWorker(fileGeneratorsPath, urlToHash, <String>[
761804
'main.dart.js',
762805
if (compileConfigs.any(
763-
(WebCompilerConfig config) => config is WasmCompilerConfig,
806+
(WebCompilerConfig config) => config is WasmCompilerConfig && !config.dryRun,
764807
)) ...<String>['main.dart.wasm', 'main.dart.mjs'],
765808
'index.html',
766809
'flutter_bootstrap.js',

packages/flutter_tools/lib/src/commands/build_web.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ class BuildWebCommand extends BuildSubCommand {
112112
'If not explicitly set, uses the compilation mode (debug, profile, release).',
113113
hide: !verboseHelp,
114114
);
115+
argParser.addFlag(
116+
'wasm-dry-run',
117+
defaultsTo: true,
118+
help:
119+
'Compiles wasm in dry run mode during JS only compilations. '
120+
'Disable to suppress warnings.',
121+
);
115122
argParser.addFlag(
116123
'no-frequency-based-minification',
117124
negatable: false,
@@ -226,6 +233,13 @@ class BuildWebCommand extends BuildSubCommand {
226233
sourceMaps: sourceMaps,
227234
renderer: webRenderer,
228235
),
236+
WasmCompilerConfig(
237+
optimizationLevel: optimizationLevel,
238+
stripWasm: boolArg('strip-wasm'),
239+
sourceMaps: sourceMaps,
240+
minify: minifyWasm,
241+
dryRun: boolArg('wasm-dry-run'),
242+
),
229243
];
230244
}
231245

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ class WasmCompilerConfig extends WebCompilerConfig {
147147
super.optimizationLevel,
148148
this.stripWasm = true,
149149
this.minify,
150+
this.dryRun = false,
150151
super.sourceMaps = true,
151152
super.renderer = WebRendererMode.defaultForWasm,
152153
});
@@ -159,6 +160,8 @@ class WasmCompilerConfig extends WebCompilerConfig {
159160

160161
final bool? minify;
161162

163+
final bool dryRun;
164+
162165
@override
163166
CompileTarget get compileTarget => CompileTarget.wasm;
164167

@@ -184,6 +187,7 @@ class WasmCompilerConfig extends WebCompilerConfig {
184187
if (!sourceMaps) '--no-source-maps',
185188
if (minify ?? buildMode == BuildMode.release) '--minify' else '--no-minify',
186189
if (buildMode == BuildMode.debug) '--extra-compiler-option=--enable-asserts',
190+
if (dryRun) '--extra-compiler-option=--dry-run',
187191
];
188192
}
189193

@@ -193,8 +197,15 @@ class WasmCompilerConfig extends WebCompilerConfig {
193197
...super._buildKeyMap,
194198
kStripWasm: stripWasm,
195199
'minify': minify,
200+
'dryRun': dryRun,
196201
WebCompilerConfig.kSourceMapsEnabled: sourceMaps,
197202
};
198203
return jsonEncode(settings);
199204
}
205+
206+
@override
207+
Map<String, Object> get buildEventAnalyticsValues => <String, Object>{
208+
...super.buildEventAnalyticsValues,
209+
'dryRun': dryRun,
210+
};
200211
}

packages/flutter_tools/test/commands.shard/hermetic/build_web_test.dart

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -412,17 +412,29 @@ void main() {
412412
TestBuildSystem.all(BuildResult(success: true), (Target target, Environment environment) {
413413
expect(target, isA<WebServiceWorker>());
414414
final List<WebCompilerConfig> configs = (target as WebServiceWorker).compileConfigs;
415-
expect(configs, hasLength(1));
416-
final WebCompilerConfig config = configs.single;
417-
expect(config.renderer, WebRendererMode.canvaskit);
418-
expect(config.compileTarget, CompileTarget.js);
419-
final List<String> options = config.toCommandOptions(BuildMode.release);
420-
expect(options, <String>[
415+
expect(configs, hasLength(2));
416+
final WebCompilerConfig jsConfig = configs[0];
417+
expect(jsConfig.renderer, WebRendererMode.canvaskit);
418+
expect(jsConfig.compileTarget, CompileTarget.js);
419+
final List<String> jsOptions = jsConfig.toCommandOptions(BuildMode.release);
420+
expect(jsOptions, <String>[
421421
'--native-null-assertions',
422422
'--no-source-maps',
423423
'-O4',
424424
'--minify',
425425
]);
426+
427+
final WebCompilerConfig wasmConfig = configs[1];
428+
expect(wasmConfig.renderer, WebRendererMode.skwasm);
429+
expect(wasmConfig.compileTarget, CompileTarget.wasm);
430+
final List<String> wasmOptions = wasmConfig.toCommandOptions(BuildMode.release);
431+
expect(wasmOptions, <String>[
432+
'-O2',
433+
'--strip-wasm',
434+
'--no-source-maps',
435+
'--minify',
436+
'--extra-compiler-option=--dry-run',
437+
]);
426438
}),
427439
},
428440
);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,12 +1255,14 @@ name: foo
12551255
WasmCompilerConfig(renderer: WebRendererMode.canvaskit),
12561256
WasmCompilerConfig(stripWasm: false),
12571257
WasmCompilerConfig(minify: false),
1258+
WasmCompilerConfig(dryRun: true),
12581259

12591260
// All properties non-default
12601261
WasmCompilerConfig(
12611262
optimizationLevel: 0,
12621263
stripWasm: false,
12631264
renderer: WebRendererMode.canvaskit,
1265+
dryRun: true,
12641266
),
12651267
];
12661268

packages/flutter_tools/test/general.shard/web/compile_web_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ environement:
105105
Event.flutterBuildInfo(
106106
label: 'web-compile',
107107
buildType: 'web',
108-
settings: 'optimizationLevel: 0; web-renderer: skwasm,canvaskit; web-target: wasm,js;',
108+
settings:
109+
'dryRun: false; optimizationLevel: 0; web-renderer: skwasm,canvaskit; web-target: wasm,js;',
109110
),
110111
]),
111112
);

0 commit comments

Comments
 (0)