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

dotnet watch doesn't launch browser correctly if the listening URL has a trailing slash #36709

Closed
eerhardt opened this issue Nov 7, 2023 · 4 comments
Labels
Area-Watch untriaged Request triage from a team member
Milestone

Comments

@eerhardt
Copy link
Member

eerhardt commented Nov 7, 2023

Describe the bug

When using dotnet watch, it parses the console output for Now listening on: <URL>. See:

private static readonly Regex NowListeningRegex = new(@"Now listening on: (?<url>.*)\s*$", RegexOptions.None | RegexOptions.Compiled, TimeSpan.FromSeconds(10));

var match = NowListeningRegex.Match(eventArgs.Data);
if (match.Success)
{
var launchUrl = match.Groups["url"].Value;
var process = (Process)sender;
process.OutputDataReceived -= OnOutput;
if (!_attemptedBrowserLaunch)
{
_attemptedBrowserLaunch = true;
_reporter.Verbose("Launching browser.");
try
{
LaunchBrowser(launchUrl);

When this URL has a trailing slash like Now listening on: http://localhost:15888/, the browser launches, but the page is broken:

image

image

The code is always appending a /, which is causing the issue:

private void LaunchBrowser(string launchUrl)
{
Debug.Assert(_reporter != null);
var fileName = Uri.TryCreate(_launchPath, UriKind.Absolute, out _) ? _launchPath : launchUrl + "/" + _launchPath;

To Reproduce

Have a web app that writes Now listening on: http://localhost:15888/ to the console. dotnet watch it and notice the browser launches, but doesn't hit the endpoint correctly.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Request triage from a team member label Nov 7, 2023
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

eerhardt added a commit to dotnet/aspire that referenced this issue Nov 7, 2023
eerhardt added a commit to dotnet/aspire that referenced this issue Nov 7, 2023
* Allow dotnet watch to work with Aspire Dashboard

dotnet watch needs the URL message to be of a specific format. Fixing our message to conform.

Fix #711

* Trim trailing slash to allow dotnet watch to work.

Workaround dotnet/sdk#36709
eerhardt added a commit to dotnet/aspire that referenced this issue Nov 7, 2023
* Allow dotnet watch to work with Aspire Dashboard

dotnet watch needs the URL message to be of a specific format. Fixing our message to conform.

Fix #711

* Trim trailing slash to allow dotnet watch to work.

Workaround dotnet/sdk#36709
danmoseley pushed a commit to dotnet/aspire that referenced this issue Nov 7, 2023
* Allow dotnet watch to work with Aspire Dashboard

dotnet watch needs the URL message to be of a specific format. Fixing our message to conform.

Fix #711

* Trim trailing slash to allow dotnet watch to work.

Workaround dotnet/sdk#36709
@ShreyasJejurkar
Copy link

ShreyasJejurkar commented Dec 24, 2023

So I just tried to repro this on .NET SDK 8.0.100, but it did not reproduce, following are the steps I did.

  1. Create a new web app with dotnet new web.
  2. Then I went to Properties/launchsettings.json folder and edited it just to have the below content.
{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:2691",
      "sslPort": 44306
    }
  },
  "profiles": {
    "http": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5039/", // <-- edited this manually, to add trailing slash for repro
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
  1. And then I ran dotnet watch from the project folder and indeed it launched the browser with the correct URL http://localhost:5039/
    image

  2. And on the console, I do see the URL without trailing slash
    image

Please let me know if I am missing anywhere as part of repro steps.

@eerhardt
Copy link
Member Author

eerhardt commented Jan 2, 2024

Here's a simple repro:

  1. dotnet new web
  2. Change the Program.cs file to be:
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();

var app = builder.Build();
app.Lifetime.ApplicationStarted.Register(() => Console.WriteLine("Now listening on: http://localhost:7126/"));

app.MapGet("/", () => "Hello World!");

app.Run();

(you can change the port number to whatever number you have in your launchSettings.json).

  1. dotnet watch

You'll see

image

And when the browser launches, it will have 2 trailing slashes, like in the picture in the original post.

@tmat
Copy link
Member

tmat commented Jan 29, 2025

Fixed by #46100

@tmat tmat closed this as completed Jan 29, 2025
@tmat tmat added this to the 9.0.2xx milestone Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Watch untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

4 participants