Skip to content

Commit 5efb563

Browse files
author
Anna Gringauze
authored
Migrate most of debugging/metadata and loaders directories to null safety (#1659)
* Removed dependecies on dwds.dart inside dwds code * Migrate loaders and debugging/metadata directories to null safety * Add format errors * Removed unnecessary 'late' from metadata fields * Format * Fix failing test * Addressed CR comments * Relax some require API to allow nulls, fix test failures
1 parent 66f7aec commit 5efb563

File tree

9 files changed

+122
-97
lines changed

9 files changed

+122
-97
lines changed

dwds/lib/src/debugging/metadata/function.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.import 'dart:async';
44

5-
// @dart = 2.9
6-
75
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
86

97
import '../../loaders/strategy.dart';
@@ -43,8 +41,9 @@ class FunctionMetaData {
4341
response,
4442
evalContents: evalExpression,
4543
);
46-
var name = response.result['result']['value'] as String;
47-
if (name.isEmpty) name = 'Closure';
44+
final name = response.result?['result']?['value'] as String?;
45+
if (name == null) return FunctionMetaData('<unknown>');
46+
if (name.isEmpty) return FunctionMetaData('Closure');
4847
return FunctionMetaData(name);
4948
}
5049
}

dwds/lib/src/debugging/metadata/module_metadata.dart

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
/// Module metadata format version
86
///
97
/// Module reader always creates the current version but is able to read
@@ -77,10 +75,9 @@ class LibraryMetadata {
7775
LibraryMetadata(this.name, this.importUri, this.partUris);
7876

7977
LibraryMetadata.fromJson(Map<String, dynamic> json)
80-
: name = json['name'] as String,
81-
importUri = json['importUri'] as String,
82-
partUris =
83-
List.castFrom<dynamic, String>(json['partUris'] as List<dynamic>);
78+
: name = _readRequiredField(json, 'name'),
79+
importUri = _readRequiredField(json, 'importUri'),
80+
partUris = _readOptionalList(json, 'partUris') ?? [];
8481

8582
Map<String, dynamic> toJson() {
8683
return {
@@ -98,7 +95,7 @@ class LibraryMetadata {
9895
/// See: https://goto.google.com/dart-web-debugger-metadata
9996
class ModuleMetadata {
10097
/// Metadata format version
101-
String version;
98+
late final String version;
10299

103100
/// Module name
104101
///
@@ -125,8 +122,8 @@ class ModuleMetadata {
125122

126123
ModuleMetadata(this.name, this.closureName, this.sourceMapUri, this.moduleUri,
127124
this.soundNullSafety,
128-
{this.version}) {
129-
version ??= ModuleMetadataVersion.current.version;
125+
{String? ver}) {
126+
version = ver ?? ModuleMetadataVersion.current.version;
130127
}
131128

132129
/// Add [library] to metadata
@@ -145,12 +142,12 @@ class ModuleMetadata {
145142
}
146143

147144
ModuleMetadata.fromJson(Map<String, dynamic> json)
148-
: version = json['version'] as String,
149-
name = json['name'] as String,
150-
closureName = json['closureName'] as String,
151-
sourceMapUri = json['sourceMapUri'] as String,
152-
moduleUri = json['moduleUri'] as String,
153-
soundNullSafety = (json['soundNullSafety'] as bool) ?? false {
145+
: version = _readRequiredField(json, 'version'),
146+
name = _readRequiredField(json, 'name'),
147+
closureName = _readRequiredField(json, 'closureName'),
148+
sourceMapUri = _readRequiredField(json, 'sourceMapUri'),
149+
moduleUri = _readRequiredField(json, 'moduleUri'),
150+
soundNullSafety = _readOptionalField(json, 'soundNullSafety') ?? false {
154151
if (!ModuleMetadataVersion.current.isCompatibleWith(version) &&
155152
!ModuleMetadataVersion.previous.isCompatibleWith(version)) {
156153
throw Exception('Unsupported metadata version $version. '
@@ -159,7 +156,7 @@ class ModuleMetadata {
159156
'\n ${ModuleMetadataVersion.previous.version}');
160157
}
161158

162-
for (var l in json['libraries'] as List<dynamic>) {
159+
for (var l in _readRequiredList(json, 'libraries')) {
163160
addLibrary(LibraryMetadata.fromJson(l as Map<String, dynamic>));
164161
}
165162
}
@@ -176,3 +173,23 @@ class ModuleMetadata {
176173
};
177174
}
178175
}
176+
177+
T _readRequiredField<T>(Map<String, dynamic> json, String field) {
178+
if (!json.containsKey(field)) {
179+
throw FormatException('Required field $field is not set in $json');
180+
}
181+
return json[field]! as T;
182+
}
183+
184+
T? _readOptionalField<T>(Map<String, dynamic> json, String field) =>
185+
json[field] as T?;
186+
187+
List<T> _readRequiredList<T>(Map<String, dynamic> json, String field) {
188+
final list = _readRequiredField<List<dynamic>>(json, field);
189+
return List.castFrom<dynamic, T>(list);
190+
}
191+
192+
List<T>? _readOptionalList<T>(Map<String, dynamic> json, String field) {
193+
final list = _readOptionalField<List<dynamic>>(json, field);
194+
return list == null ? null : List.castFrom<dynamic, T>(list);
195+
}

dwds/lib/src/debugging/metadata/provider.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.import 'dart:async';
44

5-
// @dart = 2.9
6-
75
import 'dart:async';
86
import 'dart:convert';
97

@@ -194,8 +192,7 @@ class MetadataProvider {
194192
_addSdkMetadata();
195193
for (var contents in merged.split('\n')) {
196194
try {
197-
if (contents == null ||
198-
contents.isEmpty ||
195+
if (contents.isEmpty ||
199196
contents.startsWith('// intentionally empty:')) continue;
200197
final moduleJson = json.decode(contents);
201198
final metadata =
@@ -237,7 +234,7 @@ class MetadataProvider {
237234
for (var path in library.partUris) {
238235
// Parts in metadata are relative to the library Uri directory.
239236
final partPath = p.url.join(p.dirname(library.importUri), path);
240-
_scripts[library.importUri].add(partPath);
237+
_scripts[library.importUri]!.add(partPath);
241238
_scriptToModule[partPath] = metadata.name;
242239
}
243240
}

dwds/lib/src/handlers/injector.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ class DwdsInjector {
123123
return response.change(body: body, headers: newHeaders);
124124
} else {
125125
final loadResponse = await _loadStrategy.handler(request);
126-
if (loadResponse != null) return loadResponse;
126+
if (loadResponse != null &&
127+
loadResponse.statusCode != HttpStatus.notFound) {
128+
return loadResponse;
129+
}
127130
return innerHandler(request);
128131
}
129132
};

dwds/lib/src/loaders/build_runner_require.dart

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'dart:convert';
86
import 'dart:io';
97

8+
import 'package:logging/logging.dart';
109
import 'package:path/path.dart' as p;
1110
import 'package:shelf/shelf.dart';
1211

@@ -18,26 +17,28 @@ import 'require.dart';
1817

1918
/// Provides a [RequireStrategy] suitable for use with `package:build_runner`.
2019
class BuildRunnerRequireStrategyProvider {
20+
final _logger = Logger('BuildRunnerRequireStrategyProvider');
21+
2122
final Handler _assetHandler;
2223
final ReloadConfiguration _configuration;
2324
final AssetReader _assetReader;
2425

25-
RequireStrategy _requireStrategy;
26+
late final RequireStrategy _requireStrategy = RequireStrategy(
27+
_configuration,
28+
_moduleProvider,
29+
_digestsProvider,
30+
_moduleForServerPath,
31+
_serverPathForModule,
32+
_sourceMapPathForModule,
33+
_serverPathForAppUri,
34+
_moduleInfoForProvider,
35+
_assetReader,
36+
);
2637

2738
BuildRunnerRequireStrategyProvider(
2839
this._assetHandler, this._configuration, this._assetReader);
2940

30-
RequireStrategy get strategy => _requireStrategy ??= RequireStrategy(
31-
_configuration,
32-
_moduleProvider,
33-
_digestsProvider,
34-
_moduleForServerPath,
35-
_serverPathForModule,
36-
_sourceMapPathForModule,
37-
_serverPathForAppUri,
38-
_moduleInfoForProvider,
39-
_assetReader,
40-
);
41+
RequireStrategy get strategy => _requireStrategy;
4142

4243
Future<Map<String, String>> _digestsProvider(
4344
MetadataProvider metadataProvider) async {
@@ -51,9 +52,18 @@ class BuildRunnerRequireStrategyProvider {
5152
throw StateError('Could not read digests at path: $digestsPath');
5253
}
5354
final body = await response.readAsString();
55+
final digests = json.decode(body) as Map<String, dynamic>;
56+
57+
for (final key in digests.keys) {
58+
if (!modules.containsKey(key)) {
59+
_logger.warning('Digest key $key is not a module name.');
60+
}
61+
}
62+
5463
return {
55-
for (var entry in (json.decode(body) as Map<String, dynamic>).entries)
56-
modules[entry.key]: entry.value as String,
64+
for (var entry in digests.entries)
65+
if (modules.containsKey(entry.key))
66+
modules[entry.key]!: entry.value as String,
5767
};
5868
}
5969

@@ -62,10 +72,17 @@ class BuildRunnerRequireStrategyProvider {
6272
(await metadataProvider.moduleToModulePath).map((key, value) =>
6373
MapEntry(key, stripTopLevelDirectory(removeJsExtension(value))));
6474

65-
Future<String> _moduleForServerPath(
66-
MetadataProvider metadataProvider, String serverPath) async =>
67-
(await metadataProvider.modulePathToModule).map((key, value) => MapEntry(
68-
stripTopLevelDirectory(key), value))[relativizePath(serverPath)];
75+
Future<String?> _moduleForServerPath(
76+
MetadataProvider metadataProvider, String serverPath) async {
77+
final modulePathToModule = await metadataProvider.modulePathToModule;
78+
final relativePath = relativizePath(serverPath);
79+
for (var e in modulePathToModule.entries) {
80+
if (stripTopLevelDirectory(e.key) == relativePath) {
81+
return e.value;
82+
}
83+
}
84+
return null;
85+
}
6986

7087
Future<String> _serverPathForModule(
7188
MetadataProvider metadataProvider, String module) async {
@@ -80,7 +97,7 @@ class BuildRunnerRequireStrategyProvider {
8097
return stripTopLevelDirectory(path);
8198
}
8299

83-
String _serverPathForAppUri(String appUri) {
100+
String? _serverPathForAppUri(String appUri) {
84101
if (appUri.startsWith('org-dartlang-app:')) {
85102
// We skip the root from which we are serving.
86103
return Uri.parse(appUri).pathSegments.skip(1).join('/');

dwds/lib/src/loaders/frontend_server_require.dart

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

5-
// @dart = 2.9
6-
75
import 'package:path/path.dart' as p;
86

97
import '../debugging/metadata/provider.dart';
@@ -19,22 +17,22 @@ class FrontendServerRequireStrategyProvider {
1917
final Future<Map<String, String>> Function() _digestsProvider;
2018
final String _basePath;
2119

22-
RequireStrategy _requireStrategy;
20+
late final RequireStrategy _requireStrategy = RequireStrategy(
21+
_configuration,
22+
_moduleProvider,
23+
(_) => _digestsProvider(),
24+
_moduleForServerPath,
25+
_serverPathForModule,
26+
_sourceMapPathForModule,
27+
_serverPathForAppUri,
28+
_moduleInfoForProvider,
29+
_assetReader,
30+
);
2331

2432
FrontendServerRequireStrategyProvider(this._configuration, this._assetReader,
2533
this._digestsProvider, this._basePath);
2634

27-
RequireStrategy get strategy => _requireStrategy ??= RequireStrategy(
28-
_configuration,
29-
_moduleProvider,
30-
(_) => _digestsProvider(),
31-
_moduleForServerPath,
32-
_serverPathForModule,
33-
_sourceMapPathForModule,
34-
_serverPathForAppUri,
35-
_moduleInfoForProvider,
36-
_assetReader,
37-
);
35+
RequireStrategy get strategy => _requireStrategy;
3836

3937
String _removeBasePath(String path) {
4038
if (_basePath.isEmpty) return path;
@@ -43,20 +41,20 @@ class FrontendServerRequireStrategyProvider {
4341
return path.startsWith(base) ? path.substring(base.length) : path;
4442
}
4543

46-
String _addBasePath(String serverPath) =>
47-
_basePath == null || _basePath.isEmpty
48-
? relativizePath(serverPath)
49-
: '$_basePath/${relativizePath(serverPath)}';
44+
String _addBasePath(String serverPath) => _basePath.isEmpty
45+
? relativizePath(serverPath)
46+
: '$_basePath/${relativizePath(serverPath)}';
5047

5148
Future<Map<String, String>> _moduleProvider(
5249
MetadataProvider metadataProvider) async =>
5350
(await metadataProvider.moduleToModulePath).map((key, value) =>
5451
MapEntry(key, relativizePath(removeJsExtension(value))));
5552

56-
Future<String> _moduleForServerPath(
53+
Future<String?> _moduleForServerPath(
5754
MetadataProvider metadataProvider, String serverPath) async {
5855
final modulePathToModule = await metadataProvider.modulePathToModule;
59-
return modulePathToModule[_removeBasePath(serverPath)];
56+
final relativeServerPath = _removeBasePath(serverPath);
57+
return modulePathToModule[relativeServerPath];
6058
}
6159

6260
Future<String> _serverPathForModule(
@@ -67,7 +65,7 @@ class FrontendServerRequireStrategyProvider {
6765
MetadataProvider metadataProvider, String module) async =>
6866
_addBasePath((await metadataProvider.moduleToSourceMap)[module] ?? '');
6967

70-
String _serverPathForAppUri(String appUri) {
68+
String? _serverPathForAppUri(String appUri) {
7169
if (appUri.startsWith('org-dartlang-app:')) {
7270
return _addBasePath(Uri.parse(appUri).path);
7371
}
@@ -79,7 +77,7 @@ class FrontendServerRequireStrategyProvider {
7977
final modules = await metadataProvider.moduleToModulePath;
8078
final result = <String, ModuleInfo>{};
8179
for (var module in modules.keys) {
82-
final modulePath = modules[module];
80+
final modulePath = modules[module]!;
8381
result[module] = ModuleInfo(
8482
// TODO: Save locations of full kernel files in ddc metadata.
8583
// Issue: https://github.com/dart-lang/sdk/issues/43684

dwds/lib/src/loaders/legacy.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.9
6-
75
import 'package:shelf/shelf.dart';
86

97
import '../debugging/metadata/provider.dart';
@@ -61,7 +59,7 @@ class LegacyStrategy extends LoadStrategy {
6159
///
6260
/// Will return `null` if the provided uri is not
6361
/// an app URI.
64-
final String Function(String appUri) _serverPathForAppUri;
62+
final String? Function(String appUri) _serverPathForAppUri;
6563

6664
LegacyStrategy(
6765
this.reloadConfiguration,
@@ -74,7 +72,7 @@ class LegacyStrategy extends LoadStrategy {
7472
) : super(assetReader);
7573

7674
@override
77-
Handler get handler => (request) => null;
75+
Handler get handler => (request) => Response.notFound(request.url);
7876

7977
@override
8078
String get id => 'legacy';
@@ -121,5 +119,5 @@ class LegacyStrategy extends LoadStrategy {
121119
_sourceMapPathForModule(metadataProviderFor(entrypoint), module);
122120

123121
@override
124-
String serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri);
122+
String? serverPathForAppUri(String appUri) => _serverPathForAppUri(appUri);
125123
}

0 commit comments

Comments
 (0)