Roslyn: Useless error message for failed source generator

Created on 17 Jul 2020  路  5Comments  路  Source: dotnet/roslyn

Version Used: dotnet sdk 5.0.100-preview.6.20318.15

Steps to Reproduce:

I'm running a project referencing a source generator on the command line via dotnet build.

It displays the warning:

CSC : warning CS8785: Generator 'ContainerSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result.

But does not explain why it failed to generate source.

Area-Compilers New Feature - Source Generators

Most helpful comment

This was implemented as part of https://github.com/dotnet/roslyn/pull/46804

All 5 comments

It appears like there is a catch all in GeneratorDriver which produces this diagnostic:

https://github.com/dotnet/roslyn/blob/ee23aef2b0443df5f7f187ff59907df1a3618ee4/src/Compilers/Core/Portable/SourceGeneration/GeneratorDriver.cs#L114-L117

This should probably log the exception that was thrown @chsienki?

Yeah, its on the board to fix :) https://github.com/dotnet/roslyn/projects/54#card-41652943

Great!

Can I just suggest that this should be really high priority? It's really hard to develop a source generator when you cant even tell what went wrong :-)

For now I'm doing the following as a workaround:

        public void Execute(SourceGeneratorContext context)
        {
            try
            {
                ExecuteInternal(context);
            }
            catch (Exception e)
            {
                //This is temporary till https://github.com/dotnet/roslyn/issues/46084 is fixed
                context.ReportDiagnostic(Diagnostic.Create(
                    new DiagnosticDescriptor(
                        "SI0000",
                        "An exception was thrown by the StrongInject generator",
                        "An exception was thrown by the StrongInject generator: '{0}'",
                        "StrongInject",
                        DiagnosticSeverity.Error,
                        isEnabledByDefault: true),
                    Location.None,
                    e.ToString()));
            }
        }

        //By not inlining we make sure we can catch assembly loading errors when jitting this method
        [MethodImpl(MethodImplOptions.NoInlining)]
        private void ExecuteInternal(SourceGeneratorContext context)
        {
            ...
        }

This was implemented as part of https://github.com/dotnet/roslyn/pull/46804

Was this page helpful?
0 / 5 - 0 ratings