Skip to content

Commit 84a72e6

Browse files
authored
[tool][web] Intercept dart2wasm errors & append JS migration footers (flutter#190476)
When compiling a web project with `--wasm` (or running a dry-run) that encounters incompatible legacy JS interop dependencies (`dart:html` or `package:js`), standard `--wasm` builds default to an uncaught `ProcessException` dumping an internal stack trace, while dry-runs lack actionable migration remediation. This change: * Invokes `ProcessUtils.run(throwOnError: false)` in `Dart2WasmTarget.build` to forward uncorrupted compiler stdout/stderr directly to the logger without throwing an unformatted `ProcessException`. * Adds `kWasmErrorsMoreInfo` constant targeting https://flutter.dev/to/wasm-errors. * Inspects standard compile failure and Wasm dry-run outputs for instances of `dart:html` or `package:js`, appending an educational migration footer when matched. * Terminates fatal Wasm builds cleanly via `throwToolExit`, preventing internal tool stack trace spillage. Fixes flutter#190466
1 parent 88b4103 commit 84a72e6

4 files changed

Lines changed: 254 additions & 5 deletions

File tree

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import '../../web/bootstrap.dart';
2727
import '../../web/compile.dart';
2828
import '../../web/file_generators/flutter_service_worker_js.dart';
2929
import '../../web/file_generators/main_dart.dart' as main_dart;
30+
import '../../web/web_constants.dart';
3031
import '../../web_template.dart';
3132
import '../build_system.dart';
3233
import '../depfile.dart';
@@ -388,12 +389,14 @@ class Dart2WasmTarget extends Dart2WebTarget {
388389
processManager: environment.processManager,
389390
);
390391

391-
final RunResult runResult = await processUtils.run(
392-
throwOnError: !compilerConfig.dryRun,
393-
compilationArgs,
394-
);
392+
final RunResult runResult = await processUtils.run(compilationArgs);
395393
if (compilerConfig.dryRun) {
396394
await _handleDryRunResult(environment, runResult);
395+
} else if (runResult.exitCode != 0) {
396+
environment.logger.printStatus(runResult.stdout);
397+
environment.logger.printError(runResult.stderr);
398+
_checkForLegacyWebImports(environment, runResult.stdout, runResult.stderr);
399+
throwToolExit('Failed to compile application for the Web.');
397400
}
398401
final File recordedUsesFile = environment.buildDir.childFile(
399402
LinkHooks.recordedUsesWasmFileName,
@@ -576,6 +579,8 @@ class Dart2WasmTarget extends Dart2WebTarget {
576579
}
577580
result ??= 'unknown';
578581

582+
_checkForLegacyWebImports(environment, stdout, stderr);
583+
579584
environment.logger.printWarning('Use --no-wasm-dry-run to disable these warnings.');
580585

581586
_analytics.send(
@@ -586,6 +591,22 @@ class Dart2WasmTarget extends Dart2WebTarget {
586591
),
587592
);
588593
}
594+
595+
static final RegExp _kLegacyImportErrorPattern = RegExp(
596+
"(?:Dart library|The unavailable library) '(${kLegacyWebLibraries.join('|')})'|"
597+
'(${kLegacyWebLibraries.join('|')}) unsupported',
598+
);
599+
600+
void _checkForLegacyWebImports(Environment environment, String stdout, String stderr) {
601+
if (_kLegacyImportErrorPattern.hasMatch(stdout) ||
602+
_kLegacyImportErrorPattern.hasMatch(stderr)) {
603+
environment.logger.printStatus(
604+
'Note: WebAssembly compilation failed due to legacy web imports.\n'
605+
'Migrate your project from dart:html and package:js to package:web and dart:js_interop.\n'
606+
'$kWasmErrorsMoreInfo',
607+
);
608+
}
609+
}
589610
}
590611

591612
/// Unpacks the dart2js or dart2wasm compilation and resources to a given

‎packages/flutter_tools/lib/src/web/web_constants.dart‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,22 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
const kWasmMoreInfo = 'See https://flutter.dev/to/wasm for more information.';
5+
const String kWasmMoreInfo = 'See https://flutter.dev/to/wasm for more information.';
6+
const String kWasmErrorsMoreInfo =
7+
'See https://flutter.dev/to/wasm-errors for diagnostic and migration guidance.';
8+
9+
/// Legacy web libraries unsupported in WebAssembly compilation.
10+
const Set<String> kLegacyWebLibraries = <String>{
11+
'dart:html',
12+
'dart:indexed_db',
13+
'dart:js',
14+
'dart:js_util',
15+
'dart:svg',
16+
'dart:web_audio',
17+
'dart:web_gl',
18+
'dart:web_sql',
19+
'package:js',
20+
};
621

722
/// Headers required to run Wasm-compiled applications with multi-threading.
823
///

‎packages/flutter_tools/test/general.shard/build_system/targets/web_dry_run_test.dart‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:math';
66

77
import 'package:file/memory.dart';
88
import 'package:flutter_tools/src/artifacts.dart';
9+
import 'package:flutter_tools/src/base/logger.dart';
910
import 'package:flutter_tools/src/base/platform.dart';
1011
import 'package:flutter_tools/src/build_info.dart';
1112
import 'package:flutter_tools/src/build_system/build_system.dart';
@@ -174,6 +175,18 @@ package:foo/some/path.dart 6:1 - dart:html unsupported (0)
174175
final Dart2WasmTarget target = createTarget();
175176
await target.build(environment);
176177

178+
final logger = environment.logger as BufferLogger;
179+
expect(
180+
logger.statusText,
181+
contains('Note: WebAssembly compilation failed due to legacy web imports.'),
182+
);
183+
expect(
184+
logger.statusText,
185+
contains(
186+
'Migrate your project from dart:html and package:js to package:web and dart:js_interop.',
187+
),
188+
);
189+
177190
expect(fakeAnalytics.sentEvents, hasLength(1));
178191

179192
final Event event = fakeAnalytics.sentEvents[0];
@@ -184,6 +197,64 @@ package:foo/some/path.dart 6:1 - dart:html unsupported (0)
184197
}),
185198
);
186199

200+
test(
201+
'dry run prints JS interop migration footer on dart:svg and dart:js_util unsupported findings',
202+
() => testbed.run(() async {
203+
processManager.addCommand(
204+
FakeCommand(
205+
command: commandArgs,
206+
exitCode: 254,
207+
stdout: '''
208+
Found incompatibilities with WebAssembly.
209+
210+
package:foo/some/path.dart 6:1 - dart:svg unsupported (5)
211+
package:bar/some/path.dart 12:4 - dart:js_util unsupported (6)
212+
''',
213+
),
214+
);
215+
final Dart2WasmTarget target = createTarget();
216+
await target.build(environment);
217+
218+
final logger = environment.logger as BufferLogger;
219+
expect(
220+
logger.statusText,
221+
contains('Note: WebAssembly compilation failed due to legacy web imports.'),
222+
);
223+
expect(
224+
logger.statusText,
225+
contains(
226+
'Migrate your project from dart:html and package:js to package:web and dart:js_interop.',
227+
),
228+
);
229+
}),
230+
);
231+
232+
test(
233+
'dry run does not print JS interop migration footer on non-web unsupported libraries like dart:ffi or incidental filenames',
234+
() => testbed.run(() async {
235+
processManager.addCommand(
236+
FakeCommand(
237+
command: commandArgs,
238+
exitCode: 254,
239+
stdout: '''
240+
Found incompatibilities with WebAssembly.
241+
242+
package:fizz/some/path.dart 80:2 - dart:ffi unsupported (3)
243+
package:foo/some/my_dart_html_wrapper.dart 103:20 - dart:io unsupported (4)
244+
''',
245+
),
246+
);
247+
final Dart2WasmTarget target = createTarget();
248+
await target.build(environment);
249+
250+
final logger = environment.logger as BufferLogger;
251+
expect(
252+
logger.statusText,
253+
isNot(contains('Note: WebAssembly compilation failed due to legacy web imports.')),
254+
);
255+
}),
256+
);
257+
187258
test(
188259
'dry run findings public packages',
189260
() => testbed.run(() async {

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

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'dart:convert';
77
import 'package:file_testing/file_testing.dart';
88
import 'package:flutter_tools/src/artifacts.dart';
99
import 'package:flutter_tools/src/base/file_system.dart';
10+
import 'package:flutter_tools/src/base/logger.dart';
1011
import 'package:flutter_tools/src/base/platform.dart';
1112
import 'package:flutter_tools/src/base/template.dart';
1213
import 'package:flutter_tools/src/build_info.dart';
@@ -1407,6 +1408,147 @@ _flutter.loader.load();
14071408
}
14081409
}
14091410

1411+
void addWasmCompilerErrorCommand(
1412+
FakeProcessManager processManager,
1413+
Environment environment,
1414+
String stderr,
1415+
) {
1416+
processManager.addCommand(
1417+
FakeCommand(
1418+
command: <String>[
1419+
..._kDart2WasmLinuxArgs,
1420+
'-Ddart.vm.profile=true',
1421+
'-Ddart.vm.product=false',
1422+
'--extra-compiler-option=--delete-tostring-package-uri=dart:ui',
1423+
'--extra-compiler-option=--delete-tostring-package-uri=package:flutter',
1424+
'--extra-compiler-option=--import-shared-memory',
1425+
'--extra-compiler-option=--shared-memory-max-pages=32768',
1426+
'-DFLUTTER_WEB_USE_SKIA=false',
1427+
'-DFLUTTER_WEB_USE_SKWASM=true',
1428+
'-DFLUTTER_WEB_CANVASKIT_URL=https://www.gstatic.com/flutter-canvaskit/abcdefghijklmnopqrstuvwxyz/',
1429+
'--extra-compiler-option=--depfile=${environment.buildDir.childFile('dart2wasm.d').absolute.path}',
1430+
'--recorded-uses=${environment.buildDir.childFile('recorded_uses_wasm.json').absolute.path}',
1431+
'--enable-experiment=record-use',
1432+
'-O2',
1433+
'--no-strip-wasm',
1434+
'--no-source-maps',
1435+
'--no-minify',
1436+
'-o',
1437+
environment.buildDir.childFile('main.dart.wasm').absolute.path,
1438+
environment.buildDir.childFile('main.dart').absolute.path,
1439+
],
1440+
exitCode: 254,
1441+
stderr: stderr,
1442+
),
1443+
);
1444+
}
1445+
1446+
test(
1447+
'Dart2WasmTarget prints JS interop migration footer on dart:html library import failure',
1448+
() => testbed.run(() async {
1449+
environment.defines[kBuildMode] = 'profile';
1450+
addWasmCompilerErrorCommand(
1451+
processManager,
1452+
environment,
1453+
"Error: Dart library 'dart:html' is not available on this platform.",
1454+
);
1455+
1456+
try {
1457+
await Dart2WasmTarget(
1458+
const WasmCompilerConfig(
1459+
optimizationLevel: 2,
1460+
stripWasm: false,
1461+
sourceMaps: false,
1462+
minify: false,
1463+
),
1464+
const NoOpAnalytics(),
1465+
).build(environment);
1466+
fail('Expected exception');
1467+
} on Exception catch (e) {
1468+
expect(e.toString(), contains('Failed to compile application for the Web.'));
1469+
}
1470+
1471+
final logger = globals.logger as BufferLogger;
1472+
expect(
1473+
logger.statusText,
1474+
contains('Note: WebAssembly compilation failed due to legacy web imports.'),
1475+
);
1476+
expect(
1477+
logger.statusText,
1478+
contains(
1479+
'Migrate your project from dart:html and package:js to package:web and dart:js_interop.',
1480+
),
1481+
);
1482+
}, overrides: <Type, Generator>{ProcessManager: () => processManager}),
1483+
);
1484+
1485+
test(
1486+
'Dart2WasmTarget prints JS interop migration footer on dart:svg and dart:js_util failures',
1487+
() => testbed.run(() async {
1488+
environment.defines[kBuildMode] = 'profile';
1489+
addWasmCompilerErrorCommand(
1490+
processManager,
1491+
environment,
1492+
"Context: The unavailable library 'dart:svg' is imported through these paths:\n"
1493+
"Error: Dart library 'dart:js_util' is not available on this platform.",
1494+
);
1495+
1496+
try {
1497+
await Dart2WasmTarget(
1498+
const WasmCompilerConfig(
1499+
optimizationLevel: 2,
1500+
stripWasm: false,
1501+
sourceMaps: false,
1502+
minify: false,
1503+
),
1504+
const NoOpAnalytics(),
1505+
).build(environment);
1506+
fail('Expected exception');
1507+
} on Exception catch (e) {
1508+
expect(e.toString(), contains('Failed to compile application for the Web.'));
1509+
}
1510+
1511+
final logger = globals.logger as BufferLogger;
1512+
expect(
1513+
logger.statusText,
1514+
contains('Note: WebAssembly compilation failed due to legacy web imports.'),
1515+
);
1516+
}, overrides: <Type, Generator>{ProcessManager: () => processManager}),
1517+
);
1518+
1519+
test(
1520+
'Dart2WasmTarget does not print JS interop migration footer on incidental mentions of dart:html in unrelated errors',
1521+
() => testbed.run(() async {
1522+
environment.defines[kBuildMode] = 'profile';
1523+
addWasmCompilerErrorCommand(
1524+
processManager,
1525+
environment,
1526+
"Error: Syntax error in file:///my_dart_html_test.dart at line 4: print('dart:html');",
1527+
);
1528+
1529+
try {
1530+
await Dart2WasmTarget(
1531+
const WasmCompilerConfig(
1532+
optimizationLevel: 2,
1533+
stripWasm: false,
1534+
sourceMaps: false,
1535+
minify: false,
1536+
),
1537+
const NoOpAnalytics(),
1538+
).build(environment);
1539+
fail('Expected exception');
1540+
} on Exception catch (e) {
1541+
expect(e.toString(), contains('Failed to compile application for the Web.'));
1542+
}
1543+
1544+
final logger = globals.logger as BufferLogger;
1545+
expect(
1546+
logger.statusText,
1547+
isNot(contains('Note: WebAssembly compilation failed due to legacy web imports.')),
1548+
);
1549+
}, overrides: <Type, Generator>{ProcessManager: () => processManager}),
1550+
);
1551+
14101552
test('Dart2WasmTarget.buildFiles respects compilerConfig.sourceMaps and matches modules', () {
14111553
final File wasmFile = environment.buildDir.childFile('main.dart.wasm')..createSync();
14121554
final File mjsFile = environment.buildDir.childFile('main.dart.mjs')..createSync();

0 commit comments

Comments
 (0)