Skip to content

Prevent CS0121 (ambiguous method calls) resulting from config binding source gen #86363

Description

@layomia

From @andrewlock in #44493 (comment):


One potential issue I can foresee with the design of the generated code (because I've run into a similar issue with my own generators) is around internal classes and InternalsVisibleTo.

The fact that the generated code is always a fixed type (GeneratedConfigurationBinder) in the global namespace, means that something like this will no longer compile:

Project 1:

[assembly:InternalsVisible2(Project2)]
var config = new ConfigurationBuilder().Build();
var settings = new SomeOptions();
section.Bind(settings);

Project 2: (has a <ProjectReference> to Project1)

var config = new ConfigurationBuilder().Build();
var settings = new MoreOptions();
section.Bind(settings); // 👈 error

Gives

[CS0121] The call is ambiguous between the following methods or properties: 'GeneratedConfigurationBinder.Configure<T>(Microsoft.Extensions.DependencyInjection.IServiceCollection, Microsoft.Extensions.Configuration.IConfiguration)' and 'GeneratedConfigurationBinder.Configure<T>(Microsoft.Extensions.DependencyInjection.IServiceCollection, Microsoft.Extensions.Configuration.IConfiguration)'

You might consider it an edge case, but it was one of the first issues I ran into when building my first source generator.

Note: I've just seen that preview 4 bits are going onto NuGet, so apologies if this is no longer relevant! :D (Edit: looks like the preview 3 package is broken for me)

Activity

  1. added this to the 8.0.0 milestone on May 17, 2023
  2. self-assigned this
    on May 17, 2023
  3. layomia commented on May 17, 2023

    @layomia
    ContributorAuthor
  4. davidfowl commented on May 18, 2023

    @davidfowl
    Member

    @andrewlock do you also have Configure<T> calls in that assembly?

  5. layomia commented on Jul 21, 2023

    @layomia
    ContributorAuthor

    @tarekgh @eerhardt @ericstj, does it seem reasonable to generate random names to avoid the clashes? Thinking we can make this change when we adopt the Roslyn interceptors feature.

    Right now this name clash issue doesn't seem crucial for 8.0, pending feedback from @andrewlock.

  6. davidfowl commented on Jul 21, 2023

    @davidfowl
    Member

    @tarekgh @eerhardt @ericstj, does it seem reasonable to generate random names to avoid the clashes? Thinking we can make this change when we adopt the Roslyn interceptors feature.

    This will fix it.

  7. tarekgh commented on Jul 23, 2023

    @tarekgh
    Member

    I have done similar things in the options source gen if you want to take a look at #89148

  8. added
    in-prThere is an active PR which will close this issue when it is merged
    and removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 25, 2023
  9. changed the title [-]Prevent CS0121 (ambiguous method calls) resulting from config binding source gen[/-] [+]Prevent CS0121 (ambiguous method calls) resulting from config binding & options source gen[/+] on Aug 18, 2023
  10. layomia commented on Aug 18, 2023

    @layomia
    ContributorAuthor

    I have done similar things in the options source gen if you want to take a look at #89148

    There was discussion in #90340 (comment) about the right approach to avoid name clashes. This needs to be addressed for the options generator, and I'd have to validate interception behavior in the config generator.

  11. layomia commented on Aug 24, 2023

    @layomia
    ContributorAuthor

    We're sticking with the interceptors approach for RC-1, where this problem does not arise. Options generator changes are being tracked in #90990.

  12. changed the title [-]Prevent CS0121 (ambiguous method calls) resulting from config binding & options source gen[/-] [+]Prevent CS0121 (ambiguous method calls) resulting from config binding source gen[/+] on Aug 24, 2023
  13. ghost locked as resolved and limited conversation to collaborators on Sep 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions