You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ConfigurationBinder source generator encounters namespace conflicts in using statements #93498
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.
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:
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.
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).
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.
Activity
Change namespace to workaround conflict
eerhardt commentedon Oct 26, 2023
I'm hitting this same issue trying to use the ConfigBinder Source Generator with the Azure SDK:
results in:
The issue here is that all the Azure SDK libraries put some extensions class in the
Microsoft.Extensions.Azurenamespace, and the compiler gets confused at this source generated code:Since it is in
Microsoft.Extensions.....it thinksusing Azure.Coremeansusing Microsoft.Extensions.Azureand there is noCorenamespace in there. I think the other errors are the same as #94065.ericstj commentedon Oct 26, 2023
We need to use a global prefix when importing namespaces here.
ericstj commentedon Oct 27, 2023
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
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.captainsafia commentedon Oct 27, 2023
Yep, here is the configuration we use for displaying type names in RDG.