Burst error BC1091: External and internal calls are not allowed inside static constructors

IMHO this really needs some tooling improvement. How am I supposed to take that error and figure out what the heck it even means let alone how to fix whatever is causing it?

The stack just seems to be a dense list of pretty much ALL my job structs - even ones that don’t have the BurstCompile attribute, but even if it showed just one job in the stack I still wouldn’t have any clue what code IN the job I should be looking at.

So through a process of just commenting out code until the error went away, I managed to narrow it down to I think the code that creates a new instance of a struct is what causes the error?

var edge = new Edge(centroidIndex, neighborCentroidIndex);
    public struct Edge : IEquatable<Edge>
    {
        public int A, B;

        public Edge(int a, int b)
        {
            A = math.min(a, b);
            B = math.max(a, b);
        }

        public bool Equals(Edge other)
        {
            return A == other.A && B == other.B;
        }

        public override bool Equals(object obj)
        {
            return obj is Edge other && Equals(other);
        }

        public override int GetHashCode()
        {
            return HashCode.Combine(A, B);
        }
    }

Context what method this code is in and maybe even the path leading to that method call would help and/or the full callstack of the error.

I don’t think Edge class is the issue, it’s where you’re instantiating it. As I see it “static constructor” is the thing to watch out for. Check any classes that have a static constructor. Perhaps this could also relate to methods attributed with [InitializeOnLoadMethod].

Like I said the stack was really non helpful but here it is for reference.

(0,0): Burst error BC1091: External and internal calls are not allowed inside static constructors: Interop.BCrypt.BCryptGenRandom(System.IntPtr hAlgorithm, byte* pbBuffer, int cbBuffer, int dwFlags)

While compiling job:
Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct1[[TiledSphere.Jobs.CalculateMidPointsJob, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null::Execute(TiledSphere.Jobs.CalculateMidPointsJob&, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|Unity.Jobs.LowLevel.Unsafe.JobRanges&, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct1[[TiledSphere.Jobs.CalculateCenterPointsJob, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null::Execute(TiledSphere.Jobs.CalculateCenterPointsJob&, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|Unity.Jobs.LowLevel.Unsafe.JobRanges&, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct1[[TiledSphere.Jobs.CalculateNeighborsJob, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null::Execute(TiledSphere.Jobs.CalculateNeighborsJob&, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|Unity.Jobs.LowLevel.Unsafe.JobRanges&, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089) Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct1[[TiledSphere.Jobs.GenerateHexMeshJob, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null::Execute(TiledSphere.Jobs.GenerateHexMeshJob&, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|Unity.Jobs.LowLevel.Unsafe.JobRanges&, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)

Thanks. I know NOTHING about hashcodes but found something on SO that I think will work and compiles.

That’s great that they are fixing this specific issue but my post was more about the fact that the error is completely opaque I think to most users. Half of your reply didn’t even make any sense to me :smile:

Unity has always been about opening up the power to create to more people, not less, and this is just IMHO something that is overly technical and hard to understand - the very type of thing DOTS is supposed to be helping with :slight_smile: So some extra tooling here to better help the developer understand where the error is, what the error actually means, and how to fix it would be great :slight_smile:

Just my feedback there. Replacing the HashCode.Combine though with some magic code I got off SO seems to have worked though. Here is that code for anyone else (or future me) that runs into this and Googles:

public override int GetHashCode()
{
    unchecked // Overflow is fine, just wrap
    {
        int hash = 17;
        // Suitable nullity checks etc, of course :)
        hash = hash * 23 + A.GetHashCode();
        hash = hash * 23 + B.GetHashCode();
        return hash;
    }
}

I also get confused by the errors, but I think it refers to the item at the top of the ‘stack’ TiledSphere.Jobs.CalculateMidPointsJob? Something marked static that is calling another method. Maybe…