OneOf icon indicating copy to clipboard operation
OneOf copied to clipboard

GenerateOneOf source generator does not work when using same class name with different generics

Open ADIX7 opened this issue 1 year ago • 1 comments

Using this code, the source generator does not generate anything:

[GenerateOneOf]
public partial class Result<TValue, TError> : OneOfBase<TValue, TError>
{
}

[GenerateOneOf]
public partial class Result<TValue, TError1, TError2> : OneOfBase<TValue, TError1, TError2>
{
}

If I rename or remove one of the classes, it does work as expected.

ADIX7 avatar May 19 '23 13:05 ADIX7

its due to

Warning	CS8785	Generator 'OneOfGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentException' with message 'The hintName 'ConsoleApp1_Result.g.cs' of the added source file must be unique within a generator.
Parameter name: hintName'	ConsoleApp1	C:\Users\...\ConsoleApp1.csproj	1	Active	Generator threw the following exception:
'System.ArgumentException: The hintName 'ConsoleApp1_Result.g.cs' of the added source file must be unique within a generator.
Parameter name: hintName
   at Microsoft.CodeAnalysis.AdditionalSourcesCollection.Add(String hintName, SourceText source)
   at OneOf.SourceGenerator.OneOfGenerator.Execute(SourceProductionContext context, ImmutableArray`1 symbols)
   at Microsoft.CodeAnalysis.UserFunctionExtensions.<>c__DisplayClass3_0`2.<WrapUserAction>b__0(TInput1 input1, TInput2 input2, CancellationToken token)'.

we can do one of the following:

  1. Put ALL the generated source code into one file
  2. Change file name generation to something like typeof(Result<,,>).ToString() which should generate ~ "Result`3[T1,T2,T3]" (we can replace "`[,]" characters with underscores
  3. Add number of generic arguments to file name (ConsoleApp1_Result_3.g.cs, ConsoleApp1_Result_2.g.cs)

side note: source generator also fails when we put the type into global namespace:

[GenerateOneOf]
public partial class Result2<TValue, TError1, TError2> : OneOfBase<TValue, TError1, TError2>
{
}

it tries to create a file with name <global namespace>_Result2.g.cs

romfir avatar Aug 25 '23 01:08 romfir