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

Source generator files cannot specify encoding, verification fails due to encoding #1194

Open
kzu opened this issue Nov 23, 2024 · 1 comment · May be fixed by #1195
Open

Source generator files cannot specify encoding, verification fails due to encoding #1194

kzu opened this issue Nov 23, 2024 · 1 comment · May be fixed by #1195

Comments

@kzu
Copy link

kzu commented Nov 23, 2024

File contents are indeed the same, but I get:

   System.InvalidOperationException : Context: Source generator application
   Context: Verifying source generated files
   encoding of 'StructId.Analyzer\StructId.ParsableGenerator\UserId.parsable.cs' was expected to be '' but was 'utf-8'

This happens regardless of how the generator adds the file (as string content, as SourceText, with or without encoding).
Since the test set up cannot specify the encoding of the generated files, it seems it's impossible to have them match:

        var test = new CSharpSourceGeneratorTest<ParsableGenerator, DefaultVerifier>
        {
            ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
            TestState =
            {
                Sources =
                {
                    """
                    // ...
                    """,
                },
                GeneratedSources =                 
                {
                    (@"StructId.Analyzer\StructId.ParsableGenerator\UserId.parsable.cs", 
                    // 👇 how to specify encoding here??
                    "// expected file contents"
                },
            },
        };

        await test.RunAsync();

It's weird that two overloads that only differ in passing the generator type (or not), would default to different encodings:

https://github.com/dotnet/roslyn-sdk/blob/main/src/Microsoft.CodeAnalysis.Testing/Microsoft.CodeAnalysis.Analyzer.Testing/SourceFileCollection.cs#L16-L25

kzu added a commit to kzu/roslyn-sdk that referenced this issue Nov 23, 2024
@kzu kzu linked a pull request Nov 23, 2024 that will close this issue
kzu added a commit to kzu/roslyn-sdk that referenced this issue Nov 23, 2024
@Youssef1313
Copy link
Member

@kzu You can already specify the encoding:

GeneratedSources =
{
    (@"StructId.Analyzer\StructId.ParsableGenerator\UserId.parsable.cs", 
    SourceText.From("// expected file contents", encoding))
}

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

Successfully merging a pull request may close this issue.

2 participants