Skip to content

Commit fe9aeac

Browse files
author
Anna Gringauze
authored
Update library heuristics as short term solution for missing metadata. (#1293)
* Update library heuristics as short term solution for missing metadata. - Return first `org-dartlang` library as `vmservice.rootLib` (or `main` if not found) - Manually add dart_sdk module metadata in `MetadataProvider`. - Update and add tests. Helps: dart-lang/sdk#44760 Helps: #1226 * Test fix * Addressed CR comments
1 parent 18440bf commit fe9aeac

15 files changed

+159
-41
lines changed

dwds/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 10.0.2-dev
2+
3+
- Fix missing sdk libraries in `getObject()` calls.
4+
- Fix incorrect `rootLib` returned by `ChromeProxyService`.
5+
16
## 10.0.1
27

38
- Support `webkit_inspection_protocol` version `^1.0.0`.

dwds/lib/src/debugging/classes.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,14 @@ class ClassHelper extends Domain {
6666
throw UnsupportedError('unknown library: $libraryId');
6767
}
6868
var libraryRef = await inspector.libraryHelper.libraryRefFor(libraryId);
69+
if (libraryRef == null) {
70+
throw Exception('Could not find library: $libraryId');
71+
}
6972
var classRef = classRefFor(libraryId, splitId.last);
7073
clazz = await _constructClass(libraryRef, classRef);
74+
if (clazz == null) {
75+
throw Exception('Could not contruct class: $classRef');
76+
}
7177
return _classes[objectId] = clazz;
7278
}
7379

dwds/lib/src/debugging/inspector.dart

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
// @dart = 2.9
66

7+
import 'package:logging/logging.dart';
78
import 'package:path/path.dart' as p;
89
import 'package:vm_service/vm_service.dart';
910
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
@@ -33,6 +34,8 @@ class AppInspector extends Domain {
3334

3435
Future<List<ScriptRef>> get scriptRefs => _cachedScriptRefs ??= _getScripts();
3536

37+
final _logger = Logger('AppInspector');
38+
3639
/// Map of scriptRef ID to [ScriptRef].
3740
final _scriptRefsById = <String, ScriptRef>{};
3841

@@ -80,18 +83,11 @@ class AppInspector extends Domain {
8083

8184
Future<void> _initialize() async {
8285
var libraries = await libraryHelper.libraryRefs;
83-
isolate.libraries.addAll(libraries);
86+
isolate.rootLib = await libraryHelper.rootLib;
8487

88+
isolate.libraries.addAll(libraries);
8589
await DartUri.recordAbsoluteUris(libraries.map((lib) => lib.uri));
8690

87-
// This relies on the convention that the 2nd to last library is the root
88-
// library (and the last one is the bootstrap library).
89-
if (libraries.length >= 2) {
90-
isolate.rootLib = libraries[libraries.length - 2];
91-
} else {
92-
isolate.rootLib = libraries.last;
93-
}
94-
9591
isolate.extensionRPCs.addAll(await _getExtensionRpcs());
9692
}
9793

@@ -328,15 +324,28 @@ function($argsString) {
328324

329325
Future<Obj> getObject(String isolateId, String objectId,
330326
{int offset, int count}) async {
331-
var library = await _getLibrary(isolateId, objectId);
332-
if (library != null) return library;
333-
var clazz = await classHelper.forObjectId(objectId);
334-
if (clazz != null) return clazz;
335-
var scriptRef = _scriptRefsById[objectId];
336-
if (scriptRef != null) return await _getScript(isolateId, scriptRef);
337-
var instance = await instanceHelper.instanceFor(remoteObjectFor(objectId),
338-
offset: offset, count: count);
339-
if (instance != null) return instance;
327+
try {
328+
var library = await _getLibrary(isolateId, objectId);
329+
if (library != null) {
330+
return library;
331+
}
332+
var clazz = await classHelper.forObjectId(objectId);
333+
if (clazz != null) {
334+
return clazz;
335+
}
336+
var scriptRef = _scriptRefsById[objectId];
337+
if (scriptRef != null) {
338+
return await _getScript(isolateId, scriptRef);
339+
}
340+
var instance = await instanceHelper.instanceFor(remoteObjectFor(objectId),
341+
offset: offset, count: count);
342+
if (instance != null) {
343+
return instance;
344+
}
345+
} catch (e, s) {
346+
_logger.log(Level.FINE, 'getObject failed with exception: $e:$s');
347+
rethrow;
348+
}
340349
throw UnsupportedError('Only libraries, instances, classes, and scripts '
341350
'are supported for getObject');
342351
}

dwds/lib/src/debugging/libraries.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,22 @@ class LibraryHelper extends Domain {
2020
/// Map of libraryRef ID to [LibraryRef].
2121
final _libraryRefsById = <String, LibraryRef>{};
2222

23+
LibraryRef _rootLib;
24+
2325
LibraryHelper(AppInspector Function() provider) : super(provider);
2426

27+
Future<LibraryRef> get rootLib async {
28+
if (_rootLib != null) return _rootLib;
29+
// TODO: read entrypoint from app metadata.
30+
// Issue: https://github.com/dart-lang/webdev/issues/1290
31+
var libraries = await libraryRefs;
32+
_rootLib = libraries.firstWhere((lib) => lib.name.contains('org-dartlang'));
33+
_rootLib =
34+
_rootLib ?? libraries.firstWhere((lib) => lib.name.contains('main'));
35+
_rootLib = _rootLib ?? (libraries.isNotEmpty ? libraries.last : null);
36+
return _rootLib;
37+
}
38+
2539
/// Returns all libraryRefs in the app.
2640
///
2741
/// Note this can return a cached result.

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,45 @@ class MetadataProvider {
2727
final Map<String, List<String>> _scripts = {};
2828
final _metadataMemoizer = AsyncMemoizer();
2929

30+
/// Implicitly imported libraries in any DDC component.
31+
///
32+
/// Currently dart_sdk module does not come with the metadata.
33+
/// To allow evaluation of expressions that use libraries and
34+
/// types from the SDK (such as a dart Type object), add the
35+
/// metadata for dart_sdk manually.
36+
///
37+
/// TODO: Generate sdk module metadata to be consumed by debugger.
38+
/// Issue: https://github.com/dart-lang/sdk/issues/45477
39+
List<String> get sdkLibraries => const [
40+
'dart:_runtime',
41+
'dart:_debugger',
42+
'dart:_foreign_helper',
43+
'dart:_interceptors',
44+
'dart:_internal',
45+
'dart:_isolate_helper',
46+
'dart:_js_helper',
47+
'dart:_js_primitives',
48+
'dart:_metadata',
49+
'dart:_native_typed_data',
50+
'dart:async',
51+
'dart:collection',
52+
'dart:convert',
53+
'dart:developer',
54+
'dart:io',
55+
'dart:isolate',
56+
'dart:js',
57+
'dart:js_util',
58+
'dart:math',
59+
'dart:typed_data',
60+
'dart:indexed_db',
61+
'dart:html',
62+
'dart:html_common',
63+
'dart:svg',
64+
'dart:web_audio',
65+
'dart:web_gl',
66+
'dart:web_sql'
67+
];
68+
3069
MetadataProvider(this.entrypoint, this._assetReader)
3170
: _soundNullSafety = false;
3271

@@ -150,6 +189,7 @@ class MetadataProvider {
150189
entrypoint.replaceAll('.bootstrap.js', '.ddc_merged_metadata');
151190
var merged = await _assetReader.metadataContents(serverPath);
152191
if (merged != null) {
192+
_addSdkMetadata();
153193
for (var contents in merged.split('\n')) {
154194
try {
155195
if (contents == null ||
@@ -197,6 +237,16 @@ class MetadataProvider {
197237
}
198238
}
199239
}
240+
241+
void _addSdkMetadata() {
242+
var moduleName = 'dart_sdk';
243+
244+
for (var lib in sdkLibraries) {
245+
_libraries.add(lib);
246+
_scripts[lib] = [];
247+
_scriptToModule[lib] = moduleName;
248+
}
249+
}
200250
}
201251

202252
class AbsoluteImportUriException implements Exception {

dwds/lib/src/debugging/modules.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ class Modules {
7575

7676
var scriptToModule = await provider.scriptToModule;
7777
for (var script in scriptToModule.keys) {
78-
var serverPath = DartUri(script, _root).serverPath;
78+
var serverPath = script.startsWith('dart:')
79+
? script
80+
: DartUri(script, _root).serverPath;
81+
7982
_sourceToModule[serverPath] = scriptToModule[script];
8083
_sourceToLibrary[serverPath] = Uri.parse(script);
8184
_libraryToModule[script] = scriptToModule[script];

0 commit comments

Comments
 (0)