Skip to content

Static data-source properties on private nested types produce CS0122 in generated code #7461

Closed

Description

@thomhurst

Summary

A public static property with a data-source attribute inside a private nested type causes generated initialization code to fail with CS0122. The generator checks the property's accessibility but emits references to its inaccessible containing type, so adding an otherwise unused private helper can break the project build.

Reproduction

Add this to a test project referencing TUnit and build with source generation enabled:

public class ExampleTests
{
    private sealed class Helper
    {
        [TUnit.Core.ClassDataSource<Resource>]
        public static Resource Value { get; set; } = null!;
    }

    public sealed class Resource;

    [TUnit.Core.Test]
    public void Example() { }
}

The emitted StaticPropertyInitializer.g.cs refers to ExampleTests.Helper outside its containing class and reports CS0122. Changing the helper to internal allows compilation.

Expected and actual

Expected: either supported initialization or an actionable diagnostic on the user's property. Actual: compiler errors in generated code referencing an inaccessible type. Please handle containing-type accessibility when generating static-property initialization.

Environment

.NET SDK 10.0.403, macOS arm64, net10.0. Reproduced using repository source; the same accessibility check remains on main at d9e9e6638e. No matching issue was found in open or closed issue searches, and there is no unreleased changelog in the checkout.

Found while adding regression fixtures for TUnit PR #7459.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Medium: edge case, mode divergence, flaky test, or workaround existsbugSomething isn't workingin-progressWork is in progress

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions