Skip to content

Commit f44bcb1

Browse files
committed
Use the package_resolver package.
This allows this to be compatible with package specs as well as package directories. See dart-lang/test#327 [email protected] Review URL: https://codereview.chromium.org//2168203002 .
1 parent 4d20e3a commit f44bcb1

File tree

4 files changed

+106
-20
lines changed

4 files changed

+106
-20
lines changed

pkgs/source_map_stack_trace/CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 1.1.0
2+
3+
* `mapStackTrace()` now uses a `SyncPackageResolver` object from the
4+
[`package_resolver` package][package_resolver] to recreate `package:` URIs.
5+
6+
* **Deprecation**: the `packageRoot` parameter to `mapStackTrace` is deprecated
7+
in favor of the `packageInfo` parameter described above. It will be removed in
8+
a future release.
9+
10+
[package_resolver]: https://pub.dartlang.org/packages/package_resolver
11+
112
## 1.0.5
213

314
* Add compatibility for member names that include named arguments.

pkgs/source_map_stack_trace/lib/source_map_stack_trace.dart

+37-15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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+
import 'package:package_resolver/package_resolver.dart';
56
import 'package:path/path.dart' as p;
67
import 'package:source_maps/source_maps.dart';
78
import 'package:stack_trace/stack_trace.dart';
@@ -12,33 +13,42 @@ import 'package:stack_trace/stack_trace.dart';
1213
/// [minified] indicates whether or not the dart2js code was minified. If it
1314
/// hasn't, this tries to clean up the stack frame member names.
1415
///
15-
/// [packageRoot] is the URI (usually a `file:` URI) for the package root that
16-
/// was used by dart2js. It can be a [String] or a [Uri]. If it's passed, stack
17-
/// frames from packages will use `package:` URLs.
16+
/// If [packageResolver] is passed, it's used to reconstruct `package:` URIs for
17+
/// stack frames that come from packages.
1818
///
1919
/// [sdkRoot] is the URI (usually a `file:` URI) for the SDK containing dart2js.
2020
/// It can be a [String] or a [Uri]. If it's passed, stack frames from the SDK
2121
/// will have `dart:` URLs.
22+
///
23+
/// [packageRoot] is deprecated and shouldn't be used in new code. This throws
24+
/// an [ArgumentError] if [packageRoot] and [packageResolver] are both passed.
2225
StackTrace mapStackTrace(Mapping sourceMap, StackTrace stackTrace,
23-
{bool minified: false, packageRoot, sdkRoot}) {
26+
{bool minified: false, SyncPackageResolver packageResolver, sdkRoot,
27+
@Deprecated("Use the packageResolver parameter instead.") packageRoot}) {
28+
if (packageRoot != null) {
29+
if (packageResolver != null) {
30+
throw new ArgumentError(
31+
"packageResolver and packageRoot may not both be passed.");
32+
}
33+
34+
packageResolver = new SyncPackageResolver.root(packageRoot);
35+
}
36+
2437
if (stackTrace is Chain) {
2538
return new Chain(stackTrace.traces.map((trace) {
26-
return new Trace.from(mapStackTrace(sourceMap, trace,
27-
minified: minified, packageRoot: packageRoot, sdkRoot: sdkRoot));
39+
return new Trace.from(mapStackTrace(
40+
sourceMap, trace,
41+
minified: minified,
42+
packageResolver: packageResolver,
43+
sdkRoot: sdkRoot));
2844
}));
2945
}
3046

31-
if (packageRoot != null && packageRoot is! String && packageRoot is! Uri) {
32-
throw new ArgumentError(
33-
'packageRoot must be a String or a Uri, was "$packageRoot".');
34-
}
35-
3647
if (sdkRoot != null && sdkRoot is! String && sdkRoot is! Uri) {
3748
throw new ArgumentError(
3849
'sdkRoot must be a String or a Uri, was "$sdkRoot".');
3950
}
4051

41-
packageRoot = packageRoot == null ? null : packageRoot.toString();
4252
var sdkLib = sdkRoot == null ? null : "$sdkRoot/lib";
4353

4454
var trace = new Trace.from(stackTrace);
@@ -61,9 +71,21 @@ StackTrace mapStackTrace(Mapping sourceMap, StackTrace stackTrace,
6171
var sourceUrl = span.sourceUrl.toString();
6272
if (sdkRoot != null && p.url.isWithin(sdkLib, sourceUrl)) {
6373
sourceUrl = "dart:" + p.url.relative(sourceUrl, from: sdkLib);
64-
} else if (packageRoot != null && p.url.isWithin(packageRoot, sourceUrl)) {
65-
sourceUrl = "package:" +
66-
p.url.relative(sourceUrl, from: packageRoot);
74+
} else if (packageResolver != null) {
75+
if (packageResolver.packageRoot != null &&
76+
p.url.isWithin(packageResolver.packageRoot.toString(), sourceUrl)) {
77+
sourceUrl = "package:" + p.url.relative(sourceUrl,
78+
from: packageResolver.packageRoot.toString());
79+
} else {
80+
for (var package in packageResolver.packageConfigMap.keys) {
81+
var packageUrl = packageResolver.packageConfigMap[package].toString();
82+
if (!p.url.isWithin(packageUrl, sourceUrl)) continue;
83+
84+
sourceUrl = "package:$package/" +
85+
p.url.relative(sourceUrl, from: packageUrl);
86+
break;
87+
}
88+
}
6789
}
6890

6991
return new Frame(

pkgs/source_map_stack_trace/pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
name: source_map_stack_trace
2-
version: 1.0.5
2+
version: 1.1.0
33
description: >
44
A package for applying source maps to stack traces.
55
author: Dart Team <[email protected]>
66
homepage: https://github.com/dart-lang/source_map_stack_trace
77

88
dependencies:
9+
package_resolver: "^1.0.0"
910
stack_trace: "^1.0.0"
1011
source_maps: "^0.10.0"
1112

pkgs/source_map_stack_trace/test/source_map_stack_trace_test.dart

+56-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
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+
import 'package:package_resolver/package_resolver.dart';
56
import 'package:source_maps/source_maps.dart';
67
import 'package:source_span/source_span.dart';
78
import 'package:stack_trace/stack_trace.dart';
@@ -102,6 +103,53 @@ foo.dart.js 10:11 baz
102103
expect(frame.column, equals(4));
103104
});
104105

106+
test("uses package: URIs for frames within packageResolver.packageRoot", () {
107+
var trace = new Trace.parse("foo.dart.js 10 foo");
108+
var builder = new SourceMapBuilder()
109+
..addSpan(
110+
new SourceMapSpan.identifier(
111+
new SourceLocation(1,
112+
line: 1, column: 3, sourceUrl: "packages/foo/foo.dart"),
113+
"qux"),
114+
new SourceSpan(
115+
new SourceLocation(8, line: 5, column: 0),
116+
new SourceLocation(12, line: 9, column: 1),
117+
"\n" * 4));
118+
119+
var mapping = parseJson(builder.build("foo.dart.js.map"));
120+
var mappedTrace = _mapTrace(mapping, trace,
121+
packageResolver: new SyncPackageResolver.root("packages/"));
122+
var frame = mappedTrace.frames.first;
123+
expect(frame.uri, equals(Uri.parse("package:foo/foo.dart")));
124+
expect(frame.line, equals(2));
125+
expect(frame.column, equals(4));
126+
});
127+
128+
test("uses package: URIs for frames within a packageResolver.packageMap URL",
129+
() {
130+
var trace = new Trace.parse("foo.dart.js 10 foo");
131+
var builder = new SourceMapBuilder()
132+
..addSpan(
133+
new SourceMapSpan.identifier(
134+
new SourceLocation(1,
135+
line: 1, column: 3, sourceUrl: "packages/foo/foo.dart"),
136+
"qux"),
137+
new SourceSpan(
138+
new SourceLocation(8, line: 5, column: 0),
139+
new SourceLocation(12, line: 9, column: 1),
140+
"\n" * 4));
141+
142+
var mapping = parseJson(builder.build("foo.dart.js.map"));
143+
var mappedTrace = _mapTrace(mapping, trace,
144+
packageResolver: new SyncPackageResolver.config({
145+
"foo": Uri.parse("packages/foo")
146+
}));
147+
var frame = mappedTrace.frames.first;
148+
expect(frame.uri, equals(Uri.parse("package:foo/foo.dart")));
149+
expect(frame.line, equals(2));
150+
expect(frame.column, equals(4));
151+
});
152+
105153
test("uses dart: URIs for frames within sdkRoot", () {
106154
var trace = new Trace.parse("foo.dart.js 10 foo");
107155
var builder = new SourceMapBuilder()
@@ -198,17 +246,21 @@ foo.dart.js 10:11 baz
198246
/// Like [mapStackTrace], but is guaranteed to return a [Trace] so it can be
199247
/// inspected.
200248
Trace _mapTrace(Mapping sourceMap, StackTrace stackTrace,
201-
{bool minified: false, packageRoot, sdkRoot}) {
249+
{bool minified: false, SyncPackageResolver packageResolver, sdkRoot,
250+
packageRoot}) {
202251
return new Trace.from(mapStackTrace(sourceMap, stackTrace,
203-
minified: minified, packageRoot: packageRoot, sdkRoot: sdkRoot));
252+
minified: minified, packageResolver: packageResolver, sdkRoot: sdkRoot,
253+
packageRoot: packageRoot));
204254
}
205255

206256
/// Like [mapStackTrace], but is guaranteed to return a [Chain] so it can be
207257
/// inspected.
208258
Chain _mapChain(Mapping sourceMap, StackTrace stackTrace,
209-
{bool minified: false, packageRoot, sdkRoot}) {
259+
{bool minified: false, SyncPackageResolver packageResolver, sdkRoot,
260+
packageRoot}) {
210261
return new Chain.forTrace(mapStackTrace(sourceMap, stackTrace,
211-
minified: minified, packageRoot: packageRoot, sdkRoot: sdkRoot));
262+
minified: minified, packageResolver: packageResolver, sdkRoot: sdkRoot,
263+
packageRoot: packageRoot));
212264
}
213265

214266
/// Runs the mapper's prettification logic on [member] and returns the result.

0 commit comments

Comments
 (0)