stride3d/stride · error · ArgumentException

Invalid path [ ] for action [ ]. Expecting last path to be…

Error message

Invalid path [{0}] for action [{1}]. Expecting last path to be a collection/array item

What it means

MemberPathAction.CollectionRemove requires the last path segment to point at the element being removed, i.e. it must be a CollectionPathItem or ArrayPathItem. Apply() throws ArgumentException when the path terminates on any other kind of segment (member or dictionary item).

Solutions

  1. Push the collection or array element segment (PushCollectionItem / PushArrayItem) as the final segment so it identifies the exact element to remove.
  2. For dictionaries, switch to MemberPathAction.DictionaryRemove instead.
  3. Inspect the path's last item type before calling Apply and branch on it.

Example fix

// before
var path = new MemberPath();
path.Push(type.GetProperty("Items"));
path.Apply(root, MemberPathAction.CollectionRemove, null);
// after
var path = new MemberPath();
path.Push(type.GetProperty("Items"));
path.PushCollectionItem(indexToRemove);
path.Apply(root, MemberPathAction.CollectionRemove, null);
Defensive patterns

Strategy: validation

Validate before calling

var last = path.Items[^1];
if (action == MemberPathAction.CollectionRemove && last is not (CollectionPathItem or ArrayPathItem))
    throw new InvalidOperationException("CollectionRemove requires a collection/array-item path segment.");

Type guard

static bool CanCollectionRemove(MemberPath p) => p.Items.Count > 0 && p.Items[^1] is CollectionPathItem or ArrayPathItem;

Try / catch

try { path.Apply(root, MemberPathAction.CollectionRemove, null); }
catch (ArgumentException ex) when (ex.Message.Contains("Expecting last path to be a collection/array item")) { /* reshape the path */ }

Prevention

When it happens

Trigger: Calling Apply(root, MemberPathAction.CollectionRemove, null) on a path whose final segment is a MemberPathItem, DictionaryPathItem, or the empty/member path rather than a collection/array element.

Common situations: Building a path with Push(member) only and trying to remove from the collection property; confusing dictionary removal (DictionaryRemove) with list removal and reusing a dictionary-shaped path.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/6f6ac91e1019dfab. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core.Reflection/MemberPath.cs:246

        if (items == null || items.Count == 0)
        {
            throw new InvalidOperationException("This instance doesn't contain any path. Use Push() methods to populate paths");
        }

        var lastItem = items[^1];
        switch (actionType)
        {
            case MemberPathAction.CollectionAdd:
                if (lastItem is not CollectionPathItem)
                {
                    throw new ArgumentException("Invalid path [{0}] for action [{1}]. Expecting last path to be a collection item".ToFormat(this, actionType));
                }
                break;
            case MemberPathAction.CollectionRemove:
                if (lastItem is not (CollectionPathItem or ArrayPathItem))
                {
                    throw new ArgumentException("Invalid path [{0}] for action [{1}]. Expecting last path to be a collection/array item".ToFormat(this, actionType));
                }
                break;

            case MemberPathAction.DictionaryRemove:
                if (lastItem is not DictionaryPathItem)
                {
                    throw new ArgumentException("Invalid path [{0}] for action [{1}]. Expecting last path to be a dictionary item".ToFormat(this, actionType));
                }
                break;
        }

        var stack = stackTLS;
        try
        {
            object? nextObject = rootObject;

            if (stack == null)
            {

View on GitHub (pinned to 96fad776d2)