Skip to content

[Before(Assembly)] and assembly names with dashes #2919

@wuzzeb

Description

@wuzzeb

In 0.56.5, if the assembly name has a dash in it and you use a [Before(Assembly)] attribute, you get a compile error in the generated code.

https://github.com/wuzzeb/tunit50-test contains a very small example of the problem, with a test assembly named tunit50-test. The compile error is

  tunit50-test failed with 5 error(s) (1.3s)
    /home/wuzzeb/projects/tunit50-test/obj/Debug/net9.0/TUnit.Core.SourceGenerator/TUnit.Core.SourceGenerator.Generators.HookMetadataGenerator/GeneratedHookSource.g.cs(34,20): error CS1002: ; expected
    /home/wuzzeb/projects/tunit50-test/obj/Debug/net9.0/TUnit.Core.SourceGenerator/TUnit.Core.SourceGenerator.Generators.HookMetadataGenerator/GeneratedHookSource.g.cs(34,13): error CS0818: Implicitly-typed variables must be initialized
    /home/wuzzeb/projects/tunit50-test/obj/Debug/net9.0/TUnit.Core.SourceGenerator/TUnit.Core.SourceGenerator.Generators.HookMetadataGenerator/GeneratedHookSource.g.cs(34,21): error CS0103: The name 'test_assembly' does not exist in the current context
    /home/wuzzeb/projects/tunit50-test/obj/Debug/net9.0/TUnit.Core.SourceGenerator/TUnit.Core.SourceGenerator.Generators.HookMetadataGenerator/GeneratedHookSource.g.cs(35,73): error CS0103: The name 'test_assembly' does not exist in the current context
    /home/wuzzeb/projects/tunit50-test/obj/Debug/net9.0/TUnit.Core.SourceGenerator/TUnit.Core.SourceGenerator.Generators.HookMetadataGenerator/GeneratedHookSource.g.cs(36,64): error CS0103: The name 'test_assembly' does not exist in the current context

The specific generated code for the line with the error is

    private static void PopulateSourcesDictionaries()
    {
        var tunit50-test_assembly = typeof(global::Hooks).Assembly;
        global::TUnit.Core.Sources.BeforeAssemblyHooks.GetOrAdd(tunit50-test_assembly, _ => new global::System.Collections.Concurrent.ConcurrentBag<global::TUnit.Core.Hooks.BeforeAssemblyHookMethod>());
        global::TUnit.Core.Sources.BeforeAssemblyHooks[tunit50-test_assembly].Add(
           ....

Note the variable named tunit50-test_assembly which gives the error because variables can't have dashes in them. If the name of the assembly is changed to tunit50_test with an underscore, the test compiles fine.

You can see the full build error in this action run

https://github.com/wuzzeb/tunit50-test/actions/runs/16992511137/job/48175160235

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions