Skip to content

ConfigurationBinder source generator encounters namespace conflicts in using statements #93498

Closed

Description

@ericstj

Description

When the first part of a namespace of a bound type exists under the Microsoft namespace an error will occur

using Microsoft.Extensions.Configuration;
var c = new ConfigurationBuilder().Build();
c.Get<Foo.Bar.BType>();

namespace Microsoft.Foo
{
    internal class AType {}
}

namespace Foo.Bar
{
    internal class BType {}
}

C:\scratch\namespaceConflict\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(22,15): error CS0234: The type or namespace name 'Bar' does not exist in the namespace 'Microsoft.Foo' (are you missing an assembly reference?) [C:\scratch\namespaceConflict\namespaceConflict.csproj]

This is happening because the generator places using statements in a namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration and this will cause the compiler to first search in containing namespaces at every level before considering the global namespace for usings placed in that namespace.

Hit this when porting eShopOnContainers.

Reproduction Steps

Build
namespaceConflict.zip

Expected behavior

Builds without issue

Actual behavior

Compilation error.

Regression?

No

Known Workarounds

Alias the reference that has the conflicting namespace, change namespace names, etc.

Configuration

No response

Other information

No response

Activity

  1. ghost added
    untriaged
    on Oct 14, 2023
  2. added this to the 9.0.0 milestone on Oct 14, 2023
  3. ghost removed
    untriaged
    on Oct 14, 2023
  4. added
    untriaged
    source-generator
    on Oct 14, 2023
  5. ghost removed
    untriaged
    on Oct 14, 2023
  6. eerhardt commented on Oct 26, 2023

    @eerhardt
    Member

    I'm hitting this same issue trying to use the ConfigBinder Source Generator with the Azure SDK:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
        <NoWarn>$(NoWarn);SYSLIB1100;SYSLIB1101</NoWarn>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.0-rtm.23511.16" />
        <PackageReference Include="Azure.Data.Tables" Version="12.8.1" />
      </ItemGroup>
    
    </Project>
    using Azure.Data.Tables;
    using Microsoft.Extensions.Configuration;
    
    Console.WriteLine("Hello, World!");
    
    static void BindToConfiguration(TableClientOptions options, IConfiguration configuration)
    {
        var configurationOptionsSection = configuration.GetSection("ConfigurationOptions");
        configurationOptionsSection.Bind(options);
    }

    results in:

    1>C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(22,17,22,21): error CS0234: The type or namespace name 'Core' does not exist in the namespace 'Microsoft.Extensions.Azure' (are you missing an assembly reference?)
    1>C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(23,17,23,21): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'Microsoft.Extensions.Azure' (are you missing an assembly reference?)
    1>C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(71,71,71,89): error CS0246: The type or namespace name 'DiagnosticsOptions' could not be found (are you missing a using directive or an assembly reference?)
    1>C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(145,71,145,83): error CS0246: The type or namespace name 'RetryOptions' could not be found (are you missing a using directive or an assembly reference?)
    1>C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(195,71,195,84): error CS0246: The type or namespace name 'ClientOptions' could not be found (are you missing a using directive or an assembly reference?)
    1>C:\Users\eerhardt\source\repos\ConsoleApp101\ConsoleApp101\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(249,71,249,89): error CS0246: The type or namespace name 'TableClientOptions' could not be found (are you missing a using directive or an assembly reference?)
    

    The issue here is that all the Azure SDK libraries put some extensions class in the Microsoft.Extensions.Azure namespace, and the compiler gets confused at this source generated code:

    namespace Microsoft.Extensions.Configuration.Binder.SourceGeneration
    {
        using Azure.Core;
        using Azure.Data.Tables;

    Since it is in Microsoft.Extensions..... it thinks using Azure.Core means using Microsoft.Extensions.Azure and there is no Core namespace in there. I think the other errors are the same as #94065.

  7. ericstj commented on Oct 26, 2023

    @ericstj
    MemberAuthor

    We need to use a global prefix when importing namespaces here.

  8. ericstj commented on Oct 27, 2023

    @ericstj
    MemberAuthor

    We should actually never add using statements here. Those using statements are being driven by types referenced across many files, combining all the using statements in a single file can cause name conflicts (even ignoring this special case).

    I heard from @captainsafia via @eerhardt

    if you always prefix global:: for all types coming from users code, and never let users code add a using statement to your generated code, then you should be good

    We need to do this. Any reference to a type without a global:: prefix could face a name conflict, even without local usings - due to global usings.

  9. captainsafia commented on Oct 27, 2023

    @captainsafia

    Yep, here is the configuration we use for displaying type names in RDG.

  10. ghost added
    in-pr
    on Nov 1, 2023
  11. ghost removed
    in-pr
    on Nov 2, 2023
  12. ghost locked as resolved and limited conversation to collaborators on Dec 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-Extensions-Configurationsource-generator

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions