From cb7770b3a52288d10fe6d5c78a7fa878dc39acce Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Wed, 15 Jul 2020 09:54:39 -0700 Subject: [PATCH] [flutter_tools] add support for enable null safety asserts (#61114) Enable null safety asserts for web debug mode. This induces runtime asserts at the boundaries between null safe and non-null safe libraries. Adds integration test that validates assertion error is thrown. #61042 --- dev/bots/test.dart | 1 + .../web/lib/null_assert_main.dart | 22 +++++++++++++++++++ .../web/lib/null_enabled_api.dart | 9 ++++++++ .../lib/src/build_runner/devfs_web.dart | 2 ++ .../flutter_tools/lib/src/web/bootstrap.dart | 8 ++++++- .../general.shard/web/bootstrap_test.dart | 12 ++++++++++ 6 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 dev/integration_tests/web/lib/null_assert_main.dart create mode 100644 dev/integration_tests/web/lib/null_enabled_api.dart diff --git a/dev/bots/test.dart b/dev/bots/test.dart index 1579b6af41..9180a604ba 100644 --- a/dev/bots/test.dart +++ b/dev/bots/test.dart @@ -746,6 +746,7 @@ Future _runWebIntegrationTests() async { await _runWebDebugTest('lib/stack_trace.dart'); await _runWebDebugTest('lib/web_directory_loading.dart'); await _runWebDebugTest('test/test.dart'); + await _runWebDebugTest('lib/null_assert_main.dart', enableNullSafety: true); await _runWebDebugTest('lib/null_safe_main.dart', enableNullSafety: true); await _runWebDebugTest('lib/web_define_loading.dart', additionalArguments: [ diff --git a/dev/integration_tests/web/lib/null_assert_main.dart b/dev/integration_tests/web/lib/null_assert_main.dart new file mode 100644 index 0000000000..134720cd73 --- /dev/null +++ b/dev/integration_tests/web/lib/null_assert_main.dart @@ -0,0 +1,22 @@ +// 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. + +// @dart=2.8 + +import 'null_enabled_api.dart'; + +void main() { + dynamic error; + try { + // Validate that a generated null assertion is thrown. + methodThatAcceptsNonNull(null); + } catch (err) { + error = err; + } + if (error is AssertionError) { + print('--- TEST SUCCEEDED ---'); + } else { + print('--- TEST FAILED ---'); + } +} diff --git a/dev/integration_tests/web/lib/null_enabled_api.dart b/dev/integration_tests/web/lib/null_enabled_api.dart new file mode 100644 index 0000000000..45723e2662 --- /dev/null +++ b/dev/integration_tests/web/lib/null_enabled_api.dart @@ -0,0 +1,9 @@ +// 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. + +// @dart=2.9 + +void methodThatAcceptsNonNull(int x) { + print(x + 2); +} diff --git a/packages/flutter_tools/lib/src/build_runner/devfs_web.dart b/packages/flutter_tools/lib/src/build_runner/devfs_web.dart index d8d9b4a32a..d80e67267d 100644 --- a/packages/flutter_tools/lib/src/build_runner/devfs_web.dart +++ b/packages/flutter_tools/lib/src/build_runner/devfs_web.dart @@ -791,6 +791,8 @@ class WebDevFS implements DevFS { 'main_module.bootstrap.js', generateMainModule( entrypoint: entrypoint, + nullSafety: buildInfo.extraFrontEndOptions + ?.contains('--enable-experiment=non-nullable') ?? false, ), ); // TODO(jonahwilliams): refactor the asset code in this and the regular devfs to diff --git a/packages/flutter_tools/lib/src/web/bootstrap.dart b/packages/flutter_tools/lib/src/web/bootstrap.dart index e0a076b132..a065222665 100644 --- a/packages/flutter_tools/lib/src/web/bootstrap.dart +++ b/packages/flutter_tools/lib/src/web/bootstrap.dart @@ -48,12 +48,18 @@ document.head.appendChild(requireEl); /// the file `foo/bar/baz.dart` will generate a property named approximately /// `foo__bar__baz`. Rather than attempt to guess, we assume the first property of /// this object is the module. -String generateMainModule({@required String entrypoint}) { +String generateMainModule({ + @required String entrypoint, + @required bool nullSafety, +}) { return '''/* ENTRYPOINT_EXTENTION_MARKER */ // Create the main module loaded below. define("main_module.bootstrap", ["$entrypoint", "dart_sdk"], function(app, dart_sdk) { dart_sdk.dart.setStartAsyncSynchronously(true); dart_sdk._debugger.registerDevtoolsFormatter(); + if ($nullSafety) { + dart_sdk.dart.nonNullAsserts(true); + } // See the generateMainModule doc comment. var child = {}; diff --git a/packages/flutter_tools/test/general.shard/web/bootstrap_test.dart b/packages/flutter_tools/test/general.shard/web/bootstrap_test.dart index 43a579fc72..e96de475e0 100644 --- a/packages/flutter_tools/test/general.shard/web/bootstrap_test.dart +++ b/packages/flutter_tools/test/general.shard/web/bootstrap_test.dart @@ -23,9 +23,21 @@ void main() { test('generateMainModule embeds urls correctly', () { final String result = generateMainModule( entrypoint: 'foo/bar/main.js', + nullSafety: false, ); // bootstrap main module has correct defined module. expect(result, contains('define("main_module.bootstrap", ["foo/bar/main.js", "dart_sdk"], ' 'function(app, dart_sdk) {')); }); + + test('generateMainModule includes null safety switches', () { + final String result = generateMainModule( + entrypoint: 'foo/bar/main.js', + nullSafety: true, + ); + + expect(result, contains( +''' if (true) { + dart_sdk.dart.nonNullAsserts(true);''')); + }); }