Skip to content

[Breaking change]: Improvement to handling of env prefixes containing __ #31490

Closed

Description

@maryamariyan

Description

Key changes:

  • By comparing normalized prefixes to normalized keys, both the un-altered and colon-normalized prefixes match environment variables in either the __ or colon formats.
  • There is a change of behaviour that some environment variables which theoretically didn't match the filter before may now match the filter, but I don't expect this to be an issue in practice (I very much doubt anyone configures variables with both a : and __ prefix, and relies on the fact that these don't match)
  • Existing behaviour is maintained, to avoid any potential issues such as Breaking change in environment variable configuration prefix support in dotnet 6 runtime#61577 or any changes of behaviour with the special case connection string matching.

Version

.NET 7 Preview 4

Previous behavior

Some environment variables which theoretically didn't match the filter before may now match the filter

New behavior

By comparing normalized prefixes to normalized keys, both the un-altered and colon-normalized prefixes match environment variables in either the __ or colon formats.

Type of breaking change

  • Binary incompatible: Existing binaries may encounter a breaking change in behavior, such as failure to load/execute or different run-time behavior.
  • Source incompatible: Source code may encounter a breaking change in behavior when targeting the new runtime/component/SDK, such as compile errors or different run-time behavior.

Reason for change

Confusing environment variable configuration prefix behaviour was reintroduced in 6.0.2. (It worked on 6.0.0 and 6.0.1. Upgrading to 6.0.2 caused the break.) dotnet/runtime#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 dotnet/runtime#62819.

For reference, the current configuration documentation is here.

Recommended action

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); // FAILED in 6.0.2 as loadedConfig was null. The prefix of `MY_PREFIX:` can be specified instead now

Feature area

C#, Core .NET libraries, Extensions

Affected APIs

No response

Activity

  1. added
    breaking-change
    on Sep 29, 2022
  2. added
    ⌚ Not Triaged
    🏁 Release: .NET 7
    source incompatible
    on Sep 29, 2022
  3. maryamariyan commented on Sep 29, 2022

    @maryamariyan
    ContributorAuthor

    When I was trying to create this issue the .NET 7 Preview 4 option was not available so selected .NET 7 Preview 5 from the drop down list.

  4. gewarren commented on Oct 11, 2022

    @gewarren
    Collaborator

    @maryamariyan Does this breaking change only affect Unix-based OSs? See my comment/code here, which I would expect to return True on .NET 7 RC 2.

  5. dhedey commented on Oct 12, 2022

    @dhedey

    Hi @gewarren apologies, I'm unable reply to you on dotnet/runtime#65756 as the issue is closed, and I'm only a Contributor.

    But yes, I'm surprised by this behaviour. The PR includes a test case AddEnvironmentVariablesUsingPrefixWithDoubleUnderscores_Bind_PrefixMatches (see this line in github) which is a test very similar to your example. I'm surprised the test passes while your example fails, unless the tests don't run on Windows in CI. The matching of normalized key against normalized prefix should also be OS-independent.

    Your code prints True on .NET 7 RC 2 on Mac for me.
    EDIT: I've also managed to get access to a Windows 11 PC. It also prints True on Windows for me:

    Console-DotnetRuntime-65756

    Is there perhaps anything notable about your set-up or .NET 7 version?

  6. gewarren commented on Oct 12, 2022

    @gewarren
    Collaborator

    Thanks @dhedey, I did get it to print True after updating my NuGet packages for the extensions to .NET 7 (forgot about that).

  7. ghost added
    in-pr
    on Oct 12, 2022
  8. Repository owner moved this from 🔖 Ready to ✅ Done in dotnet/docs October Sprinton Nov 8, 2022
  9. ghost removed
    in-pr
    on Nov 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

🏁 Release: .NET 7breaking-changesource incompatible

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions