-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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 with partial methods with implicit ILogger definition when using partial classes #61547
Comments
Tagging subscribers to this area: @maryamariyan Issue DetailsDescriptionI found a bug that causes the error If you declare the ILogger field in a file containing part of your partial class (let's call the class
If I move the ILogger declaration from first physical file to the other containing log methods, it works. Reproduction StepsCreate a project that makes use of logging, create a Foo class like this, in two distinct files (probably even if you declare them in a single file should trigger the problem too)
If you move the declaration Expected behaviorSource Generator should find ILogger declaration in every partial class. If it isn't possible, the problem should be documented. Actual behaviorDescribed into "Reproduction Steps" Regression?No response Known WorkaroundsMoving the declaration from one physical file to the file containing logging method works, but if you define partial methods in multiple partial classes, the problem can't be solved and you can't use partial methods that implicitly get reference to the ILogger. Configuration.Net 6 Other informationNo response
|
@maryamariyan Hello I see you tagget this on 7.0.0 Also I've found other nuances that present the problem: when you inherit a class that defines a A simple repro code is this
If you try to compile it generates the error SYSLIB1019 |
Thanks for your feedback, we're gathering feedback from the logging source generator and would fix them early in the 7.0 cycle. Fixing this issue has priority as it is part of a newly released feature. 6.0 has already shipped that's why this is tagged as 7.0, unless we think this also needs to be serviced in 6.0. |
Probably we can ask someone from roslyn what's the best way to locate a field across type partial type definitions. Looks like the generator does: runtime/src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs Lines 525 to 555 in 24df302
|
I'd expect you want to search on the class symbol rather than the syntax - looks like you already have / use the semantic model to grab symbols at this point. With the symbol, you should be able to iterate through all the class members rather than just the ones on that part of the declaration. |
I'm no expert in source generators, but I had a stab at it: #71308 It fixes both scenarios mentioned here, and all existing tests are still green. |
This can be worked around by defining redundant |
That's a reasonable workaround, although I would guess analyzers, such as ReSharper would compain of 'field is assigned but never used'. Hopefully this PR will be approved soon and it'll make it into .NET 7 🤞 |
Description
I found a bug that causes the error
SYSLIB1019
when it shouldn't be triggered.If you declare the ILogger field in a file containing part of your partial class (let's call the class
Foo
)and you define the log methods that uses source generation logging in another physical file that's of course a partial class ofFoo
, the compiler complain with the errorSYSLIB1019 Couldn't find a field of type Microsoft.Extensions.Logging.ILogger in class Foo
If I move the ILogger declaration from first physical file to the other containing log methods, it works.
Reproduction Steps
Create a project that makes use of logging, create a Foo class like this, in two distinct files (probably even if you declare them in a single file should trigger the problem too)
If you move the declaration
ILogger _logger;
to the second partial class, it works.Expected behavior
Source Generator should find ILogger declaration in every partial class.
If it isn't possible, the problem should be documented.
Actual behavior
Described into "Reproduction Steps"
Regression?
No response
Known Workarounds
Moving the declaration from one physical file to the file containing logging method works, but if you define partial methods in multiple partial classes, the problem can't be solved and you can't use partial methods that implicitly get reference to the ILogger.
Configuration
.Net 6
Visual Studio 2022
Other information
Having to move ILogger definition, even if easy, isn't the best because when using dependency injection the workflow is usually to declare the constructor parameter you want to inject and then use helpers like VS suggestion action "create and assign field" that of course defines the filed in the class containing the constructor
The text was updated successfully, but these errors were encountered: