Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 8ca3f6e

Browse files
authored
Use VMScriptRef to identify VM scripts, not URI (#216)
Previously, coverage collection assumed that a script could be uniquely identified by URI, which is not a valid assumption. For example, when a part is loaded via two libraries, both of which are loaded by an isolate, the VM will track these are two scripts that map to the same URI. During collection, we now track each script by its (unique) VMScriptRef. This ensures we lookup the correct script when computing the affected line for each hit token. The hitmap remains URI based, since in the end, we want a single, unified set of line->hitCount mappings per script. Fixes dart-lang/coverage#194
1 parent 13299e1 commit 8ca3f6e

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

lib/src/collect.dart

+9-11
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ Future _waitIsolatesPaused(VMServiceClient service, {Duration timeout}) async {
8080
Future<List<Map<String, dynamic>>> _getCoverageJson(
8181
VMServiceClient service, VMSourceReport report) async {
8282
var scriptRefs = report.ranges.map((r) => r.script).toSet();
83-
var scripts = <Uri, VMScript>{};
84-
for (var script in await Future.wait<VMScript>(
85-
scriptRefs.map((ref) => ref.load()).toList())) {
86-
scripts[script.uri] = script;
83+
var scripts = <VMScriptRef, VMScript>{};
84+
for (var ref in scriptRefs) {
85+
scripts[ref] = await ref.load();
8786
}
8887

8988
// script uri -> { line -> hit count }
@@ -94,7 +93,7 @@ Future<List<Map<String, dynamic>>> _getCoverageJson(
9493

9594
hitMaps.putIfAbsent(range.script.uri, () => <int, int>{});
9695
var hitMap = hitMaps[range.script.uri];
97-
var script = scripts[range.script.uri];
96+
var script = scripts[range.script];
9897
for (VMScriptToken hit in range.hits ?? []) {
9998
var line = script.sourceLocation(hit).line + 1;
10099
hitMap[line] = hitMap.containsKey(line) ? hitMap[line] + 1 : 1;
@@ -108,27 +107,26 @@ Future<List<Map<String, dynamic>>> _getCoverageJson(
108107
// Output JSON
109108
var coverage = <Map<String, dynamic>>[];
110109
hitMaps.forEach((uri, hitMap) {
111-
var script = scripts[uri];
112-
coverage.add(_toScriptCoverageJson(script, hitMap));
110+
coverage.add(_toScriptCoverageJson(uri, hitMap));
113111
});
114112
return coverage;
115113
}
116114

117115
/// Returns a JSON hit map backward-compatible with pre-1.16.0 SDKs.
118116
Map<String, dynamic> _toScriptCoverageJson(
119-
VMScript script, Map<int, int> hitMap) {
117+
Uri scriptUri, Map<int, int> hitMap) {
120118
var json = <String, dynamic>{};
121119
var hits = <int>[];
122120
hitMap.forEach((line, hitCount) {
123121
hits.add(line);
124122
hits.add(hitCount);
125123
});
126-
json['source'] = '${script.uri}';
124+
json['source'] = '$scriptUri';
127125
json['script'] = {
128126
'type': '@Script',
129127
'fixedId': true,
130-
'id': 'libraries/1/scripts/${Uri.encodeComponent(script.uri.toString())}',
131-
'uri': '${script.uri}',
128+
'id': 'libraries/1/scripts/${Uri.encodeComponent(scriptUri.toString())}',
129+
'uri': '$scriptUri',
132130
'_kind': 'library',
133131
};
134132
json['hits'] = hits;

0 commit comments

Comments
 (0)