-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logging Source Generator fails to compile due to CS0246 and CS0265 errors if type for generic constraint is in a different namespace #58550
Comments
Tagging subscribers to this area: @maryamariyan Issue DetailsDescriptionIf a containing class for a class used to provide the partial methods for the Logging Source Generator uses as class as a type constraint which is defined in another namespace, the project fails to compile with errors similar to the below.
If the type which is used for the type constraint of the class is in the same namespace, then project compiles and runs as expected. Minimal repro
|
Author: | martincostello |
---|---|
Assignees: | - |
Labels: |
|
Milestone: | - |
Do you really want to punt that to 7.0? Most, if not all, source gen errors for the JSON source generator were classified as blocking. Many of the source gen compiler errors are sufficiently obscure that without looking at the g.cs, they are not easy to understand. Having said that, if you can't fix it for 6.0 you want to make sure that you put that bug fairly prominently into the release notes/documentation including the workaround by @martincostello |
For what it's worth, I found this in a library code base converting the logging which was using the nested private Log class pattern to the source generators and hit this on three classes, two of which were abstract. I went with non-abstract for writing up the repro as it was easier to follow. |
Another workaround is to use full qualified name of class in another namespace, that is being used as constraint, e.g. // code
public partial class MessagePrinter<T> where T : Repro.AnotherNamespace.Message
{}
// code |
Running into this as well trying to convert lots of my code to use this.. happens in 5 files for my MediatR extensions. Example: using MediatR;
using Microsoft.Extensions.Logging;
namespace LoggingGeneratorErrorExample;
internal partial class LoggingNotificationHandlerWrapper<TInner, TNotification> : INotificationHandler<TNotification>
where TNotification : INotification
where TInner : INotificationHandler<TNotification>
{
private readonly ILogger<TInner> logger = null!;
public Task Handle(TNotification notification, CancellationToken cancellationToken)
=> Task.CompletedTask;
[LoggerMessage(Level = LogLevel.Debug, Message = "Test Message")]
private partial void LogNotification();
} <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>True</EmitCompilerGeneratedFiles>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MediatR" Version="9.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
</ItemGroup>
</Project> Fails with:
Generated code emitted by the generator:
|
Fwiw, I moved the conversion of my code base to the logger src gen to 7.0. I had way too many build errors for complex examples (mostly this case) that I stopped my attempts. |
I was just hoping this can be fixed sooner not just to get the feature working in non-trivial setups but also b/c the in-box generators are a good source of reference (and documentation.. especially for the newer Roslyn 4 incremental generator APIs) for other people writing generators |
Yeah I second that, but as there was no feedback from MS when I specifically asked if they want to punt that. There are quite a few logger src gen issues, which either point to missing test coverage, or more likely, that the developer experience is still bad (as the json src gen had similar problems). |
Thanks for the feedback. Will take a look and make improvements here. |
A couple small observations around possible fixes. I don't think partial files need to restate all the derived types and generic parameter constraints, so it may be possible to just omit things in the generated file. Another approach we've often taken in generated files is to fully qualify all type references, so if we aren't doing that already that might be another thing to consider. cc @layomia @stephentoub @elinor-fung who've built other generators and may have an opinion here or wish to address a similar bug in other generators. |
+1 to both @ericstj's suggestions. For this issue, I think the simplest and correct thing to do would be to just remove the constraints when emitting code for the parent classes and logger class. Line 81 in b8a8c7a
Line 95 in b8a8c7a
For the p/invoke generator, we don't have this specific problem with generic constraints (since DllImports can't be in generic types), but in general we try to both avoid unnecessarily re-specifying things on partial class/method definitions and use fully qualified names in the generated code. |
- Supports usage of "in" modifier - Improves support for generic constraints Fixes dotnet#58550, dotnet#62644
… / when dealing with constraints (dotnet#64593) * Fixes some logging source gen bugs: - Supports usage of "in" modifier - Improves support for generic constraints Fixes dotnet#58550, dotnet#62644 * Apply PR feedback * Add another test
Description
If a containing class for a class used to provide the partial methods for the Logging Source Generator uses as class as a type constraint which is defined in another namespace, the project fails to compile with errors similar to the below.
If the type which is used for the type constraint of the class is in the same namespace, then project compiles and runs as expected.
The compilation errors can be worked around by using global using statements, such as:
Minimal repro
Repro.csproj
Program.cs
Message.cs
MessagePrinter.cs
Expected Output
Repro> dotnet run Repro.Message info: Repro.MessagePrinter[1] The message is Hello.
Configuration
.NET SDK
6.0.100-rc.1.21424.38
Microsoft.Extensions.Logging
6.0.0-rc.1.21424.15
Regression?
No.
The text was updated successfully, but these errors were encountered: