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 binding source generator fails with CS8598 error #89010

Closed
martincostello opened this issue Jul 17, 2023 · 6 comments · Fixed by #89045 or #89117
Closed

Configuration binding source generator fails with CS8598 error #89010

martincostello opened this issue Jul 17, 2023 · 6 comments · Fixed by #89045 or #89117
Assignees
Labels
area-Extensions-Configuration bug source-generator Indicates an issue with a source generator feature
Milestone

Comments

@martincostello
Copy link
Member

Description

Attempting to enable the Configuration binding source generator with a struct used as a custom configuration type causes the source generator to emit code that fails to compile with a CS8598 error.

Repro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\GeneratedConfigurationBinder.g.cs(122,17): error CS8598: The suppression operator is not allowed in this context [Repro\Repro.csproj]

The method generated is below. The failing line is element! = temp4;.

public static void BindCore(IConfiguration configuration, ref IDictionary<string, Geolocation> obj, BinderOptions? binderOptions)
{
    if (obj is null)
    {
        throw new ArgumentNullException(nameof(obj));
    }

    foreach (IConfigurationSection section in configuration.GetChildren())
    {
        if (!(obj.TryGetValue(section.Key!, out Geolocation element)))
        {
            element = InitializeGeolocation(section, binderOptions);;
        }
        var temp4 = InitializeGeolocation(configuration, binderOptions);;
        BindCore(section, ref temp4, binderOptions);
        element! = temp4; // CS8598 occurs here
        obj[section.Key!] = element;
    }
}

Reproduction Steps

  1. Clone martincostello/config-binding-source-generator-cs8598-repro
  2. Run dotnet build

Expected behavior

The application compiles and configuration binding works equivalently to runtime binding.

Actual behavior

The application fails to compile with a CS8598 error.

Regression?

No.

Known Workarounds

None.

Configuration

.NET SDK 8.0.100-preview.6.23330.14

Other information

No response

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

ghost commented Jul 17, 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

Attempting to enable the Configuration binding source generator with a struct used as a custom configuration type causes the source generator to emit code that fails to compile with a CS8598 error.

Repro\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\GeneratedConfigurationBinder.g.cs(122,17): error CS8598: The suppression operator is not allowed in this context [Repro\Repro.csproj]

The method generated is below. The failing line is element! = temp4;.

public static void BindCore(IConfiguration configuration, ref IDictionary<string, Geolocation> obj, BinderOptions? binderOptions)
{
    if (obj is null)
    {
        throw new ArgumentNullException(nameof(obj));
    }

    foreach (IConfigurationSection section in configuration.GetChildren())
    {
        if (!(obj.TryGetValue(section.Key!, out Geolocation element)))
        {
            element = InitializeGeolocation(section, binderOptions);;
        }
        var temp4 = InitializeGeolocation(configuration, binderOptions);;
        BindCore(section, ref temp4, binderOptions);
        element! = temp4; // CS8598 occurs here
        obj[section.Key!] = element;
    }
}

Reproduction Steps

  1. Clone martincostello/config-binding-source-generator-cs8598-repro
  2. Run dotnet build

Expected behavior

The application compiles and configuration binding works equivalently to runtime binding.

Actual behavior

The application fails to compile with a CS8598 error.

Regression?

No.

Known Workarounds

None.

Configuration

.NET SDK 8.0.100-preview.6.23330.14

Other information

No response

Author: martincostello
Assignees: -
Labels:

untriaged, area-Extensions-Configuration

Milestone: -

@tarekgh tarekgh added the source-generator Indicates an issue with a source generator feature label Jul 17, 2023
@tarekgh tarekgh added this to the 8.0.0 milestone Jul 17, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 17, 2023
@layomia
Copy link
Contributor

layomia commented Jul 17, 2023

Thanks for providing a repro. Taking a look at this & #89019.

@layomia
Copy link
Contributor

layomia commented Jul 17, 2023

Side note: I notice that you're using a TypeConverter for the Geolocation type in question. The generator doesn't probe for or generate code to support TypeConverter usage. This is to facilitate AOT / trim compat, given that's the primary goal for the feature. We have an issue tracking a new extensibility feature that we can consider post .NET 8 - #83599.

After this (#89010) bug is fixed, could you try the generator again to see if the binding behavior matches your expectations?

@layomia layomia added the bug label Jul 17, 2023
@martincostello
Copy link
Member Author

Sure. The repro is adapted from a private repo that has a suite of tests. Once it uses a version of the SDK that should compile, it should be trivial to see if the binding behaviour works correctly.

@layomia
Copy link
Contributor

layomia commented Jul 17, 2023

Looking at the core parsing logic, it looks like the generator would not handle your inputs as expected - https://github.com/martincostello/config-binding-source-generator-cs8598-repro/blob/57998e5dcd44f6ec59429240bcc63839f6d1a70e/Repro/Geolocation.cs#L69.

So IIUC the input IConfiguration shape would be something like { Geolocation: "3, 4" } rather than { Geolocation: { Latitude: 3, Longitude: 4 } }. I believe the generator would only support the latter.

--

I say this just to point out the extensibility feature if you want to chime in there.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 17, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 18, 2023
@tarekgh tarekgh reopened this Jul 18, 2023
@tarekgh
Copy link
Member

tarekgh commented Jul 18, 2023

Reopen as looks the changes are reverted.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 18, 2023
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 19, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Aug 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-Extensions-Configuration bug source-generator Indicates an issue with a source generator feature
Projects
None yet
3 participants