Skip to content

Typed identifiers generated in global namespace fail to compile #779

@mishioo

Description

@mishioo

When typed id is placed in the global namespace, the project with generated code fails to compile with rather unhelpful build report.

using LeanCode.DomainModels.Ids;

// note no namespace declaration

[TypedId(TypedIdFormat.RawGuid)]
public readonly partial record struct Id;

results with

Restore complete (0.5s)
  Example failed with 7 error(s) (0.4s)
    /home/doktornauk/example/Example/src/obj/Debug/net9.0/LeanCode.DomainModels.Generators/LeanCode.DomainModels.Generators.TypedIdGenerator/Id.g.cs(3,11): error CS1001: Identifier expected
    /home/doktornauk/example/Example/src/obj/Debug/net9.0/LeanCode.DomainModels.Generators/LeanCode.DomainModels.Generators.TypedIdGenerator/Id.g.cs(3,11): error CS1514: { expected
    /home/doktornauk/example/Example/src/obj/Debug/net9.0/LeanCode.DomainModels.Generators/LeanCode.DomainModels.Generators.TypedIdGenerator/Id.g.cs(3,12): error CS0116: A namespace cannot directly contain members such as fields, methods or statements
    /home/doktornauk/example/Example/src/obj/Debug/net9.0/LeanCode.DomainModels.Generators/LeanCode.DomainModels.Generators.TypedIdGenerator/Id.g.cs(3,28): error CS1001: Identifier expected
    /home/doktornauk/example/Example/src/obj/Debug/net9.0/LeanCode.DomainModels.Generators/LeanCode.DomainModels.Generators.TypedIdGenerator/Id.g.cs(3,28): error CS1514: { expected
    /home/doktornauk/example/Example/src/obj/Debug/net9.0/LeanCode.DomainModels.Generators/LeanCode.DomainModels.Generators.TypedIdGenerator/Id.g.cs(4,1): error CS1022: Type or namespace definition, or end-of-file expected
    /home/doktornauk/example/Example/src/obj/Debug/net9.0/LeanCode.DomainModels.Generators/LeanCode.DomainModels.Generators.TypedIdGenerator/Id.g.cs(77,2): error CS1513: } expected

Build failed with 7 error(s) in 1.1s

This result comes from using ISymbol.ContainingNamespace.ToDisplayString() here, which resolves to <global namespace> for the global namespace, which is not a valid identifier in C# code.

We should either properly support identifiers in the global namespace or emit a diagnostic report instead of non-compiling code (preferably maybe?).

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