dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.71k stars 3.98k forks source link

Source Generators Not Generating Sources #49249

Open daemogar opened 3 years ago

daemogar commented 3 years ago

I am having trouble getting the source generators to run/work. I am not sure what is not working. I have been working on this off and on for several days, and then today when I open the solution it just had worked at some point. Not sure if it was while opening the project or what caused the success. I went and added a new file to create a new source generator and then nothing. Now when I build the project, I get the following warning when building:

warning CS8032: An instance of analyzer SampleSourceGenerators.HelloWorldGenerator cannot be created from C:\Users\daemogar\Source\Repos\BlazorSamples\SampleSourceGenerators\bin\Debug\net5.0\SampleSourceGenerators.dll : Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

I have added this project to my sample Blazor app github repo so the issue can be easily duplicated. Sample Source Generator Project

I created this source generator project, added it to my solution, and modified the associated SampleSourceGenerators.csproj file to be as shown below. I also added the sample HelloWorldGenerator.cs by copy/paste from the online examples. I then went to the project where I wanted to use the generated source and added the modification shown below to the Website.Server.csproj.

I initially was using VSCode 1.51.0, but with source generators, I switched over to using Visual Studio 2019 Preview 6 since I am not aware of source generators working in VSCode yet. I am also using the .NET SDK framework 5.0.100-rc.2.20479.15.

Build Output Window

Build started...
1>------ Build started: Project: SampleSourceGenerators, Configuration: Debug Any CPU ------
1>You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
1>SampleSourceGenerators -> C:\Users\daemogar\Source\Repos\BlazorSamples\SampleSourceGenerators\bin\Debug\net5.0\SampleSourceGenerators.dll
2>------ Build started: Project: WebSite.Server, Configuration: Debug Any CPU ------
2>CSC : warning CS8032: An instance of analyzer SampleSourceGenerators.HelloWorldGenerator cannot be created from C:\Users\daemogar\Source\Repos\BlazorSamples\SampleSourceGenerators\bin\Debug\net5.0\SampleSourceGenerators.dll : Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified..
2>CSC : warning CS8032: An instance of analyzer SampleSourceGenerators.HelloWorldGenerator cannot be created from C:\Users\daemogar\Source\Repos\BlazorSamples\SampleSourceGenerators\bin\Debug\net5.0\SampleSourceGenerators.dll : Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified..
2>WebSite.Server -> C:\Users\daemogar\Source\Repos\BlazorSamples\SampleHostedApplication\WebSite\Server\bin\Debug\net5.0\WebSite.Server.dll
2>WebSite.Server -> C:\Users\daemogar\Source\Repos\BlazorSamples\SampleHostedApplication\WebSite\Server\bin\Debug\net5.0\WebSite.Server.Views.dll
2>Done building project "WebSite.Server.csproj".
========== Build: 2 succeeded, 0 failed, 3 up-to-date, 0 skipped ==========

SampleSourceGenerators/SampleSourceGenerators.csproj

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
        <LangVersion>preview</LangVersion>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.8.0-5.final" PrivateAssets="all" />
        <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.1" PrivateAssets="all" />
    </ItemGroup>

</Project>

SampleHostedApplication/Website/Server/Website.Server.csproj

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

dotnet --info

.NET SDK (reflecting any global.json):
 Version:   5.0.100-rc.2.20479.15
 Commit:    da7dfa8840

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18363
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\5.0.100-rc.2.20479.15\

Host (useful for support):
  Version: 5.0.0-rc.2.20475.5
  Commit:  c5a3f49c88

.NET SDKs installed:
  1.0.0-preview1-002702 [C:\Program Files\dotnet\sdk]
  1.0.0-preview2-003121 [C:\Program Files\dotnet\sdk]
  1.0.0-preview2-003131 [C:\Program Files\dotnet\sdk]
  1.0.1 [C:\Program Files\dotnet\sdk]
  1.0.2 [C:\Program Files\dotnet\sdk]
  1.0.4 [C:\Program Files\dotnet\sdk]
  1.1.0 [C:\Program Files\dotnet\sdk]
  2.0.2 [C:\Program Files\dotnet\sdk]
  2.0.3 [C:\Program Files\dotnet\sdk]
  2.1.2 [C:\Program Files\dotnet\sdk]
  2.1.4 [C:\Program Files\dotnet\sdk]
  2.1.101 [C:\Program Files\dotnet\sdk]
  2.1.103 [C:\Program Files\dotnet\sdk]
  2.1.104 [C:\Program Files\dotnet\sdk]
  2.1.200 [C:\Program Files\dotnet\sdk]
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.202 [C:\Program Files\dotnet\sdk]
  2.1.400 [C:\Program Files\dotnet\sdk]
  2.1.401 [C:\Program Files\dotnet\sdk]
  2.1.402 [C:\Program Files\dotnet\sdk]
  2.1.403 [C:\Program Files\dotnet\sdk]
  2.1.500 [C:\Program Files\dotnet\sdk]
  2.1.502 [C:\Program Files\dotnet\sdk]
  2.1.503 [C:\Program Files\dotnet\sdk]
  2.1.504 [C:\Program Files\dotnet\sdk]
  2.1.505 [C:\Program Files\dotnet\sdk]
  2.1.507 [C:\Program Files\dotnet\sdk]
  2.1.508 [C:\Program Files\dotnet\sdk]
  2.1.509 [C:\Program Files\dotnet\sdk]
  2.1.512 [C:\Program Files\dotnet\sdk]
  2.1.514 [C:\Program Files\dotnet\sdk]
  2.1.517 [C:\Program Files\dotnet\sdk]
  2.1.602 [C:\Program Files\dotnet\sdk]
  2.1.604 [C:\Program Files\dotnet\sdk]
  2.1.700 [C:\Program Files\dotnet\sdk]
  2.1.801 [C:\Program Files\dotnet\sdk]
  3.1.300 [C:\Program Files\dotnet\sdk]
  3.1.302 [C:\Program Files\dotnet\sdk]
  3.1.402 [C:\Program Files\dotnet\sdk]
  5.0.100-preview.8.20417.9 [C:\Program Files\dotnet\sdk]
  5.0.100-rc.1.20452.10 [C:\Program Files\dotnet\sdk]
  5.0.100-rc.2.20479.15 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-preview.8.20414.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-rc.1.20451.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.0-rc.2.20475.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.0-rc2-3002702 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.3-servicing-26724-03 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.18 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-preview.8.20407.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-rc.1.20451.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.0-rc.2.20475.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.8 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-preview.8.20411.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-rc.1.20452.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.0-rc.2.20475.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
sharwell commented 3 years ago

<TargetFramework>net5.0</TargetFramework>

Source generators must target netstandard2.0.

sharwell commented 3 years ago

@jasonmalinowski @chsienki this seems like a case where the wrong error is getting reported for an incorrectly targeted source generator.

daemogar commented 3 years ago

The project has been updated to use netstandard2.0 and the error is gone; however, the generated source does not appear to be able, nor do I see it in the obj/bin folders (when I used net5.0 I saw the generated file in the obj folder).

daemogar commented 3 years ago

I did more testing last night, and it appears that the sources are being generated (basically). Changes or new sources are not available until closing and reopening Visual Studio.

Once restarted, I have access to the generated source like in all the samples. I can [control]-[click] on it and it takes me to the generated source and I can see it even after making a change to the generator, it appears to update (most of the time, it is not reliable). That being said, it still requires me to relaunch Visual Studio to use the latest source.

Example Experience/Scenario

I have a generator that makes a class SampleClass with a static method SampleMethod() and all it does is Debug.WriteLine("test"); It never shows up in Visual Studio but I can tell it generates cause I will see issues in the output window. I have to clean the solution and build the project, close and reopen Visual Studio, and most of the time, the code is available. Now, I can use it in my project. All basically well. I decide to change the text "test" to "something else". I cannot tell if it worked or not, but clean and build again just in case (I have also tried skipping this step with the same results). When I [control]-[click] on the method where I use it, it shows the change, but when I run the code, it has not changed; however, when I restart Visual Studio it works just fine (the change is realized).

jasonmalinowski commented 3 years ago

Source generators must target netstandard2.0.

So we outright blocked things targeting the .NET Framework; @chsienki I believe the block doesn't block .NET Core referencing ones, albeit it won't really work in VS for Windows?

I am not aware of source generators working in VSCode yet.

The experience is a bit rougher there, but as long as you have the latest C# Extension installed you should have basics working fine.

That being said, it still requires me to relaunch Visual Studio to use the latest source.

Yeah, the one limitation we have in VS due to .NET limitations is once we've loaded your generator into our process, we don't have a way to unload it at this point. #48083 is another example of that too.

pr8x commented 3 years ago

Experiencing the same problem. The source generator is definitely executed (verified with Debugger.Launch()), but the file is never generated. I am using the hello world example from here. Even searched the entire disk but couldn't find a file generated with the hint I gave it. Is there anything I am missing?

Visual Studio: Version 16.8.3 SDK: 5.0.101

lidgren commented 3 years ago

+1 I am also unable to generate any content after upgrading to latest visual studio.

mstancombe commented 3 years ago

So we outright blocked things targeting the .NET Framework; @chsienki I believe the block doesn't block .NET Core referencing ones, albeit it won't really work in VS for Windows?

@jasonmalinowski Will support for projects targeting .net 5 be coming in the future?

jasonmalinowski commented 3 years ago

@mstancombe just to make it clear (since your question can be read two ways), a generator can run in a project targeting any framework. The generator itself must target netstandard2.0. The problem with moving that forward is generators today run in places that aren't .NET Core/.NET 5: they run in Visual Studio itself (which is still a .NET Framework app as of this writing), and other places. Do you have need for the generator to target something newer?

mstancombe commented 3 years ago

@jasonmalinowski I guess our issue was we wanted to use the source generator to use some classes in another internal library that was targetting net5, not .net standard, and as the source generator has to target netstandard 2.0, then the other internal library now has to target netstandard 2.0, which sets off a chain of retargetting.
I can see why you need to use netstandard 2.0 if they are to work in vs2019 as well as vscode. It was just a bit suprising that it didn't work. Thanks for clarifying why it targets net standard 2.0, and hopefully that clarifies why we wanted to target it to net5 :)

AraHaan commented 3 years ago

My source generator seems to have the problem despite calling ->

var generated = string.Format(
    CultureInfo.InvariantCulture,
    "// <autogenerated/>{0}{1}{0}{0}[assembly: GitInformationAttribute(\"{2}\", \"{3}\", \"{4}\", typeof({5}{6}))]{0}",
    Environment.NewLine,
    splittedLen > 0 && !string.Equals(
        gitinformationNamespace,
        usingStr.ToString(),
        StringComparison.Ordinal) ? $"using {usingStr};{Environment.NewLine}using {gitinformationNamespace};" : $"using {gitinformationNamespace};",
    this.RunGit("describe --all --always --dirty", Directory.GetParent(gitBuildInfoJsonFile.Path).FullName),
    this.RunGit("rev-parse --short HEAD", Directory.GetParent(gitBuildInfoJsonFile.Path).FullName),
    this.RunGit("name-rev --name-only HEAD", Directory.GetParent(gitBuildInfoJsonFile.Path).FullName),
    splittedLen > 0 ? splitted[splittedLen - 1] : options!.AssemblyType,
    options!.IsGeneric ? "<>" : string.Empty);
context.AddSource("GitAssemblyInfo.g.cs", SourceText.From(generated, Encoding.UTF8));

Yes I use manual string because I do not know how (other than UsingDelclarations for adding usings programmically using roslyn code instead of doing strings like this) to add attributes to be applied assembly level itself.

Yes I should use _ = context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.projectdir", out var projectdir); instead of Directory.GetParent(gitBuildInfoJsonFile.Path).FullName to get the value from the ProjectDir msbuild property.

A way to somehow fix this would be appreciated, for some reason cswin32 displays them but for me vs2019 16.10 preview says my source generator is not generating files.

And yes it uses System.Diagnostics.Process to run those git commands.

sharwell commented 3 years ago

@AraHaan source generators must not perform file I/O, either directly (e.g. File.Read) or indirectly (e.g. through git). If your generator has any I/O, the resulting behavior is undefined.

A way to somehow fix this would be appreciated

You can use Nerdbank.GitVersioning to inject Git properties as build properties, that can then be declared as compiler-visible properties accessed through GlobalOptions. It is unlikely that it will ever be possible to invoke Git directly from a source generator.

AraHaan commented 3 years ago

@AraHaan source generators must not perform file I/O, either directly (e.g. File.Read) or indirectly (e.g. through git). If your generator has any I/O, the resulting behavior is undefined.

A way to somehow fix this would be appreciated

You can use Nerdbank.GitVersioning to inject Git properties as build properties, that can then be declared as compiler-visible properties accessed through GlobalOptions. It is unlikely that it will ever be possible to invoke Git directly from a source generator.

And it exposes the results of what the git describe --all --always --dirty (returns heads/(branch or tag name)(-dirty if it is dirty), git rev-parse --short HEAD (short 7 character commit hash), and git name-rev --name-only HEAD (only the tag or branch name that is being compiled for only if the repository was cloned) commands do as msbuild properties?

I guess I could use an inline build task to expose them as an msbuild property and then access it with the source generator if I absolutely needed to.

sharwell commented 3 years ago

And it exposes the results of [commands] as msbuild properties?

It might not offer all of what you're after, but it probably does offer at least a subset, plus a solid example for how to implement any others you might need.

AraHaan commented 3 years ago

And it exposes the results of [commands] as msbuild properties?

It might not offer all of what you're after, but it probably does offer at least a subset, plus a solid example for how to implement any others you might need.

Alright removed all I/O and visual studio still says that it does not produce files even though I call context.AddSource in the code 🤔.

Edit: restarting VS2019 oddly made it show them up.

spydacarnage commented 3 years ago

Edit: restarting VS2019 oddly made it show them up.

I believe that due to the way source generators are loaded, the view in Solution Explorer, as well as any Intellisense, etc., is set when VS loads. You need to restart it to change that.

canton7 commented 3 years ago

I believe that due to the way source generators are loaded, the view in Solution Explorer, as well as any Intellisense, etc., is set when VS loads. You need to restart it to change that.

See #48083

hammypants commented 2 years ago

@AraHaan source generators must not perform file I/O, either directly (e.g. File.Read) or indirectly (e.g. through git). If your generator has any I/O, the resulting behavior is undefined.

???

https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.cookbook.md#additional-file-transformation

CoenraadS commented 2 years ago

I am experiencing this also, although my code-generator is already targeting 2.0:

Target project:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Attributes\Attributes.csproj" />
    <ProjectReference Include="..\CodeGen\CodeGen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>

</Project>

CodeGenProject:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
    <PackageReference Include="Microsoft.CodeAnalysis" Version="3.11.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2"/>
    <ProjectReference Include="..\Attributes\Attributes.csproj" />
  </ItemGroup>

</Project>

CSC : warning CS8032: An instance of analyzer CodeGen.Class1 cannot be created from C:\Users\Coenraad\source\repos\ConsoleApp1\CodeGen\bin\Debug\netstandard2.0\CodeGen.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Microsoft Visual Studio Community 2019
Version 16.10.4
VisualStudio.16.Release/16.10.4+31515.178
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Community

After I installed Visual Studio 2022 it works. I think it's related to the SDK version installed, it uses dlls from their (GAC) and not whatever is defined in the project. Very confusing.

ChrML commented 2 years ago

Is there a path towards allowing Source Generators to be written in .NET5/.NET6? .NETStandard 2.0 seems kinda obsolete for new code.

AraHaan commented 2 years ago

Not yet. Perhaps in 2 more years of waiting it can be done.

FrankSzendzielarz commented 2 years ago

I am experiencing this also, although my code-generator is already targeting 2.0:

Target project:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\Attributes\Attributes.csproj" />
    <ProjectReference Include="..\CodeGen\CodeGen.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>

</Project>

CodeGenProject:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" />
    <PackageReference Include="Microsoft.CodeAnalysis" Version="3.11.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.2"/>
    <ProjectReference Include="..\Attributes\Attributes.csproj" />
  </ItemGroup>

</Project>

CSC : warning CS8032: An instance of analyzer CodeGen.Class1 cannot be created from C:\Users\Coenraad\source\repos\ConsoleApp1\CodeGen\bin\Debug\netstandard2.0\CodeGen.dll : Could not load file or assembly 'Microsoft.CodeAnalysis, Version=3.11.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.

Microsoft Visual Studio Community 2019
Version 16.10.4
VisualStudio.16.Release/16.10.4+31515.178
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Community

After I installed Visual Studio 2022 it works. I think it's related to the SDK version installed, it uses dlls from their (GAC) and not whatever is defined in the project. Very confusing.

Noooooo! Dont tell me I have to install vs 2k22 to get past this? Battling with getting this working in 2k19 now for over a day and its flaky as fudge.

AraHaan commented 2 years ago

What, it should be working on 2019 as well.

FrankSzendzielarz commented 2 years ago

Tried everything including all the reference hacks recommended by the cookbook and on s.o. etc. I either get warnings that the source generator is ignored because it cant find a dll, or it simply fails to Execute while running Initialize. I have exhausted all options i can find apart from merging my codegenerator and the assemblies it references into one assembly, downgrading the .net core sdk, or upgrading VS to 2022

erictuvesson commented 2 years ago

@FrankSzendzielarz Try to manually build (dotnet build) the project that is referencing the source generator. Visual Studio is terrible at reporting errors from the source generators and just causes an immediate crash in worst case scenarios.

FrankSzendzielarz commented 2 years ago

@FrankSzendzielarz Try to manually build (dotnet build) the project that is referencing the source generator. Visual Studio is terrible at reporting errors from the source generators and just causes an immediate crash in worst case scenarios.

Thanks mate i just installed 2k22 , before i got this message. Works fine now. Though it is easy to confuse things with project references: the source generator project also has an analyzer, and both the projevt referencing it and the project itself reference a common types project. With nuget it works fine. With project refs it is hard to get vs to behave

AraHaan commented 2 years ago

@FrankSzendzielarz Try to manually build (dotnet build) the project that is referencing the source generator. Visual Studio is terrible at reporting errors from the source generators and just causes an immediate crash in worst case scenarios.

Thanks mate i just installed 2k22 , before i got this message. Works fine now. Though it is easy to confuse things with project references: the source generator project also has an analyzer, and both the projevt referencing it and the project itself reference a common types project. With nuget it works fine. With project refs it is hard to get vs to behave

the way I would fix that issue is to have the generator itself generate those common types instead on top of what it normally generates if those common types are needed by the generated code.

FrankSzendzielarz commented 2 years ago

Thanks that's a good idea. Does that work for the analyzer? The input code needs to have those types in place for the CodeAnalyzer. How does that work out in terms of timing? In any case with Nuget packages things work ok.

vulegend commented 2 years ago

I am trying to set a very simple source generator in VS 2022. The main API is targeting net6. I have to restart every time to get the new code generated. I've tried multiple things and nothing works, it just forces me to restart VS every time i want the source generator to generate new code.

DanielBaumert commented 2 years ago

I am trying to set a very simple source generator in VS 2022. The main API is targeting net6. I have to restart every time to get the new code generated. I've tried multiple things and nothing works, it just forces me to restart VS every time i want the source generator to generate new code.

same problem.

vjppaz commented 2 years ago

I get the same issue, here is what's happening on my version:

versions:

Visual Studio 2019: doesn't work Visual Studio 2022: works fine (but needs to restart the Visual Studio everytime I do some changes on the code generator) Azure Pipeline windows-2019: doesn't work Azure Pipeline windows-2022: doesn't work

vjppaz commented 2 years ago

I get the same issue, here is what's happening on my version:

versions:

  • codeanalysis version 4.0.1
  • .net 5.0

Visual Studio 2019: doesn't work Visual Studio 2022: works fine (but needs to restart the Visual Studio everytime I do some changes on the code generator) Azure Pipeline windows-2019: doesn't work Azure Pipeline windows-2022: doesn't work

I need at least to make the Azure Pipeline windows-2022 work and I'll be fine with it, If anyone can help me it would be great

FrankSzendzielarz commented 2 years ago

What works for me is that my source generator is in a separate project. The code references that project as a nuget, for other reasons, with a 'highest version' selector in the reference. I make changes in the analyzer generator project and update the package version. In the relying project i hit Rebuild. I use debugger.launch in the source generator to invoke debugger

vjppaz commented 2 years ago

UPDATE: after downgrading the Microsoft.CodeAnalysis.CSharp and Microsoft.CodeAnalysis nuget of my CodeGenerator project both VS 2019 and VS 2022 now working fine.

Drenalol commented 2 years ago

Microsoft.CodeAnalysis.CSharp and Microsoft.CodeAnalysis

Which versions?

FrankSzendzielarz commented 2 years ago

Mine is working fine with 4.0.1 and a fresh 2022 install, well, as fine and stable as I can figure out getting it with my current setup.

Maybe you need to update your .NET Core installation and SDK

SteveCollins commented 2 years ago

Same issue with having to rebuild and restart every time. I'm on VS 2022 Enterprise latest and 4.0.1 of CodeAnalysis and 3.3.3 of Analyzers

TheCakeMonster commented 2 years ago

Can one of the PMs who is monitoring this issue please up it in the priority list? We have a lot of developers who are now in an awful mess trying to use source generators and completely failing to be able to use it. We developers have customers who depend on us, and we can't deliver.

I have 3 versions of Visual Studio installed and none of them allow me to do any debugging of changes I am trying to make to a source generator. Despite a whole day of trying, I find a 8 line code change to be impossible.

As a priority, can one of the developers who knows source generators intimately, please help us work out some way - any way - to get debugging information out of one?

Personally, I now wish I hadn't tried to use this feature. The user [developer] experience is, well, not as good as I am used to Microsoft trying to attain.

FrankSzendzielarz commented 2 years ago

Can one of the PMs who is monitoring this issue please up it in the priority list? We have a lot of developers who are now in an awful mess trying to use source generators and completely failing to be able to use it. We developers have customers who depend on us, and we can't deliver.

I have 3 versions of Visual Studio installed and none of them allow me to do any debugging of changes I am trying to make to a source generator. Despite a whole day of trying, I find a 8 line code change to be impossible.

As a priority, can one of the developers who knows source generators intimately, please help us work out some way - any way - to get debugging information out of one?

Personally, I now wish I hadn't tried to use this feature. The user [developer] experience is, well, not as good as I am used to Microsoft trying to attain.

Hey cakemonster, what d you mean dbg info? If you mean debug the generator i am able to use debugger.launch . using the technique i earlier mentiioned. Agree this whole area needs eyes on it.

TheCakeMonster commented 2 years ago

@FrankSzendzielarz Unfortunately Debugger.Launch isn't working for me in any of the 3 versions of VS I have installed - 2019, 2022 release and 2022 preview. I've also tried Debug.WriteLine and Console.WriteLine and nothing works. I'm in a proper pickle!

Debugger.Launch certainly used to work, and that's how I developed the original version of the generator. Now I have something that I have tried to launch and need to change, and I can't work out how to debug the new code I am trying to add, because something has changed and none of the VS versions allow step-through debugging.

Judging by the length of time this thread has been open, and the number of posts, I'm not alone. There is something very unreliable here, and we would really appreciate some eyes on it ASAP to help us out.

CyrusNajmabadi commented 2 years ago

@TheCakeMonster can you write unit tests for your cases and validate and fixup things that way? That is the most supported, most reliably, and most quick way to work in this space.

sharwell commented 2 years ago

@vulegend https://github.com/dotnet/roslyn/issues/49249#issuecomment-979991840

@DanielBaumert https://github.com/dotnet/roslyn/issues/49249#issuecomment-980046511

@SteveCollins https://github.com/dotnet/roslyn/issues/49249#issuecomment-988036785

All of these posts are covered by an unrelated issue: #48083

Since the current issue is getting long and covering multiple topics, I've hidden specific comments that are known to be tracked separately so we can focus on the others.

I have 3 versions of Visual Studio installed and none of them allow me to do any debugging of changes I am trying to make to a source generator. Despite a whole day of trying, I find a 8 line code change to be impossible.

Debugging unit tests is the way to debug source generators. Set a breakpoint, right click in Test Explorer → Debug Tests.

TheCakeMonster commented 2 years ago

@CyrusNajmabadi @sharwell Thanks, that's an interesting alternative. I feel a bit silly not having thought of it!

I'll look into how to do testing of the part that's causing me trouble. It's specifically the identification of classes to be targeted (and what attributes are applied) that I need to test, so I'll have to work out how to get that to work.

Thank you for the input.

AraHaan commented 2 years ago

I think there was a more supported way to debug source generators directly implemented in vs2019+.

FrankSzendzielarz commented 2 years ago

I have no issues debugging. The key is to hit Rebuild not Build and use Debugger.Launch. The code generator is in a separate project from the project where it is applied, and I use a nuget reference, with a latest package version wildcard. The code gen and analyzer project is updated with an increment to the package version every time i change something, which inthen Build. Then when I hit Rebuild on the project referencing the analyzer the debugger launches. Latest versions of everything including Core SDK

WeihanLi commented 2 years ago

You can use the Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing to debug your source generator

There're two samples:

AraHaan commented 2 years ago

Yep, but it seems the current version of that (on nuget.org) is 1.1.x or so.

sharwell commented 2 years ago

Yep, but it seems the current version of that (on nuget.org) is 1.1.x or so.

You can use this configuration: https://github.com/tunnelvisionlabs/language-types/blob/main/nuget.config

The daily builds are published to dotnet-tools.

AraHaan commented 2 years ago

Yep, but it seems the current version of that (on nuget.org) is 1.1.x or so.

You can use this configuration: https://github.com/tunnelvisionlabs/language-types/blob/main/nuget.config

The daily builds are published to dotnet-tools.

Thanks, also know of a way to manipulate being able to load the daily builds of the roslyn packages used for source generators so I can get started on making my generator incremental (as it seems that my 7.0.100-dev sdk requires roslyn 4.0.1 which does not seem to be ok for me)? So when trying to use my generator with those packages it would fail to load roslyn 4.2.0.0.

sharwell commented 2 years ago

You should be able to write an incremental analyzer using Microsoft.CodeAnalysis.Common (or Microsoft.CodeAnalysis.CSharp) version 4.0.1. I'm not aware of any current need for a source generator to reference newer than 4.0.1.