dotnet / roslyn-sdk

Roslyn-SDK templates and Syntax Visualizer
MIT License
517 stars 256 forks source link

Source Generators C# sample does not build #946

Closed isolin closed 2 years ago

isolin commented 2 years ago

I wanted to try the source generator samples from this repository, but following the instructions in the respective README I just get compilation errors.

Steps to reproduce:

  1. clone the CSharp/SourceGenerators folder
  2. cd to the GeneratedDemo folder
  3. dotnet run in CLI

The following errors are reported (multiple times):

error CS0246: The type or namespace name 'ISourceGenerator' could not be found
error CS0246: The type or namespace name 'GeneratorAttribute' could not be found 
error CS0246: The type or namespace name 'Generator' could not be found
error CS0246: The type or namespace name 'GeneratorExecutionContext' could not be found
error CS0246: The type or namespace name 'GeneratorInitializationContext' could not be found
error CS0246: The type or namespace name 'ISyntaxContextReceiver' could not be found
error CS0246: The type or namespace name 'GeneratorSyntaxContext' could not be found 

I am at Ubuntu 20.04, with 3.1.416 and 6.0.101 SDKs installed.

How can the errors be fixed?

UPDATE: To fix the issues above I needed to update the references section in SourceGeneratorSamples.csproj to

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.0.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" />
  </ItemGroup>

and add <LangVersion>10.0</LangVersion> to GeneratedDemo.csproj

CallumMcLoughlin commented 2 years ago

I can reproduce this issue by running through your steps (only taking the SourceGenerators folder) however the following steps work:

  1. Clone entire repo
  2. cd to SourceGenerators folder
  3. dotnet build
  4. cd to SourceGenerators/GeneratedDemo
  5. dotnet run

Considering this is how the README.md specifies how to run this sample i'm not so sure this is an actual issue with the samples.

sharwell commented 2 years ago

This is the expected behavior. The demo imports files from other locations in the repository, and will break if those files are missing.