From d00acae4ae89478e26e92f07311282f4100dd63b Mon Sep 17 00:00:00 2001 From: Nicholas Shahan Date: Sat, 4 Feb 2023 04:09:48 +0000 Subject: [PATCH] [ddc] Remove old dwds versions fallback support The dart:developer APIs `registerExtension()` and `postEvent()` no longer write directly to the console. Fixes: https://github.com/dart-lang/sdk/issues/48103 Change-Id: I41ee807eb376abbac87d0f353dde22bebd732faa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/279233 Reviewed-by: Anna Gringauze Commit-Queue: Nicholas Shahan --- .../js_dev_runtime/patch/developer_patch.dart | 16 ++-- tests/dartdevc/developer_events_test.dart | 81 +++++-------------- tests/dartdevc_2/developer_events_test.dart | 81 +++++-------------- 3 files changed, 47 insertions(+), 131 deletions(-) diff --git a/sdk/lib/_internal/js_dev_runtime/patch/developer_patch.dart b/sdk/lib/_internal/js_dev_runtime/patch/developer_patch.dart index 743ba2c0371..dcdfe6f60b8 100644 --- a/sdk/lib/_internal/js_dev_runtime/patch/developer_patch.dart +++ b/sdk/lib/_internal/js_dev_runtime/patch/developer_patch.dart @@ -13,6 +13,7 @@ import 'dart:convert' show json; import 'dart:isolate'; var _issuedRegisterExtensionWarning = false; +var _issuedPostEventWarning = false; final _developerSupportWarning = 'from dart:developer is only supported in ' 'build/run/test environments where the developer event method hooks have ' 'been set by package:dwds v11.1.0 or higher.'; @@ -86,10 +87,7 @@ _registerExtension(String method, ServiceExtensionHandler handler) { // See hooks assigned by package:dwds: // https://github.com/dart-lang/webdev/blob/de05cf9fbbfe088be74bb61df4a138289a94d902/dwds/web/client.dart#L223 JS('', r'#.$emitRegisterEvent(#)', dart.global_, method); - return; } - // TODO(48103) Remove use of debug log in Dart 3.0.0. - JS('', 'console.debug("dart.developer.registerExtension", #)', method); } /// Returns a JS `Promise` that resolves with the result of invoking @@ -124,16 +122,20 @@ bool get extensionStreamHasListener => _debuggerAttached; @patch void _postEvent(String eventKind, String eventData) { + if (!_debuggerAttached) { + if (!_issuedPostEventWarning) { + var message = 'postEvent() $_developerSupportWarning'; + JS('', 'console.warn(#)', message); + _issuedPostEventWarning = true; + } + return; + } // TODO(46377) Update this check when we have a documented API for DDC apps. if (JS('!', r'!!#.$emitDebugEvent', dart.global_)) { // See hooks assigned by package:dwds: // https://github.com/dart-lang/webdev/blob/de05cf9fbbfe088be74bb61df4a138289a94d902/dwds/web/client.dart#L220 JS('', r'#.$emitDebugEvent(#, #)', dart.global_, eventKind, eventData); - return; } - // TODO(48103) Remove use of debug log in Dart 3.0.0. - JS('', 'console.debug("dart.developer.postEvent", #, #)', eventKind, - eventData); } @patch diff --git a/tests/dartdevc/developer_events_test.dart b/tests/dartdevc/developer_events_test.dart index cdad5c87034..ff7fd02c466 100644 --- a/tests/dartdevc/developer_events_test.dart +++ b/tests/dartdevc/developer_events_test.dart @@ -7,7 +7,6 @@ library developer_events_test; import 'dart:developer' show postEvent, registerExtension, ServiceExtensionResponse; -import 'dart:convert'; import 'package:js/js.dart'; import 'package:expect/expect.dart'; @@ -50,69 +49,17 @@ class _TestDebugEvent { } void main() { - testBackwardsCompatibility(); - testWarningMessages(); + testRegisterExtensionWarningMessage(); testPostEvent(); testRegisterExtension(); } -/// Verify backwards compatibility for sending messages on the console.debug log -/// in the chrome browser when the hooks have not been set. -// TODO(nshahan) Remove testing of debug log after package:dwds removes support. -// https://github.com/dart-lang/webdev/issues/1342` -void testBackwardsCompatibility() { - var consoleDebugLog = []; - var savedConsoleDebug = consoleDebug; - var savedDwdsVersion = dwdsVersion; - - try { - // Patch our own console.debug function for testing. - consoleDebug = allowInterop((arg1, [arg2, arg3]) => consoleDebugLog.add([ - arg1, - if (arg2 != null) arg2, - if (arg3 != null) arg3, - ])); - // Provide a version to signal there is an attached debugger. - dwdsVersion = '1.0.0-for-test'; - - // All post and register events should go to the console debug log. - var data0 = {'key0': 'value0'}; - postEvent('kind0', data0); - - expect(consoleDebugLog.single[0], 'dart.developer.postEvent'); - expect(consoleDebugLog.single[1], 'kind0'); - Expect.contains('"key0":"value0"', consoleDebugLog.single[2]); - - var testHandler = (String s, Map m) async => - ServiceExtensionResponse.result('test result'); - - registerExtension('ext.method0', testHandler); - expect(consoleDebugLog.length, 2); - expect(consoleDebugLog[1].first, 'dart.developer.registerExtension'); - expect(consoleDebugLog[1].last, 'ext.method0'); - - var data1 = {'key1': 'value1'}; - postEvent('kind1', data1); - - expect(consoleDebugLog.length, 3); - expect(consoleDebugLog[2][0], 'dart.developer.postEvent'); - expect(consoleDebugLog[2][1], 'kind1'); - Expect.contains('"key1":"value1"', consoleDebugLog[2][2]); - - registerExtension('ext.method1', testHandler); - expect(consoleDebugLog.length, 4); - expect(consoleDebugLog[3].first, 'dart.developer.registerExtension'); - expect(consoleDebugLog[3].last, 'ext.method1'); - } finally { - // Restore actual console.debug function. - consoleDebug = savedConsoleDebug; - dwdsVersion = savedDwdsVersion; - } -} - -/// Verify that warning messages are printed on the first call of postEvent() -/// and registerExtension() when the hooks are undefined. -void testWarningMessages() { +/// Verify that warning messages are printed on the first call of +/// `registerExtension()` when the hooks are undefined. +/// +/// Calls to `postEvent()` are always a no-op when no extension has been +/// registered to listen which can never happen if the hook is undefined. +void testRegisterExtensionWarningMessage() { final consoleWarnLog = []; var savedConsoleWarn = consoleWarn; try { @@ -130,8 +77,9 @@ void testWarningMessages() { var data1 = {'key1': 'value1'}; postEvent('kind1', data1); - // No warnings should be issued because postEvent is a no-op. - expect(consoleWarnLog.length, 0); + // No warnings should be issued because postEvent is a no-op when no + // extensions have been registered to listen. + expect(consoleWarnLog.isEmpty, true); consoleWarnLog.clear(); @@ -154,6 +102,15 @@ void testWarningMessages() { // A warning is only issued on the first call of `registerExtension()`. expect(consoleWarnLog.length, 1); + + consoleWarnLog.clear(); + + // The earlier call to registerExtension() was a no-op and printed a warning + // because no debugger hooks are defined. This means more calls to + // `postEvent()` are still no ops. + postEvent('kind0', data0); + postEvent('kind1', data1); + expect(consoleWarnLog.isEmpty, true); } finally { // Restore actual console.warn function. consoleWarn = savedConsoleWarn; diff --git a/tests/dartdevc_2/developer_events_test.dart b/tests/dartdevc_2/developer_events_test.dart index 993b5a6dd66..b14581086a0 100644 --- a/tests/dartdevc_2/developer_events_test.dart +++ b/tests/dartdevc_2/developer_events_test.dart @@ -7,7 +7,6 @@ library developer_events_test; import 'dart:developer' show postEvent, registerExtension, ServiceExtensionResponse; -import 'dart:convert'; import 'package:js/js.dart'; import 'package:expect/expect.dart'; @@ -50,69 +49,17 @@ class _TestDebugEvent { } void main() { - testBackwardsCompatibility(); - testWarningMessages(); + testRegisterExtensionWarningMessage(); testPostEvent(); testRegisterExtension(); } -/// Verify backwards compatibility for sending messages on the console.debug log -/// in the chrome browser when the hooks have not been set. -// TODO(nshahan) Remove testing of debug log after package:dwds removes support. -// https://github.com/dart-lang/webdev/issues/1342` -void testBackwardsCompatibility() { - var consoleDebugLog = []; - var savedConsoleDebug = consoleDebug; - var savedDwdsVersion = dwdsVersion; - - try { - // Patch our own console.debug function for testing. - consoleDebug = allowInterop((arg1, [arg2, arg3]) => consoleDebugLog.add([ - arg1, - if (arg2 != null) arg2, - if (arg3 != null) arg3, - ])); - // Provide a version to signal there is an attached debugger. - dwdsVersion = '1.0.0-for-test'; - - // All post and register events should go to the console debug log. - var data0 = {'key0': 'value0'}; - postEvent('kind0', data0); - - expect(consoleDebugLog.single[0], 'dart.developer.postEvent'); - expect(consoleDebugLog.single[1], 'kind0'); - Expect.contains('"key0":"value0"', consoleDebugLog.single[2]); - - var testHandler = (String s, Map m) async => - ServiceExtensionResponse.result('test result'); - - registerExtension('ext.method0', testHandler); - expect(consoleDebugLog.length, 2); - expect(consoleDebugLog[1].first, 'dart.developer.registerExtension'); - expect(consoleDebugLog[1].last, 'ext.method0'); - - var data1 = {'key1': 'value1'}; - postEvent('kind1', data1); - - expect(consoleDebugLog.length, 3); - expect(consoleDebugLog[2][0], 'dart.developer.postEvent'); - expect(consoleDebugLog[2][1], 'kind1'); - Expect.contains('"key1":"value1"', consoleDebugLog[2][2]); - - registerExtension('ext.method1', testHandler); - expect(consoleDebugLog.length, 4); - expect(consoleDebugLog[3].first, 'dart.developer.registerExtension'); - expect(consoleDebugLog[3].last, 'ext.method1'); - } finally { - // Restore actual console.debug function. - consoleDebug = savedConsoleDebug; - dwdsVersion = savedDwdsVersion; - } -} - -/// Verify that warning messages are printed on the first call of postEvent() -/// and registerExtension() when the hooks are undefined. -void testWarningMessages() { +/// Verify that warning messages are printed on the first call of +/// `registerExtension()` when the hooks are undefined. +/// +/// Calls to `postEvent()` are always a no-op when no extension has been +/// registered to listen which can never happen if the hook is undefined. +void testRegisterExtensionWarningMessage() { final consoleWarnLog = []; var savedConsoleWarn = consoleWarn; try { @@ -130,8 +77,9 @@ void testWarningMessages() { var data1 = {'key1': 'value1'}; postEvent('kind1', data1); - // No warnings should be issued because postEvent is a no-op. - expect(consoleWarnLog.length, 0); + // No warnings should be issued because postEvent is a no-op when no + // extensions have been registered to listen. + expect(consoleWarnLog.isEmpty, true); consoleWarnLog.clear(); @@ -154,6 +102,15 @@ void testWarningMessages() { // A warning is only issued on the first call of `registerExtension()`. expect(consoleWarnLog.length, 1); + + consoleWarnLog.clear(); + + // The earlier call to registerExtension() was a no-op and printed a warning + // because no debugger hooks are defined. This means more calls to + // `postEvent()` are still no ops. + postEvent('kind0', data0); + postEvent('kind1', data1); + expect(consoleWarnLog.isEmpty, true); } finally { // Restore actual console.warn function. consoleWarn = savedConsoleWarn;