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

C# debugger does not apply environment variables defined in launch.json #8097

Open
mtroch opened this issue Mar 27, 2025 · 1 comment
Open

Comments

@mtroch
Copy link

mtroch commented Mar 27, 2025

Type: Bug

Issue Description

According to the documentation at https://code.visualstudio.com/docs/csharp/debugger-settings#_environment-variables a user may set the env launch property to define environment variables but that doesn't work.

Steps to Reproduce

Define a launch configuration in your launch.json, and set some environment variables using either env or environment

{
	"name": "my-launch-configuration",
	"type": "dotnet",
	"request": "launch",  
	"projectPath": "${workspaceFolder}/src/path/to/my-project.csproj",
	"env": {
		"SOME_KEY": "some_value",
	},
	"environment": {
		"SOME_KEY": "some_value",
	},
}

Run that launch configuration from the VSCode Debugger extension. Step through the following C# statement Environment.GetEnvironmentVariable("SOME_KEY").

Expected Behavior

Environment.GetEnvironmentVariable("SOME_KEY") returns "some_value"

Actual Behavior

Environment.GetEnvironmentVariable("SOME_KEY") returns null

Also notice the launch.json opened in VSCode warns you that the env and environment properties in your configuration are not supported, it underlines them with yellow squigglies, hovering the mouse over them displays a bubble that says "Property env is not allowed". The environment property is not documented so that makes sense, but the env is documented so one would expect it to work. Those warnings are also reported in VSCode's "problems" tab.

Logs

C# log

Post the output from Output-->C# here

C# LSP Trace Logs

Post the output from Output-->C# LSP Trace Logs here. Requires the `C#` output window log level to be set to `Trace`

Environment information

VSCode version: 1.98.2
C# Extension: 2.69.25
Using OmniSharp: false

Dotnet Information .NET SDK: Version: 9.0.104 Commit: 7931ad4860 Workload version: 9.0.100-manifests.dc2cb94f MSBuild version: 17.12.27+7931ad486

Runtime Environment:
OS Name: ubuntu
OS Version: 24.04
OS Platform: Linux
RID: ubuntu.24.04-arm64
Base Path: /usr/lib/dotnet/sdk/9.0.104/

.NET workloads installed:
There are no installed workloads to display.
Configured to use loose manifests when installing new manifests.

Host:
Version: 9.0.3
Architecture: arm64
Commit: 7931ad4860

.NET SDKs installed:
8.0.114 [/usr/lib/dotnet/sdk]
9.0.104 [/usr/lib/dotnet/sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 8.0.14 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.3 [/usr/lib/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 8.0.14 [/usr/lib/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.3 [/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.17.64 ms-dotnettools.csdevkit-1.17.64-linux-arm64
csharp ms-dotnettools 2.69.25 ms-dotnettools.csharp-2.69.25-linux-arm64
gitlens eamodio 16.3.3 eamodio.gitlens-16.3.3
makefile-tools ms-vscode 0.12.17 ms-vscode.makefile-tools-0.12.17
vscode-docker ms-azuretools 1.29.4 ms-azuretools.vscode-docker-1.29.4
vscode-dotnet-runtime ms-dotnettools 2.3.0 ms-dotnettools.vscode-dotnet-runtime-2.3.0

Extension version: 1.17.64
VS Code version: Code 1.98.2 (ddc367ed5c8936efe395cffeec279b04ffd7db78, 2025-03-12T13:32:45.399Z)
OS version: Windows_NT arm64 10.0.26100
Modes:
Remote OS version: Linux arm64 5.15.167.4-microsoft-standard-WSL2

System Info
Item Value
CPUs Snapdragon(R) X 12-core X1E80100 @ 3.40 GHz (12 x 2974)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: unavailable_software
Load (avg) undefined
Memory (System) 31.57GB (13.61GB free)
Process Argv --crash-reporter-id 6562aa1f-3698-49b6-9917-d0a2a1ddfe0f
Screen Reader no
VM 0%
Item Value
Remote WSL: Ubuntu-24.04
OS Linux arm64 5.15.167.4-microsoft-standard-WSL2
CPUs unknown (12 x 0)
Memory (System) 15.48GB (12.30GB free)
VM 0%
@gregg-miskelly
Copy link
Contributor

TLDR: You want to add your environment variables to a <project-directory>/Properties/launchSettings.json file.

Full Explanation: There are two different type values you can use to debug a C# program -- you can use dotnet, which call into the project system to determine how the specified project should be debugged, or you can use coreclr (or one of the other lower-level debug adapter types depending on what the type of project you have is) where you can fully specify how you want to debug, but you need to keep that in sync with any project changes (example: the target framework). dotnet doesn't support env (hopefully IntelliSense was giving you squiggles about this?). Instead, it pulls environment variables from the 'Properties/launchSettings.json' file, which is not just VS Code-specific, but also used by full VS, and the .NET CLI. If you need to have this specified in launch.json, you can switch to the coreclr type. In this case I would suggest using the '.NET: Generate Assets for Build and Debug' command from the command well, as that will give you a good launch.json template to start from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants