iOfficeAI/OfficeCLI · error · ArgumentException

Invalid path index '{idx}' in segment '{part}'. Index must b

Error message

Invalid path index '{idx}' in segment '{part}'. Index must be >= 1.

What it means

Thrown by the GenericXmlQuery path parser when a bracket index parses as an integer but is less than 1. The navigator uses 1-based indexing (matching document/XML positional conventions), so index 0 or negatives are rejected.

Source

Thrown at src/officecli/Core/GenericXmlQuery.cs:263

            {
                // BUG-R36-01 fix: when ']' is missing (e.g. "slide[") the expression
                // part[(bracketIdx+1)..^1] produces a negative-length range crash.
                // Detect and reject unclosed brackets with a clean ArgumentException.
                var closingIdx = part.IndexOf(']', bracketIdx + 1);
                if (closingIdx < 0)
                    throw new ArgumentException($"Malformed path segment '{part}'. Bracket '[' is not closed. Expected format: name[index] or name[@attr=value].");
                var name = PathAliases.Resolve(part[..bracketIdx]);
                var indexStr = part[(bracketIdx + 1)..^1];
                if (!int.TryParse(indexStr, out var idx))
                    // A predicate in the index slot (row[Score>0], row[not(V)])
                    // means the caller reached the single-node path navigator
                    // with a FILTER. get is one-node-by-path by contract;
                    // point at the verbs that run the selector engine.
                    throw new ArgumentException(AttributeFilter.IsContentFilterPath($"[{indexStr}]")
                        ? $"'{part}' is a predicate, but this verb navigates by position and expects a numeric index (e.g. {part[..part.IndexOf('[')]}[2]). Predicates work on 'query' (read) and 'set'/'remove' (mutate matched elements)."
                        : $"Invalid path index '{indexStr}' in segment '{part}'. Expected a numeric index.");
                if (idx < 1)
                    throw new ArgumentException($"Invalid path index '{idx}' in segment '{part}'. Index must be >= 1.");
                segments.Add((name, idx));
            }
            else
            {
                segments.Add((PathAliases.Resolve(part), null));
            }
        }
        return segments;
    }

    /// <summary>
    /// Navigate an OpenXML element tree by path segments (localName + optional 1-based index).
    /// Returns null if any segment cannot be resolved.
    /// </summary>
    public static OpenXmlElement? NavigateByPath(OpenXmlElement root, IReadOnlyList<(string Name, int? Index)> segments)
    {
        OpenXmlElement? current = root;
        foreach (var seg in segments)

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Use a 1-based index: the first element is [1], not [0].
  2. When converting from a 0-based programmatic index, add 1 before building the path.
  3. Validate computed indices are >= 1 before formatting the path.

Example fix

// before
Get(xml, $"rows/row[{arrayIndex}]"); // arrayIndex is 0-based
// after
Get(xml, $"rows/row[{arrayIndex + 1}]"); // convert to 1-based
Defensive patterns

Strategy: validation

Validate before calling

static string ToOneBasedSegment(string name, int zeroBased) =>
    $"{name}[{Math.Max(1, zeroBased + 1)}]";

Try / catch

try { Get(xml, path); }
catch (ArgumentException ex) when (ex.Message.Contains("Index must be >= 1"))
{ /* adjust index to 1-based */ }

Prevention

When it happens

Trigger: Calling a path navigator with a segment like 'row[0]' or 'slide[-1]' — a valid integer that is below the 1-based minimum.

Common situations: Assuming zero-based indexing (common from array/JSON backgrounds); passing a computed index without adjusting from 0-based to 1-based; negative index from an off-by-one calc.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/8f1c3626b25d70fc. Report an issue: GitHub.