roslyn: CS8795 Showing When it Shouldn't
Not sure if this is the right repo if not let me know and I can move.
When using the new LoggerMessage
attribute we get errors all over the place even though this is working correctly. In case it’s relevant we use partial classes to keep the individual messages close to the code that actually uses them rather than having a single huge Log
class in one place.
There is also a similar/related report on Dev Communitey here: https://developercommunity.visualstudio.com/t/issue-when-using-loggermessage-and-static-extensio/1638197
Version:
VS2022 Current (17.0.5)
Steps to Reproduce:
Put this in a new console app and install the NuGet package Microsoft.Extensions.Logging
you should see the error CS8795 Partial method ‘Log.Example(ILogger)’ must have an implementation part because it has accessibility modifiers.
using Microsoft.Extensions.Logging;
internal static partial class Log
{
[LoggerMessage(
EventId = 0,
Level = LogLevel.Warning,
Message = "A message",
SkipEnabledCheck = true)]
public static partial void Example(this ILogger logger);
}
Expected Behavior:
There should be no error shown as this is valid code because the attribute generates the code.
Actual Behavior:
The error is shown.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 21 (11 by maintainers)
@stap123 This is probably caused by an exception in the generator (or the generator driver). Currently it’s not possible to see those exceptions (#54098), which makes it difficult to fix. The generator also doesn’t recover from those errors until you reload the project or restart the IDE (#58625).
@stap123 Awesome, thanks for following up!
@stap123 OK, thanks for the extra info. I’m going to close then as fixed by #58990 then. The reason I asked about the NuGet package addition specifically was because the bug was manifesting if source generators changed. When you restart VS everything gets loaded the first time and so we’ll have everything correct then.
The fix is on its way and targeting Preview 5, so hopefully you don’t see this after that. If you still are, please reactivate!
I’m guessing this is the same issue as: https://developercommunity.visualstudio.com/t/Code-generated-by-Source-Generator-is-ma/1636837
We don’t yet understand why this is happening, but we are able to reproduce it and it’s a high priority work item.