Skip to content

Error: ModuleInitializerAttribute is inaccessible due to its protection level in .NET Framework #3731

@sliekens

Description

@sliekens

The automatic Polyfill reference for legacy frameworks seems to have internal visibility, and TUnit can't access it.

$ dotnet run
/workspaces/repro/MyTests/obj/Debug/net481/TUnit.Core.SourceGenerator/TUnit.Core.SourceGenerator.CodeGenerators.DisableReflectionScannerGenerator/DisableReflectionScanner.g.cs(7,46): error CS0122: 'ModuleInitializerAttribute' is inaccessible due to its protection level
/workspaces/repro/MyTests/obj/Debug/net481/TUnit.Core.SourceGenerator/TUnit.Core.SourceGenerator.CodeGenerators.AssemblyLoaderGenerator/AssemblyLoader.g.cs(7,46): error CS0122: 'ModuleInitializerAttribute' is inaccessible due to its protection level

Repro steps (tested in WSL2 with Mono 6.12 / .NET 10 RC2 CLI for bootstrapping the project):

dotnet new console -o MyTests -f net10.0
dotnet add MyTests package TUnit

sed -i 's/net10.0/net481/g' MyTests/MyTests.csproj
sed -i '/<Nullable>enable<\/Nullable>/a \    <LangVersion>14</LangVersion>' MyTests/MyTests.csproj

rm MyTests/Program.cs
cat << 'EOF' > MyTests/MyTests.cs
namespace MyTests;

public class MyTests
{
    [Test]
    public async Task MyTest()
    {
        await Assert.That(true).IsTrue();
    }
}
EOF

dotnet run --project MyTests

As a workaround, I disabled the automatic polyfills and installed it manually:

sed -i '/<LangVersion>14<\/LangVersion>/a \    <EnableTUnitPolyfills>false</EnableTUnitPolyfills>' MyTests/MyTests.csproj
dotnet add MyTests package Polyfill
dotnet run --project MyTests

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions