Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tons of errors if building for AOT (IL3050) #1831

Closed
rodrigovaras opened this issue Feb 13, 2025 · 12 comments · Fixed by #1833, #1876 or #1888
Closed

Tons of errors if building for AOT (IL3050) #1831

rodrigovaras opened this issue Feb 13, 2025 · 12 comments · Fixed by #1833, #1876 or #1888

Comments

@rodrigovaras
Copy link

I'm porting many xUnit test into TUnit for the whole purpose of enabling AOT testing.
When I build my unit test for AOT i get many msbuild errors (that can be supressed) like this:

Binding.Model net9.0 succeeded (0.4s) → artifacts\bin\Binding.Model\debug_net9.0\Teksoft.Binding.Model.dll
Binding.Core net9.0 succeeded (0.5s) → artifacts\bin\Binding.Core\debug_net9.0\Teksoft.Binding.Core.dll
Binding.Mock.Test succeeded (0.2s) → artifacts\bin\Binding.Mock.Test\debug\Teksoft.Binding.Mock.Test.dll
Binding.Core.Test failed with 153 error(s) (33.6s) → test\artifacts\bin\Binding.Core.Test\debug_win-x64\Teksoft.Binding.Core.Test.dll
ILC : AOT analysis error IL3050: TUnit.SourceGenerated.<BindingTest_06054ffcd3014f66b2e6a48ef6c3dd64_Generated>F56AB95644891240B5A9CDB79558D9E1D87B85B2920919123C46334919BE9EEA4__BindingTest.<>c__DisplayClass2_1.b__0(): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues overload or the GetValuesAsUnderlyingType method instead.
ILC : AOT analysis error IL3050: TUnit.SourceGenerated.<BindingTest_a35d90e75525436994564935f5a0d5a8_Generated>F570D2CCCFB64BDB85DD9E28A7353E4C7CED39F399E1BB4761E7F1A3AA04086B1__BindingTest.<>c__DisplayClass2_1.b__0(): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues overload or the GetValuesAsUnderlyingType method instead

I suspect it could be caused by TUnit generated code ?
Would be great to fix this for AOT generation.

Using .NET 9

@rodrigovaras
Copy link
Author

Suppressing IL3050 shows me another one:

ILC : error IL3000: TUnit.Core.TestContext.OutputDirectory.get: 'System.Reflection.Assembly.Location.get' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.

@thomhurst
Copy link
Owner

Can you try v0.12.11 ?

@rodrigovaras
Copy link
Author

Thanks a lot for your quick response.
I tried the new package and it seems that it fixed some of the errors but not all, now i'm getting IL3050 on other lines ?
error IL3050: TUnit.SourceGenerated.<BindingTest_160b1b9aaed44bd98fbe0a0a1940d296_Generated>FDE7E2FA00B970CD12565B1FB31E234D4BB416C85205B10F81D4AC2CA1B60156C__BindingTest.<>c__DisplayClass2_1.b__0(): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues overload or the GetValuesAsUnderlyingType method instead.

@thomhurst thomhurst reopened this Feb 13, 2025
@thomhurst
Copy link
Owner

Are you able to create a small repro? I'm not seeing it on my machine

@rodrigovaras
Copy link
Author

rodrigovaras commented Feb 13, 2025

Any tip on how can i see the generated code ? It seems that after the error I can't see it on disk the offending path on the error.
I moved to v0.12.11 and I clean the project by deleting the obj*
My unit test project has several project references so i can't easily upload a zip file.
BTW, I don't use [Matrix] attribute

@thomhurst
Copy link
Owner

You can see generated code by placing this in your csproj and rebuilding:

    <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>SourceGeneratedViewer</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>

@rodrigovaras
Copy link
Author

I scope down the problem to this:
public class BindingTest
{
[Test]
public async Task SimpleValueTest()
{
var model = new
{
prop1 = 200
};

    var bar = new
    {
        model
    };

    var foo = new
    {
        bar
    };

    using var propertyBinding = new PropertyBinding(foo, "bar.model.prop1");
    await Assert.That(propertyBinding.Value).IsEqualTo(200);
}

internal enum CustomKey
{
    Key1,
    Key2,
    Key3,
}

}

Removing or adding the enum CustomKey make the problem appear/disappear. It seems that the type even if not being used cause the code generation

@rodrigovaras
Copy link
Author

I can see the code generated file but no reference into Enum.GetValues()

@thomhurst
Copy link
Owner

I still can't reprocate it

@rodrigovaras
Copy link
Author

Attached a trivial project with TUnit that generate warning IL3050

C:\Users\rodrigov\source\repos\TUnitTest>dotnet publish -c Debug -r win-x64
Restore complete (1.2s)
TUnitTest succeeded with 5 warning(s) (23.2s) → TUnitTest\bin\Debug\net9.0\win-x64\publish
C:\Users\rodrigov\source\repos\TUnitTest\TUnitTest\obj\Debug\net9.0\win-x64\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.CodeGenerators.TestsGenerator\MyTestClass-129113562c9f4231ac214a5a5a75fc3b.Generated.cs(30): AOT analysis warning IL3050: TUnit.SourceGenerated.<MyTestClass_129113562c9f4231ac214a5a5a75fc3b_Generated>F8A7604BAA1E57C258A95C07CA5AD199C30E7388A712E54D03793FFD0FACEC1DB__MyTestClass.Tests0(String): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues overload or the GetValuesAsUnderlyingType method instead.
C:\Users\rodrigov\source\repos\TUnitTest\TUnitTest\obj\Debug\net9.0\win-x64\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.CodeGenerators.TestsGenerator\MyTestClass-129113562c9f4231ac214a5a5a75fc3b.Generated.cs(41): AOT analysis warning IL3050: TUnit.SourceGenerated.<MyTestClass_129113562c9f4231ac214a5a5a75fc3b_Generated>F8A7604BAA1E57C258A95C07CA5AD199C30E7388A712E54D03793FFD0FACEC1DB__MyTestClass.<>c.b__2_1(): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues overload or the GetValuesAsUnderlyingType method instead.
C:\Users\rodrigov\source\repos\TUnitTest\TUnitTest\obj\Debug\net9.0\win-x64\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.CodeGenerators.TestsGenerator\MyTestClass-129113562c9f4231ac214a5a5a75fc3b.Generated.cs(74): AOT analysis warning IL3050: TUnit.SourceGenerated.<MyTestClass_129113562c9f4231ac214a5a5a75fc3b_Generated>F8A7604BAA1E57C258A95C07CA5AD199C30E7388A712E54D03793FFD0FACEC1DB__MyTestClass.Tests0(String): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues overload or the GetValuesAsUnderlyingType method instead.
C:\Users\rodrigov\source\repos\TUnitTest\TUnitTest\obj\Debug\net9.0\win-x64\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.CodeGenerators.TestsGenerator\MyTestClass-129113562c9f4231ac214a5a5a75fc3b.Generated.cs(80): AOT analysis warning IL3050: TUnit.SourceGenerated.<MyTestClass_129113562c9f4231ac214a5a5a75fc3b_Generated>F8A7604BAA1E57C258A95C07CA5AD199C30E7388A712E54D03793FFD0FACEC1DB__MyTestClass.Tests0(String): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues overload or the GetValuesAsUnderlyingType method instead.
C:\Users\rodrigov\source\repos\TUnitTest\TUnitTest\obj\Debug\net9.0\win-x64\TUnit.Core.SourceGenerator\TUnit.Core.SourceGenerator.CodeGenerators.TestsGenerator\MyTestClass-129113562c9f4231ac214a5a5a75fc3b.Generated.cs(74): AOT analysis warning IL3050: TUnit.SourceGenerated.<MyTestClass_129113562c9f4231ac214a5a5a75fc3b_Generated>F8A7604BAA1E57C258A95C07CA5AD199C30E7388A712E54D03793FFD0FACEC1DB__MyTestClass.<>c__DisplayClass2_1.b__0(): Using member 'System.Enum.GetValues(Type)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. It might not be possible to create an array of the enum type at runtime. Use the GetValues overload or the GetValuesAsUnderlyingType method instead.

Build succeeded with 5 warning(s) in 25.3s

C

TUnitTest.zip

@thomhurst thomhurst linked a pull request Feb 17, 2025 that will close this issue
@thomhurst
Copy link
Owner

@rodrigovaras can you try v0.13.15

@rodrigovaras
Copy link
Author

@thomhurst i'm impressed on your excellent support. I move into 0.13.5 and all the other warnings disappear, but now the ILC build setps is giving me 2 new errors:
Binding.Mock.Test succeeded (0.5s) → artifacts\bin\Binding.Mock.Test\debug\Teksoft.Binding.Mock.Test.dll
Binding.Expression.Test failed with 3 error(s) (42.0s) → test\artifacts\bin\Binding.Expression.Test\debug_win-x64\Teksoft.Binding.Expression.Test.dll
ILC : Trim analysis error IL2026: TUnit.Core.SourceGeneratedMethodInformation.PrintMembers(StringBuilder): Using member 'TUnit.Core.SourceGeneratedMethodInformation.ReflectionInformation.get' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Reflection API.
C:\Users\rodrigov.nuget\packages\runtime.win-x64.microsoft.dotnet.ilcompiler\9.0.2\framework\System.Linq.Parallel.dll : error IL3053: Assembly 'System.Linq.Parallel' produced AOT analysis warnings.
C:\Users\rodrigov.nuget\packages\microsoft.dotnet.ilcompiler\9.0.2\build\Microsoft.NETCore.Native.targets(317,5): er

I assume this can't be reproduced by the simple .zip project I send it before it seems that my more elaborated project reveal the problem.

Another similar project on my solution that has AOT enabled also complain on the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants