Fix logging source generator CS0234 error with FormattableString namespace#118687
Fix logging source generator CS0234 error with FormattableString namespace#118687
Conversation
…space Co-authored-by: stephentoub <[email protected]>
|
@tarekgh, it looks like we only run the logging generator tests on .NET Core? |
There was a problem hiding this comment.
Pull Request Overview
The PR fixes a namespace error in the logging source generator where FormattableString.Invariant() was incorrectly referenced with the wrong namespace, causing CS0234 compilation errors when using multiple parameters in log templates.
- Corrects namespace reference from
System.Diagnostics.CodeAnalysis.FormattableStringtoSystem.FormattableString - Resolves compilation failures when using logging source generator with multi-parameter templates
- Applies minimal fix to maintain existing functionality
This was the case since Maryam introduced this test. I don't know why she restricted to netcore app only but it will be a good idea to enable netfx runs as we support it. CC @ericstj |
|
@copilot can you add a test to force the generator to generate the code in the changed line? |
I believe there already is one... but that will only happen on netfx not core, hence my checking tfms. |
I see it now under it is under the check |
|
@copilot please ignore my previous ask: |
Co-authored-by: tarekgh <[email protected]>
Head branch was pushed to by a user without write access
Added test |
|
@stephentoub should we close this PR as we have #118691 now? |
Ok. I was thinking we could get this in and do that separately, per your previous comment, but I'm fine just getting that all in at the same time. Up to you. |
|
Will merge #118691 instead |
The logging source generator was incorrectly referencing
System.Diagnostics.CodeAnalysis.FormattableString.Invariant()instead ofSystem.FormattableString.Invariant(), causing CS0234 compilation errors when using the generator with multiple parameters.The issue occurs in
LoggerMessageGenerator.Emitter.csline 180 where the generated code attempts to call:"global::System.Diagnostics.CodeAnalysis.FormattableString.Invariant("However,
FormattableStringis located in theSystemnamespace, notSystem.Diagnostics.CodeAnalysis. The correct reference should be:"global::System.FormattableString.Invariant("This error manifests when projects use the logging source generator with templates containing multiple parameters that trigger the FormattableString code path, resulting in compilation failures like:
The fix applies a minimal change to correct the namespace reference, resolving the compilation error while maintaining all existing functionality. A test has been added to verify the fix and ensure no CS0234 errors occur.
Fixes #118686.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.