Skip to content

'Already defines a member' error in partial classes #31

@sadeep-8x8

Description

@sadeep-8x8

Description

If there are log lines at the same location in multiple files in partial classes, build fails with errors like

45>LogCallInterceptors.g.cs(274,28): Error CS0111 : Type 'LoggerInterceptors' already defines a member called 'Log_***_86_20' with the same parameter types
45>LoggerMessage.g.cs(400,30): Error CS0111 : Type 'AutoLoggerMessage' already defines a member called 'Log_***_86_20' with the same parameter types

The GeneratedMethodName() in LogerScopeCall and LogMessageCall uses the namespace, class, and loc to generate the method, which is likely the cause. Consider the following patch to mitigate this issue (I didn't have time to investigate the implications of changing the generated method, but hope the following patch helps):

diff --git a/src/AutoLoggerMessageGenerator/Models/LogMessageCall.cs b/src/AutoLoggerMessageGenerator/Models/LogMessageCall.cs
index 3f9e065..b52ccfd 100644
--- a/src/AutoLoggerMessageGenerator/Models/LogMessageCall.cs
+++ b/src/AutoLoggerMessageGenerator/Models/LogMessageCall.cs
@@ -1,5 +1,5 @@
 using System.Collections.Immutable;
-using AutoLoggerMessageGenerator.Utilities;
+using static AutoLoggerMessageGenerator.Utilities.IdentifierHelper;
 
 namespace AutoLoggerMessageGenerator.Models;
 
@@ -16,8 +16,8 @@ internal readonly record struct LogMessageCall(
 )
 {
     public string GeneratedMethodName =>
-        IdentifierHelper.ToValidCSharpMethodName(
-            $"{Constants.LogMethodPrefix}{Namespace}{ClassName}_{Location.Line}_{Location.Character}"
+        ToValidCSharpMethodName(
+            $"{Constants.LogMethodPrefix}{Namespace}{ClassName}_{GetFileNameHash(Location.FilePath)}_{Location.Line}_{Location.Character}"
         );
 
     public bool Equals(LogMessageCall other) =>
diff --git a/src/AutoLoggerMessageGenerator/Models/LoggerScopeCall.cs b/src/AutoLoggerMessageGenerator/Models/LoggerScopeCall.cs
index 135a12c..246d87e 100644
--- a/src/AutoLoggerMessageGenerator/Models/LoggerScopeCall.cs
+++ b/src/AutoLoggerMessageGenerator/Models/LoggerScopeCall.cs
@@ -14,7 +14,7 @@ internal readonly record struct LoggerScopeCall(
 {
     public string GeneratedMethodName =>
         IdentifierHelper.ToValidCSharpMethodName(
-            $"{Constants.LogScopeMethodPrefix}{Namespace}{ClassName}_{Location.Line}_{Location.Character}"
+            $"{Constants.LogScopeMethodPrefix}{Namespace}{ClassName}_{IdentifierHelper.GetFileNameHash(Location.FilePath)}_{Location.Line}_{Location.Character}"
         );
 
     public bool Equals(LoggerScopeCall other) =>
diff --git a/src/AutoLoggerMessageGenerator/Utilities/IdentifierHelper.cs b/src/AutoLoggerMessageGenerator/Utilities/IdentifierHelper.cs
index 301ea8d..604e38f 100644
--- a/src/AutoLoggerMessageGenerator/Utilities/IdentifierHelper.cs
+++ b/src/AutoLoggerMessageGenerator/Utilities/IdentifierHelper.cs
@@ -25,4 +25,11 @@ internal static class IdentifierHelper
     public const string ValidCSharpParameterNameRegex = @"^@?[a-zA-Z_][a-zA-Z0-9_]*$";
 
     private static Regex IsValidCSharpParameterNameRegex => new(ValidCSharpParameterNameRegex, RegexOptions.Compiled);
+
+    public static string GetFileNameHash(string filePath)
+    {
+        var fileName = Path.GetFileNameWithoutExtension(filePath);
+        var hash = fileName.GetHashCode() & 0x7FFFFFFF;
+        return hash.ToString("X");
+    }
 }

Reproduction Steps

Create a partial class (same namespace, class in two files. Add loglines to the same location so that the generated member names clash).

Expected behavior

Able to use AutoLoggerMessage with partial classes.

Actual behavior

Unable to build if there are loglines at the same locations.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions