Environment variables not available when running/debugging in Rider

How does Rider handle environment variables?

What I'm experiencing is that some environment variables we use to override values from appsettings.json, are not available when using Rider, but they are when using Visual Studio.

Is this expected behavior?

If so, how can I have a consistent behavior between VS and Rider?

Comment actions Permalink

Hello Riccardo,

Thank you for contacting Rider support.
Here you can find how to set the environment variables for specific run configuration. 
If it doesn't help, would you mind specifying your use-case in Visual Studio in more detail? How exactly you override the environment variables in Visual Studio?

Thank you in advance!

Comment actions Permalink

Hi Dmitry

This is my use case: we have web application A which depends on two services S1, and S2. Most of the times, you're working on A, using instances for S1 and S2 deployed to a development server. Settings for S1 and S2 are stored in appsettings.json; application also add EnvironmentVariablesConfigurationSource to allow overriding these settings from environment variables.

Occasionally, mostly when debugging issues, we need to use a local instance for S1 or S2; to avoid changing appsettings.json, we simply override some settings using environment variables.

This works transparently when using VS, ie you simply set the variable (eg using "setx SOME_KEY SomeValue"), then start visual studio, and then when you get "SOME_KEY" from configuration, we get "SomeValue".

When I do the same with Rider, I always get the value as set in appsettings.json instead.



Comment actions Permalink

Hello Riccardo,

Thank you for sharing the details of your use case. Regrettably, I was not able to reproduce the issue. After restart Rider takes the environment variables fine and successfully overrides the values from appsettings.json. 

In the following simple case, having the IConfigurationRoot:
`var built = new ConfigurationBuilder().AddJsonFile("appsettings.json").AddEnvironmentVariables("MYPREFIX_").Build();`

having the appsettings.json as:

"Variable1": "Value"

I get "Value" from appsettings.json via `built.GetValue<string>("Variable1")`
If I set environment variable as `setx MYPREFIX_Variable1 some_value` and restart Rider, then with via the same command `built.GetValue<string>("Variable1")`, instead of value from appsettings.json file, I get the value of environment variable I set .

Would you mind confirming that you close Rider completely (not just back to Welcome screen)?

I  also suggest ensuring that Rider sees the variable you set: `Shift+Shift in Rider | type and select "IDE Scripting Console" | Groovy | type "def env = System.getenv()" | press Ctrl+Enter`.
In the `Run` tab you will see the output. Please ensure and let me know if there is a variable you set.

Would you mind sharing a repro project with us so we could investigate the issue?

Comment actions Permalink

Hi Dmitry

I actually noticed that after I've restarted my PC, it was working as expected...

I think I'vefigured out what is happening... as I wrote, most of the time when I need to do this, I'm already working on one or two solutions, so when I set the variable, Rider is already running... Visual studio starts a new instance instead, so it is aware of the variable I've just set.

Does this make sense?

If this is the case, I guess there's not much I can do, at least with env variables... I guess I could use secrets instead.




Comment actions Permalink

Hello Riccardo,

Rider is a single-instance application, that is the reason for the behavior you encounter. You should close all the "instances"(windows) of Rider, so then, with the next start of Rider the environment variables you set will be taken into account.

We have [this](https://youtrack.jetbrains.com/issue/IDEABKL-5934) open issue on our tracker. We would appreciate it if you could upvote this issue to demonstrate the additional interest.

Should you have any other questions or difficulties, please let me know.
Have a great day!


Please sign in to leave a comment.