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

Possible regression between Preview 8 and Release Candidate 1: Source Generators issue #13869

Closed
Ilia-Kosenkov opened this issue Sep 28, 2020 · 7 comments

Comments

@Ilia-Kosenkov
Copy link

Consider a simple two-project solution. One project is Source Generator with a single file:

Generator/HelloWorldGenerator.cs
 using System.Text;
 using Microsoft.CodeAnalysis;
 using Microsoft.CodeAnalysis.Text;

 namespace Generator
 {
    [Generator]
    public class HelloWorldGenerator : ISourceGenerator
    {
        public void Execute(SourceGeneratorContext context)
        {
            var sourceBuilder = new StringBuilder(@"

 using System;
 namespace HelloWorldGenerated
 {
    public static class HelloWorld
    {
        public static void SayHello() 
        {
            Console.WriteLine(""Hello from generated code!"");
            Console.WriteLine(""Listing syntax trees:"");
 ");

            var syntaxTrees = context.Compilation.SyntaxTrees;
            foreach (SyntaxTree tree in syntaxTrees)
            {
                sourceBuilder.AppendLine($@"Console.WriteLine(@"" - {tree.FilePath}"");");
            }
            sourceBuilder.Append(@"
        }
    }
 }");

            context.AddSource("helloWorldGenerator", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
        }

        public void Initialize(InitializationContext context)
        {
        }
        
    }
 }
Generator/Generator.csproj
<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>netstandard2.0</TargetFramework>
		<LangVersion>9</LangVersion>
		<Nullable>enable</Nullable>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.7.0" PrivateAssets="all" />
		<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.0" PrivateAssets="all" />
	</ItemGroup>

</Project>

Another project consumes the generated code:

Test/Program.cs
namespace Test
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            HelloWorldGenerated.HelloWorld.SayHello();
        }

    }

}

Test/Test.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
	  <LangVersion>9</LangVersion>
	  <Nullable>enable</Nullable>
	  <StartupObject>Test.Program</StartupObject>
  </PropertyGroup>

	<ItemGroup>
		<ProjectReference Include="..\Generator\Generator.csproj" 
                      OutputItemType ="Analyzer"
                      ReferenceOutputAssembly="false"/>
	</ItemGroup>
</Project>

It can be compiled using .NET 5.0.100-preview.8.20417.9, both on Windows 10 x64 and in WSL 1 of the same system (both Win 10 and WSL Ubuntu 18.04 have the same SDK version).
However, I have another machine which runs the same Windows 10 x64, but has .NET 5.0.100-rc.1.20452.10 both for Windows and for WSL 2 Ubuntu 18.04. I am unable to compile the same project neither using Win nor *nix version.
The error message provided is quite unusual:

CSC : warning CS8032: An instance of analyzer Generator.HelloWorldGenerator cannot be created from C:\Users\...\CSharp_9\Generator\bin\Debug\netstandard2.0\Generator.dll : Method 'Initialize' in type 'Generator.HelloWorldGenerator' from assembly 'Generator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.. [C:\Users\...\CSharp_9\Test\Test.csproj]

Method Initialize is one of the two methods of interface ISourceGenerator, and is trivially implemented in the example above. I inspected the produced .dll and found IL code for this method where it should be.

Provided both machines pull exactly the same referenced packages, I am unable to pinpoint any other reason for why the project fails to compile other than something in RC1 is different compared to Preview8.

@marcpopMSFT
Copy link
Member

@dsplaisted Mind taking a quick look if this is something we should investigate for GA?

@dsplaisted
Copy link
Member

Looks like ProduceReferenceAssembly may not be working correctly together with source generators. @jaredpar, can you route to someone who can look at the source generators side of this?

@jaredpar
Copy link
Member

jaredpar commented Oct 1, 2020

@chsienki

@chsienki
Copy link
Contributor

chsienki commented Oct 1, 2020

There was a breaking change to the generator implementation in the Roslyn version referenced between preview8 and RC1.

Please see the following guide to update to latest https://github.com/dotnet/roslyn/blob/master/docs/features/source-generators.cookbook.md#breaking-changes

@Ilia-Kosenkov
Copy link
Author

Hi @chsienki & others, thank you for your research on my issue. I, unfortunately, missed that breaking change in source generators.
I wonder why did this happen? Did I reference incorrect packages? Is the new ISourceGenerator available only in the preview version of the package?
Unfortunately, the error message is not very informative, and the signature of the Initialize method is not reported, so it is hard to spot the problem without a deep understanding of how source generators work.

@chsienki
Copy link
Contributor

chsienki commented Oct 1, 2020

Yes. The change occurred in Roslyn 3.8.0-3.final and the API is available going forward.

At this stage the generators feature is still pre-release, and we wanted to make this breaking change before stabilizing the API. We're not expecting to make any further changes once released, or going forward with the pre-release.

@chsienki
Copy link
Contributor

chsienki commented Oct 2, 2020

I've updated the cookbook to include some more information about the changes, and an example of the warning a user will see which should hopefully make it easier to diagnose in the future (and surface in search results).

I'm going to close this out as I believe everything is working as expected, but please open an issue on the Roslyn repo if you're still seeing issues with the new API.

@chsienki chsienki closed this as completed Oct 2, 2020
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

5 participants