[ddc] Fix assert location in expression eval

When assertions appear in a debugger expression they now have a
synthetic source location available. This also allows for the lookup
of the actual source.

Issue: https://github.com/dart-lang/sdk/issues/43986
Fixes: https://github.com/dart-lang/sdk/issues/54956
Change-Id: I34ae5f810593b40282929d02cb290de86603922f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/356287
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
diff --git a/pkg/dev_compiler/lib/src/kernel/compiler.dart b/pkg/dev_compiler/lib/src/kernel/compiler.dart
index da50e2c..0ee2b1d 100644
--- a/pkg/dev_compiler/lib/src/kernel/compiler.dart
+++ b/pkg/dev_compiler/lib/src/kernel/compiler.dart
@@ -4075,23 +4075,23 @@
 
     SourceLocation? location;
     late String conditionSource;
-    if (node.location != null) {
-      var encodedSource =
-          node.enclosingComponent!.uriToSource[node.location!.file]!.source;
+    var assertLocation = node.location;
+    if (assertLocation != null) {
+      var fileUri = assertLocation.file;
+      var encodedSource = node.enclosingComponent!.uriToSource[fileUri]!.source;
       var source = utf8.decode(encodedSource, allowMalformed: true);
-
       conditionSource =
           source.substring(node.conditionStartOffset, node.conditionEndOffset);
+      // Assertions that appear in debugger expressions have a synthetic Uri
+      // that is different than the current library where the expression will
+      // be evaluated.
+      var savedUri = _currentUri;
+      _currentUri = fileUri;
       location = _toSourceLocation(node.conditionStartOffset)!;
+      _currentUri = savedUri;
     } else {
-      // Location is null in expression compilation when modules
-      // are loaded from kernel using expression compiler worker.
-      // Show the error only in that case, with the condition AST
-      // instead of the source.
-      //
-      // TODO(annagrin): Can we add some information to the kernel,
-      // or add better printing for the condition?
-      // Issue: https://github.com/dart-lang/sdk/issues/43986
+      // If the location is ever null, only show the error with the condition
+      // AST instead of the source.
       conditionSource = node.condition.toString();
     }
     return js.statement(' if (!#) #;', [
diff --git a/pkg/dev_compiler/test/expression_compiler/assertions_enabled_test.dart b/pkg/dev_compiler/test/expression_compiler/assertions_enabled_test.dart
index 911c02e..73b5ed7 100644
--- a/pkg/dev_compiler/test/expression_compiler/assertions_enabled_test.dart
+++ b/pkg/dev_compiler/test/expression_compiler/assertions_enabled_test.dart
@@ -58,8 +58,8 @@
               expression: '() { assert(false); return 0; } ()',
               expectedError: allOf(
                 contains('Error: Assertion failed:'),
-                contains('<unknown source>:-1:-1'),
-                contains('BoolLiteral(false)'),
+                contains('org-dartlang-debug:synthetic_debug_expression:1:13'),
+                contains('false'),
                 contains('is not true'),
               ));
         });