From 655ddb2b351b32c0e058d9a04ee88e2494ab0ef4 Mon Sep 17 00:00:00 2001 From: Jens Johansen Date: Wed, 13 Dec 2023 10:59:55 +0100 Subject: [PATCH] Send over script uri This is different than library uri when working in parts, put also in patched libraries and in the future (possibly) in ammended libraries. --- dwds/lib/src/debugging/modules.dart | 22 ++++++++++++++- .../batched_expression_evaluator.dart | 15 ++++++++-- .../src/services/chrome_proxy_service.dart | 2 ++ .../lib/src/services/expression_compiler.dart | 1 + .../services/expression_compiler_service.dart | 3 ++ .../src/services/expression_evaluator.dart | 15 ++++++++++ dwds/lib/src/utilities/dart_uri.dart | 4 +++ .../expression_compiler_service_test.dart | 5 ++++ dwds/test/expression_evaluator_test.dart | 16 +++++------ dwds/test/fixtures/fakes.dart | 5 ++++ .../lib/src/frontend_server_client.dart | 28 ++++++++++++++++--- 11 files changed, 101 insertions(+), 15 deletions(-) diff --git a/dwds/lib/src/debugging/modules.dart b/dwds/lib/src/debugging/modules.dart index 37b857af1..3ce1d1f62 100644 --- a/dwds/lib/src/debugging/modules.dart +++ b/dwds/lib/src/debugging/modules.dart @@ -17,6 +17,10 @@ class Modules { // The Dart server path to library import uri final _sourceToLibrary = {}; + + // The Dart server path to resolved uri aka "file uri". + final _sourceToResolvedUri = {}; + var _moduleMemoizer = AsyncMemoizer(); final Map _libraryToModule = {}; @@ -34,6 +38,7 @@ class Modules { // across hot reloads. _sourceToModule.clear(); _sourceToLibrary.clear(); + _sourceToResolvedUri.clear(); _libraryToModule.clear(); _moduleMemoizer = AsyncMemoizer(); _entrypoint = entrypoint; @@ -51,6 +56,12 @@ class Modules { return _sourceToLibrary[serverPath]; } + /// Returns the resolived uri (or file uri) for the provided Dart server path. + Future resolvedUriForSource(String serverPath) async { + await _moduleMemoizer.runOnce(_initializeMapping); + return _sourceToResolvedUri[serverPath]; + } + Future moduleForLibrary(String libraryUri) async { await _moduleMemoizer.runOnce(_initializeMapping); return _libraryToModule[libraryUri]; @@ -62,7 +73,8 @@ class Modules { return _sourceToModule; } - /// Initializes [_sourceToModule] and [_sourceToLibrary]. + /// Initializes [_sourceToModule], [_sourceToLibrary] and + /// [_sourceToResolvedUri]. Future _initializeMapping() async { final provider = globalToolConfiguration.loadStrategy.metadataProviderFor(_entrypoint); @@ -81,15 +93,23 @@ class Modules { _sourceToModule[libraryServerPath] = module; _sourceToLibrary[libraryServerPath] = Uri.parse(library); + final resolvedUri = DartUri.uriToResolvedUri[library]; + if (resolvedUri != null) { + _sourceToResolvedUri[libraryServerPath] = Uri.parse(resolvedUri); + } _libraryToModule[library] = module; for (var script in scripts) { final scriptServerPath = script.startsWith('dart:') ? script : DartUri(script, _root).serverPath; + final resolvedUri = DartUri.uriToResolvedUri[script]; _sourceToModule[scriptServerPath] = module; _sourceToLibrary[scriptServerPath] = Uri.parse(library); + if (resolvedUri != null) { + _sourceToResolvedUri[scriptServerPath] = Uri.parse(resolvedUri); + } } } else { _logger.warning('No module found for library $library'); diff --git a/dwds/lib/src/services/batched_expression_evaluator.dart b/dwds/lib/src/services/batched_expression_evaluator.dart index b5e56c2af..6b5cf4d0d 100644 --- a/dwds/lib/src/services/batched_expression_evaluator.dart +++ b/dwds/lib/src/services/batched_expression_evaluator.dart @@ -19,11 +19,18 @@ import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart'; class EvaluateRequest { final String isolateId; final String? libraryUri; + final String? scriptUri; final String expression; final Map? scope; final completer = Completer(); - EvaluateRequest(this.isolateId, this.libraryUri, this.expression, this.scope); + EvaluateRequest( + this.isolateId, + this.libraryUri, + this.scriptUri, + this.expression, + this.scope, + ); } class BatchedExpressionEvaluator extends ExpressionEvaluator { @@ -56,6 +63,7 @@ class BatchedExpressionEvaluator extends ExpressionEvaluator { Future evaluateExpression( String isolateId, String? libraryUri, + String? scriptUri, String expression, Map? scope, ) async { @@ -65,7 +73,8 @@ class BatchedExpressionEvaluator extends ExpressionEvaluator { 'Batched expression evaluator closed', ); } - final request = EvaluateRequest(isolateId, libraryUri, expression, scope); + final request = + EvaluateRequest(isolateId, libraryUri, scriptUri, expression, scope); _requestController.sink.add(request); return request.completer.future; } @@ -116,6 +125,7 @@ class BatchedExpressionEvaluator extends ExpressionEvaluator { .evaluateExpression( first.isolateId, first.libraryUri, + first.scriptUri, first.expression, first.scope, ) @@ -130,6 +140,7 @@ class BatchedExpressionEvaluator extends ExpressionEvaluator { final RemoteObject list = await super.evaluateExpression( first.isolateId, first.libraryUri, + first.scriptUri, batchedExpression, first.scope, ); diff --git a/dwds/lib/src/services/chrome_proxy_service.dart b/dwds/lib/src/services/chrome_proxy_service.dart index 913db0f5b..1d1e4bc24 100644 --- a/dwds/lib/src/services/chrome_proxy_service.dart +++ b/dwds/lib/src/services/chrome_proxy_service.dart @@ -624,6 +624,8 @@ ${globalToolConfiguration.loadStrategy.loadModuleSnippet}("dart_sdk").developer. () => evaluator.evaluateExpression( isolateId, library?.uri, + // TODO: Get correct script uri. + library?.uri, expression, scope, ), diff --git a/dwds/lib/src/services/expression_compiler.dart b/dwds/lib/src/services/expression_compiler.dart index 6135a707e..d62289acb 100644 --- a/dwds/lib/src/services/expression_compiler.dart +++ b/dwds/lib/src/services/expression_compiler.dart @@ -61,6 +61,7 @@ abstract class ExpressionCompiler { Future compileExpressionToJs( String isolateId, String libraryUri, + String scriptUri, int line, int column, Map jsModules, diff --git a/dwds/lib/src/services/expression_compiler_service.dart b/dwds/lib/src/services/expression_compiler_service.dart index 36a51f4d6..5b7f087e3 100644 --- a/dwds/lib/src/services/expression_compiler_service.dart +++ b/dwds/lib/src/services/expression_compiler_service.dart @@ -160,6 +160,7 @@ class _Compiler { Future compileExpressionToJs( String libraryUri, + String scriptUri, int line, int column, Map jsModules, @@ -256,6 +257,7 @@ class ExpressionCompilerService implements ExpressionCompiler { Future compileExpressionToJs( String isolateId, String libraryUri, + String scriptUri, int line, int column, Map jsModules, @@ -265,6 +267,7 @@ class ExpressionCompilerService implements ExpressionCompiler { ) async => (await _compiler.future).compileExpressionToJs( libraryUri, + scriptUri, line, column, jsModules, diff --git a/dwds/lib/src/services/expression_evaluator.dart b/dwds/lib/src/services/expression_evaluator.dart index 1aca08c68..47bd1ce84 100644 --- a/dwds/lib/src/services/expression_evaluator.dart +++ b/dwds/lib/src/services/expression_evaluator.dart @@ -84,10 +84,12 @@ class ExpressionEvaluator { /// /// [isolateId] current isolate ID. /// [libraryUri] dart library to evaluate the expression in. + /// [scriptUri] dart script or file to evaluate the expression in. /// [expression] dart expression to evaluate. Future evaluateExpression( String isolateId, String? libraryUri, + String? scriptUri, String expression, Map? scope, ) async { @@ -114,6 +116,13 @@ class ExpressionEvaluator { ); } + if (scriptUri == null) { + return createError( + EvaluationErrorKind.invalidInput, + 'no script uri', + ); + } + final module = await _modules.moduleForLibrary(libraryUri); if (module == null) { return createError( @@ -131,6 +140,7 @@ class ExpressionEvaluator { final compilationResult = await _compiler.compileExpressionToJs( isolateId, libraryUri.toString(), + libraryUri.toString(), 0, 0, {}, @@ -283,7 +293,11 @@ class ExpressionEvaluator { final dartLocation = locationMap.dartLocation; final dartSourcePath = dartLocation.uri.serverPath; + print("dartSourcePath = $dartSourcePath"); final libraryUri = await _modules.libraryForSource(dartSourcePath); + print("libraryUri = $libraryUri"); + final resolvedUri = await _modules.resolvedUriForSource(dartSourcePath); + print("resolvedUri = $resolvedUri"); if (libraryUri == null) { return createError( EvaluationErrorKind.internal, @@ -319,6 +333,7 @@ class ExpressionEvaluator { final compilationResult = await _compiler.compileExpressionToJs( isolateId, libraryUri.toString(), + (resolvedUri ?? libraryUri).toString(), dartLocation.line, dartLocation.column, {}, diff --git a/dwds/lib/src/utilities/dart_uri.dart b/dwds/lib/src/utilities/dart_uri.dart index f8aa66ab2..9f04ed4a7 100644 --- a/dwds/lib/src/utilities/dart_uri.dart +++ b/dwds/lib/src/utilities/dart_uri.dart @@ -132,6 +132,8 @@ class DartUri { /// file:///.pub-cache/hosted/pub.dev/path-1.8.0/lib/src/path_set.dart, static final Map _uriToResolvedUri = {}; + static Map get uriToResolvedUri => _uriToResolvedUri; + /// All of the known libraries, indexed by their absolute file URL. static final Map _resolvedUriToUri = {}; @@ -140,6 +142,8 @@ class DartUri { /// This map is empty if not a google3 app. static final Map _g3RelativeUriToResolvedUri = {}; + static Map get resolvedUriToUri => _resolvedUriToUri; + /// Returns package, app, or dart uri for a resolved path. static String? toPackageUri(String uri) { final packageUri = _resolvedUriToUri[uri]; diff --git a/dwds/test/expression_compiler_service_test.dart b/dwds/test/expression_compiler_service_test.dart index d791aebd6..4453e3990 100644 --- a/dwds/test/expression_compiler_service_test.dart +++ b/dwds/test/expression_compiler_service_test.dart @@ -199,6 +199,7 @@ void main() async { final compilationResult = await service.compileExpressionToJs( '0', 'org-dartlang-app:/try.dart', + 'org-dartlang-app:/try.dart', 2, 1, {}, @@ -247,6 +248,7 @@ void main() async { final compilationResult1 = await service.compileExpressionToJs( '0', 'org-dartlang-app:/try.dart', + 'org-dartlang-app:/try.dart', 2, 1, {}, @@ -257,6 +259,7 @@ void main() async { final compilationResult2 = await service.compileExpressionToJs( '0', 'org-dartlang-app:/try.dart', + 'org-dartlang-app:/try.dart', 2, 1, {}, @@ -312,6 +315,7 @@ void main() async { final compilationResult1 = service.compileExpressionToJs( '0', 'org-dartlang-app:/try.dart', + 'org-dartlang-app:/try.dart', 2, 1, {}, @@ -322,6 +326,7 @@ void main() async { final compilationResult2 = service.compileExpressionToJs( '0', 'org-dartlang-app:/try.dart', + 'org-dartlang-app:/try.dart', 2, 1, {}, diff --git a/dwds/test/expression_evaluator_test.dart b/dwds/test/expression_evaluator_test.dart index a4a336d0a..7cc7cf544 100644 --- a/dwds/test/expression_evaluator_test.dart +++ b/dwds/test/expression_evaluator_test.dart @@ -96,8 +96,8 @@ void main() async { }); test('can evaluate expression', () async { - final result = - await evaluator.evaluateExpression('1', 'main.dart', 'true', {}); + final result = await evaluator + .evaluateExpression('1', 'main.dart', 'main.dart', 'true', {}); expect( result, const TypeMatcher() @@ -162,8 +162,8 @@ void main() async { test('returns error if closed', () async { evaluator.close(); - final result = - await evaluator.evaluateExpression('1', 'main.dart', 'true', {}); + final result = await evaluator + .evaluateExpression('1', 'main.dart', 'main.dart', 'true', {}); expect( result, const TypeMatcher() @@ -181,8 +181,8 @@ void main() async { }); test('can evaluate expression', () async { - final result = - await evaluator.evaluateExpression('1', 'main.dart', 'true', {}); + final result = await evaluator + .evaluateExpression('1', 'main.dart', 'main.dart', 'true', {}); expect( result, const TypeMatcher() @@ -192,8 +192,8 @@ void main() async { test('returns error if closed', () async { evaluator.close(); - final result = - await evaluator.evaluateExpression('1', 'main.dart', 'true', {}); + final result = await evaluator + .evaluateExpression('1', 'main.dart', 'main.dart', 'true', {}); expect( result, const TypeMatcher() diff --git a/dwds/test/fixtures/fakes.dart b/dwds/test/fixtures/fakes.dart index a24381d7a..486a42f2b 100644 --- a/dwds/test/fixtures/fakes.dart +++ b/dwds/test/fixtures/fakes.dart @@ -161,6 +161,10 @@ class FakeModules implements Modules { @override Future libraryForSource(String serverPath) async => Uri(path: _library); + @override + Future resolvedUriForSource(String serverPath) async => + Uri(path: _library); + @override Future moduleForSource(String serverPath) async => _module; @@ -438,6 +442,7 @@ class FakeExpressionCompiler implements ExpressionCompiler { Future compileExpressionToJs( String isolateId, String libraryUri, + String scriptUri, int line, int column, Map jsModules, diff --git a/frontend_server_common/lib/src/frontend_server_client.dart b/frontend_server_common/lib/src/frontend_server_client.dart index aba399997..a6f74959a 100644 --- a/frontend_server_common/lib/src/frontend_server_client.dart +++ b/frontend_server_common/lib/src/frontend_server_client.dart @@ -213,6 +213,7 @@ class _CompileExpressionToJsRequest extends _CompilationRequest { _CompileExpressionToJsRequest( Completer completer, this.libraryUri, + this.scriptUri, this.line, this.column, this.jsModules, @@ -222,6 +223,7 @@ class _CompileExpressionToJsRequest extends _CompilationRequest { : super(completer); String libraryUri; + String scriptUri; int line; int column; Map jsModules; @@ -478,6 +480,7 @@ class ResidentCompiler { /// Compiles dart expression to JavaScript. Future compileExpressionToJs( String libraryUri, + String scriptUri, int line, int column, Map jsModules, @@ -489,8 +492,16 @@ class ResidentCompiler { } var completer = Completer(); - _controller.add(_CompileExpressionToJsRequest(completer, libraryUri, line, - column, jsModules, jsFrameValues, moduleName, expression)); + _controller.add(_CompileExpressionToJsRequest( + completer, + libraryUri, + scriptUri, + line, + column, + jsModules, + jsFrameValues, + moduleName, + expression)); return completer.future; } @@ -509,6 +520,7 @@ class ResidentCompiler { var inputKey = Uuid().generateV4(); server.stdin.writeln('compile-expression-to-js $inputKey'); server.stdin.writeln(request.libraryUri); + server.stdin.writeln(request.scriptUri); server.stdin.writeln(request.line); server.stdin.writeln(request.column); request.jsModules.forEach((k, v) { @@ -611,14 +623,22 @@ class TestExpressionCompiler implements ExpressionCompiler { Future compileExpressionToJs( String isolateId, String libraryUri, + String scriptUri, int line, int column, Map jsModules, Map jsFrameValues, String moduleName, String expression) async { - var compilerOutput = await _generator.compileExpressionToJs(libraryUri, - line, column, jsModules, jsFrameValues, moduleName, expression); + var compilerOutput = await _generator.compileExpressionToJs( + libraryUri, + scriptUri, + line, + column, + jsModules, + jsFrameValues, + moduleName, + expression); if (compilerOutput != null) { var content = utf8.decode(localFileSystem