Getting an error related to system sorting. How to fix or how to look what's causing it?

For context, I’m a upgrading an existing project that was already big with lots of systems. Followed the upgrade guide, fixed compiler errors, and I can now finally run the game. But just at startup, there’s an error at DefaultWorldInitialization:

IndexOutOfRangeException: NewLength 32 is out of range of '31' Capacity.
Unity.Collections.FixedList.CheckResize[BUFFER,T] (System.Int32 newLength) (at ./Library/PackageCache/com.unity.collections@2.1.0-pre.18/Unity.Collections/FixedList.gen.cs:451)
Unity.Collections.FixedList128Bytes`1[T].set_Length (System.Int32 value) (at ./Library/PackageCache/com.unity.collections@2.1.0-pre.18/Unity.Collections/FixedList.gen.cs:2791)
Unity.Collections.FixedList128Bytes`1[T].AddNoResize (T& item) (at ./Library/PackageCache/com.unity.collections@2.1.0-pre.18/Unity.Collections/FixedList.gen.cs:2944)
Unity.Collections.FixedList128Bytes`1[T].Add (T& item) (at ./Library/PackageCache/com.unity.collections@2.1.0-pre.18/Unity.Collections/FixedList.gen.cs:2919)
Unity.Entities.ComponentSystemSorter.FindConstraints$BurstManaged (System.Int32 parentTypeIndex, Unity.Collections.LowLevel.Unsafe.UnsafeList`1[Unity.Entities.ComponentSystemSorter+SystemElement]* sysElemsPtr, Unity.Collections.NativeHashMap`2[System.Int32,System.Int32]* lookupDictionary, Unity.Entities.TypeManager+SystemAttributeKind afterkind, Unity.Entities.TypeManager+SystemAttributeKind beforekind, Unity.Collections.NativeHashSet`1[System.Int32]* badSystemTypeIndices) (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities/ComponentSystemSorter.cs:579)
Unity.Entities.ComponentSystemSorter+FindConstraints_000001CB$BurstDirectCall.Invoke (System.Int32 parentTypeIndex, Unity.Collections.LowLevel.Unsafe.UnsafeList`1[Unity.Entities.ComponentSystemSorter+SystemElement]* sysElemsPtr, Unity.Collections.NativeHashMap`2[System.Int32,System.Int32]* lookupDictionary, Unity.Entities.TypeManager+SystemAttributeKind afterkind, Unity.Entities.TypeManager+SystemAttributeKind beforekind, Unity.Collections.NativeHashSet`1[System.Int32]* badSystemTypeIndices) (at <c6f736424996433b875379f5f7b53de2>:0)
Unity.Entities.ComponentSystemSorter.FindConstraints (System.Int32 parentTypeIndex, Unity.Collections.LowLevel.Unsafe.UnsafeList`1[Unity.Entities.ComponentSystemSorter+SystemElement]* sysElemsPtr, Unity.Collections.NativeHashMap`2[System.Int32,System.Int32]* lookupDictionary, Unity.Entities.TypeManager+SystemAttributeKind afterkind, Unity.Entities.TypeManager+SystemAttributeKind beforekind, Unity.Collections.NativeHashSet`1[System.Int32]* badSystemTypeIndices) (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities/ComponentSystemSorter.cs:530)
Unity.Entities.ComponentSystemGroup.GenerateMasterUpdateList () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities/ComponentSystemGroup.cs:438)
Unity.Entities.ComponentSystemGroup.RecurseUpdate () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities/ComponentSystemGroup.cs:372)
Unity.Entities.ComponentSystemGroup.RecurseUpdate () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities/ComponentSystemGroup.cs:381)
Unity.Entities.ComponentSystemGroup.RecurseUpdate () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities/ComponentSystemGroup.cs:381)
Unity.Entities.ComponentSystemGroup.SortSystems () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities/ComponentSystemGroup.cs:559)
Unity.Entities.DefaultWorldInitialization.AddSystemToRootLevelSystemGroupsInternal (Unity.Entities.World world, System.Collections.Generic.IEnumerable`1[T] systemTypesOrig) (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities/DefaultWorldInitialization.cs:278)
Unity.Entities.DefaultWorldInitialization.Initialize (System.String defaultWorldName, System.Boolean editorWorld) (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities/DefaultWorldInitialization.cs:154)
Unity.Entities.AutomaticWorldBootstrap.Initialize () (at ./Library/PackageCache/com.unity.entities@1.0.0-pre.65/Unity.Entities.Hybrid/Injection/AutomaticWorldBootstrap.cs:16)

It has something to do with the system sorting but how do I know which systems are the culprits?

After some debugging, the problem seems to be in ComponentSystemSorter.SystemElement:

internal struct SystemElement
{
    public int TypeIndex;
    public UpdateIndex Index;
    public int OrderingBucket; // 0 = OrderFirst, 1 = none, 2 = OrderLast
    public FixedList128Bytes<int> updateBefore;
    public int nAfter;
}

The fixed list updateBefore can only hold 31 elements but my setup somehow can get to more than that.

Another thing I noticed is that updateBefore is being set using FixedList512Bytes on initialization in ComponentSystemGroup.GenerateMasterUpdateList():

for (int i = 0; i < m_managedSystemsToUpdate.Count; ++i)
{
    var system = m_managedSystemsToUpdate[i];
    var sysTypeIndex = system.m_StatePtr->m_SystemTypeIndex;
    int orderingBucket = ComputeSystemOrdering(sysTypeIndex, groupTypeIndex);
    allElems[i] = new ComponentSystemSorter.SystemElement
    {
        TypeIndex = sysTypeIndex,
        Index = new UpdateIndex(i, true),
        OrderingBucket = orderingBucket,
        updateBefore = new FixedList512Bytes<int>(),
        nAfter = 0,
    };
    systemsPerBucket[orderingBucket]++;
}

Is this a possible bug?

So I copied the entities package to the Package folder so I can make edits. I changed the type of updateBefore to FixedList512Bytes and the error is gone. This is really a possible inconsistent code that was overlooked.

Wild guess, perhaps too many constraints (like UpdateBefore) to the single system?
If so, try ordering with groups instead of constraints. Its generally easier to maintain.

Type difference is surely an oversight, though limit of 32 might be by design.

Yes, I’m already using groups. Perhaps this group is still too big. For AI, I’m using actions as entities so each action has it’s own system. Because of this, there are already lots of systems. I’m still finding out which system has this large amount of updateBefore entries.