From 6b584dad2d06ad81bc49e0fddcc678b183ab4e17 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Mon, 13 Jun 2022 16:38:01 -0700 Subject: [PATCH 1/8] Removed dependecies on dwds.dart inside dwds code --- dwds/lib/src/debugging/classes.dart | 2 +- dwds/lib/src/handlers/dev_handler.dart | 10 +++++++++- dwds/lib/src/handlers/injector.dart | 1 - dwds/lib/src/loaders/build_runner_require.dart | 5 ++++- dwds/lib/src/loaders/frontend_server_require.dart | 5 ++++- dwds/lib/src/loaders/legacy.dart | 5 ++++- dwds/lib/src/loaders/require.dart | 5 ++++- dwds/lib/src/loaders/strategy.dart | 4 +++- dwds/lib/src/readers/frontend_server_asset_reader.dart | 3 +-- dwds/lib/src/services/chrome_proxy_service.dart | 6 +++++- dwds/lib/src/services/debug_service.dart | 6 +++++- dwds/lib/src/utilities/sdk_configuration.dart | 2 +- 12 files changed, 41 insertions(+), 13 deletions(-) diff --git a/dwds/lib/src/debugging/classes.dart b/dwds/lib/src/debugging/classes.dart index f9fdfd10a..112ad8994 100644 --- a/dwds/lib/src/debugging/classes.dart +++ b/dwds/lib/src/debugging/classes.dart @@ -7,7 +7,7 @@ import 'package:vm_service/vm_service.dart'; import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart'; -import '../../dwds.dart' show ChromeDebugException; +import '../../src/services/chrome_debug_exception.dart'; import '../loaders/strategy.dart'; import '../utilities/domain.dart'; import '../utilities/shared.dart'; diff --git a/dwds/lib/src/handlers/dev_handler.dart b/dwds/lib/src/handlers/dev_handler.dart index 6445ec495..37f7c0ea8 100644 --- a/dwds/lib/src/handlers/dev_handler.dart +++ b/dwds/lib/src/handlers/dev_handler.dart @@ -21,15 +21,23 @@ import '../../data/error_response.dart'; import '../../data/isolate_events.dart'; import '../../data/register_event.dart'; import '../../data/serializers.dart'; -import '../../dwds.dart'; + +import '../connections/app_connection.dart'; +import '../connections/debug_connection.dart'; import '../debugging/execution_context.dart'; import '../debugging/remote_debugger.dart'; import '../debugging/webkit_debugger.dart'; import '../dwds_vm_client.dart'; import '../events.dart'; +import '../handlers/socket_connections.dart'; +import '../loaders/strategy.dart'; +import '../readers/asset_reader.dart'; +import '../servers/devtools.dart'; import '../servers/extension_backend.dart'; import '../services/app_debug_services.dart'; import '../services/debug_service.dart'; +import '../services/expression_compiler.dart'; +import '../utilities/sdk_configuration.dart'; import 'injector.dart'; /// When enabled, this logs VM service protocol and Chrome debug protocol diff --git a/dwds/lib/src/handlers/injector.dart b/dwds/lib/src/handlers/injector.dart index 536464373..dfdff18de 100644 --- a/dwds/lib/src/handlers/injector.dart +++ b/dwds/lib/src/handlers/injector.dart @@ -13,7 +13,6 @@ import 'package:crypto/crypto.dart'; import 'package:logging/logging.dart'; import 'package:shelf/shelf.dart'; -import '../../dwds.dart'; import '../loaders/strategy.dart'; import '../version.dart'; diff --git a/dwds/lib/src/loaders/build_runner_require.dart b/dwds/lib/src/loaders/build_runner_require.dart index a03ea679a..ddcf66c9f 100644 --- a/dwds/lib/src/loaders/build_runner_require.dart +++ b/dwds/lib/src/loaders/build_runner_require.dart @@ -10,7 +10,10 @@ import 'dart:io'; import 'package:path/path.dart' as p; import 'package:shelf/shelf.dart'; -import '../../dwds.dart'; +import '../debugging/metadata/provider.dart'; +import '../loaders/strategy.dart'; +import '../readers/asset_reader.dart'; +import '../services/expression_compiler.dart'; import 'require.dart'; /// Provides a [RequireStrategy] suitable for use with `package:build_runner`. diff --git a/dwds/lib/src/loaders/frontend_server_require.dart b/dwds/lib/src/loaders/frontend_server_require.dart index 925714486..d3b7eb84f 100644 --- a/dwds/lib/src/loaders/frontend_server_require.dart +++ b/dwds/lib/src/loaders/frontend_server_require.dart @@ -6,7 +6,10 @@ import 'package:path/path.dart' as p; -import '../../dwds.dart'; +import '../debugging/metadata/provider.dart'; +import '../loaders/strategy.dart'; +import '../readers/asset_reader.dart'; +import '../services/expression_compiler.dart'; import 'require.dart'; /// Provides a [RequireStrategy] suitable for use with Frontend Server. diff --git a/dwds/lib/src/loaders/legacy.dart b/dwds/lib/src/loaders/legacy.dart index df4c02206..f910711b7 100644 --- a/dwds/lib/src/loaders/legacy.dart +++ b/dwds/lib/src/loaders/legacy.dart @@ -6,7 +6,10 @@ import 'package:shelf/shelf.dart'; -import '../../dwds.dart'; +import '../debugging/metadata/provider.dart'; +import '../loaders/strategy.dart'; +import '../readers/asset_reader.dart'; +import '../services/expression_compiler.dart'; /// A load strategy for the legacy module system. class LegacyStrategy extends LoadStrategy { diff --git a/dwds/lib/src/loaders/require.dart b/dwds/lib/src/loaders/require.dart index 7058e1561..30413d7af 100644 --- a/dwds/lib/src/loaders/require.dart +++ b/dwds/lib/src/loaders/require.dart @@ -9,7 +9,10 @@ import 'dart:convert'; import 'package:path/path.dart' as p; import 'package:shelf/shelf.dart'; -import '../../dwds.dart'; +import '../debugging/metadata/provider.dart'; +import '../loaders/strategy.dart'; +import '../readers/asset_reader.dart'; +import '../services/expression_compiler.dart'; /// Find the path we are serving from the url. /// diff --git a/dwds/lib/src/loaders/strategy.dart b/dwds/lib/src/loaders/strategy.dart index a861ff237..c98ec8d45 100644 --- a/dwds/lib/src/loaders/strategy.dart +++ b/dwds/lib/src/loaders/strategy.dart @@ -6,7 +6,9 @@ import 'package:shelf/shelf.dart'; -import '../../dwds.dart'; +import '../debugging/metadata/provider.dart'; +import '../readers/asset_reader.dart'; +import '../services/expression_compiler.dart'; LoadStrategy _globalLoadStrategy; diff --git a/dwds/lib/src/readers/frontend_server_asset_reader.dart b/dwds/lib/src/readers/frontend_server_asset_reader.dart index 5a4ba09ec..31326f8ba 100644 --- a/dwds/lib/src/readers/frontend_server_asset_reader.dart +++ b/dwds/lib/src/readers/frontend_server_asset_reader.dart @@ -10,8 +10,7 @@ import 'dart:io'; import 'package:package_config/package_config.dart'; import 'package:path/path.dart' as p; -import '../../dwds.dart'; -import 'asset_reader.dart'; +import '../readers/asset_reader.dart'; /// A reader for Dart sources and related source maps provided by the Frontend /// Server. diff --git a/dwds/lib/src/services/chrome_proxy_service.dart b/dwds/lib/src/services/chrome_proxy_service.dart index f8de6e86e..7bc8aa21a 100644 --- a/dwds/lib/src/services/chrome_proxy_service.dart +++ b/dwds/lib/src/services/chrome_proxy_service.dart @@ -15,7 +15,7 @@ import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart'; import '../../data/debug_event.dart'; import '../../data/register_event.dart'; -import '../../dwds.dart'; +import '../connections/app_connection.dart'; import '../debugging/debugger.dart'; import '../debugging/execution_context.dart'; import '../debugging/inspector.dart'; @@ -26,7 +26,11 @@ import '../debugging/remote_debugger.dart'; import '../debugging/skip_list.dart'; import '../events.dart'; import '../loaders/strategy.dart'; +import '../readers/asset_reader.dart'; +import '../services/chrome_debug_exception.dart'; +import '../services/expression_compiler.dart'; import '../utilities/dart_uri.dart'; +import '../utilities/sdk_configuration.dart'; import '../utilities/shared.dart'; import 'expression_evaluator.dart'; diff --git a/dwds/lib/src/services/debug_service.dart b/dwds/lib/src/services/debug_service.dart index cf5d1409e..9df8d705c 100644 --- a/dwds/lib/src/services/debug_service.dart +++ b/dwds/lib/src/services/debug_service.dart @@ -19,11 +19,15 @@ import 'package:sse/server/sse_handler.dart'; import 'package:vm_service/vm_service.dart'; import 'package:web_socket_channel/web_socket_channel.dart'; -import '../../dwds.dart'; +import '../connections/app_connection.dart'; +import '../loaders/strategy.dart'; +import '../readers/asset_reader.dart'; +import '../services/expression_compiler.dart'; import '../debugging/execution_context.dart'; import '../debugging/remote_debugger.dart'; import '../events.dart'; import '../utilities/shared.dart'; +import '../utilities/sdk_configuration.dart'; import 'chrome_proxy_service.dart'; bool _acceptNewConnections = true; diff --git a/dwds/lib/src/utilities/sdk_configuration.dart b/dwds/lib/src/utilities/sdk_configuration.dart index eb650187e..0439e01e6 100644 --- a/dwds/lib/src/utilities/sdk_configuration.dart +++ b/dwds/lib/src/utilities/sdk_configuration.dart @@ -120,7 +120,7 @@ class SdkConfiguration { class DefaultSdkConfigurationProvider extends SdkConfigurationProvider { DefaultSdkConfigurationProvider(); - late SdkConfiguration _configuration = _create(); + late final SdkConfiguration _configuration = _create(); /// Create and validate configuration matching the default SDK layout. @override From 679dc2958ca78bc52d487b638cecd55b5ee4e1e1 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Tue, 14 Jun 2022 12:17:58 -0700 Subject: [PATCH 2/8] Migrate loaders and debugging/metadata directories to null safety --- dwds/lib/src/debugging/metadata/function.dart | 7 +- .../debugging/metadata/module_metadata.dart | 65 +++++++++++-------- dwds/lib/src/debugging/metadata/provider.dart | 7 +- dwds/lib/src/handlers/injector.dart | 4 +- .../lib/src/loaders/build_runner_require.dart | 58 +++++++++++------ .../src/loaders/frontend_server_require.dart | 54 ++++++++------- dwds/lib/src/loaders/legacy.dart | 8 +-- dwds/lib/src/loaders/require.dart | 14 ++-- dwds/lib/src/loaders/strategy.dart | 22 +++---- dwds/test/evaluate_common.dart | 1 + dwds/test/fixtures/context.dart | 1 + 11 files changed, 137 insertions(+), 104 deletions(-) diff --git a/dwds/lib/src/debugging/metadata/function.dart b/dwds/lib/src/debugging/metadata/function.dart index f252624dd..bc435a0b8 100644 --- a/dwds/lib/src/debugging/metadata/function.dart +++ b/dwds/lib/src/debugging/metadata/function.dart @@ -2,8 +2,6 @@ // for details. 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:async'; -// @dart = 2.9 - import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart'; import '../../loaders/strategy.dart'; @@ -43,8 +41,9 @@ class FunctionMetaData { response, evalContents: evalExpression, ); - var name = response.result['result']['value'] as String; - if (name.isEmpty) name = 'Closure'; + final name = response.result?['result']?['value'] as String?; + if (name == null) return FunctionMetaData(''); + if (name.isEmpty) return FunctionMetaData('Closure'); return FunctionMetaData(name); } } diff --git a/dwds/lib/src/debugging/metadata/module_metadata.dart b/dwds/lib/src/debugging/metadata/module_metadata.dart index 2bf2c693b..9f4ae4c30 100644 --- a/dwds/lib/src/debugging/metadata/module_metadata.dart +++ b/dwds/lib/src/debugging/metadata/module_metadata.dart @@ -2,8 +2,6 @@ // for details. 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 - /// Module metadata format version /// /// Module reader always creates the current version but is able to read @@ -62,25 +60,25 @@ class ModuleMetadataVersion { /// See: https://goto.google.com/dart-web-debugger-metadata class LibraryMetadata { /// Library name as defined in pubspec.yaml - final String name; + late final String name; /// Library importUri /// /// Example package:path/path.dart - final String importUri; + late final String importUri; /// All file uris from the library /// /// Can be relative paths to the directory of the fileUri - final List partUris; + late final List partUris; LibraryMetadata(this.name, this.importUri, this.partUris); - LibraryMetadata.fromJson(Map json) - : name = json['name'] as String, - importUri = json['importUri'] as String, - partUris = - List.castFrom(json['partUris'] as List); + LibraryMetadata.fromJson(Map json) { + name = _readRequiredField(json, 'name'); + importUri = _readRequiredField(json, 'importUri'); + partUris = _readOptionalList(json, 'partUris') ?? []; + } Map toJson() { return { @@ -98,35 +96,35 @@ class LibraryMetadata { /// See: https://goto.google.com/dart-web-debugger-metadata class ModuleMetadata { /// Metadata format version - String version; + late final String version; /// Module name /// /// Used as a name of the js module created by the compiler and /// as key to store and load modules in the debugger and the browser - final String name; + late final String name; /// Name of the function enclosing the module /// /// Used by debugger to determine the top dart scope - final String closureName; + late final String closureName; /// Source map uri - final String sourceMapUri; + late final String sourceMapUri; /// Module uri - final String moduleUri; + late final String moduleUri; /// True if the module corresponding to this metadata was compiled with sound /// null safety enabled. - final bool soundNullSafety; + late final bool soundNullSafety; final Map libraries = {}; ModuleMetadata(this.name, this.closureName, this.sourceMapUri, this.moduleUri, this.soundNullSafety, - {this.version}) { - version ??= ModuleMetadataVersion.current.version; + {String? ver}) { + version = ver ?? ModuleMetadataVersion.current.version; } /// Add [library] to metadata @@ -144,13 +142,13 @@ class ModuleMetadata { } } - ModuleMetadata.fromJson(Map json) - : version = json['version'] as String, - name = json['name'] as String, - closureName = json['closureName'] as String, - sourceMapUri = json['sourceMapUri'] as String, - moduleUri = json['moduleUri'] as String, - soundNullSafety = (json['soundNullSafety'] as bool) ?? false { + ModuleMetadata.fromJson(Map json) { + version = _readRequiredField(json, 'version'); + name = _readRequiredField(json, 'name'); + closureName = _readRequiredField(json, 'closureName'); + sourceMapUri = _readRequiredField(json, 'sourceMapUri'); + moduleUri = _readRequiredField(json, 'moduleUri'); + soundNullSafety = _readOptionalField(json, 'soundNullSafety') ?? false; if (!ModuleMetadataVersion.current.isCompatibleWith(version) && !ModuleMetadataVersion.previous.isCompatibleWith(version)) { throw Exception('Unsupported metadata version $version. ' @@ -159,7 +157,7 @@ class ModuleMetadata { '\n ${ModuleMetadataVersion.previous.version}'); } - for (var l in json['libraries'] as List) { + for (var l in _readRequiredList(json, 'libraries')) { addLibrary(LibraryMetadata.fromJson(l as Map)); } } @@ -176,3 +174,18 @@ class ModuleMetadata { }; } } + +// TODO: format errors! +T _readRequiredField(Map json, String field) => + json[field]! as T; + +T? _readOptionalField(Map json, String field) => + json[field] as T?; + +List _readRequiredList(Map json, String field) => + List.castFrom(json[field] as List); + +List? _readOptionalList(Map json, String field) => + json.containsKey(field) + ? List.castFrom(json[field] as List) + : null; diff --git a/dwds/lib/src/debugging/metadata/provider.dart b/dwds/lib/src/debugging/metadata/provider.dart index 822ac4715..ae4696e5b 100644 --- a/dwds/lib/src/debugging/metadata/provider.dart +++ b/dwds/lib/src/debugging/metadata/provider.dart @@ -2,8 +2,6 @@ // for details. 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:async'; -// @dart = 2.9 - import 'dart:async'; import 'dart:convert'; @@ -194,8 +192,7 @@ class MetadataProvider { _addSdkMetadata(); for (var contents in merged.split('\n')) { try { - if (contents == null || - contents.isEmpty || + if (contents.isEmpty || contents.startsWith('// intentionally empty:')) continue; final moduleJson = json.decode(contents); final metadata = @@ -237,7 +234,7 @@ class MetadataProvider { for (var path in library.partUris) { // Parts in metadata are relative to the library Uri directory. final partPath = p.url.join(p.dirname(library.importUri), path); - _scripts[library.importUri].add(partPath); + _scripts[library.importUri]!.add(partPath); _scriptToModule[partPath] = metadata.name; } } diff --git a/dwds/lib/src/handlers/injector.dart b/dwds/lib/src/handlers/injector.dart index dfdff18de..d0089a41f 100644 --- a/dwds/lib/src/handlers/injector.dart +++ b/dwds/lib/src/handlers/injector.dart @@ -123,7 +123,9 @@ class DwdsInjector { return response.change(body: body, headers: newHeaders); } else { final loadResponse = await _loadStrategy.handler(request); - if (loadResponse != null) return loadResponse; + if (loadResponse != null && loadResponse.statusCode != 404) { + return loadResponse; + } return innerHandler(request); } }; diff --git a/dwds/lib/src/loaders/build_runner_require.dart b/dwds/lib/src/loaders/build_runner_require.dart index ddcf66c9f..dbdda0a08 100644 --- a/dwds/lib/src/loaders/build_runner_require.dart +++ b/dwds/lib/src/loaders/build_runner_require.dart @@ -2,11 +2,10 @@ // for details. 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 - import 'dart:convert'; import 'dart:io'; +import 'package:logging/logging.dart'; import 'package:path/path.dart' as p; import 'package:shelf/shelf.dart'; @@ -18,26 +17,28 @@ import 'require.dart'; /// Provides a [RequireStrategy] suitable for use with `package:build_runner`. class BuildRunnerRequireStrategyProvider { + final _logger = Logger('BuildRunnerRequireStrategyProvider'); + final Handler _assetHandler; final ReloadConfiguration _configuration; final AssetReader _assetReader; - RequireStrategy _requireStrategy; + late final RequireStrategy _requireStrategy = RequireStrategy( + _configuration, + _moduleProvider, + _digestsProvider, + _moduleForServerPath, + _serverPathForModule, + _sourceMapPathForModule, + _serverPathForAppUri, + _moduleInfoForProvider, + _assetReader, + ); BuildRunnerRequireStrategyProvider( this._assetHandler, this._configuration, this._assetReader); - RequireStrategy get strategy => _requireStrategy ??= RequireStrategy( - _configuration, - _moduleProvider, - _digestsProvider, - _moduleForServerPath, - _serverPathForModule, - _sourceMapPathForModule, - _serverPathForAppUri, - _moduleInfoForProvider, - _assetReader, - ); + RequireStrategy get strategy => _requireStrategy; Future> _digestsProvider( MetadataProvider metadataProvider) async { @@ -51,9 +52,18 @@ class BuildRunnerRequireStrategyProvider { throw StateError('Could not read digests at path: $digestsPath'); } final body = await response.readAsString(); + final digests = json.decode(body) as Map; + + for (final key in digests.keys) { + if (!modules.containsKey(key)) { + _logger.warning('Digest key $key is not a module name.'); + } + } + return { - for (var entry in (json.decode(body) as Map).entries) - modules[entry.key]: entry.value as String, + for (var entry in digests.entries) + if (modules.containsKey(entry.key)) + modules[entry.key]!: entry.value as String, }; } @@ -63,9 +73,17 @@ class BuildRunnerRequireStrategyProvider { MapEntry(key, stripTopLevelDirectory(removeJsExtension(value)))); Future _moduleForServerPath( - MetadataProvider metadataProvider, String serverPath) async => - (await metadataProvider.modulePathToModule).map((key, value) => MapEntry( - stripTopLevelDirectory(key), value))[relativizePath(serverPath)]; + MetadataProvider metadataProvider, String serverPath) async { + final modulePathToModule = await metadataProvider.modulePathToModule; + final relativePath = relativizePath(serverPath); + for (var e in modulePathToModule.entries) { + if (stripTopLevelDirectory(e.key) == relativePath) { + return e.value; + } + } + _logger.warning('No module for server path $serverPath'); + return ''; + } Future _serverPathForModule( MetadataProvider metadataProvider, String module) async { @@ -80,7 +98,7 @@ class BuildRunnerRequireStrategyProvider { return stripTopLevelDirectory(path); } - String _serverPathForAppUri(String appUri) { + String? _serverPathForAppUri(String appUri) { if (appUri.startsWith('org-dartlang-app:')) { // We skip the root from which we are serving. return Uri.parse(appUri).pathSegments.skip(1).join('/'); diff --git a/dwds/lib/src/loaders/frontend_server_require.dart b/dwds/lib/src/loaders/frontend_server_require.dart index d3b7eb84f..9e4dce317 100644 --- a/dwds/lib/src/loaders/frontend_server_require.dart +++ b/dwds/lib/src/loaders/frontend_server_require.dart @@ -2,8 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @dart = 2.9 - +import 'package:logging/logging.dart'; import 'package:path/path.dart' as p; import '../debugging/metadata/provider.dart'; @@ -14,36 +13,38 @@ import 'require.dart'; /// Provides a [RequireStrategy] suitable for use with Frontend Server. class FrontendServerRequireStrategyProvider { + final _logger = Logger('FrontendServerRequireStrategyProvider'); final ReloadConfiguration _configuration; final AssetReader _assetReader; final Future> Function() _digestsProvider; final String _basePath; - RequireStrategy _requireStrategy; + late final RequireStrategy _requireStrategy = RequireStrategy( + _configuration, + _moduleProvider, + (_) => _digestsProvider(), + _moduleForServerPath, + _serverPathForModule, + _sourceMapPathForModule, + _serverPathForAppUri, + _moduleInfoForProvider, + _assetReader, + ); FrontendServerRequireStrategyProvider(this._configuration, this._assetReader, this._digestsProvider, String basePath) - : _basePath = basePathForServerUri(basePath); + : _basePath = basePathForServerUri(basePath) { + _logger.info('Base path: $_basePath'); + } - RequireStrategy get strategy => _requireStrategy ??= RequireStrategy( - _configuration, - _moduleProvider, - (_) => _digestsProvider(), - _moduleForServerPath, - _serverPathForModule, - _sourceMapPathForModule, - _serverPathForAppUri, - _moduleInfoForProvider, - _assetReader, - ); + RequireStrategy get strategy => _requireStrategy; - String _removeBasePath(String path) => + String? _removeBasePath(String path) => path.startsWith(_basePath) ? path.substring(_basePath.length) : null; - String _addBasePath(String serverPath) => - _basePath == null || _basePath.isEmpty - ? relativizePath(serverPath) - : '$_basePath/${relativizePath(serverPath)}'; + String _addBasePath(String serverPath) => _basePath.isEmpty + ? relativizePath(serverPath) + : '$_basePath/${relativizePath(serverPath)}'; Future> _moduleProvider( MetadataProvider metadataProvider) async => @@ -53,7 +54,14 @@ class FrontendServerRequireStrategyProvider { Future _moduleForServerPath( MetadataProvider metadataProvider, String serverPath) async { final modulePathToModule = await metadataProvider.modulePathToModule; - return modulePathToModule[_removeBasePath(serverPath)]; + final relativeServerPath = _removeBasePath(serverPath); + if (relativeServerPath == null || + !modulePathToModule.containsKey(relativeServerPath)) { + _logger.warning( + 'No module found for server path: $serverPath. Server path is not under $_basePath.'); + return ''; + } + return modulePathToModule[relativeServerPath]!; } Future _serverPathForModule( @@ -64,7 +72,7 @@ class FrontendServerRequireStrategyProvider { MetadataProvider metadataProvider, String module) async => _addBasePath((await metadataProvider.moduleToSourceMap)[module] ?? ''); - String _serverPathForAppUri(String appUri) { + String? _serverPathForAppUri(String appUri) { if (appUri.startsWith('org-dartlang-app:')) { return _addBasePath(Uri.parse(appUri).path); } @@ -76,7 +84,7 @@ class FrontendServerRequireStrategyProvider { final modules = await metadataProvider.moduleToModulePath; final result = {}; for (var module in modules.keys) { - final modulePath = modules[module]; + final modulePath = modules[module]!; result[module] = ModuleInfo( // TODO: Save locations of full kernel files in ddc metadata. // Issue: https://github.com/dart-lang/sdk/issues/43684 diff --git a/dwds/lib/src/loaders/legacy.dart b/dwds/lib/src/loaders/legacy.dart index f910711b7..02e01487e 100644 --- a/dwds/lib/src/loaders/legacy.dart +++ b/dwds/lib/src/loaders/legacy.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'package:shelf/shelf.dart'; import '../debugging/metadata/provider.dart'; @@ -61,7 +59,7 @@ class LegacyStrategy extends LoadStrategy { /// /// Will return `null` if the provided uri is not /// an app URI. - final String Function(String appUri) _serverPathForAppUri; + final String? Function(String appUri) _serverPathForAppUri; LegacyStrategy( this.reloadConfiguration, @@ -74,7 +72,7 @@ class LegacyStrategy extends LoadStrategy { ) : super(assetReader); @override - Handler get handler => (request) => null; + Handler get handler => (request) => Response.notFound(request.url); @override String get id => 'legacy'; @@ -121,5 +119,5 @@ class LegacyStrategy extends LoadStrategy { _sourceMapPathForModule(metadataProviderFor(entrypoint), module); @override - String serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri); + String? serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri); } diff --git a/dwds/lib/src/loaders/require.dart b/dwds/lib/src/loaders/require.dart index 30413d7af..1bdfa469a 100644 --- a/dwds/lib/src/loaders/require.dart +++ b/dwds/lib/src/loaders/require.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'dart:convert'; import 'package:path/path.dart' as p; @@ -20,7 +18,6 @@ import '../services/expression_compiler.dart'; /// https://localhost/base/index.html => /base /// https://localhost/base => /base String basePathForServerUri(String url) { - if (url == null) return null; final uri = Uri.parse(url); var base = uri.path.endsWith('.html') ? p.dirname(uri.path) : uri.path; if (base.isNotEmpty) { @@ -131,7 +128,7 @@ class RequireStrategy extends LoadStrategy { /// /// Will return `null` if the provided uri is not /// an app URI. - final String Function(String appUri) _serverPathForAppUri; + final String? Function(String appUri) _serverPathForAppUri; /// Returns a map from module id to module info. /// @@ -157,13 +154,14 @@ class RequireStrategy extends LoadStrategy { @override Handler get handler => (request) async { if (request.url.path.endsWith(_requireDigestsPath)) { + final entrypoint = request.url.queryParameters['entrypoint']; + if (entrypoint == null) return Response.notFound('${request.url}'); final metadataProvider = - metadataProviderFor(request.url.queryParameters['entrypoint']); - if (metadataProvider == null) return null; + metadataProviderFor(request.url.queryParameters['entrypoint']!); final digests = await _digestsProvider(metadataProvider); return Response.ok(json.encode(digests)); } - return null; + return Response.notFound('${request.url}'); }; @override @@ -296,7 +294,7 @@ if(!window.\$requireLoader) { } @override - String serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri); + String? serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri); @override Future> moduleInfoForEntrypoint(String entrypoint) => diff --git a/dwds/lib/src/loaders/strategy.dart b/dwds/lib/src/loaders/strategy.dart index c98ec8d45..07ff471e5 100644 --- a/dwds/lib/src/loaders/strategy.dart +++ b/dwds/lib/src/loaders/strategy.dart @@ -2,24 +2,17 @@ // for details. 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 - import 'package:shelf/shelf.dart'; import '../debugging/metadata/provider.dart'; import '../readers/asset_reader.dart'; import '../services/expression_compiler.dart'; -LoadStrategy _globalLoadStrategy; +late LoadStrategy _globalLoadStrategy; set globalLoadStrategy(LoadStrategy strategy) => _globalLoadStrategy = strategy; -LoadStrategy get globalLoadStrategy { - if (_globalLoadStrategy == null) { - throw StateError('Global load strategy not set'); - } - return _globalLoadStrategy; -} +LoadStrategy get globalLoadStrategy => _globalLoadStrategy; abstract class LoadStrategy { final AssetReader _assetReader; @@ -126,12 +119,17 @@ abstract class LoadStrategy { /// /// Will return `null` if the provided uri is not /// an app URI. - String serverPathForAppUri(String appUri); + String? serverPathForAppUri(String appUri); /// Returns the [MetadataProvider] for the application located at the provided /// [entrypoint]. - MetadataProvider metadataProviderFor(String entrypoint) => - _providers[entrypoint]; + MetadataProvider metadataProviderFor(String entrypoint) { + if (_providers.containsKey(entrypoint)) { + return _providers[entrypoint]!; + } else { + throw StateError('No metadata provider for $entrypoint'); + } + } /// Initializes a [MetadataProvider] for the application located at the /// provided [entrypoint]. diff --git a/dwds/test/evaluate_common.dart b/dwds/test/evaluate_common.dart index ad452f692..3f861f646 100644 --- a/dwds/test/evaluate_common.dart +++ b/dwds/test/evaluate_common.dart @@ -71,6 +71,7 @@ void testAll({ setUpAll(() async { setCurrentLogWriter(debug: debug); await context.setUp( + compilationMode: compilationMode, enableExpressionEvaluation: true, verboseCompiler: debug, basePath: basePath, diff --git a/dwds/test/fixtures/context.dart b/dwds/test/fixtures/context.dart index 198bb46a4..e9be74240 100644 --- a/dwds/test/fixtures/context.dart +++ b/dwds/test/fixtures/context.dart @@ -277,6 +277,7 @@ class TestContext { assetReader = webRunner.devFS.assetServer; assetHandler = webRunner.devFS.assetServer.handleRequest; + _logger.warning('BasePath: $basePath'); requireStrategy = FrontendServerRequireStrategyProvider( reloadConfiguration, assetReader, () async => {}, basePath) .strategy; From 90ff5caed6954f2ac95e935b880f4942745247a6 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Fri, 17 Jun 2022 15:30:50 -0700 Subject: [PATCH 3/8] Add format errors --- .../debugging/metadata/module_metadata.dart | 23 +++++++++++-------- .../src/loaders/frontend_server_require.dart | 6 ++--- dwds/test/fixtures/context.dart | 1 - 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/dwds/lib/src/debugging/metadata/module_metadata.dart b/dwds/lib/src/debugging/metadata/module_metadata.dart index 9f4ae4c30..048d781f9 100644 --- a/dwds/lib/src/debugging/metadata/module_metadata.dart +++ b/dwds/lib/src/debugging/metadata/module_metadata.dart @@ -175,17 +175,22 @@ class ModuleMetadata { } } -// TODO: format errors! -T _readRequiredField(Map json, String field) => - json[field]! as T; +T _readRequiredField(Map json, String field) { + if (!json.containsKey(field)) { + throw FormatException('Required field $field is not set in $json'); + } + return json[field]! as T; +} T? _readOptionalField(Map json, String field) => json[field] as T?; -List _readRequiredList(Map json, String field) => - List.castFrom(json[field] as List); +List _readRequiredList(Map json, String field) { + final list = _readRequiredField>(json, field); + return List.castFrom(list); +} -List? _readOptionalList(Map json, String field) => - json.containsKey(field) - ? List.castFrom(json[field] as List) - : null; +List? _readOptionalList(Map json, String field) { + final list = _readOptionalField>(json, field); + return list == null ? null : List.castFrom(list); +} diff --git a/dwds/lib/src/loaders/frontend_server_require.dart b/dwds/lib/src/loaders/frontend_server_require.dart index 680870757..17ff20782 100644 --- a/dwds/lib/src/loaders/frontend_server_require.dart +++ b/dwds/lib/src/loaders/frontend_server_require.dart @@ -56,10 +56,8 @@ class FrontendServerRequireStrategyProvider { MetadataProvider metadataProvider, String serverPath) async { final modulePathToModule = await metadataProvider.modulePathToModule; final relativeServerPath = _removeBasePath(serverPath); - if (relativeServerPath == null || - !modulePathToModule.containsKey(relativeServerPath)) { - _logger.warning( - 'No module found for server path: $serverPath. Server path is not under $_basePath.'); + if (!modulePathToModule.containsKey(relativeServerPath)) { + _logger.warning('No module found for server path: $serverPath.'); return ''; } return modulePathToModule[relativeServerPath]!; diff --git a/dwds/test/fixtures/context.dart b/dwds/test/fixtures/context.dart index ca27c9a36..1d1a26b68 100644 --- a/dwds/test/fixtures/context.dart +++ b/dwds/test/fixtures/context.dart @@ -291,7 +291,6 @@ class TestContext { assetReader = webRunner.devFS.assetServer; assetHandler = webRunner.devFS.assetServer.handleRequest; - _logger.warning('BasePath: $basePath'); requireStrategy = FrontendServerRequireStrategyProvider( reloadConfiguration, assetReader, () async => {}, basePath) .strategy; From 171b9a9e881e46b00a1aad62275b0577ff377a56 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Fri, 17 Jun 2022 15:46:45 -0700 Subject: [PATCH 4/8] Removed unnecessary 'late' from metadata fields --- .../debugging/metadata/module_metadata.dart | 37 +++++++++---------- dwds/lib/src/handlers/injector.dart | 2 +- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/dwds/lib/src/debugging/metadata/module_metadata.dart b/dwds/lib/src/debugging/metadata/module_metadata.dart index 048d781f9..5074c20f8 100644 --- a/dwds/lib/src/debugging/metadata/module_metadata.dart +++ b/dwds/lib/src/debugging/metadata/module_metadata.dart @@ -60,25 +60,24 @@ class ModuleMetadataVersion { /// See: https://goto.google.com/dart-web-debugger-metadata class LibraryMetadata { /// Library name as defined in pubspec.yaml - late final String name; + final String name; /// Library importUri /// /// Example package:path/path.dart - late final String importUri; + final String importUri; /// All file uris from the library /// /// Can be relative paths to the directory of the fileUri - late final List partUris; + final List partUris; LibraryMetadata(this.name, this.importUri, this.partUris); - LibraryMetadata.fromJson(Map json) { - name = _readRequiredField(json, 'name'); - importUri = _readRequiredField(json, 'importUri'); + LibraryMetadata.fromJson(Map json) : + name = _readRequiredField(json, 'name'), + importUri = _readRequiredField(json, 'importUri'), partUris = _readOptionalList(json, 'partUris') ?? []; - } Map toJson() { return { @@ -102,22 +101,22 @@ class ModuleMetadata { /// /// Used as a name of the js module created by the compiler and /// as key to store and load modules in the debugger and the browser - late final String name; + final String name; /// Name of the function enclosing the module /// /// Used by debugger to determine the top dart scope - late final String closureName; + final String closureName; /// Source map uri - late final String sourceMapUri; + final String sourceMapUri; /// Module uri - late final String moduleUri; + final String moduleUri; /// True if the module corresponding to this metadata was compiled with sound /// null safety enabled. - late final bool soundNullSafety; + final bool soundNullSafety; final Map libraries = {}; @@ -142,13 +141,13 @@ class ModuleMetadata { } } - ModuleMetadata.fromJson(Map json) { - version = _readRequiredField(json, 'version'); - name = _readRequiredField(json, 'name'); - closureName = _readRequiredField(json, 'closureName'); - sourceMapUri = _readRequiredField(json, 'sourceMapUri'); - moduleUri = _readRequiredField(json, 'moduleUri'); - soundNullSafety = _readOptionalField(json, 'soundNullSafety') ?? false; + ModuleMetadata.fromJson(Map json): + version = _readRequiredField(json, 'version'), + name = _readRequiredField(json, 'name'), + closureName = _readRequiredField(json, 'closureName'), + sourceMapUri = _readRequiredField(json, 'sourceMapUri'), + moduleUri = _readRequiredField(json, 'moduleUri'), + soundNullSafety = _readOptionalField(json, 'soundNullSafety') ?? false { if (!ModuleMetadataVersion.current.isCompatibleWith(version) && !ModuleMetadataVersion.previous.isCompatibleWith(version)) { throw Exception('Unsupported metadata version $version. ' diff --git a/dwds/lib/src/handlers/injector.dart b/dwds/lib/src/handlers/injector.dart index d0089a41f..ee740a2bb 100644 --- a/dwds/lib/src/handlers/injector.dart +++ b/dwds/lib/src/handlers/injector.dart @@ -123,7 +123,7 @@ class DwdsInjector { return response.change(body: body, headers: newHeaders); } else { final loadResponse = await _loadStrategy.handler(request); - if (loadResponse != null && loadResponse.statusCode != 404) { + if (loadResponse.statusCode != HttpStatus.notFound) { return loadResponse; } return innerHandler(request); From 3a93741b2fd2a80cca06594ee92fb519ca1b2405 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Fri, 17 Jun 2022 15:49:01 -0700 Subject: [PATCH 5/8] Format --- .../debugging/metadata/module_metadata.dart | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/dwds/lib/src/debugging/metadata/module_metadata.dart b/dwds/lib/src/debugging/metadata/module_metadata.dart index 5074c20f8..fffd5916e 100644 --- a/dwds/lib/src/debugging/metadata/module_metadata.dart +++ b/dwds/lib/src/debugging/metadata/module_metadata.dart @@ -74,10 +74,10 @@ class LibraryMetadata { LibraryMetadata(this.name, this.importUri, this.partUris); - LibraryMetadata.fromJson(Map json) : - name = _readRequiredField(json, 'name'), - importUri = _readRequiredField(json, 'importUri'), - partUris = _readOptionalList(json, 'partUris') ?? []; + LibraryMetadata.fromJson(Map json) + : name = _readRequiredField(json, 'name'), + importUri = _readRequiredField(json, 'importUri'), + partUris = _readOptionalList(json, 'partUris') ?? []; Map toJson() { return { @@ -141,13 +141,13 @@ class ModuleMetadata { } } - ModuleMetadata.fromJson(Map json): - version = _readRequiredField(json, 'version'), - name = _readRequiredField(json, 'name'), - closureName = _readRequiredField(json, 'closureName'), - sourceMapUri = _readRequiredField(json, 'sourceMapUri'), - moduleUri = _readRequiredField(json, 'moduleUri'), - soundNullSafety = _readOptionalField(json, 'soundNullSafety') ?? false { + ModuleMetadata.fromJson(Map json) + : version = _readRequiredField(json, 'version'), + name = _readRequiredField(json, 'name'), + closureName = _readRequiredField(json, 'closureName'), + sourceMapUri = _readRequiredField(json, 'sourceMapUri'), + moduleUri = _readRequiredField(json, 'moduleUri'), + soundNullSafety = _readOptionalField(json, 'soundNullSafety') ?? false { if (!ModuleMetadataVersion.current.isCompatibleWith(version) && !ModuleMetadataVersion.previous.isCompatibleWith(version)) { throw Exception('Unsupported metadata version $version. ' From e70b5dfd86c922be478f7cee5fb3b9392dcf783a Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Fri, 17 Jun 2022 17:08:38 -0700 Subject: [PATCH 6/8] Migrated more dwds directories to null safety: connection (some files) handlers (some files) readers servers services (some files) utilities --- dwds/lib/src/connections/app_connection.dart | 2 - dwds/lib/src/handlers/injector.dart | 23 +++--- .../readers/frontend_server_asset_reader.dart | 51 +++++++----- .../readers/proxy_server_asset_reader.dart | 21 ++--- dwds/lib/src/servers/devtools.dart | 4 +- dwds/lib/src/servers/extension_backend.dart | 10 +-- dwds/lib/src/servers/extension_debugger.dart | 59 +++++++------- .../services/expression_compiler_service.dart | 79 ++++++++----------- dwds/lib/src/utilities/dart_uri.dart | 36 +++------ 9 files changed, 128 insertions(+), 157 deletions(-) diff --git a/dwds/lib/src/connections/app_connection.dart b/dwds/lib/src/connections/app_connection.dart index 04669c312..1dd7ed1f1 100644 --- a/dwds/lib/src/connections/app_connection.dart +++ b/dwds/lib/src/connections/app_connection.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'dart:async'; import 'dart:convert'; diff --git a/dwds/lib/src/handlers/injector.dart b/dwds/lib/src/handlers/injector.dart index ee740a2bb..9ad89cbac 100644 --- a/dwds/lib/src/handlers/injector.dart +++ b/dwds/lib/src/handlers/injector.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'dart:async'; import 'dart:convert'; import 'dart:io'; @@ -32,7 +30,7 @@ const _clientScript = 'dwds/src/injected/client'; /// information. class DwdsInjector { final LoadStrategy _loadStrategy; - final Future _extensionUri; + final Future? _extensionUri; final _devHandlerPaths = StreamController(); final _logger = Logger('DwdsInjector'); final bool _enableDevtoolsLaunch; @@ -41,14 +39,14 @@ class DwdsInjector { DwdsInjector( this._loadStrategy, { - Future extensionUri, - bool enableDevtoolsLaunch, - bool useSseForInjectedClient, - bool emitDebugEvents, + Future? extensionUri, + bool enableDevtoolsLaunch = false, + bool useSseForInjectedClient = true, + bool emitDebugEvents = true, }) : _extensionUri = extensionUri, _enableDevtoolsLaunch = enableDevtoolsLaunch, - _useSseForInjectedClient = useSseForInjectedClient ?? true, - _emitDebugEvents = emitDebugEvents ?? true; + _useSseForInjectedClient = useSseForInjectedClient, + _emitDebugEvents = emitDebugEvents; /// Returns the embedded dev handler paths. /// @@ -60,6 +58,9 @@ class DwdsInjector { if (request.url.path.endsWith('$_clientScript.js')) { final uri = await Isolate.resolvePackageUri( Uri.parse('package:$_clientScript.js')); + if (uri == null) { + throw StateError('Cannot resolve "package:$_clientScript.js"'); + } final result = await File(uri.toFilePath()).readAsString(); return Response.ok(result, headers: { HttpHeaders.contentTypeHeader: 'application/javascript' @@ -139,7 +140,7 @@ String _injectClientAndHoistMain( String appId, String devHandlerPath, String entrypointPath, - String extensionUri, + String? extensionUri, LoadStrategy loadStrategy, bool enableDevtoolsLaunch, bool emitDebugEvents, @@ -193,7 +194,7 @@ String _injectedClientSnippet( String appId, String devHandlerPath, String entrypointPath, - String extensionUri, + String? extensionUri, LoadStrategy loadStrategy, bool enableDevtoolsLaunch, bool emitDebugEvents, diff --git a/dwds/lib/src/readers/frontend_server_asset_reader.dart b/dwds/lib/src/readers/frontend_server_asset_reader.dart index 31326f8ba..4fc8309ee 100644 --- a/dwds/lib/src/readers/frontend_server_asset_reader.dart +++ b/dwds/lib/src/readers/frontend_server_asset_reader.dart @@ -2,11 +2,10 @@ // for details. 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 - import 'dart:convert'; import 'dart:io'; +import 'package:logging/logging.dart'; import 'package:package_config/package_config.dart'; import 'package:path/path.dart' as p; @@ -15,6 +14,7 @@ import '../readers/asset_reader.dart'; /// A reader for Dart sources and related source maps provided by the Frontend /// Server. class FrontendServerAssetReader implements AssetReader { + final _logger = Logger('FrontendServerAssetReader'); final File _mapOriginal; final File _mapIncremental; final File _jsonOriginal; @@ -49,29 +49,38 @@ class FrontendServerAssetReader implements AssetReader { .absolute(p.join(_packageRoot, '.dart_tool/package_config.json')))); @override - Future dartSourceContents(String serverPath) async { - if (!serverPath.endsWith('.dart')) return null; - final packageConfig = await _packageConfig; - - Uri fileUri; - if (serverPath.startsWith('packages/')) { - final packagePath = serverPath.replaceFirst('packages/', 'package:'); - fileUri = packageConfig.resolve(Uri.parse(packagePath)); - } else { - fileUri = p.toUri(p.join(_packageRoot, serverPath)); + Future dartSourceContents(String serverPath) async { + if (serverPath.endsWith('.dart')) { + final packageConfig = await _packageConfig; + + Uri? fileUri; + if (serverPath.startsWith('packages/')) { + final packagePath = serverPath.replaceFirst('packages/', 'package:'); + fileUri = packageConfig.resolve(Uri.parse(packagePath)); + } else { + fileUri = p.toUri(p.join(_packageRoot, serverPath)); + } + if (fileUri != null) { + final source = File(fileUri.toFilePath()); + if (source.existsSync()) return source.readAsString(); + } } - - final source = File(fileUri.toFilePath()); - if (!await source.exists()) return null; - return await source.readAsString(); + _logger.severe('Cannot find source contents for $serverPath'); + return null; } @override - Future sourceMapContents(String serverPath) async { - if (!serverPath.endsWith('lib.js.map')) return null; - if (!serverPath.startsWith('/')) serverPath = '/$serverPath'; - // Strip the .map, sources are looked up by their js path. - return _mapContents[p.withoutExtension(serverPath)]; + Future sourceMapContents(String serverPath) async { + if (serverPath.endsWith('lib.js.map')) { + if (!serverPath.startsWith('/')) serverPath = '/$serverPath'; + // Strip the .map, sources are looked up by their js path. + serverPath = p.withoutExtension(serverPath); + if (_mapContents.containsKey(serverPath)) { + return _mapContents[serverPath]; + } + } + _logger.severe('Cannot find source map contents for $serverPath'); + return null; } /// Updates the internal caches by reading the Frontend Server output files. diff --git a/dwds/lib/src/readers/proxy_server_asset_reader.dart b/dwds/lib/src/readers/proxy_server_asset_reader.dart index 70352a236..960809eca 100644 --- a/dwds/lib/src/readers/proxy_server_asset_reader.dart +++ b/dwds/lib/src/readers/proxy_server_asset_reader.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'dart:async'; import 'dart:convert'; import 'dart:io'; @@ -20,14 +18,11 @@ import 'asset_reader.dart'; class ProxyServerAssetReader implements AssetReader { final _logger = Logger('ProxyServerAssetReader'); - Handler _handler; - http.Client _client; + late final Handler _handler; + late final http.Client _client; ProxyServerAssetReader(int assetServerPort, - {String root, String host, bool isHttps}) { - host ??= 'localhost'; - root ??= ''; - isHttps ??= false; + {String root = '', String host = 'localhost', bool isHttps = false}) { final scheme = isHttps ? 'https://' : 'http://'; final inner = HttpClient() ..maxConnectionsPerHost = 200 @@ -37,19 +32,19 @@ class ProxyServerAssetReader implements AssetReader { ? IOClient(inner..badCertificateCallback = (cert, host, port) => true) : IOClient(inner); var url = '$scheme$host:$assetServerPort/'; - if (root?.isNotEmpty ?? false) url += '$root/'; + if (root.isNotEmpty) url += '$root/'; _handler = proxyHandler(url, client: _client); } @override - Future dartSourceContents(String serverPath) => + Future dartSourceContents(String serverPath) => _readResource(serverPath); @override - Future sourceMapContents(String serverPath) => + Future sourceMapContents(String serverPath) => _readResource(serverPath); - Future _readResource(String path) async { + Future _readResource(String path) async { // Handlers expect a fully formed HTML URI. The actual hostname and port // does not matter. final response = @@ -71,7 +66,7 @@ class ProxyServerAssetReader implements AssetReader { } @override - Future metadataContents(String serverPath) => + Future metadataContents(String serverPath) => _readResource(serverPath); @override diff --git a/dwds/lib/src/servers/devtools.dart b/dwds/lib/src/servers/devtools.dart index d1c696470..e1deb70e7 100644 --- a/dwds/lib/src/servers/devtools.dart +++ b/dwds/lib/src/servers/devtools.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'dart:io'; typedef DevtoolsLauncher = Future Function(String hostname); @@ -17,7 +15,7 @@ class DevTools { /// Null until [close] is called. /// /// All subsequent calls to [close] will return this future. - Future _closed; + Future? _closed; DevTools(this.hostname, this.port, this._server); diff --git a/dwds/lib/src/servers/extension_backend.dart b/dwds/lib/src/servers/extension_backend.dart index 584ab7526..cf22f8792 100644 --- a/dwds/lib/src/servers/extension_backend.dart +++ b/dwds/lib/src/servers/extension_backend.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'dart:async'; import 'dart:io'; @@ -20,13 +18,12 @@ import 'extension_debugger.dart'; const authenticationResponse = 'Dart Debug Authentication Success!\n\n' 'You can close this tab and launch the Dart Debug Extension again.'; -Logger _logger = Logger('ExtensionBackend'); - /// A backend for the Dart Debug Extension. /// /// Sets up an SSE handler to communicate with the extension background. /// Uses that SSE channel to create an [ExtensionDebugger]. class ExtensionBackend { + static final _logger = Logger('ExtensionBackend'); final String hostname; final int port; final HttpServer _server; @@ -34,7 +31,7 @@ class ExtensionBackend { /// Null until [close] is called. /// /// All subsequent calls to [close] will return this future. - Future _closed; + Future? _closed; ExtensionBackend._( SocketHandler socketHandler, this.hostname, this.port, this._server) @@ -47,7 +44,8 @@ class ExtensionBackend { cascade = cascade.add((request) { if (request.url.path == authenticationPath) { return Response.ok(authenticationResponse, headers: { - 'Access-Control-Allow-Origin': request.headers['origin'], + if (request.headers.containsKey('origin')) + 'Access-Control-Allow-Origin': request.headers['origin']!, 'Access-Control-Allow-Credentials': 'true' }); } diff --git a/dwds/lib/src/servers/extension_debugger.dart b/dwds/lib/src/servers/extension_debugger.dart index 42e088990..09336717c 100644 --- a/dwds/lib/src/servers/extension_debugger.dart +++ b/dwds/lib/src/servers/extension_debugger.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'dart:async'; import 'dart:collection'; import 'dart:convert'; @@ -32,12 +30,12 @@ class ExtensionDebugger implements RemoteDebugger { /// Null until [close] is called. /// /// All subsequent calls to [close] will return this future. - Future _closed; + Future? _closed; - String instanceId; - ExecutionContext _executionContext; + String? instanceId; + ExecutionContext? _executionContext; - ExecutionContext get executionContext => _executionContext; + ExecutionContext? get executionContext => _executionContext; final _devToolsRequestController = StreamController(); @@ -139,7 +137,7 @@ class ExtensionDebugger implements RemoteDebugger { /// over the SSE connection. @override Future sendCommand(String command, - {Map params}) { + {Map? params}) { final completer = Completer(); final id = newId(); _completers[id] = completer; @@ -174,7 +172,7 @@ class ExtensionDebugger implements RemoteDebugger { Future getScriptSource(String scriptId) async => (await sendCommand('Debugger.getScriptSource', params: {'scriptId': scriptId})) - .result['scriptSource'] as String; + .result!['scriptSource'] as String; @override Future pause() => sendCommand('Debugger.pause'); @@ -194,14 +192,14 @@ class ExtensionDebugger implements RemoteDebugger { } @override - Future stepInto({Map params}) => + Future stepInto({Map? params}) => sendCommand('Debugger.stepInto', params: params); @override Future stepOut() => sendCommand('Debugger.stepOut'); @override - Future stepOver({Map params}) => + Future stepOver({Map? params}) => sendCommand('Debugger.stepOver', params: params); @override @@ -212,7 +210,7 @@ class ExtensionDebugger implements RemoteDebugger { @override Future evaluate(String expression, - {bool returnByValue, int contextId}) async { + {bool? returnByValue, int? contextId}) async { final params = { 'expression': expression, }; @@ -223,12 +221,8 @@ class ExtensionDebugger implements RemoteDebugger { params['contextId'] = contextId; } final response = await sendCommand('Runtime.evaluate', params: params); - if (response.result.containsKey('exceptionDetails')) { - throw ChromeDebugException( - response.result['exceptionDetails'] as Map); - } else { - return RemoteObject(response.result['result'] as Map); - } + final result = _validateResult(response.result); + return RemoteObject(result['result'] as Map); } @override @@ -240,12 +234,8 @@ class ExtensionDebugger implements RemoteDebugger { }; final response = await sendCommand('Debugger.evaluateOnCallFrame', params: params); - if (response.result.containsKey('exceptionDetails')) { - throw ChromeDebugException( - response.result['exceptionDetails'] as Map); - } else { - return RemoteObject(response.result['result'] as Map); - } + final result = _validateResult(response.result); + return RemoteObject(result['result'] as Map); } @override @@ -256,14 +246,10 @@ class ExtensionDebugger implements RemoteDebugger { }; final response = await sendCommand('Debugger.getPossibleBreakpoints', params: params); - if (response.result.containsKey('exceptionDetails')) { - throw ChromeDebugException( - response.result['exceptionDetails'] as Map); - } else { - final locations = response.result['locations'] as List; - return List.from(locations - .map((map) => WipBreakLocation(map as Map))); - } + final result = _validateResult(response.result); + final locations = result['locations'] as List; + return List.from( + locations.map((map) => WipBreakLocation(map as Map))); } @override @@ -315,4 +301,15 @@ class ExtensionDebugger implements RemoteDebugger { throw ArgumentError('unknown state: $state'); } } + + Map _validateResult(Map? result) { + if (result == null) { + throw ChromeDebugException({'result': null}); + } + if (result.containsKey('exceptionDetails')) { + throw ChromeDebugException( + result['exceptionDetails'] as Map); + } + return result; + } } diff --git a/dwds/lib/src/services/expression_compiler_service.dart b/dwds/lib/src/services/expression_compiler_service.dart index 0c8a9b5e3..63a10097f 100644 --- a/dwds/lib/src/services/expression_compiler_service.dart +++ b/dwds/lib/src/services/expression_compiler_service.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'dart:async'; import 'dart:isolate'; @@ -15,18 +13,15 @@ import '../utilities/dart_uri.dart'; import '../utilities/sdk_configuration.dart'; import 'expression_compiler.dart'; -final _logger = Logger('ExpressionCompilerService'); - class _Compiler { - final Isolate _worker; - final StreamQueue _responseQueue; + static final _logger = Logger('_Compiler'); + final StreamQueue _responseQueue; final ReceivePort _receivePort; final SendPort _sendPort; - Future _dependencyUpdate; + Future? _dependencyUpdate; _Compiler._( - this._worker, this._responseQueue, this._receivePort, this._sendPort, @@ -71,11 +66,11 @@ class _Compiler { ) async { sdkConfiguration.validate(); - final librariesUri = sdkConfiguration.librariesUri; - final workerUri = sdkConfiguration.compilerWorkerUri; + final librariesUri = sdkConfiguration.librariesUri!; + final workerUri = sdkConfiguration.compilerWorkerUri!; final sdkSummaryUri = soundNullSafety - ? sdkConfiguration.soundSdkSummaryUri - : sdkConfiguration.unsoundSdkSummaryUri; + ? sdkConfiguration.soundSdkSummaryUri! + : sdkConfiguration.unsoundSdkSummaryUri!; final args = [ '--experimental-expression-compiler', @@ -97,29 +92,26 @@ class _Compiler { _logger.finest('$workerUri ${args.join(' ')}'); final receivePort = ReceivePort(); - final isolate = await Isolate.spawnUri( + await Isolate.spawnUri( workerUri, args, receivePort.sendPort, // Note(annagrin): ddc snapshot is generated with no asserts, so we have // to run it unchecked in case the calling isolate is checked, as it // happens, for example, when debugging webdev in VSCode or running tests - // using 'pub run' + // using 'dart run' checked: false, ); final responseQueue = StreamQueue(receivePort); final sendPort = await responseQueue.next as SendPort; - final service = _Compiler._(isolate, responseQueue, receivePort, sendPort); + final service = _Compiler._(responseQueue, receivePort, sendPort); return service; } Future updateDependencies(Map modules) async { - if (_worker == null) { - throw StateError('Expression compilation service has stopped'); - } final updateCompleter = Completer(); _dependencyUpdate = updateCompleter.future; @@ -131,14 +123,13 @@ class _Compiler { 'inputs': [ for (var moduleName in modules.keys) { - 'path': modules[moduleName].fullDillPath, - if (modules[moduleName].summaryPath != null) - 'summaryPath': modules[moduleName].summaryPath, + 'path': modules[moduleName]!.fullDillPath, + 'summaryPath': modules[moduleName]!.summaryPath, 'moduleName': moduleName }, ] }); - final result = response == null ? false : response['succeeded'] as bool; + final result = (response['succeeded'] as bool?) ?? false; if (result) { _logger.info('Updated dependencies.'); } else { @@ -160,11 +151,13 @@ class _Compiler { String moduleName, String expression, ) async { - if (_worker == null) { - throw StateError('Expression compilation service has stopped'); + _logger.finest('Waiting for dependencies to update'); + if (_dependencyUpdate == null) { + _logger + .warning('Dependencies are not updated before compiling expressions'); + return ExpressionCompilationResult('', true); } - _logger.finest('Waiting for dependencies to update'); await _dependencyUpdate; _logger.finest('Compiling "$expression" at $libraryUri:$line'); @@ -180,20 +173,16 @@ class _Compiler { 'moduleName': moduleName, }); - var succeeded = false; - var result = ''; + final errors = response['errors'] as List?; + final e = response['exception']; + final s = response['stackTrace']; + final error = (errors != null && errors.isNotEmpty) + ? errors.first + : (e != null ? '$e:$s' : ''); + final procedure = response['compiledProcedure'] as String; + final succeeded = (response['succeeded'] as bool?) ?? false; + final result = succeeded ? procedure : error; - if (response != null) { - final errors = response['errors'] as List; - final e = response['exception']; - final s = response['stackTrace']; - final error = (errors != null && errors.isNotEmpty) - ? errors.first - : (e != null ? '$e:$s' : ''); - final procedure = response['compiledProcedure'] as String; - succeeded = response['succeeded'] as bool; - result = succeeded ? procedure : error; - } if (succeeded) { _logger.finest('Compiled "$expression" to: $result'); } else { @@ -229,6 +218,7 @@ class _Compiler { /// /// Users need to stop the service by calling [stop]. class ExpressionCompilerService implements ExpressionCompiler { + final _logger = Logger('ExpressionCompilerService'); final _compiler = Completer<_Compiler>(); final String _address; final FutureOr _port; @@ -237,13 +227,10 @@ class ExpressionCompilerService implements ExpressionCompiler { final SdkConfigurationProvider _sdkConfigurationProvider; - ExpressionCompilerService( - this._address, - this._port, - this._assetHandler, { - bool verbose = false, - SdkConfigurationProvider sdkConfigurationProvider, - }) : _verbose = verbose, + ExpressionCompilerService(this._address, this._port, this._assetHandler, + {bool verbose = false, + SdkConfigurationProvider? sdkConfigurationProvider}) + : _verbose = verbose, _sdkConfigurationProvider = sdkConfigurationProvider ?? DefaultSdkConfigurationProvider(); @@ -262,7 +249,7 @@ class ExpressionCompilerService implements ExpressionCompiler { @override Future initialize( - {String moduleFormat, bool soundNullSafety = false}) async { + {required String moduleFormat, bool soundNullSafety = false}) async { if (_compiler.isCompleted) return; final compiler = await _Compiler.start( diff --git a/dwds/lib/src/utilities/dart_uri.dart b/dwds/lib/src/utilities/dart_uri.dart index 1afefb784..4b8b4af79 100644 --- a/dwds/lib/src/utilities/dart_uri.dart +++ b/dwds/lib/src/utilities/dart_uri.dart @@ -2,8 +2,6 @@ // for details. 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 - import 'package:logging/logging.dart'; import 'package:package_config/package_config.dart'; import 'package:path/path.dart' as p; @@ -30,7 +28,7 @@ class DartUri { /// served, not to the package. /// /// The optional [root] is the directory the app is served from. - factory DartUri(String uri, [String root]) { + factory DartUri(String uri, [String? root]) { final serverPath = globalLoadStrategy.serverPathForAppUri(uri); if (serverPath != null) { return DartUri._(serverPath); @@ -60,7 +58,7 @@ class DartUri { String toString() => 'DartUri: $serverPath'; /// Construct from a package: URI - factory DartUri._fromPackageUri(String uri, {String root}) { + factory DartUri._fromPackageUri(String uri, {String? root}) { final packagePath = 'packages/${uri.substring("package:".length)}'; if (root != null) { final relativePath = p.url.join(root, packagePath); @@ -70,7 +68,7 @@ class DartUri { } /// Construct from a file: URI - factory DartUri._fromFileUri(String uri, {String root}) { + factory DartUri._fromFileUri(String uri, {String? root}) { final libraryName = _resolvedUriToUri[uri]; if (libraryName != null) return DartUri(libraryName, root); // This is not one of our recorded libraries. @@ -78,7 +76,7 @@ class DartUri { } /// Construct from a path, relative to the directory being served. - factory DartUri._fromRelativePath(String uri, {String root}) { + factory DartUri._fromRelativePath(String uri, {String? root}) { uri = uri[0] == '.' ? uri.substring(1) : uri; uri = uri[0] == '/' ? uri.substring(1) : uri; @@ -98,16 +96,7 @@ class DartUri { static final _logger = Logger('DartUri'); /// The way we resolve file: URLs into package: URLs - static PackageConfig _packageConfig; - - /// SDK installation directory. - /// - /// Directory where the SDK client code built with is installed, - /// - /// For example: `/Users/me/.dart-sdks/2.15.0` - /// - /// Used to resolve SDK urls according to vm_service protocol. - static SdkConfiguration _sdkConfiguration; + static PackageConfig? _packageConfig; /// All of the known absolute library paths, indexed by their library URL. /// @@ -138,10 +127,10 @@ class DartUri { static final Map _resolvedUriToUri = {}; /// Returns package, app, or dart uri for a resolved path. - static String toPackageUri(String uri) => _resolvedUriToUri[uri]; + static String? toPackageUri(String uri) => _resolvedUriToUri[uri]; /// Returns resolved path for a package, app, or dart uri. - static String toResolvedUri(String uri) => _uriToResolvedUri[uri]; + static String? toResolvedUri(String uri) => _uriToResolvedUri[uri]; /// The directory in which we're running. /// @@ -155,15 +144,14 @@ class DartUri { /// Record library and script uris to enable resolving library and script paths. static Future initialize(SdkConfiguration sdkConfiguration) async { - _sdkConfiguration = sdkConfiguration; final packagesUri = p.toUri(p.join(currentDirectory, '.dart_tool/package_config.json')); clear(); - // Allow for tests can supplying empty configurations. - if (_sdkConfiguration.sdkDirectory != null) { - _sdkConfiguration.validateSdkDir(); + // Allow for tests to supply empty configurations. + if (sdkConfiguration.sdkDirectory != null) { + sdkConfiguration.validateSdkDir(); } await _loadPackageConfig(packagesUri); @@ -200,7 +188,7 @@ class DartUri { return; } - String libraryPath; + String? libraryPath; switch (uri.scheme) { case 'dart': // TODO(annagrin): Support resolving `dart:` uris. @@ -214,7 +202,7 @@ class DartUri { libraryPath = p.url.join(currentDirectoryUri, uri.path.substring(1)); break; case 'package': - libraryPath = _packageConfig?.resolve(uri).toString(); + libraryPath = _packageConfig?.resolve(uri)?.toString(); break; default: throw ArgumentError.value(libraryUri, 'URI scheme not allowed'); From bd9879e6a46e7f58d6e5f66ec7fdf45ef398d14f Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Tue, 21 Jun 2022 09:50:42 -0700 Subject: [PATCH 7/8] Fix test failures --- dwds/lib/src/services/expression_compiler_service.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dwds/lib/src/services/expression_compiler_service.dart b/dwds/lib/src/services/expression_compiler_service.dart index 63a10097f..0733918cd 100644 --- a/dwds/lib/src/services/expression_compiler_service.dart +++ b/dwds/lib/src/services/expression_compiler_service.dart @@ -14,7 +14,7 @@ import '../utilities/sdk_configuration.dart'; import 'expression_compiler.dart'; class _Compiler { - static final _logger = Logger('_Compiler'); + static final _logger = Logger('ExpressionCompilerService'); final StreamQueue _responseQueue; final ReceivePort _receivePort; final SendPort _sendPort; From 0f63c3766251f9d630c35af293b22e7f174d7f33 Mon Sep 17 00:00:00 2001 From: Anna Gringauze Date: Tue, 21 Jun 2022 10:23:36 -0700 Subject: [PATCH 8/8] Fix injector test failure --- dwds/test/fixtures/fakes.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dwds/test/fixtures/fakes.dart b/dwds/test/fixtures/fakes.dart index d3c57a4f5..d5daad717 100644 --- a/dwds/test/fixtures/fakes.dart +++ b/dwds/test/fixtures/fakes.dart @@ -267,7 +267,7 @@ class FakeStrategy implements LoadStrategy { shelf.Handler get handler => (request) => (request.url.path == 'someDummyPath') ? shelf.Response.ok('some dummy response') - : null; + : shelf.Response.notFound('someDummyPath'); @override String get id => 'dummy-id';