vscode-csharp icon indicating copy to clipboard operation
vscode-csharp copied to clipboard

launch.json does not pickup a seondary launchConfigurationId from Properties/launchSettings.JSON

Open patoJamriska opened this issue 1 year ago • 19 comments

Trying to debug an application using different environment variables for the use use of appSettings.env.json i.e.:

"environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
}

does not pick up a secondary profile. So I can have a Development env and a UAT env but it only picks up the first one when I hit debug. Same thing happens on Compounds:

launch.json sample:

{
"version": "0.2.0",
    "configurations": [
      {
        "name": "MyAppDev",
        "type": "dotnet",
        "request": "launch",
        "projectPath": "${workspaceFolder}/MyApp/MyApp.csproj",
        "launchConfigurationId": "TargetFramework=;MyAppDev"
      },
      {
        "name": "MyAppUAT",
        "type": "dotnet",
        "request": "launch",
        "projectPath": "${workspaceFolder}/MyApp/MyApp.csproj",
        "launchConfigurationId": "TargetFramework=;MyAppUAT"
      },
]
}

LaunchSettings.Json sample:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "profiles": {
    "MyAppDev": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "clients",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
      },
      "applicationUrl": "https://localhost:44334"
    }
    "MyAppUAT": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "clients",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "UAT",
      },
      "applicationUrl": "https://localhost:44334"
    }
  }
}

If the CSharp: Report an issue command doesn't appear, make sure that you have C# extension version 1.17.0 or newer installed.

Environment data

VS Code version: 1.93.1 (Universal) C# Extension version: current latest and Pre-release versions

Steps to reproduce

  1. go to Debug
  2. select profile 1, run, it loads c# app on development.
  3. select profile 2, run, it loads c# app still on development instead of UAT

Expected behavior

the UI will pick the correct profile to launch

Additional context

This was working on a few versions ago, but I didnt had to use vscode on this project for a while until recently that realized it was not picking up my other environments.

patoJamriska avatar Sep 20 '24 20:09 patoJamriska

@patoJamriska Do you have C# Dev Kit installed?

launchConfigurationId was not intended to really be saved in a launch.json but was supposed to be used by the dynamic configuration provider which is used internally by the resolver in C# Dev Kit.

The dotnet debug type only supports using projectPath if you are using launch.json

WardenGnaw avatar Sep 20 '24 22:09 WardenGnaw

@WardenGnaw I had a similar issue to @patoJamriska because the documentation isn't very clear on this. Is launchSettingsProfile property on a launch configuration in .vscode/launch.json the expected way to provide which launchSettings profile to use?

When I tried (this was ~2mo back at this point) it just ignored everything and would always pick the first profile in launchSettings.json anyways. This was true of manually configured launch profiles or the dynamic configuration.

I also found applicationUrls of the selected profile is ignored; workaround was setting ASPNETCORE_URLS as an env variable.

Anyways about the documentation

  • It should make clear in which file launchSettingsProfile should be configured
  • It would be helpful if the "launchSettingsProfile": "ProfileNameGoesHere" example showed a few lines above/below.
  • If launchSettingsProfile is a valid option for launch.json, it should appear in the launch profile schema/autocomplete suggestions (today it does not)
  • If launchConfigurationId is internal, it should be removed from autocomplete suggestions and documented as such so users don't try to manually configure what the VSCode generates for them and have nothing happen as a result.

stewartadam avatar Sep 23 '24 23:09 stewartadam

@patoJamriska Do you have C# Dev Kit installed?

launchConfigurationId was not intended to really be saved in a launch.json but was supposed to be used by the dynamic configuration provider which is used internally by the resolver in C# Dev Kit.

The dotnet debug type only supports using projectPath if you are using launch.json

Yes, I am using the C# developer extension

patoJamriska avatar Sep 24 '24 18:09 patoJamriska

@WardenGnaw I had a similar issue to @patoJamriska because the documentation isn't very clear on this. Is launchSettingsProfile property on a launch configuration in .vscode/launch.json the expected way to provide which launchSettings profile to use?

When I tried (this was ~2mo back at this point) it just ignored everything and would always pick the first profile in launchSettings.json anyways. This was true of manually configured launch profiles or the dynamic configuration.

I also found applicationUrls of the selected profile is ignored; workaround was setting ASPNETCORE_URLS as an env variable.

Anyways about the documentation

  • It should make clear in which file launchSettingsProfile should be configured
  • It would be helpful if the "launchSettingsProfile": "ProfileNameGoesHere" example showed a few lines above/below.
  • If launchSettingsProfile is a valid option for launch.json, it should appear in the launch profile schema/autocomplete suggestions (today it does not)
  • If launchConfigurationId is internal, it should be removed from autocomplete suggestions and documented as such so users don't try to manually configure what the VSCode generates for them and have nothing happen as a result.

applicationUrls actually instead I use the launchUrl which is also in the properties/launchSettings. And I agree it is not clear that it is dependent on this file but it looks like the plugin is definetly trying to emulate how Visual studio works in that regards, but it is incomplete.

patoJamriska avatar Sep 24 '24 19:09 patoJamriska

I have still being doing research on this Because of other issues arising on the work I am doing, and run into this on the MS documentation. https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-8.0#development-and-launchsettingsjson

This section actually shows how to use the coreclr to pass environment variables at the launch.json level, but obviosuly that is not an option since we are using the c# plugin and the launch is "type": "dotnet". Not sure if there has been anyone looking into this besides what has been shared by now?

PD: going thru the github LaunchSettings is using coreclr, and doing a bit more digging on the console, I can see that the dotnet type does launch coreclr in the brackground regardless, so I think until this can be properly investigated and I can see some reply about it, I will most likely switch to use coreclr again on the launch.json to have full control on vscode. I removed PII from this on my logs so you can have the sample of the launch on the console:

{ "command": "launch", "arguments": { "name": "yourProjectNameDev", "type": "coreclr", "request": "launch", "projectPath": "/yourProjectPath.csproj", "launchConfigurationId": "TargetFramework=;MyApp_Dev", "__configurationTarget": 6, "preRestartTask": "dotnet: build /yourProjectPath.csproj", "program": "/yourProjectPath/bin/Debug/net8.0/yourProjectPath", "args": "", "cwd": "/yourProjectPath", "env": { "ASPNETCORE_URLS": "https://localhost:44324", "ASPNETCORE_HTTPS_PORT": "44324", "LOCAL_DEVELOPMENT": "true", "ASPNETCORE_ENVIRONMENT": "Development" }, "console": "internalConsole", "checkForDevCert": true, "launchSettingsProfile": null, "debugConsoleVerbosity": "verbose", "debuggerStartDebugTelemetry": { "buildCompletedTimestamp": "0x1922f6721ba", "totalBuildTime": 3353, "projectType": "Web" }, "serverReadyAction": { "action": "openExternally", "pattern": "\\bNow listening on:\\s+https?://\\S+", "uriFormat": "https://localhost:44324/clients" }, "hotReloadOnSave": true, "hotReloadVerbosity": "detailed", "stopAtEntry": false, "justMyCode": true, "requireExactSource": true, "enableStepFiltering": true, "logging": { "exceptions": true, "moduleLoad": true, "programOutput": true, "browserStdOut": true, "elapsedTiming": false, "threadExit": false, "processExit": true, "engineLogging": false, "diagnosticsLog": { "protocolMessages": false, "dispatcherMessages": "none", "debugEngineAPITracing": "none", "debugRuntimeEventTracing": false, "expressionEvaluationTracing": false, "startDebuggingTracing": false }, "consoleUsageMessage": true }, "suppressJITOptimizations": false, "symbolOptions": { "searchPaths": [], "searchMicrosoftSymbolServer": false, "searchNuGetOrgSymbolServer": false, "cachePath": "", "moduleFilter": { "mode": "loadAllButExcluded", "excludedModules": [], "includedModules": [], "includeSymbolsNextToModules": true, "includeSymbolsOnDemand": true } }, "expressionEvaluationOptions": { "allowImplicitFuncEval": true, "allowToString": true, "allowFastEvaluate": true, "showRawValues": false }, "internalConsoleOptions": "openOnSessionStart", "__sessionId": "90ecc55a-5069-42e8-a332-14ce69577c5f" }, "type": "request", "seq": 2 }

patoJamriska avatar Sep 26 '24 17:09 patoJamriska

Did something changed or update? I had problem that launching any configuration in launch.json leads to loading first config from launchSettings.json which screw my environment variables, and I got here, but projectPath and launchConfigurationId are not allowed as properties in configuration (anymore?)

I check docs

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-8.0#development-and-launchsettingsjson

and setting launchSettingsProfile property in launch.json config solved my issue and it's even better that config is shared, so is this issue resolved?

CaCTuCaTu4ECKuu avatar Nov 04 '24 00:11 CaCTuCaTu4ECKuu

Did something changed or update? I had problem that launching any configuration in launch.json leads to loading first config from launchSettings.json which screw my environment variables, and I got here, but projectPath and launchConfigurationId are not allowed as properties in configuration (anymore?)

I check docs

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-8.0#development-and-launchsettingsjson

and setting launchSettingsProfile property in launch.json config solved my issue and it's even better that config is shared, so is this issue resolved?

Can you show us an example of how you have your files for this? I just tested myself and the same exact behaviour is still happening. I have to comment out the other profiles to be able to launch the one I need.

patoJamriska avatar Dec 04 '24 17:12 patoJamriska

@patoJamriska this is my launchSettings.json from VS 2022 Image And that's VS Code launch.json Image

As you see I define name of profile in launchSettingsProfile and it catches environment variables from there (thats all I needed personally)

CaCTuCaTu4ECKuu avatar Dec 04 '24 21:12 CaCTuCaTu4ECKuu

@patoJamriska this is my launchSettings.json from VS 2022 Image And that's VS Code launch.json Image

As you see I define name of profile in launchSettingsProfile and it catches environment variables from there (thats all I needed personally)

Correct, but you are using the "type": "coreclr", in your launch.json. If you use the documented version of "type": "dotnet", this is where it is not working and only picks one configuration from launchSettings.json.

patoJamriska avatar Dec 17 '24 18:12 patoJamriska

Has anyone had a chance to look into this? or has anyone tested anything different?

patoJamriska avatar Feb 10 '25 14:02 patoJamriska

This used to work, but now it doesn't. Running the second launch task always seems to invoke the first (the browser pops up, instead of my CLI migrator).

.vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C#: WebAPI [https]",
      "type": "dotnet",
      "request": "launch",
      "projectPath": "${workspaceFolder}/Example/Example.csproj",
      "launchConfigurationId": "TargetFramework=;https"
    },
    {
      "name": "C#: WebAPI [migrate]",
      "type": "dotnet",
      "request": "launch",
      "projectPath": "${workspaceFolder}/Example/Example.csproj",
      "launchConfigurationId": "TargetFramework=;migrate"
    }
  ]
}

Example/Properties/launchSettings.json

{
  "profiles": {
    "https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
      }
    },
    "migrate": {
      "commandName": "Project",
      "commandLineArgs": "migrate",
      "dotnetRunMessages": true,
      "environmentVariables": {
        "DOTNET_ENVIRONMENT": "Development"
      }
    }
  }
}

And this key, launchSettingsProfile, doesn't seem to be allowed.

Property launchSettingsProfile is not allowed.

anthonymastreanvae avatar Mar 11 '25 13:03 anthonymastreanvae

Ok, so this issue is still open for 7 months now. I have tried coming back to it several times to try to see if there has been. any changes and still doe sthe same behaviour. Falling back into using the coreclr template is an option but requires so much work to setup every single little thing. Wish it could be given more time specially for those of us not using windows as a development environment

patoJamriska avatar Apr 01 '25 15:04 patoJamriska

We're seeing three different messages across different machines (not sure which are coming from this setting):

Unable to set 'TargetFramework=;build' as the active configuration. Please delete this configuration and generate a new one.
Unable to determine debug settings for project 'd:/Example/Example.csproj'
'd:/Example/Example.csproj' does not support debugging. No launchable target found.

So, we've been diffing extensions and settings and found that this setting was enabled inexplicably. Disabling it improves the situation somewhat:

"csharp.preview.improvedLaunchExperience": false

anthonymastreanvae avatar Apr 02 '25 21:04 anthonymastreanvae

That actually let me launch any of the profiles now. Thank you @anthonymastreanvae . How did you figure that out?

patoJamriska avatar Apr 23 '25 19:04 patoJamriska

diffing extensions and settings

Compared extensions and settings across a machine with and without the problem 😭

anthonymastreanvae avatar Apr 23 '25 20:04 anthonymastreanvae

We also found a way to use the C# Dev Kit "improved" launch experience to launch non-default profiles, but I didn't record the steps or take any screenshots. You can have one working experience, but not both.

anthonymastreanvae avatar Apr 23 '25 21:04 anthonymastreanvae

I'm seeing this issue as well. About a month ago the launchConfigurationId stopped being respected by VS Code when using dotnet as the type.

Kombuchelada avatar Apr 27 '25 15:04 Kombuchelada

We also found a way to use the C# Dev Kit "improved" launch experience to launch non-default profiles, but I didn't record the steps or take any screenshots. You can have one working experience, but not both.

If you guys figure that out please post it here for the rest of the world to figure it out, I know until you poiunted out the setting we were jumping hoops to get teh different environments run. It was a bit painful for those just transitioning outside of a windows environment.

patoJamriska avatar May 05 '25 13:05 patoJamriska

@patoJamriska I can't explain why it works but after some trial and error, I got the following work:

With the following contents in the file Properties/launchSettings.json (simplified):

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": { /* ... */ },
  "profiles": {
    "http": { /* ... */ },
    "https": { /* ... */ },
    "IIS Express": { /* ... */ }
  }
}

And these contents on .vscode/launch.json (simplified):

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "C#: TestDome.Console.Server Debug",
      "type": "dotnet",
      "request": "launch",
      "projectPath": "${workspaceFolder}/TestDome.Console.Server.csproj",
      "launchConfigurationId": "TargetFramework=;https"
    }
  ]
}

The https part after the semicolon must match one of the registered profiles in the launchSettings.json file. I did not change the VS Code setting indicated previously (csharp.preview.improvedLaunchExperience).

Please check if it works on your side as well, this could be the solution we're looking for! 🙋‍♂️

LSViana avatar Jun 05 '25 02:06 LSViana

Can confirm LSViana's solution works. Thanks!

It's super important to keep the TargetFramework=; in front of the profile name.

thomai-d avatar Aug 17 '25 08:08 thomai-d

@LSViana solution works great, however we still can't have compound configurations that pull dynamically from the launchSettings,json - something like

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "$schema": "vscode://schemas/launch",
    "version": "0.2.0",
    "configurations": [
        {
            "name": "DotNetLaunch",
            "type": "dotnet",
            "request": "launch",
            "projectPath": "${workspaceFolder}${pathSeparator}MyProject${pathSeparator}MyProject.csproj",
            "launchConfigurationId": "TargetFramework=;${command:extension.pickLaunchConfigurationId}" // does not work
        },
        {
            "type": "pwa-msedge",
            "name": "Launch Microsoft Edge",
            "request": "launch",
            "runtimeArgs": [
                "--remote-debugging-port=9222"
            ],
            "url": "http://localhost:8080", 
            "presentation": {
                "hidden": true
            }
        },
        {
            "type": "vscode-edge-devtools.debug",
            "name": "Open Edge DevTools",
            "request": "attach",
             "url": "http://localhost:8080", 
            "presentation": {
                "hidden": true
            }
        }
    ],
    "compounds": [
        {
            "name": "Run My Program, Launch Edge and attach DevTools",
            "configurations": [
                "DotNetLaunch",
                "Launch Microsoft Edge",
                "Open Edge DevTools"
            ]
        }
    ]
}

but that fails, even input substitution fails

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "$schema": "vscode://schemas/launch",
    "version": "0.2.0",
    "inputs": [
        {
            "type": "promptString",
            "id": "launchSettingsProfileName",
            "description": "Choose your launch settings profile name,",
            "default": "local"
        }
    ],
    "configurations": [
        {
            "name": "DotNetLaunch",
            "type": "dotnet",
            "request": "launch",
            "projectPath": "${workspaceFolder}${pathSeparator}MyProject${pathSeparator}MyProject.csproj",
            "launchConfigurationId": "TargetFramework=;${input:launchSettingsProfileName}" // does not work
        },
        {
            "type": "pwa-msedge",
            "name": "Launch Microsoft Edge",
            "request": "launch",
            "runtimeArgs": [
                "--remote-debugging-port=9222"
            ],
            "url": "http://localhost:8080",
            "presentation": {
                "hidden": true
            }
        },
        {
            "type": "vscode-edge-devtools.debug",
            "name": "Open Edge DevTools",
            "request": "attach",
             "url": "http://localhost:8080",
            "presentation": {
                "hidden": true
            }
        }
    ],
    "compounds": [
        {
            "name": "Run My Program, Launch Edge and attach DevTools",
            "configurations": [
                "DotNetLaunch",
                "Launch Microsoft Edge",
                "Open Edge DevTools"
            ]
        }
    ]
}

I think its being passed directly into microsofts c# closed source adapter and that isn't processing variables.

Only way would be to hardcode all of the launch config names, and all their respective compounds I think

without access to microsofts closed source dev kit I don't think we can proceed further

jdwalker avatar Oct 07 '25 12:10 jdwalker