Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[blazor][wasm][debug]Press alt-shift-d and open firefox debug tab attached to the blazor app #46132

Merged
merged 10 commits into from
Jan 23, 2023

Conversation

thaystg
Copy link
Member

@thaystg thaystg commented Jan 17, 2023

Error if firefox is not opened with remote debugging enabled on port 6000:
image

Error if firefox is opened but about:debugging tab is not opened:
image

Debugging working:

HostedBlazorWebassemblyApp.Mozilla.Firefox.2023-01-17.18-38-33.mp4

The goal of this PR is give the customer a similar experience on firefox that it has when debugging on chrome/edge.

We cannot open automatically the about:debugging tab as we do in chrome/edge for security reasons.
A message is shown on app asking to open about:debugging tab.
Then we connect remotely to this tab and use these functions to configure, connect and open a new tab to debug the blazor app:

  • AboutDebugging.actions.addNetworkLocation
  • AboutDebugging.actions.connectRuntime
  • AboutDebugging.actions.selectPage
  • AboutDebugging.actions.inspectDebugTarget

I didn't find a way to call await functions using evaluateJSAsync from firefox debug protocol, that is why I need some if's before invoke the functions.

Related to dotnet/runtime#80722

Fixes dotnet/runtime#80043

@thaystg thaystg requested a review from a team as a code owner January 17, 2023 03:39
@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Jan 17, 2023
@thaystg thaystg requested review from lewing and radical January 17, 2023 20:52
Copy link
Member

@mkArtakMSFT mkArtakMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @thaystg.
I see that this was not a trivial code due to being forced to read the content in the browser to determine what needs to happen and when.
While this is an ok approach, it'll be relatively high maintenance, as these strings can change over time. Also, I wonder how this will work for non-english culture OS-es? Are these strings still in english ?

link.target = '_blank';
link.rel = 'noopener noreferrer';
link.click();
if (isFirefox) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a lot of Firefox-specific code. Maybe it's worth to move it into a helper function so it's easier to read the logic here:

if (isFirefox)
  launchFirefoxDebugger();
else{
 ...
}

capturedUrl = matchFirefox.Groups["url"].Value;
taskCompletionSource.TrySetResult(capturedUrl);
}
if (isFirefox)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the condition in the if block above has evaluated to true isn't that already an indication that isFirefox is true? is there possibility for the check above to evaluate to true but isFirefox to be false ?

I'm just trying to see if it's possible to combine these two if blocks together.

#pragma warning disable CA1835
static async Task<string> ReceiveMessageLoop(TcpClient browserDebugClientConnect, CancellationToken token)
{
NetworkStream toStream = browserDebugClientConnect.GetStream();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't a using be added to this so the toStream gets disposed after it's not needed any more?

@thaystg
Copy link
Member Author

thaystg commented Jan 19, 2023

Thanks @thaystg. I see that this was not a trivial code due to being forced to read the content in the browser to determine what needs to happen and when. While this is an ok approach, it'll be relatively high maintenance, as these strings can change over time. Also, I wonder how this will work for non-english culture OS-es? Are these strings still in english ?

Which strings?
Like listTabs? watchTargets? watchResources? They are from the firefox remote debugging protocol.
https://firefox-source-docs.mozilla.org/devtools/backend/protocol.html#remote-debugging-protocol

Copy link
Member

@mkArtakMSFT mkArtakMSFT left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @thaystg !

warningToDebug.style.display = 'none';
}, 600);
};
warningToDebug.appendChild(closeWarningButton);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite a lot of code to include if this is only for displaying an error about debugging. Since we have to include all this code even in production bundles, would it be viable to shrink it? For example we don't need an error to be nicely styled. I think it would be enough even just to use alert(someMessage). Or if it's unlikely this ever occurs, we could just console.error(someMessage).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite a lot of code to include if this is only for displaying an error about debugging. Since we have to include all this code even in production bundles, would it be viable to shrink it? For example we don't need an error to be nicely styled. I think it would be enough even just to use alert(someMessage). Or if it's unlikely this ever occurs, we could just console.error(someMessage).

This will probably happen usually, but if you think a console.warning is better for sure I can change it :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would ideally prefer to start with something minimal like output to console (or alert if it's desirable to be visible in the main UI).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

@@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<Reference Include="Newtonsoft.Json" Version="13.0.2" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we avoid adding this dependency? Or if there's some reason why System.Text.Json is insufficient, could you clarify what it is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same used on BrowserDebugProxy, if you prefer I can try to use System.Text.Json.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see, so it won't change the dependencies as far as customers are concerned.

However, having the direct dependency here does mean more dependabot-type noise and TBH it's just really surprising to see Json.NET code show up when we've consistently used STJ elsewhere in this repo. If it's not too disruptive to switch over to STJ for this that would be helpful! If that's impractical or a lot of work let me know.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed :)

else if (!string.IsNullOrEmpty(toCmd))
{
await SendMessageToBrowser(toStream, JObject.FromObject(new { type = "getWatcher", isServerTargetSwitchingEnabled = true, to = toCmd }), token);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this definitely the right way to factor the code? Since debugging-WebAssembly-on-Firefox isn't Blazor-specific (it should ideally be possible even if you're not using Blazor at all), wouldn't it be more applicable to have this code in the runtime library that contains the debug proxy itself?

Apologies if I'm misunderstanding the layering here!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also alt-shift-d for chrome is not Blazor specific, so this code should also be moved.
I added here to be near to the code that does the same for chrome.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a way to get all the tabs available for debugging using a simple request as we do for chrome. We need to use the firefox remote debugging protocol to do it.

Copy link
Member

@SteveSandersonMS SteveSandersonMS Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, up to you to decide the best place for it. If - longer term - we can factor out the non-Blazor-specific parts into a runtime package (presumably BrowserDebugProxy) that sounds ideal. Would it be worth adding an issue to track that task?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@SteveSandersonMS SteveSandersonMS left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates!

@thaystg thaystg merged commit 1a1ab56 into dotnet:main Jan 23, 2023
@ghost ghost added this to the 8.0-preview1 milestone Jan 23, 2023
atossell91 pushed a commit to atossell91/aspnetcore that referenced this pull request Jan 23, 2023
…ached to the blazor app (dotnet#46132)

* Press alt-shift-d and open firefox debug tab attached to the blazor app

* remove debugger.launch.

* removing unrelated changes

* Removing unnecessary changes on chrome debugging.

* addressing @mkArtakMSFT comments

* Addressing @mkArtakMSFT comments.

* Addressing Steve comments, adding a console.warning message and remove the beautiful message, removed the Newtonsoft from the send message, todo: remove the Newtonsoft from receive message.

* Completely removing newtonsoft usage as asked by steve.

* Change warning message.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Blazor Debugging on Firefox
3 participants