dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.86k stars 671 forks source link

Use applicationUrl from launchSettings.json when launching browser, like VS #2868

Open alexvy86 opened 5 years ago

alexvy86 commented 5 years ago

Issue Description

I'm using Kestrel for local development. During a debugging session, the URL that's being passed to the browser (with ${auto-detect-url} in launch.json) is always based on one of the endpoints configured for Kestrel, and is always using localhost as the hostname. E.g. if my Kestrel configuration looks like this:

{
    "Kestrel": {
        "EndPoints": {
            "Https": {
                "Url": "https://my.domain.local:4430"
            }
        },
        "Certificates": {
            "Default": {
                "Subject": "*.domain.local",
                "Store": "Root",
                "Location": "LocalMachine",
                "AllowInvalid": true
            }
        }
    }
}

The URL that's passed to the browser seems to be the one from the Kestrel endpoint, with my.domain.local replaced with localhost. I believe that's how Kestrel ends up using them for its purposes (it only cares about the port it needs to bind to), but for debugging purposes, I actually want to browse to https://my.domain.local:4430 instead of https://localhost:4430, since my SSL cert is for the former. I know there's a dev cert for localhost, but in my use case the hostname matters to the application, so I can't use a URL with localhost in it.

Now, my launchSettings.json file looks like this:

{
  "profiles": {
    "MyProject": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "https://my.domain.local:4430"
    }
  }
}

Visual Studio uses the value of applicationUrl when it starts the browser after I hit F5, but VS Code doesn't, and I think it'd be good to match the behavior. Furthermore, it seems like the only option to launch the browser with a non-localhost URL.

Steps to Reproduce

In any project, set applicationUrl in launchSettings.json to a non-localhost URL, and start a debugging session in VS Code.

Expected Behavior

The browser navigates to the URL specified in applicationUrl.

Actual Behavior

The browser navigates to a URL that matches one of the endpoints configured for Kestrel, always using localhost instead of whatever URL was written in the Kestrel endpoint configuration.

Logs

A bit of relevant log from the debugging session:

2019-02-21T00:22:29.209|DEBUG||Loaded hosting startup assembly MyProject |Microsoft.AspNetCore.Hosting.Internal.WebHost|assemblyName=MyProject

Hosting environment: Development

Content root path: F:\path\to\my\project\MyProject

Launching browser (cmd.exe /C start https://localhost:4430)

Now listening on: https://[::]:4430

Application started. Press Ctrl+C to shut down.

OmniSharp log

Starting OmniSharp server at 2/21/2019, 12:38:54 AM
    Target: f:\path\to\my\project\MySolution.sln

OmniSharp server started.
    Path: C:\Users\alexv\.vscode\extensions\ms-vscode.csharp-1.17.1\.omnisharp\1.32.8\OmniSharp.exe
    PID: 11204

[info]: OmniSharp.Stdio.Host
        Starting OmniSharp on Windows 6.2.9200.0 (x64)
[info]: OmniSharp.Services.DotNetCliService
        DotNetPath set to dotnet
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 3 MSBuild instance(s)
            1: Visual Studio Professional 2017 15.9.28307.344 - "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin"
            2: Visual Studio Enterprise 2019 16.0.28602.52 - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin"
            3: StandAlone 15.0 - "C:\Users\alexv\.vscode\extensions\ms-vscode.csharp-1.17.1\.omnisharp\1.32.8\msbuild\15.0\Bin"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: Visual Studio Professional 2017 15.9.28307.344 - "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin"
[info]: OmniSharp.Cake.CakeProjectSystem
        Detecting Cake files in 'f:\path\to\my\project'.
[info]: OmniSharp.Cake.CakeProjectSystem
        Could not find any Cake files
[info]: OmniSharp.WorkspaceInitializer
        Project system 'OmniSharp.DotNet.DotNetProjectSystem' is disabled in the configuration.
[info]: OmniSharp.MSBuild.ProjectSystem
        Detecting projects in 'f:\path\to\my\project\MySolution.sln'.
[info]: OmniSharp.MSBuild.ProjectManager
        Queue project update for 'f:\path\to\my\project\MyProject\MyProject.csproj'
[info]: OmniSharp.Script.ScriptProjectSystem
        Detecting CSX files in 'f:\path\to\my\project'.
[info]: OmniSharp.Script.ScriptProjectSystem
        Could not find any CSX files
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpWorkspaceOptionsProvider
[info]: OmniSharp.MSBuild.ProjectManager
        Loading project: f:\path\to\my\project\MyProject\MyProject.csproj
[info]: OmniSharp.WorkspaceInitializer
        Configuration finished.
[info]: OmniSharp.Stdio.Host
        Omnisharp server running using Stdio at location 'f:\path\to\my\project' on host 17172.
[info]: OmniSharp.MSBuild.ProjectManager
        Successfully loaded project file 'f:\path\to\my\project\MyProject\MyProject.csproj'.
[info]: OmniSharp.MSBuild.ProjectManager
        Adding project 'f:\path\to\my\project\MyProject\MyProject.csproj'
[info]: OmniSharp.MSBuild.ProjectManager
        Update project: MyProject

C# log

Empty

Environment information

VSCode version: 1.31.1 C# Extension: 1.17.1

Dotnet Information .NET Core SDK (reflecting any global.json): Version: 2.2.101 Commit: 236713b0b7 Runtime Environment: OS Name: Windows OS Version: 10.0.17763 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\2.2.101\ Host (useful for support): Version: 2.2.0 Commit: 1249f08fed .NET Core SDKs installed: 2.1.202 [C:\Program Files\dotnet\sdk] 2.1.402 [C:\Program Files\dotnet\sdk] 2.1.403 [C:\Program Files\dotnet\sdk] 2.1.500 [C:\Program Files\dotnet\sdk] 2.1.502 [C:\Program Files\dotnet\sdk] 2.1.503 [C:\Program Files\dotnet\sdk] 2.1.600-preview-009426 [C:\Program Files\dotnet\sdk] 2.1.600-preview-009472 [C:\Program Files\dotnet\sdk] 2.2.101 [C:\Program Files\dotnet\sdk] .NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download
Visual Studio Code Extensions |Extension|Author|Version| |---|---|---| |application-insights|VisualStudioOnlineApplicationInsights|0.4.2| |azure-account|ms-vscode|0.8.0| |azurecli|ms-vscode|0.4.2| |azurerm-vscode-tools|msazurermtools|0.4.2| |bower|donjayamanne|0.0.3| |csharp|ms-vscode|1.17.1| |debugger-for-chrome|msjsdiag|4.11.2| |docker-explorer|formulahendry|0.1.6| |dotenv|mikestead|1.0.1| |EditorConfig|EditorConfig|0.13.0| |githistory|donjayamanne|0.4.6| |gitignore|codezombiech|0.6.0| |gitlens|eamodio|9.5.1| |LiveServer|ritwickdey|5.5.1| |material-icon-theme|PKief|3.6.3| |mssql|ms-mssql|1.4.0| |PowerShell|ms-vscode|1.11.0| |python|ms-python|2019.1.0| |rest-client|humao|0.21.1| |theme-dracula|dracula-theme|2.16.0| |vscode-azureappservice|ms-azuretools|0.12.0| |vscode-azurefunctions|ms-azuretools|0.14.0| |vscode-docker|PeterJausovec|0.5.2| |vscode-eslint|dbaeumer|1.8.0| |vscode-markdownlint|DavidAnson|0.24.0| |vscode-nginx|shanoor|0.6.0| |vscode-proto3|zxh404|0.2.2| |vscode-pull-request-github|GitHub|0.4.0| |vscode-yaml|redhat|0.3.0| |vsliveshare|ms-vsliveshare|0.3.1225| |xml|DotJoshJohnson|2.4.0|;
gregg-miskelly commented 5 years ago

I am not sure why our server URL detection code replaces the host name with 'localhost'. But it sounds fixable.

In the mean time, in case you are not aware, you can configure how we start the browser (or remove it all together and launch the browser manually).

alexvy86 commented 5 years ago

Yes, for now my workaround was to set the URL manually in launch.json.

I'm curious, how is auto-detect-url determined? I read in the discussion for another issue that you wait for a particular message in the application's stdout to know when the web server is running, so at first I thought you simply grabbed the URL where the web server says it is listening, but as you can see in the logs above, that one (https://[::]:4430) doesn't exactly match the one that the browser gets (https://localhost:4430).

In any case, I'm pretty sure it's the kestrel endpoint URL that is being used as a base and not applicationUrl because only when i change the port of the kestrel endpoint, do I se the corresponding change when launching the browser. applicationUrl seems to be left unused... But I imagine that's the value you'd want to use, to match the VS experience?

karl-roberts commented 4 years ago

@alexvy86 Can you provide more detail on how you set the URL manually in launch.json? I'm experiencing the same issue here

alexvy86 commented 4 years ago

@karl-roberts I've been away from the project that triggered this so I might be misremembering something, and maybe you already figured it out, but I think this is what you'd need, specifically the launchBrowser.args part (or the OS-specific variants).

{
   "version": "0.2.0",
   "configurations": [
        {
            "name": ".NET Core Launch (web)",
            "type": "coreclr",
            "request": "launch",
            "program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/my-app.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart",
            "launchBrowser": {
                "enabled": true,
                "args": "https://url-you-want-to-launch",
                "windows": {
                    "command": "cmd.exe",
                    "args": "/C start https://url-you-want-to-launch"
                },
                "osx": {
                    "command": "open"
                },
                "linux": {
                    "command": "xdg-open"
                }
            },
        }
    ,]
}