Skip to content

Confusing environment variable configuration prefix behaviour reintroduced in 6.0.2 #65756

Closed

Description

@dhedey

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:

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

  1. ghost added
    untriaged
    on Feb 23, 2022
  2. added a commit that references this issue on Feb 23, 2022
    b8cde59
  3. ghost added
    in-pr
    on Feb 23, 2022
  4. removed
    untriaged
    on Mar 1, 2022
  5. added this to the 7.0.0 milestone on Mar 1, 2022
  6. added a commit that references this issue on Mar 23, 2022
    9cd49ff
  7. ghost removed
    in-pr
    on Mar 23, 2022
  8. added a commit that references this issue on Mar 30, 2022
    d1056ba
  9. ghost locked as resolved and limited conversation to collaborators on Apr 22, 2022
  10. gewarren commented on Oct 11, 2022

    @gewarren
    Contributor

    (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?

    using Microsoft.Extensions.Configuration;
    
    Environment.SetEnvironmentVariable("MY_PREFIX__ConfigKey", "myFooValue");
    
    IConfiguration config = new ConfigurationBuilder()
        .AddEnvironmentVariables(prefix: "MY_PREFIX__")
        .Build();
    
    var loadedValue = config.GetValue<string?>("ConfigKey", null);
    
    Console.WriteLine(String.Equals("myFooValue", loadedValue));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions