Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration binder generator creates namespace ambiguity error #94836

Closed
paulguz-datapa opened this issue Nov 16, 2023 · 10 comments
Closed

Configuration binder generator creates namespace ambiguity error #94836

paulguz-datapa opened this issue Nov 16, 2023 · 10 comments
Labels
area-Extensions-Configuration source-generator Indicates an issue with a source generator feature
Milestone

Comments

@paulguz-datapa
Copy link

paulguz-datapa commented Nov 16, 2023

Description

After upgrading our ASP.Net 7 application (that started life as Core 1.1 and upgraded through several .Net Core versions) to .Net 8, when building in a DevOps pipeline, the following error happens:

BindingExtensions.g.cs(131,71): error CS0104: 'Options' is an ambiguous reference between 'myProject.Options' and 'Microsoft.Extensions.Options.Options'

According to the docs, the Configuration Binding Source Generator is disabled by default, yet we have needed to add <EnableConfigurationBindingGenerator>false</EnableConfigurationBindingGenerator> to the csproj to work around the error. We are not using AOT, and never have.

Note, the project references a Microsoft.NET.Sdk.BlazorWebAssembly project. I'm not sure if that's relevant.

Reproduction Steps

  • Take an existing .Net 7 web project,
  • put an Options class in the root namespace and use with WebApplicationBuilder.Services.Configure.
  • Upgrade to .Net 8.
  • Do not intentionally enable the Configuration-binding source generator.
  • Do not use AOT

Expected behavior

Project should build successfully. Configuration-binding source generator should not be enabled.

Actual behavior

Project fails to build with the following error:

BindingExtensions.g.cs(131,71): error CS0104: 'Options' is an ambiguous reference between 'myProject.Options' and 'Microsoft.Extensions.Options.Options'

Configuration-binding source generator appears to be enabled despite it supposedly being disabled by default.

Regression?

No response

Known Workarounds

<EnableConfigurationBindingGenerator>false</EnableConfigurationBindingGenerator> in the csproj

Configuration

.Net 8

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 16, 2023
@ghost
Copy link

ghost commented Nov 16, 2023

Tagging subscribers to this area: @dotnet/area-extensions-configuration
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

After upgrading our ASP.Net 7 application (that started life as 1.1 and upgraded through several .Net Core versions) to .Net 8, when building in a DevOps pipeline, the following error happens:

BindingExtensions.g.cs(131,71): error CS0104: 'Options' is an ambiguous reference between 'myProject.Options' and 'Microsoft.Extensions.Options.Options'

According to the docs, the Configuration Binding Source Generator is disabled by default, yet we have needed to add false to the csproj to work around the error.

Reproduction Steps

  • Take an existing .Net 7 web project,
  • put an Options class in the root namespace and use with WebApplicationBuilder.Services.Configure.
  • Upgrade to .Net 8.
  • Do not intentionally enable the Configuration-binding source generator.

Expected behavior

Project should build successfully. Configuration-binding source generator should not be enabled.

Actual behavior

Project fails to build with the following error:

BindingExtensions.g.cs(131,71): error CS0104: 'Options' is an ambiguous reference between 'myProject.Options' and 'Microsoft.Extensions.Options.Options'

Configuration-binding source generator appears to be enabled despite it supposedly being disabled by default.

Regression?

No response

Known Workarounds

false in the csproj

Configuration

.Net 8

Other information

No response

Author: paulguz-datapa
Assignees: -
Labels:

area-Extensions-Configuration

Milestone: -

@eerhardt
Copy link
Member

Is PublishTrimmed set to true in your app? When a web application has PublishTrimmed or PublishAot set to true, the source generators are enabled in .NET 8. The idea is that when you are trimming, the source generators are the only way to guarantee config binding will work after trimming (or AOT).

See dotnet/aspnetcore#48416 and dotnet/sdk#34022.

However, if your app is a Blazor WASM app, it shouldn't be enabled by default.

https://github.com/dotnet/sdk/blob/601383646e1547c6bc77c3295be63ff3939b0f2e/src/BlazorWasmSdk/Targets/Microsoft.NET.Sdk.BlazorWebAssembly.Current.targets#L24-L25

@tarekgh
Copy link
Member

tarekgh commented Nov 16, 2023

@paulguz-datapa could you please provide a sample project reproduce the issue? or compile your app with /bl and share the produced msbuild logs.

@tarekgh tarekgh added this to the 9.0.0 milestone Nov 16, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 16, 2023
@tarekgh tarekgh added the needs-author-action An issue or pull request that requires more info or actions from the author. label Nov 16, 2023
@ghost
Copy link

ghost commented Nov 16, 2023

This issue has been marked needs-author-action and may be missing some important information.

@tarekgh
Copy link
Member

tarekgh commented Nov 16, 2023

Also, #94267 maybe fixing the namespace ambiguity too. You may try the 8.0.2 builds from https://github.com/dotnet/installer or when you share a sample project, I can try it too.

CC @eiriktsarpalis @ericstj

@paulguz-datapa
Copy link
Author

@eerhardt Yes, we are enabling PublishTrimmed.

@tarekgh There is a sample solutiuon here.

@ghost ghost added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Nov 17, 2023
@tarekgh tarekgh added source-generator Indicates an issue with a source generator feature and removed needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration labels Nov 17, 2023
@tarekgh tarekgh modified the milestones: 9.0.0, 8.0.x Nov 17, 2023
@tarekgh
Copy link
Member

tarekgh commented Nov 17, 2023

Thanks @paulguz-datapa for providing the sample and answering the questions.

Enabling PublishTrimmed will automatically enable the configuration binding source generator. You already know the workaround if needed <EnableConfigurationBindingGenerator>false</EnableConfigurationBindingGenerator>.

For the ambiguity build errors, I have confirmed through your sample it is fixed by #94267. This fix is scheduled to be released next month as the first 8.0 servicing release 8.0.1. With the fix, you'll find the generated code changed from something like:

            if (type == typeof(Options))
            {
                var temp = (Options)instance;
                return;
            }

To

            if (type == typeof(global::CBGError.Options))
            {
                var temp = (global::CBGError.Options)instance;
                return;
            }

I am closing the issue but feel free to send any more question if you have any.

@tarekgh tarekgh closed this as completed Nov 17, 2023
@paulguz-datapa
Copy link
Author

thanks @tarekgh

@eerhardt PublishTrimmed enabling source generators doesn't seem to be documented (the What's New doc only mentions AOT and there's no sign at the Trimming Options doc either)

@eerhardt
Copy link
Member

eerhardt commented Dec 1, 2023

@captainsafia - do you know of a good place to document this behavior? Also cc @gewarren

We made this change with dotnet/sdk#34022

@captainsafia
Copy link
Member

@captainsafia - do you know of a good place to document this behavior? Also cc @gewarren

We made this change with dotnet/sdk#34022

The trimming options doc page referenced by @paulguz-datapa seems as good a place as any.

I opened a docs issue to track this over at dotnet/docs#38555

@github-actions github-actions bot locked and limited conversation to collaborators Jan 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Configuration source-generator Indicates an issue with a source generator feature
Projects
None yet
Development

No branches or pull requests

4 participants