Skip to content

[Bug]: Source Generator Fails to Include Required Namespace for Static Class Reference #295

@Soar360

Description

@Soar360

Describe the bug 🐞

When using SplatRegistrations.Register<T>() with a key from a static class in a different namespace, the source generator fails to include the required using directive in the generated code, resulting in a compilation error.

Step to reproduce

  1. Define a static class Keys in namespace NS1:

    namespace ConsoleApp2.NS1
    {
        public static class Keys
        {
            public static string Key1 = "Key1";
            public static string Key2 = "Key2";
        }
    }
  2. In a different namespace (NS2), use SplatRegistrations.Register with Keys.Key1:

    using ConsoleApp2.NS1;
    using Splat;
    
    namespace ConsoleApp2.NS2
    {
        public class Class1
        {
            static Class1()
            {
                SplatRegistrations.Register<Class1>(Keys.Key1);
            }
        }
    }
  3. Build the project.

Reproduction repository

https://github.com/reactiveui/ReactiveUI

Expected behavior

The source generator should include the necessary using ConsoleApp2.NS1; directive in the generated file, or fully qualify the reference to Keys.Key1, allowing the project to compile successfully.

Screenshots 🖼️

The generated file Splat.DI.Reg.g.cs contains an unresolved reference to Keys.Key1, causing a compilation error:

error CS0103: The name 'Keys' does not exist in the current context

Generated code snippet (with the issue):

static partial void SetupIOCInternal(Splat.IDependencyResolver resolver)
{
    resolver.Register<global::ConsoleApp2.NS2.Class1>(() => new global::ConsoleApp2.NS2.Class1(), Keys.Key1);
}

IDE

Visual Studio 2022

Operating system

No response

Version

No response

Device

No response

ReactiveUI Version

No response

Additional information ℹ️

  <ItemGroup>
    <PackageReference Include="Splat" Version="19.2.1" />
    <PackageReference Include="Splat.DependencyInjection.SourceGenerator" Version="2.1.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

The issue appears to be that the source generator does not preserve or include the namespace of static classes referenced as registration keys, leading to missing using statements in the generated file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions