godotengine / godot-docs

Godot Engine official documentation
https://docs.godotengine.org
Other
3.87k stars 3.16k forks source link

System.AccessViolationException when creating a new Node2D in a unit test with xUnit #9475

Open DevSavvySerge opened 3 months ago

DevSavvySerge commented 3 months ago

Tested versions

System information

Windows 10 - Godot 4.2.2

Issue description

System.AccessViolationException when creating a new Node2D in a unit test with xUnit

Description

When running a simple unit test using xUnit that creates a new instance of Node2D, the test host process crashes with a System.AccessViolationException. This issue occurs consistently and seems to indicate memory corruption.

Reproducibility

100% - The error occurs every time the test is run.

Additional Information

Steps to reproduce

Steps to Reproduce

  1. Create a new project in Godot.

  2. Add a C# script file named UnitTest1.cs with the following code:

    using Godot;
    using Xunit;
    
    namespace Test2
    {
        public class UnitTest1
        {
            [Fact]
            public void Test1()
            {
                var o = new Node2D();
            }
        }
    }
  3. Run the unit test using the dotnet test command.

Expected Result

The test should pass without any exceptions.

Actual Result

The test host process crashes with the following error:

The active test run was aborted. Reason: Test host process crashed : Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Godot.NativeInterop.NativeFuncs.godotsharp_string_new_with_utf16_chars(Godot.NativeInterop.godot_string ByRef, Char*)
   at Godot.NativeInterop.Marshaling.ConvertStringToNative(System.String)
   at Godot.NativeInterop.NativeFuncs.godotsharp_string_name_new_from_string(System.String)
   at Godot.StringName..ctor(System.String)
   at Godot.StringName.op_Implicit(System.String)
   at Godot.Node2D..cctor()
   at Godot.Node2D..ctor()
   at Test2.UnitTest1.Test1()
   ...

Minimal reproduction project (MRP)

N/A

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="coverlet.collector" Version="6.0.0" />
    <PackageReference Include="GodotSharp" Version="4.2.2" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
    <PackageReference Include="xunit" Version="2.5.3" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
  </ItemGroup>

  <ItemGroup>
    <Using Include="Xunit" />
  </ItemGroup>

</Project>
AThousandShips commented 3 months ago

Is xUnit officially supported in any way? This doesn't seem like a Godot issue necessarily

DevSavvySerge commented 3 months ago

Be it xUnit or any other framework (but Godot?), the issue actually is you cannot create Node2d based objects which makes it impossible for running some tests automatically.

AThousandShips commented 3 months ago

Are you running the code from within Godot? Or as a separate application?

DevSavvySerge commented 3 months ago

As a separate application. To my current knowledge, the only way to run code from within Godot is to actually run a scene.

AThousandShips commented 3 months ago

It is not possible to run Godot code as an isolated application, see https://github.com/godotengine/godot/issues/92906#issuecomment-2156129842

This might be something to document

DevSavvySerge commented 3 months ago

Would the only solution be to create a scene and trigger tests from within? Meaning creating some kind of tests runner running within godot?

AThousandShips commented 3 months ago

Indeed, the C# bindings are just bindings, all the background code is run in c++ within the engine, with a few exceptions all the classes in C# are just calling engine methods to do the work, there's no version of the engine written in C# directly

DevSavvySerge commented 3 months ago

Ok, thank you for the quick answer. That is important knowledge to start with.

AThousandShips commented 3 months ago

Let's see if the dotnet team thinks this should be documented more clearly, but moving this to the documentation repo as it's a documentation issue and not part of the class reference

reuben-ahmed commented 1 month ago

@DevSavvySerge , do you have a simple example of executing that C# code in a unit test? We're having the same issue here, I understand the problem, but I don't know how to fix it yet lol.