iOfficeAI/OfficeCLI · warning · ArgumentException

Invalid path '{path}': empty path segment ('//') is not allo

Error message

Invalid path '{path}': empty path segment ('//') is not allowed.

What it means

Thrown by NormalizeExcelPath when the path contains '//' anywhere (after the leading-slash check). This indicates an empty interior segment, which previously slipped past regex matchers and exposed raw OOXML local names. DOCX already rejected this; XLSX was brought to parity.

Source

Thrown at src/officecli/Handlers/Excel/ExcelHandler.Helpers.Sheet.cs:111

    // quotes and un-double embedded quotes ('It''s'!A1 → It's). Excel REQUIRES
    // the quoted form for names with spaces/punctuation, so refs pasted from
    // formulas arrive quoted. Unquoted names pass through unchanged.
    internal static string UnquoteSheetName(string s)
        => s.Length >= 2 && s[0] == '\'' && s[^1] == '\''
            ? s[1..^1].Replace("''", "'")
            : s;

    internal string NormalizeExcelPath(string path)
    {
        // Reject malformed segment separators that previously slipped past
        // the regex matchers and exposed raw OOXML local names. DOCX already
        // rejects these; bring XLSX up to parity.
        if (path.Length > 1 && path != "/" && path.EndsWith("/"))
            throw new ArgumentException($"Invalid path '{path}': trailing '/' is not allowed.");
        if (path.StartsWith("//"))
            throw new ArgumentException($"Invalid path '{path}': leading '//' is not allowed.");
        if (path.Contains("//"))
            throw new ArgumentException($"Invalid path '{path}': empty path segment ('//') is not allowed.");
        // Handle "/Sheet1!A1" — strip leading '/' when '!' is present so native
        // notation is parsed correctly. EXCEPT when the first slash segment
        // names an existing sheet: '!' is legal inside a sheet NAME (Excel
        // forbids only : \ / ? * [ ]), and reinterpreting "/Q1!Results" as
        // bang notation made such a sheet permanently unaddressable.
        if (path.StartsWith('/') && path.Contains('!'))
        {
            var seg0 = path[1..];
            var seg0Slash = seg0.IndexOf('/');
            var first = seg0Slash < 0 ? seg0 : seg0[..seg0Slash];
            if (!GetWorksheets().Any(w => w.Name.Equals(first, StringComparison.OrdinalIgnoreCase)))
                path = path[1..];
        }
        if (path.Equals("/workbook", StringComparison.OrdinalIgnoreCase)) return "/";
        if (path.StartsWith('/')) return path;
        // Excel-quoted sheet ref: 'My Data'!A1 — the '!' separator is the one
        // FOLLOWING the closing quote (the name itself may contain '!').
        string? qSheet = null; var rest = "";

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Filter out empty segments before joining: split on '/', remove empties, rejoin with single '/'.
  2. Validate with a single Contains("//") check and refuse early.
  3. Use a small path-builder helper that never emits empty segments.

Example fix

// before
string path = "/" + string.Join('/', segments); // segments may contain ""
// after
string path = "/" + string.Join('/', segments.Where(s => !string.IsNullOrEmpty(s)));
Defensive patterns

Strategy: validation

Validate before calling

static bool HasEmptySegment(string p) => p.Contains("//");
static string CollapseSlashes(string p)
    => System.Text.RegularExpressions.Regex.Replace(p, "/{2,}", "/");

Type guard

null

Try / catch

try { NormalizeExcelPath(path); }
catch (ArgumentException ex) when (ex.Message.Contains("empty path segment"))
{ path = System.Text.RegularExpressions.Regex.Replace(path, "/{2,}", "/"); }

Prevention

When it happens

Trigger: Calling an Excel path API with '/Sheet1//A1', '/A/B//C', '/Sheet1///table[1]', or any path with two consecutive slashes not at the start (that case is error 705).

Common situations: Splitting and rejoining segments where an intermediate segment is empty; user input with accidental double-slash; transforming a path through a filter that drops an empty middle component.

Related errors


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