Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 13, 2025

The logging source generator was incorrectly referencing System.Diagnostics.CodeAnalysis.FormattableString.Invariant() instead of System.FormattableString.Invariant(), causing CS0234 compilation errors when using the generator with multiple parameters.

The issue occurs in LoggerMessageGenerator.Emitter.cs line 180 where the generated code attempts to call:

"global::System.Diagnostics.CodeAnalysis.FormattableString.Invariant("

However, FormattableString is located in the System namespace, not System.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:

error CS0234: The type or namespace name 'FormattableString' does not exist in the namespace 'System.Diagnostics.CodeAnalysis'

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.

Copilot AI changed the title [WIP] [10.0-preview.7] Cannot compile using logging source generator due to CS0234 errors Fix logging source generator CS0234 error with FormattableString namespace Aug 13, 2025
Copilot AI requested a review from stephentoub August 13, 2025 17:30
Copilot finished work on behalf of stephentoub August 13, 2025 17:30
@stephentoub
Copy link
Member

@tarekgh, it looks like we only run the logging generator tests on .NET Core?

@stephentoub stephentoub marked this pull request as ready for review August 13, 2025 17:38
@Copilot Copilot AI review requested due to automatic review settings August 13, 2025 17:38
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.FormattableString to System.FormattableString
  • Resolves compilation failures when using logging source generator with multi-parameter templates
  • Applies minimal fix to maintain existing functionality

@stephentoub stephentoub added this to the 10.0.0 milestone Aug 13, 2025
@stephentoub stephentoub requested a review from tarekgh August 13, 2025 17:38
@stephentoub stephentoub enabled auto-merge (squash) August 13, 2025 17:39
@tarekgh
Copy link
Member

tarekgh commented Aug 13, 2025

it looks like we only run the logging generator tests on .NET Core?

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

@tarekgh
Copy link
Member

tarekgh commented Aug 13, 2025

@copilot can you add a test to force the generator to generate the code in the changed line?

@stephentoub
Copy link
Member

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.

@tarekgh
Copy link
Member

tarekgh commented Aug 13, 2025

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 _hasStringCreate. Let's finish this PR and then we can enable the netfx test run in another PR.

@tarekgh
Copy link
Member

tarekgh commented Aug 13, 2025

@copilot please ignore my previous ask: can you add a test to force the generator to generate the code in the changed line?.

auto-merge was automatically disabled August 13, 2025 18:40

Head branch was pushed to by a user without write access

Copy link
Contributor Author

Copilot AI commented Aug 13, 2025

@copilot can you add a test to force the generator to generate the code in the changed line?

Added test TestFormattableStringInvariantCodeGeneration that verifies the FormattableString namespace fix. The test ensures no CS0234 errors occur and validates that if FormattableString.Invariant is used, it references the correct namespace (global::System.FormattableString.Invariant instead of the incorrect global::System.Diagnostics.CodeAnalysis.FormattableString.Invariant). Commit: 6e080ff

Copilot AI requested a review from tarekgh August 13, 2025 18:41
Copilot finished work on behalf of tarekgh August 13, 2025 18:41
@tarekgh
Copy link
Member

tarekgh commented Aug 13, 2025

@stephentoub should we close this PR as we have #118691 now?

Copilot finished work on behalf of tarekgh August 13, 2025 19:01
@stephentoub
Copy link
Member

@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.

@stephentoub
Copy link
Member

Will merge #118691 instead

@stephentoub stephentoub deleted the copilot/fix-118686 branch August 14, 2025 17:33
@github-actions github-actions bot locked and limited conversation to collaborators Sep 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[10.0-preview.7] Cannot compile using logging source generator due to CS0234 errors

3 participants