dotnet / roslyn-sdk

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

Source Generator clarification #520

Open HurricanKai opened 4 years ago

HurricanKai commented 4 years ago

I'm trying to get Source Generators to work, and first followed the samples provided in this article, which simply didn't work, the analyzer assembly is found, but nothing is ever outputted, and the compilation fails where generated code is used. I've then tried to copy just this repos /samples/CSharp/SourceGenerators/ folder, which led to the same result. Only when cloning this entire repo does the sample (and any other code I throw into a generator) produce anything. Is there a minimal sample that builds on its own? Also, is the VS component ".NET Compiler Platform SDK" required for Source Generators? I didn't think so since it wasn't mentioned in the said article, but I'm considering everything now.

HurricanKai commented 4 years ago

So I found the minimal sample (it seems?) min.zip I'm not sure what's going on here, There's this special sdk <Import Project="Sdk.props" Sdk="Microsoft.DotNet.Arcade.Sdk" /> which seems to be imported from global.json using

  "msbuild-sdks": {
    "Microsoft.DotNet.Arcade.Sdk": "5.0.0-beta.20224.11"
  }

and there is this /eng/Versions.props file, which seems to be required by said special SDK it just contains

<?xml version="1.0" encoding="utf-8"?>
<Project>
  <PropertyGroup>
    <UsingToolMicrosoftNetCompilers>true</UsingToolMicrosoftNetCompilers>
  </PropertyGroup>
</Project>

removing the file, renaming the folder, even when moving the contents to a Directory.Build.props will fail the build. Also, all builds are automatically forwarded to /artifacts/ which I guess is also an import of that SDK. I'm guessing this special SDK is coming from dotnet/arcade which would make sense. But what about it makes everything work just fine? What part do I need (or do I need all of it?)

tpetrina commented 4 years ago

I have the same issue - it is impossible to build source generators outside of the roslyn-sdk repo. Just as you mentioned copying and pasting into a different folder doesn't work anymore. Apparently something is off with paths. Look at the example code in the linked article:

<!-- Add this as a new ItemGroup, replacing paths and names appropriately -->
<ItemGroup>
    <!-- Note that this is not a "normal" ProjectReference.
         It needs the additional 'OutputItemType' and 'ReferenceOutputAssmbly' attributes. -->
    <ProjectReference Include="path-to-sourcegenerator-project.csproj" 
                      OutputItemType="Analyzer"
                      ReferenceOutputAssembly="false" />
</ItemGroup>

However, the sample code has the following:

  <ItemGroup>
    <Analyzer Include="$(OutDir)\..\..\..\SourceGeneratorSamples\$(Configuration)\netstandard2.0\SourceGeneratorSamples.dll" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\SourceGeneratorSamples\SourceGeneratorSamples.csproj" />
  </ItemGroup>

The paths are wrong when you move this solution outside of the repo, it should be: $(OutDir)\..\..\..\..\SourceGeneratorSamples\bin\$(Configuration)\netstandard2.0\SourceGeneratorSamples.dll"

One extra .. and bin due to the output path usually being bin\Debug\netstandard2.0.

tpetrina commented 4 years ago

Note, I am running on macOS.

centreboard commented 4 years ago

Copying SourceGenerators to a new directory on Windows :heavy_check_mark: 3.1.300-preview-015135 dotnet build does work on a clean copy of SourceGenerators :x: 5.0.100-preview.3.20216.6 dotnet build fails. After the solution has been built either with msbuild from VS 16.6 preview 5 or dotnet build from 3.1.300-preview-015135 it will work while there are no file edits to force compilation.

edit: Corrected earlier statement - 5.0.100-preview.3.20216.6 will fail again once you edit a file