spectreconsole/spectre.console · error · InvalidOperationException

Could not find layout '{name}'

Error message

Could not find layout '{name}'

What it means

Thrown by Layout.GetLayout after traversing the entire layout tree (via a stack-based DFS) without finding a child whose Name matches the requested name (case-insensitive). The name was valid but no matching node exists.

Source

Thrown at src/Spectre.Console/Widgets/Layout/Layout.cs:173

        var stack = new Stack<Layout>();
        stack.Push(this);

        while (stack.Count > 0)
        {
            var current = stack.Pop();
            if (name.Equals(current.Name, StringComparison.OrdinalIgnoreCase))
            {
                return current;
            }

            foreach (var layout in current.GetChildren())
            {
                stack.Push(layout);
            }
        }

        throw new InvalidOperationException($"Could not find layout '{name}'");
    }

    /// <summary>
    /// Splits the layout into rows.
    /// </summary>
    /// <param name="children">The layout to split into rows.</param>
    /// <returns>The same instance so that multiple calls can be chained.</returns>
    public Layout SplitRows(params Layout[] children)
    {
        Split(LayoutSplitter.Row, children);
        return this;
    }

    /// <summary>
    /// Splits the layout into columns.
    /// </summary>
    /// <param name="children">The layout to split into columns.</param>
    /// <returns>The same instance so that multiple calls can be chained.</returns>

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Verify the exact name spelling against the names used when creating layouts.
  2. Ensure the target layout was actually added as a child via a Split operation.
  3. Enumerate children to confirm available names before lookup.

Example fix

// before
var target = root.GetLayout("contnt"); // typo: should be 'content'

// after
var target = root.GetLayout("content");
Defensive patterns

Strategy: try-catch

Validate before calling

var allNames = CollectLayoutNames(root);
if (!allNames.Contains(name)) throw new KeyNotFoundException($"Layout '{name}' not found");

Try / catch

try { var child = root.GetLayout(name); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Could not find"))
{ /* handle missing layout, fall back */ }

Prevention

When it happens

Trigger: Calling GetLayout with a name that does not correspond to any child layout in the current tree.

Common situations: Typo in the layout name, looking up a name before the layout tree is built, or referencing a layout that was never added via Split/SplitRows.

Related errors


AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13). Data as JSON: /api/errors/1a1105dfcc9a7b54. Report an issue: GitHub.