Skip to content

Commit 3b921ef

Browse files
[release/6.0] Fix Logging Source Generator to handle file-scoped namespaces (#57894) (#59100)
* Handle file-scoped namespaces (#57894) Handle namespace not being emitted when file-scoped namespaces are used for a log class. Relates to #57880. * Fix build issue Co-authored-by: Martin Costello <martin@martincostello.com>
1 parent 28d0c70 commit 3b921ef

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,21 @@ public IReadOnlyList<LoggerClass> GetLogClasses(IEnumerable<ClassDeclarationSynt
438438
{
439439
// determine the namespace the class is declared in, if any
440440
SyntaxNode? potentialNamespaceParent = classDec.Parent;
441-
while (potentialNamespaceParent != null && potentialNamespaceParent is not NamespaceDeclarationSyntax)
441+
while (potentialNamespaceParent != null &&
442+
potentialNamespaceParent is not NamespaceDeclarationSyntax
443+
#if ROSLYN4_0_OR_GREATER
444+
&& potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax
445+
#endif
446+
)
442447
{
443448
potentialNamespaceParent = potentialNamespaceParent.Parent;
444449
}
450+
451+
#if ROSLYN4_0_OR_GREATER
452+
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent)
453+
#else
445454
if (potentialNamespaceParent is NamespaceDeclarationSyntax namespaceParent)
455+
#endif
446456
{
447457
nspace = namespaceParent.Name.ToString();
448458
while (true)

src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorEmitterTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ internal class Class2 { }
147147
await VerifyAgainstBaselineUsingFile("TestWithNestedClass.generated.txt", testSourceCode);
148148
}
149149

150+
[Fact]
151+
public async Task TestBaseline_TestWithFileScopedNamespace_Success()
152+
{
153+
string testSourceCode = @"
154+
namespace Microsoft.Extensions.Logging.Generators.Tests.TestClasses;
155+
156+
internal static partial class TestWithDefaultValues
157+
{
158+
[LoggerMessage]
159+
public static partial void M0(ILogger logger, LogLevel level);
160+
}";
161+
await VerifyAgainstBaselineUsingFile("TestWithDefaultValues.generated.txt", testSourceCode);
162+
}
163+
150164
private async Task VerifyAgainstBaselineUsingFile(string filename, string testSourceCode)
151165
{
152166
string baseline = await File.ReadAllTextAsync(Path.Combine("Baselines", filename)).ConfigureAwait(false);

src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorParserTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,24 @@ public partial class Nested
350350
Assert.Empty(diagnostics);
351351
}
352352

353+
[Fact]
354+
public async Task FileScopedNamespaceOK()
355+
{
356+
IReadOnlyList<Diagnostic> diagnostics = await RunGenerator(@"
357+
using Microsoft.Extensions.Logging;
358+
359+
namespace MyLibrary;
360+
361+
internal partial class Logger
362+
{
363+
[LoggerMessage(EventId = 1, Level = LogLevel.Information, Message = ""Hello {Name}!"")]
364+
public static partial void Greeting(ILogger logger, string name);
365+
}
366+
");
367+
368+
Assert.Empty(diagnostics);
369+
}
370+
353371
[Theory]
354372
[InlineData("false")]
355373
[InlineData("true")]

0 commit comments

Comments
 (0)