launch.json "env" attribute ignored from VSCode when debugging
Issue Description
On a remote WSL configuration, I try to run from VSCode a profile defined in /.vscode/launch.json The parameter "env" seems ignored
{
"name": "C#: My Project Debug",
"type": "dotnet",
"request": "launch",
"projectPath": "${workspaceFolder}/path.to.my.csproj",
"env": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
},
Steps to Reproduce
Create a simple aspnet project
in /.vscode/launch.json have a profile
{
"name": "C#: My Project Debug",
"type": "dotnet",
"request": "launch",
"projectPath": "${workspaceFolder}/path.to.my.csproj",
"env": {
"ASPNETCORE_ENVIRONMENT": "Staging"
}
},
In Program.cs:
var builder = WebApplication.CreateBuilder(args);
string Env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
Console.WriteLine($"- {Env}");
Console.WriteLine(builder.Environment.EnvironmentName);
F5 from VSCode
Expected Behavior
Output should display two lines :
- Staging
Staging
Actual Behavior
Output display show no environment variable set
-
Production
Remarks
I tried also with a classic configuration on windows (no remote WSL), same behavior
I got no issue if I add to my project the file Properties/launchSettings.json and define environmentVariables values
But I assumed that you can use launch.json without launchSettings.json
Environment information
VSCode version: 1.100.2 C# Extension: 2.76.27 Using OmniSharp: false
Dotnet Information
.NET SDK: Version: 9.0.105 Commit: b7deac6d42 Workload version: 9.0.100-manifests.3b27a542 MSBuild version: 17.12.32+b7deac6d4Runtime Environment: OS Name: ubuntu OS Version: 24.04 OS Platform: Linux RID: ubuntu.24.04-x64 Base Path: /usr/lib/dotnet/sdk/9.0.105/
.NET workloads installed: There are no installed workloads to display. Configured to use loose manifests when installing new manifests.
Host: Version: 9.0.4 Architecture: x64 Commit: b7deac6d42
.NET SDKs installed: 8.0.116 [/usr/lib/dotnet/sdk] 9.0.105 [/usr/lib/dotnet/sdk]
.NET runtimes installed: Microsoft.AspNetCore.App 8.0.16 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 9.0.4 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 8.0.16 [/usr/lib/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 9.0.4 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]
Other architectures found: None
Environment variables: Not set
global.json file: Not found
Learn more: https://aka.ms/dotnet/info
Download .NET: https://aka.ms/dotnet/download
Visual Studio Code Extensions
| Extension | Author | Version | Folder Name |
|---|---|---|---|
| csdevkit | ms-dotnettools | 1.19.63 | ms-dotnettools.csdevkit-1.19.63-linux-x64 |
| csharp | ms-dotnettools | 2.76.27 | ms-dotnettools.csharp-2.76.27-linux-x64 |
| csharp-organize-usings | jeremycaron | 1.0.6 | jeremycaron.csharp-organize-usings-1.0.6 |
| csharp-to-plantuml | pierre3 | 1.4.0 | pierre3.csharp-to-plantuml-1.4.0 |
| docker | docker | 0.6.0 | docker.docker-0.6.0-linux-x64 |
| git-graph | mhutchie | 1.30.0 | mhutchie.git-graph-1.30.0 |
| nugetpackagemanagergui | aliasadidev | 2.1.1 | aliasadidev.nugetpackagemanagergui-2.1.1 |
| plantuml | jebbs | 2.18.1 | jebbs.plantuml-2.18.1 |
| prettyxml | PrateekMahendrakar | 6.0.0 | prateekmahendrakar.prettyxml-6.0.0 |
| resx-editor | timheuer | 0.2.34 | timheuer.resx-editor-0.2.34 |
| vscode-containers | ms-azuretools | 2.0.1 | ms-azuretools.vscode-containers-2.0.1 |
| vscode-dotnet-runtime | ms-dotnettools | 2.3.3 | ms-dotnettools.vscode-dotnet-runtime-2.3.3 |
| vscode-httpyac | anweber | 6.16.7 | anweber.vscode-httpyac-6.16.7 |
| vscode-nupkg | eridem | 1.0.1 | eridem.vscode-nupkg-1.0.1 |
| vscodeintellicode-csharp | ms-dotnettools | 2.2.3 | ms-dotnettools.vscodeintellicode-csharp-2.2.3-linux-x64 |
This is the expected behavior. Short answer: You want to use launchSettings.json to configure environment variables. Full documentation: https://code.visualstudio.com/docs/csharp/debugger-settings. I would recommend reading the walk though for command line arguments.
I am trying to understand. I already read the document and your answer seems like "don(t bother, do like this"
But the document tells you have 3 different approach possible. That's the first sentence.
The launchSettings.json approach seems convenient only because it is shareable with VS. I don't need it, and I don't have to keep the "Properties" folder in my code.
Also hitting F5 on VSCode always end by generating a launch.json file on my side.
And finally, why the purpose of this setting, that doesn't seem to work (aka why this issue) ?
Totally agreed that this stuff is not as straight forward as I would like. And we are definitely open to suggestions on how to better document it. I personally think we also need a command to automatically populate a launchSettings.json file, but we haven't gotten to implementing this. The documentation is the way it is because we are trying to still describe the legacy system, but from your issue I am thinking that it should be restructured to always describe the launchSettings.json way first, with maybe a note on how to do things in the coreclr/clr system.
One big thing that I can see is confusing from your question is that for all options that have "launch.json ✔️" doesn't mean that option works in launch.json if you are using "type": "dotnet". What that is trying to say is that the option works in launch.json if you are using coreclr or clr. The dotnet type pulls all settings from launchSettings.json or settings.json.
The launchSettings.json approach seems convenient only because it is shareable with VS
There are three advantages that launchSettings.json has:
- When used with C# Dev Kit (not sure if you are using Dev Kit or just the C# extension) there are a bunch of different gestures you can start debugging (in-memory launch configurations, button to run the program from the editor, right click gestures from Solution Explorer).
launchSettings.jsonworks with all of them. - You get to use the
dotnetdebug configuration, and thedotnetdebug configuration doesn't require a bunch of details about your project to be duplicated in launch.json (ex: target framework, output directory, etc) - As you referenced, it is shared between VS and VS Code. But you can also use it at the command line with
dotnet run.
why the purpose of this setting
Hopefully this is obvious from my previous statement, but just in case, what "launch.json ✔️" is trying to say is that you can use it with coreclr or clr. For dotnet, it must be in launchSettings.json.
Reactivating to track whatever documentation tweaks we decide we should make
the option works in launch.json if you are using coreclr or clr. The dotnet type pulls all settings from launchSettings.json or settings.json
Yeah, that's the issue. I was sure that coreclr or dotnet, it will be the same.
The launch.json is convenient in a multi-project workspace in vscode, as I get all the "run debug" in one place
Will try both type and see what's more convenient
Thanks.
whatever documentation tweaks we decide we should make
I see in the link provided there are missing properties like projectPath. Now I am confused with a property like Program. What's the difference, what if I have both, ...
I would tend to some clarification, like settings in launch.json are valid only with the coreclr/clr type and/or here a list of properties processed by the C# Dev Kit whatever the type used
I see in the link provided there are missing properties like
projectPath. Now I am confused with a property likeProgram. What's the difference, what if I have both, ...
projectPath is the path to the .csproj file that you want to run (example: ${workspaceFolder}/myproject.csproj). programis the path to the built executable to run (example:${workspaceFolder}/bin/Debug/net9.0/myproject.dllor${workspaceFolder}/bin/Debug/net9.0/myproject.exe`)