Repository navigation
Description
Description
#62819 was merged into 6.0.2 to revert a breaking change in 6.0.0, but the reversion caused a breaking change compared to 6.0.0/6.0.1, by reverting the (good) part of #62819.
The current implementation of the EnvironmentVariablesConfigurationProvider has undocumented, and in my opinion, rather confusing, behaviour with respect to prefixes containing the string __. I would expect for a variable Prefix__Setting=value to match against prefix Prefix__, and be equivalent to providing Setting=value, but this is not the case. At present (6.0.2+), the user must replace __ with : in the prefix in order to match.
For reference, the current configuration documentation is here.
This issue was originally flagged in my comment here: #61577 (comment) - and there have been a few related issues, worth bearing in mind for context:
- Environment Variable configuration provider doesn't work if the prefix has 2 underscores #40911
- Breaking change in environment variable configuration prefix support in dotnet 6 #61577
It should be noted that these issues are caused by regressions against existing matches. I believe this issue can be fixed without causing previous matches to fail.
Reproduction Steps
Using 6.0.2+, the following fails:
Environment.SetEnvironmentVariable("MY_PREFIX__ConfigKey, "myFooValue");
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables("MY_PREFIX__")
.Build();
var loadedValue = configuration.GetValue<string?>("ConfigKey", null);
Assert.Equals("myFooValue", loadedValue); // FAILS in 6.0.2 as loadedConfig is null. The prefix of `MY_PREFIX:` needs to be specified instead now
Expected behavior
I would expect that an environment variable MY_PREFIX__ConfigKey would match the prefix MY_PREFIX__.
The __ is represented as an entirely safe alternative to : in the docs, for the purposes of matching environment variables in an OS-independent manner. I would expect any normalization to : for the purpose of matching the prefix to be hidden in the class implementation and not leak into user-facing space.
Actual behavior
See reproduction steps.
Regression?
Yes. It worked on 6.0.0 and 6.0.1. Upgrading to 6.0.2 caused the break.
Known Workarounds
Replace all __ in the provided prefix with : - eg new ConfigurationBuilder().AddEnvironmentVariables("MY_PREFIX__").Build(); would become new ConfigurationBuilder().AddEnvironmentVariables("MY_PREFIX:").Build();
Configuration
.NET 6.0.2+, OS: Problem confirmed on MacOS and Linux (various distros).
Other information
No response
Activity
Improves handling of env prefixes containing __
Improves handling of env prefixes containing __ (#65757)
Improves handling of env prefixes containing __ (dotnet#65757)
gewarren commentedon Oct 11, 2022
(RESOLVED - I hadn't updated my NuGet packages to .NET 7.) @dhedey I'm writing up the breaking change article for this and am trying to reproduce the before/after behavior. I'm running .NET 7 RC 2, which should contain your fix, but the following code still outputs
False. Is that because I'm running it on Windows and is this expected?