Unable to get Source Generator to do anything

Viewed 28

I've been wrestling with Source Generators but there's a lack of tutorials and information that are hurting.

I want to generate some C# classes from a database. Using a T4 template to do this is difficult and problematic, because of issues I'm having with using SQL in T4 templates and similar.

The description of Source Generator is that "The generator can create new C# source files on the fly that are added to the user's compilation. In this way, you have code that runs during compilation. It inspects your program to produce additional source files that are compiled together with the rest of your code." which seems to match what I want.

This seems significantly more promising.

I've created a project, and put a [Generator] into it using this tutorial.

Full source:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace x
{

    [Generator]
    internal class TestGenerator : ISourceGenerator
    {
        public void Execute(GeneratorExecutionContext context)
        {
            File.Create(@"C:\temp\ITRUNS.TXT");

            var sourceBuilder = new StringBuilder(@"
using System;
namespace HelloWorldGenerated
{
    public static class HelloWorld
    {
        public static void SayHello() 
        {
            Console.WriteLine(""Hello from generated code!"");
            Console.WriteLine(""The following syntax trees existed in the compilation that created this program:"");
");

            Debugger.Launch();
            // using the context, get a list of syntax trees in the users compilation
            var syntaxTrees = context.Compilation.SyntaxTrees;

            // add the filepath of each tree to the class we're building
            foreach (SyntaxTree tree in syntaxTrees)
            {
                sourceBuilder.AppendLine($@"Console.WriteLine(@"" - {tree.FilePath}"");");
            }

            // finish creating the source to inject
            sourceBuilder.Append(@"
        }
    }
}");

            // inject the created source into the users compilation
            context.AddSource("helloWorldGenerator", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
        }
    

        public void Initialize(GeneratorInitializationContext context)
        {

        }
    }
}

I want this to run when I build the solution and create some .cs files

However, it does not run. I put some code in the execute method to create a file, and it does not create the file.

The tutorial says:

  1. Add the source generator from a project as an analyzer and add preview to the LangVersion to the project file like this:

I don't really know what this means. Which project? I tried to download the samples as suggested, but I can't get them to build.

When I examine the code, it's quite hard to understand what they've done that is different.

And what they say in the tutorial about adding the analyzer, doesn't seem to present anywhere in the sample code!

I tried adding the project reference in the csproj that it said to add, however that didn't work - it did create an item within Analyzers but it just has a red - and says 'Ignored' on the tooltip.

I honestly don't know what else I can do to figure out how to get this to work.

I also don't know for sure if it will do what I want - autogenerate a bunch of .cs files with code in that I can use.

I'm going to give it one more go then I'll just write a console application to do it manually instead. Any ideas are welcome.

0 Answers
Related