spectreconsole/spectre.console · error · InvalidOperationException

Cannot split the same layout twice

Error message

Cannot split the same layout twice

What it means

Thrown by the private Layout.Split method when the layout already has children. A layout can only be split once; calling SplitRows or the column split a second time is not permitted because the node is already subdivided.

Source

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

            }
        }
    }

    private IEnumerable<Layout> GetChildren(bool visibleOnly = false)
    {
        return visibleOnly ? _children.Where(c => c.IsVisible) : _children;
    }

    private bool HasChildren(bool visibleOnly = false)
    {
        return visibleOnly ? _children.Any(c => c.IsVisible) : _children.Any();
    }

    private void Split(LayoutSplitter splitter, Layout[] layouts)
    {
        if (_children.Length > 0)
        {
            throw new InvalidOperationException("Cannot split the same layout twice");
        }

        _splitter = splitter ?? throw new ArgumentNullException(nameof(splitter));
        _children = layouts ?? throw new ArgumentNullException(nameof(layouts));
    }

    private Dictionary<Layout, LayoutRender> MakeRenderMap(RenderOptions options, int maxWidth)
    {
        var result = new Dictionary<Layout, LayoutRender>();

        var renderWidth = maxWidth;
        var renderHeight = options.Height ?? options.ConsoleSize.Height;
        var regionMap = MakeRegionMap(maxWidth, renderHeight);

        foreach (var (layout, region) in regionMap.Where(x => !x.Layout.HasChildren(visibleOnly: true)))
        {
            var segments = layout.Renderable.Render(options with { Height = region.Height }, region.Width);

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Split each layout node only once.
  2. To restructure, create a fresh layout tree instead of re-splitting an existing node.
  3. Remove the duplicate Split call.

Example fix

// before
layout.SplitRows(new Layout("a"), new Layout("b"));
layout.SplitRows(new Layout("c")); // throws

// after
var layout = new Layout("root");
layout.SplitRows(new Layout("a"), new Layout("b"));
// do not split again; build a new tree if restructuring is needed
Defensive patterns

Strategy: validation

Validate before calling

// Only call SplitRows/SplitColumns once per layout node.
// Track split state in your own code if dynamic reconfiguration is needed.

Prevention

When it happens

Trigger: Calling SplitRows (or SplitColumns) twice on the same Layout instance, or splitting a layout that already received children.

Common situations: Reconfiguring a layout at runtime by re-splitting, or calling Split after a prior Split in initialization code.

Related errors


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