mcintyre321 / OneOf

Easy to use F#-like ~discriminated~ unions for C# with exhaustive compile time matching
MIT License
3.34k stars 155 forks source link

OneOfBase Source Generation #86

Closed Paul-Williams closed 3 years ago

Paul-Williams commented 3 years ago

I am having trouble with source generation with OneOfBase and am wondering if I am missing something.

For example, given:

public partial class StringOrNumber : OneOfBase<string, int> { }

The above code, taken from the readme, does not compile, with error: CS7036: There is no argument given that corresponds to the required formal parameter 'input' of 'OneOfBase<string, int>.OneOfBase(OneOf<string, int>)

To make that go away I have to add a constructor, such as: public StringOrNumber(OneOf<string, int> input) : base(input) { }

But then the code generator fails because it wants to create the constructor.

romfir commented 3 years ago

do you use GenerateOneOfAttribute? It won't work without it.

  [GenerateOneOf]
  public partial class StringOrNumber : OneOfBase<string, int> { }
Paul-Williams commented 3 years ago

@romfir Hi, I have just tested again in two, new, empty console apps. One being .NET 5, the other Framework 4.8.

The class is defined as:

  [GenerateOneOf]
  public partial class StringOrNumber : OneOfBase<string, int> { }

The .NET 5 project compiles fine. However in the 4.8 project the GenerateOneOf attribute is not recognized and I get the errors: Error CS0246 The type or namespace name 'GenerateOneOfAttribute' could not be found (are you missing a using directive or an assembly reference?)

Error CS7036 There is no argument given that corresponds to the required formal parameter 'input' of 'OneOfBase<string, int>.OneOfBase(OneOf<string, int>)

And a red squiggle under [GenerateOneOf] and 'StringOrNumber'

I have added the two NuGet packages (OneOf & OneOf.SourceGenerator) to both projects.

The project I was previously having issues in was indeed a .NET Framework 4.8 project.

romfir commented 3 years ago

@Paul-Williams it will be fixed in #87, as a workaround you can add it as an analyzer manually via GUI after adding nuget:

  1. Right click on Analyzers under References
  2. Browse
  3. Add your-project-path\packages\OneOf.SourceGenerator.3.0.186\analyzers\dotnet\cs\OneOf.SourceGenerator.dll
Paul-Williams commented 3 years ago

@romfir Thanks so much. Seems I have taken up your evening on this one. Will take a look-see and report back.

Paul-Williams commented 3 years ago

@romfir Tested again with same .Net Framework 4.8 console app after implementing your workaround: Workaround is working fine. Errors are gone and 'Object Browser' is showing all expected generated methods on class 'StringOrNumber'.

Nice one.

ahmad2smile commented 2 years ago

Same issue as @Paul-Williams had, but for me on .NET 6 running OneOf + SourceGen at 3.0.203

romfir commented 2 years ago

@ahmad2smile I think there is some problem in VS2022, when analyzer returns an error it is not being run again. I don't know if we can do anything about it, I'll try to search for some answers. For know I think unloading and reloading a solution should fix it.

edit:

whats more, for a second after rebuild it shows error about duplicate files so maybe it is being run: image

code
    [GenerateOneOf]
    public partial class StringOrNumber : OneOfBase { }

    [GenerateOneOf]
    public partial class StringOrNumber1 : OneOfBase { }

    [GenerateOneOf]
    public partial class StringOrNumber3 : OneOfBase { }

    [GenerateOneOf]
    public partial class StringOrNumber4 : OneOfBase { }