Skip to content

Commit 65fd8e1

Browse files
authored
Account for server root (#686)
* Account for server root
1 parent 127a1e7 commit 65fd8e1

File tree

10 files changed

+59
-29
lines changed

10 files changed

+59
-29
lines changed

dwds/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.7.2
2+
3+
- Account for root directory path when using `package:` URIs with `DartUri`.
4+
15
## 0.7.1
26

37
- Fix a bug where we would try to create a new isolate even for a failed

dwds/lib/src/debugging/debugger.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class Debugger extends Domain {
169169
}
170170

171171
Future<Null> _initialize() async {
172-
sources = Sources(_assetHandler, _remoteDebugger, _logWriter);
172+
sources = Sources(_assetHandler, _remoteDebugger, _logWriter, _root);
173173
// We must add a listener before enabling the debugger otherwise we will
174174
// miss events.
175175
// Allow a null debugger/connection for unit tests.

dwds/lib/src/debugging/sources.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ class Sources {
4747

4848
final RemoteDebugger _remoteDebugger;
4949

50-
Sources(this._assetHandler, this._remoteDebugger, this._logWriter);
50+
final String _root;
51+
52+
Sources(
53+
this._assetHandler, this._remoteDebugger, this._logWriter, this._root);
5154

5255
/// Returns all [Location] data for a provided Dart source.
5356
Set<Location> locationsForDart(String serverPath) =>
@@ -77,7 +80,11 @@ class Sources {
7780
for (var entry in lineEntry.entries) {
7881
var index = entry.sourceUrlId;
7982
if (index == null) continue;
80-
var dartUri = DartUri(mapping.urls[index], script.url);
83+
// TODO(grouma) - This work seems expensive and likely should be
84+
// cached.
85+
var path = p.join(
86+
p.dirname(Uri.parse(script.url).path), mapping.urls[index]);
87+
var dartUri = DartUri(path, _root);
8188
var location = Location.from(
8289
script.scriptId,
8390
lineEntry,

dwds/lib/src/utilities/dart_uri.dart

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import 'dart:io';
66

7-
import 'package:path/path.dart' as p;
87
import 'package:package_resolver/package_resolver.dart';
8+
import 'package:path/path.dart' as p;
99

1010
/// The URI for a particular Dart file, able to canonicalize from various
1111
/// different representations.
@@ -97,28 +97,34 @@ class DartUri {
9797
/// packages/path/src/path.dart. The optional [serverUri] is the full URI of the
9898
/// JS script. The dirname of that path should give us the missing prefix.
9999
factory DartUri(String uri, [String serverUri]) {
100-
// TODO(401): Remove serverUri after D24 is stable.
101-
if (uri.startsWith('package:')) return DartUri._fromPackageUri(uri);
100+
if (uri.startsWith('package:')) {
101+
return DartUri._fromPackageUri(uri, serverUri: serverUri);
102+
}
102103
if (uri.startsWith('org-dartlang-app:')) return DartUri._fromAppUri(uri);
103104
if (uri.startsWith('google3:')) return DartUri._fromGoogleUri(uri);
104105
if (uri.startsWith('file:')) return DartUri._fromFileUri(uri);
105-
if (uri.startsWith('/packages/')) return DartUri._fromServerPath(uri);
106-
if (uri.startsWith('/')) return DartUri._fromServerPath(uri);
106+
if (uri.startsWith('/packages/')) {
107+
return DartUri._fromRelativePath(uri, serverUri: serverUri);
108+
}
109+
if (uri.startsWith('/')) return DartUri._fromRelativePath(uri);
107110
if (uri.startsWith('http:') || uri.startsWith('https:')) {
108111
return DartUri(Uri.parse(uri).path);
109112
}
110-
// Work around short paths if we have been provided the context.
111-
if (serverUri != null) {
112-
var path = Uri.parse(serverUri).path;
113-
var dir = p.dirname(path);
114-
return DartUri._fromServerPath(p.normalize(p.join(dir, uri)));
115-
}
113+
116114
throw FormatException('Unsupported URI form', uri);
117115
}
118116

117+
/// Returns the dirname for the server URI.
118+
static String _dirForServerUri(String uri) => p.dirname(Uri.parse(uri).path);
119+
119120
/// Construct from a package: URI
120-
factory DartUri._fromPackageUri(String uri) {
121-
return DartUri._('packages/${uri.substring("package:".length)}');
121+
factory DartUri._fromPackageUri(String uri, {String serverUri}) {
122+
var packagePath = 'packages/${uri.substring("package:".length)}';
123+
if (serverUri != null) {
124+
return DartUri._fromRelativePath(
125+
p.join(_dirForServerUri(serverUri), packagePath));
126+
}
127+
return DartUri._(packagePath);
122128
}
123129

124130
/// Construct from a file: URI
@@ -138,17 +144,21 @@ class DartUri {
138144
factory DartUri._fromAppUri(String uri) {
139145
// We ignore the first segment of the path, which is the root
140146
// from which we're serving.
141-
// TODO: To be able to convert to an org-dartlang-app: URI we will
142-
// need to know the root - possibly keep it as a static?
143-
return DartUri._(Uri.parse(uri).pathSegments.skip(1).join('/').toString());
147+
var path = Uri.parse(uri).pathSegments.skip(1).join('/').toString();
148+
return DartUri._(path);
144149
}
145150

146151
DartUri._(this.serverPath);
147152

148153
/// Construct from a path, relative to the directory being served.
149-
factory DartUri._fromServerPath(String uri) {
154+
factory DartUri._fromRelativePath(String uri, {String serverUri}) {
150155
uri = uri[0] == '.' ? uri.substring(1) : uri;
151156
uri = uri[0] == '/' ? uri.substring(1) : uri;
157+
158+
if (serverUri != null) {
159+
return DartUri._fromRelativePath(
160+
p.join(_dirForServerUri(serverUri), uri));
161+
}
152162
return DartUri._(uri);
153163
}
154164
}

dwds/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: dwds
2-
version: 0.7.1
2+
version: 0.7.2
33
author: Dart Team <[email protected]>
44
homepage: https://github.com/dart-lang/webdev/tree/master/dwds
55
description: >-

dwds/test/dart_uri_test.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import 'package:test/test.dart';
77

88
void main() {
99
group('DartUri', () {
10-
test('normalizes server paths', () {
11-
var uri = DartUri('../foo.dart', '/packages/blah/src/blah.dart');
12-
expect(uri.serverPath, 'packages/blah/foo.dart');
13-
});
14-
1510
test('parses package : paths', () {
1611
var uri = DartUri('package:path/path.dart');
1712
expect(uri.serverPath, 'packages/path/path.dart');
1813
});
1914

15+
test('parses package : paths with root', () {
16+
var uri = DartUri(
17+
'package:path/path.dart', 'http://localhost:8080/foo/bar/blah');
18+
expect(uri.serverPath, 'foo/bar/packages/path/path.dart');
19+
});
20+
2021
test('parses org-dartlang-app paths', () {
2122
var uri = DartUri('org-dartlang-app:////blah/main.dart');
2223
expect(uri.serverPath, 'blah/main.dart');

dwds/test/debugging/sources_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void main() {
1818
};
1919
var logs = <LogRecord>[];
2020
var sources = Sources(TestingAssetHandler(assets), null,
21-
(level, message) => logs.add(LogRecord(level, message, '')));
21+
(level, message) => logs.add(LogRecord(level, message, '')), '');
2222
var serverUri = 'http://localhost:1234/';
2323
await sources.scriptParsed(ScriptParsedEvent(WipEvent({
2424
'params': {

webdev/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.5.3-dev
2+
3+
- Depend on the latest `package:dwds`.
4+
15
## 2.5.2
26

37
- Update SDK dependency to minimum of 2.5.0.

webdev/lib/src/version.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webdev/pubspec.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: webdev
22
# Every time this changes you need to run `pub run build_runner build`.
3-
version: 2.5.2
3+
version: 2.5.3-dev
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/webdev
66
description: >-
@@ -46,3 +46,7 @@ dev_dependencies:
4646

4747
executables:
4848
webdev:
49+
50+
dependency_overrides:
51+
dwds:
52+
path: ../dwds

0 commit comments

Comments
 (0)