Skip to content

Commit 60c04b8

Browse files
Markzipancommit-bot@chromium.org
authored andcommitted
[dartdevc] DDK now outputs relative paths in source maps
Bug: https://buganizer.corp.google.com/issues/133784498 Change-Id: Ifbaae661ac401d0a935a100fbbb7fa2e42325abc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/105703 Commit-Queue: Mark Zhou <markzipan@google.com> Reviewed-by: Nate Bosch <nbosch@google.com> Reviewed-by: Jake Macdonald <jakemac@google.com>
1 parent 112dbab commit 60c04b8

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

pkg/dev_compiler/lib/src/compiler/shared_command.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,28 @@ Uri sourcePathToRelativeUri(String source, {bool windows}) {
344344

345345
/// Adjusts the source paths in [sourceMap] to be relative to [sourceMapPath],
346346
/// and returns the new map. Relative paths are in terms of URIs ('/'), not
347-
/// local OS paths (e.g., windows '\').
347+
/// local OS paths (e.g., windows '\'). Sources with a multi-root scheme
348+
/// matching [multiRootScheme] are adjusted to be relative to
349+
/// [multiRootOutputPath].
348350
// TODO(jmesserly): find a new home for this.
349351
Map placeSourceMap(Map sourceMap, String sourceMapPath,
350-
Map<String, String> bazelMappings, String customScheme) {
352+
Map<String, String> bazelMappings, String multiRootScheme,
353+
{String multiRootOutputPath}) {
351354
var map = Map.from(sourceMap);
352355
// Convert to a local file path if it's not.
353356
sourceMapPath = path.fromUri(sourcePathToUri(sourceMapPath));
354357
var sourceMapDir = path.dirname(path.absolute(sourceMapPath));
355358
var list = (map['sources'] as List).toList();
356-
map['sources'] = list;
357359

358360
String makeRelative(String sourcePath) {
359361
var uri = sourcePathToUri(sourcePath);
360362
var scheme = uri.scheme;
361-
if (scheme == 'dart' || scheme == 'package' || scheme == customScheme) {
363+
if (scheme == 'dart' || scheme == 'package' || scheme == multiRootScheme) {
364+
if (scheme == multiRootScheme) {
365+
var multiRootPath = '$multiRootOutputPath${uri.path}';
366+
multiRootPath = path.relative(multiRootPath, from: sourceMapDir);
367+
return multiRootPath;
368+
}
362369
return sourcePath;
363370
}
364371

@@ -379,6 +386,7 @@ Map placeSourceMap(Map sourceMap, String sourceMapPath,
379386
for (int i = 0; i < list.length; i++) {
380387
list[i] = makeRelative(list[i] as String);
381388
}
389+
map['sources'] = list;
382390
map['file'] = makeRelative(map['file'] as String);
383391
return map;
384392
}

pkg/dev_compiler/lib/src/kernel/command.dart

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,14 @@ Future<CompilerResult> _compile(List<String> args,
130130
// the correct location and keeps the real file location hidden from the
131131
// front end.
132132
var multiRootScheme = argResults['multi-root-scheme'] as String;
133+
var multiRootPaths = (argResults['multi-root'] as Iterable<String>)
134+
.map(Uri.base.resolve)
135+
.toList();
136+
var multiRootOutputPath =
137+
_longestPrefixingPath(path.absolute(output), multiRootPaths);
133138

134139
var fileSystem = MultiRootFileSystem(
135-
multiRootScheme,
136-
(argResults['multi-root'] as Iterable<String>)
137-
.map(Uri.base.resolve)
138-
.toList(),
139-
fe.StandardFileSystem.instance);
140+
multiRootScheme, multiRootPaths, fe.StandardFileSystem.instance);
140141

141142
Uri toCustomUri(Uri uri) {
142143
if (uri.scheme == '') {
@@ -354,7 +355,8 @@ Future<CompilerResult> _compile(List<String> args,
354355
jsUrl: path.toUri(output).toString(),
355356
mapUrl: path.toUri(output + '.map').toString(),
356357
bazelMapping: options.bazelMapping,
357-
customScheme: multiRootScheme);
358+
customScheme: multiRootScheme,
359+
multiRootOutputPath: multiRootOutputPath);
358360

359361
outFiles.add(file.writeAsString(jsCode.code));
360362
if (jsCode.sourceMap != null) {
@@ -390,7 +392,8 @@ JSCode jsProgramToCode(JS.Program moduleTree, ModuleFormat format,
390392
String jsUrl,
391393
String mapUrl,
392394
Map<String, String> bazelMapping,
393-
String customScheme}) {
395+
String customScheme,
396+
String multiRootOutputPath}) {
394397
var opts = JS.JavaScriptPrintingOptions(
395398
allowKeywordsInProperties: true, allowSingleLineIfStatements: true);
396399
JS.SimpleJavaScriptPrintingContext printer;
@@ -409,7 +412,8 @@ JSCode jsProgramToCode(JS.Program moduleTree, ModuleFormat format,
409412
Map builtMap;
410413
if (buildSourceMap && sourceMap != null) {
411414
builtMap = placeSourceMap(
412-
sourceMap.build(jsUrl), mapUrl, bazelMapping, customScheme);
415+
sourceMap.build(jsUrl), mapUrl, bazelMapping, customScheme,
416+
multiRootOutputPath: multiRootOutputPath);
413417
var jsDir = path.dirname(path.fromUri(jsUrl));
414418
var relative = path.relative(path.fromUri(mapUrl), from: jsDir);
415419
var relativeMapUrl = path.toUri(relative).toString();
@@ -505,3 +509,14 @@ String _findPackagesFilePath() {
505509
dir = parent;
506510
}
507511
}
512+
513+
/// Inputs must be absolute paths. Returns null if no prefixing path is found.
514+
String _longestPrefixingPath(String basePath, List<Uri> prefixingPaths) {
515+
return prefixingPaths.fold(null, (String previousValue, Uri element) {
516+
if (basePath.startsWith(element.path) &&
517+
(previousValue == null || previousValue.length < element.path.length)) {
518+
return element.path;
519+
}
520+
return previousValue;
521+
});
522+
}

0 commit comments

Comments
 (0)