Skip to content

Commit 73c0f8d

Browse files
Handle file-scoped namespaces (#57894)
Handle namespace not being emitted when file-scoped namespaces are used for a log class. Relates to #57880.
1 parent 01923f4 commit 73c0f8d

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,11 +438,13 @@ 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+
potentialNamespaceParent is not FileScopedNamespaceDeclarationSyntax)
442444
{
443445
potentialNamespaceParent = potentialNamespaceParent.Parent;
444446
}
445-
if (potentialNamespaceParent is NamespaceDeclarationSyntax namespaceParent)
447+
if (potentialNamespaceParent is BaseNamespaceDeclarationSyntax namespaceParent)
446448
{
447449
nspace = namespaceParent.Name.ToString();
448450
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)