[wasm] CI: Fix firefox debugger tests failure (#86476)
authorAnkit Jain <radical@gmail.com>
Fri, 19 May 2023 20:36:54 +0000 (16:36 -0400)
committerGitHub <noreply@github.com>
Fri, 19 May 2023 20:36:54 +0000 (16:36 -0400)
Tests are failing with:

```
  Failed DebuggerTests.ArrayTests.InspectGenericValueTypeArrayLocals(line: 89, col: 8, method_name: "DebuggerTests.ArrayTestsClass.GenericValueTypeLoca"..., test_prev_frame: False, frame_idx: 0, use_cfo: True) [>
  Error Message:
   System.InvalidCastException : Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken.
  Stack Trace:
     at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)
   at DebuggerTests.FirefoxInspectorClient.HandleMessage(String msg, CancellationToken token) in /_/src/mono/wasm/debugger/DebuggerTestSuite/FirefoxInspectorClient.cs:line 184
   at Microsoft.WebAssembly.Diagnostics.RunLoop.RunActualAsync(CancellationTokenSource x) in /_/src/mono/wasm/debugger/BrowserDebugProxy/Common/RunLoop.cs:line 142
   at Microsoft.WebAssembly.Diagnostics.RunLoop.<>c__DisplayClass20_0.<<RunAsync>b__0>d.MoveNext() in /_/src/mono/wasm/debugger/BrowserDebugProxy/Common/RunLoop.cs:line 54
--- End of stack trace from previous location ---
   at System.Threading.Channels.AsyncOperation`1.GetResult(Int16 token)
   at System.Threading.Tasks.ValueTask`1.ValueTaskSourceAsTask.<>c.<.cctor>b__4_0(Object state)
```

One of the arguments of the message is a `JObject`, and thus fails with
`.Value<string>()`.

The full message:
```json
{
   "from" : "server1.conn0.windowGlobal10737418242/windowGlobalTarget2",
   "resources" : [
      {
         "message" : {
            "arguments" : [
               "mono_wasm_debug_event_raised:aef14bca-5519-4dfe-b35a-f867abc123ae",
               {
                  "actor" : "server1.conn0.windowGlobal10737418242/longstractor35",
                  "initial" : "{\"eventName\":\"AssemblyLoaded\",\"assembly_name\":\"System.Private.CoreLib.dll\",\"assembly_b64\":\"V2JJTAAAAAADAAAACCAAAEgAAACYYj4AVAAAAPRDPgAAIAAAAEQ+AEwAAABMBAAAAIA+AAAGAABMRD4ADAAAAACgPgAAAgAATEo+ANBjPgAAAAAASAAAAAIABQDQ4RkAyIAkAAkAAAAAAAAAsFMUADjSAwBQ4RkAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHgIoJAIABipeAigkAgAGAheNogAAAiUWA5x9AQAABCo6AigkAgAGAgN9AQAABCo6AigkAgAGAgN9AgAABCo6AigkAgAGAgN9AwAABCoeAigDMAUAjQAAAAEAABEgAAIAAOD+DyAAAQAAcwEAAAoMCAoCBgMEBW8CAAAKCwctIg4EEgAWBhYoAQAAKygDAAAKDBIC/hYBAAAbb3cDAAZRFyoHGDM+IAAFAACNpAAAAigEAAAKCgIGAwQFbwIAAAotIg4EEgAWBhYoAQAAKygDAAAKDBIC/hYBAAAbb3cDAAZRFyoOBBRRFiomAgMEKA8AAAZ6RgIsDQIo6gAABgMEKAkAAAYqTgIWai8MKFcAAAYDBCgJAAAGAio2KFcAAAYUFigJAAAGKjICagMEKAsAAAYmAioDMAIAsgEAAAIAABEPAChSAAAGCwcgFAABADBJByAGAAEAMBsHIAIAAQA7zQAAAAcgBgABADsUAQAAOHcBAAAHIAgAAQA7sgAAAAcgCwABADsrAQAAByAUAAEAOzYBAAA4UQEAAAcgJQABADAbByAWAAEAOw4BAAAHICUAAQA7pwAAADguAQAAByAtAAEALhUHIDkAAQAuQAcgQgABAC5cOBEBAAAELTADLAgDKBMAAAYsJQMozwUABiwLKLsdAAZzUmwABioovB0ABgMo0RoABgNzVGwABioDK",
                  "length" : 7737462,
                  "type" : "longString"
               },
               "{}"
            ],
            "columnNumber" : 12734,
            "filename" : "http://localhost:9400/dotnet.runtime.js",
            "innerWindowID" : 10737418242,
            "level" : "debug",
            "lineNumber" : 3,
            "private" : true,
            "sourceId" : null,
            "timeStamp" : 1684452298362.82
         },
         "resourceType" : "console-message"
      }
   ],
   "type" : "resource-available-form"
}
```

src/mono/wasm/debugger/DebuggerTestSuite/FirefoxInspectorClient.cs

index 931c4f4..6ea8486 100644 (file)
@@ -181,7 +181,34 @@ class FirefoxInspectorClient : InspectorClient
                         // FIXME: unnecessary alloc
                         foreach (JToken? argument in res["resources"]?[0]?["message"]?["arguments"]?.Value<JArray>() ?? new JArray())
                         {
-                            args.Add(JObject.FromObject(new { value = argument.Value<string>()}));
+                            if (argument is null)
+                                continue;
+
+                            string? strValue = null;
+                            if (argument is JObject argObj && argObj["type"]?.Value<string>() == "longString" && argObj["initial"] is JToken argInitialToken)
+                            {
+                                /*
+                                    "arguments" : [
+                                       "mono_wasm_debug_event_raised:aef14bca-5519-4dfe-b35a-f867abc123ae",
+                                       {
+                                          "actor" : "server1.conn0.windowGlobal10737418242/longstractor35",
+                                          "initial" : "{\"eventName .... ",
+                                          "length" : 7737462,
+                                          "type" : "longString"
+                                       },
+                                       "{}"
+                                    ]
+                                */
+                                strValue = argInitialToken.Value<string>();
+                            }
+                            else if (argument.Type is JTokenType.String)
+                            {
+                                strValue = argument.Value<string>();
+                            }
+
+                            // fallback
+                            strValue ??= argument.ToString();
+                            args.Add(JObject.FromObject(new { value = strValue }));
                         }
                         res = JObject.FromObject(new
                             {