Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Enhance Walkthrough Troubleshooting Tipps #186

@ZeeOcho

Description

@ZeeOcho

I would like to propose to add some hints to the walkthrough that may make setup and troubleshooting easier. They are the result of me implementing my first code generator, so this can be considered user feedback :). I should note that I did not have any previous experience in Roslyn based code generation, so some of this might be obvious for people that used it before.

Following the example Code Generator was quite easy for me (I needed to do quite some updating of VS and Windows, but I got it running quite straight forward). I had some errors in my own implementation of my code generator and wasn't quite sure how to troubleshoot them. The following bits of information would have probably helped me with that.

  1. Each source file containing code generation attributes will result in corresponding .generated.cs - files in the /obj/ - Directory of the project. They contain the code generated from your generator.

  2. If your code generator throws an exception in the ICodeGenerator.GenerateAsync or IRichCodeGenerator.GenerateRichAsync, it will result in 2 errors in the build process. One is containing the type and text of the exception, the other will say (somewhat obscure) "MSB3073 The command "dotnet codegen "@obj\Debug\netstandard2.0\CodeGenTest1.csproj.dotnet-codegen.rsp"" exited with code 3 beendet." (translated from german, so the text might not be accurate.
    image

  3. You can output messages to the build log:

3.1 You can output messages to the build log by using the IProgress<Diagnostic> progress parameter, like this:

progress.Report(Diagnostic.Create(
    new DiagnosticDescriptor("CA1001", "Cannot apply attribute to non-partial class", "Make class partial", "Microsoft.Design", DiagnosticSeverity.Error, true)
    , Location.Create(applyToClass.SyntaxTree, applyToClass.Span)));

3.2 Or use the CodeGeneration.Roslyn.Logger extension methods like this:

Logger.Error("This message comes from Logger extension", "SOMECODE");

Giving you the following build log in VS:
image

  1. If you Console.WriteLine in your code generator, it will show up in your build output window in VS.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions