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

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

Open
ADIX7 opened this issue May 19, 2023 · 1 comment

Comments

@ADIX7
Copy link

ADIX7 commented May 19, 2023

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.

@romfir
Copy link
Contributor

romfir commented Aug 25, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants