roslyn: WRN_GeneratorFailedDuringGeneration isn't helpful without a StackTrace
I currently have a source generator that fails with:
CSC : warning CS8785: Generator 'MySourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentOutOfRangeException' with message 'Specified argument was out of the range of valid values.'
I think providing a StackTrace will be very useful.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 8
- Comments: 15 (14 by maintainers)
Workaround
It turns out the exception is printed in the “description” field of the diagnostic. This field isn’t printed as part of a normal build, but if you enable SARIF output it will be included in that. You can add the following command line argument to your build:
The output file compiler-diagnostics.sarif will contain the text “Generator threw the following exception” followed by the exception and stack trace.
Goal
Provide a single strategy for viewing details of exceptions that occur in compiler extensibility points. Currently some messages show only in the SARIF output, while others show in both a binlog output and the SARIF output. We could resolve this by making SARIF output the primary investigation strategy, or by modifying the code to include more details in the binlog output. Specifics for each error output are given below.
References
Here’s the code where the diagnostic is produced for source generators. This output places the exception stack in the description, which only shows up in the SARIF file.
https://github.com/dotnet/roslyn/blob/dc3d0694a4b31b8e27038431888cd4e8dd5b6cb6/src/Compilers/Core/Portable/SourceGeneration/GeneratorDriver.cs#L342-L365
Here’s the code where the diagnostic is produced for analyzers. This places the exception stack in the message, which shows up in the SARIF file as well as the binlog (and higher build output verbosity configurations). It also places the exception stack in the description, so the SARIF file will show both copies.
https://github.com/dotnet/roslyn/blob/main/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs#L1226-L1236
Here’s the code where the diagnostic is produced for the analyzer driver. The exception stack is only placed in the description, so this situation behaves like source generator exceptions.
https://github.com/dotnet/roslyn/blob/dc3d0694a4b31b8e27038431888cd4e8dd5b6cb6/src/Compilers/Core/Portable/DiagnosticAnalyzer/AnalyzerExecutor.cs#L1276-L1284
@xakep139 We don’t currently have the resources to work on resolving this, but if someone submitted a pull request to implement the behavior described in https://github.com/dotnet/roslyn/issues/48008#issuecomment-1341131212 it would significantly improve the timeline for delivering this fix. 😄