tui-cs/Terminal.Gui · critical · LayoutException

ComputedLayout for "{superView}": "{to}" references a SubVie

Error message

ComputedLayout for "{superView}": "{to}" references a SubView ("{from}").

What it means

During layout, the topological sort that orders views detected that a view's Pos/Dim (e.g. Pos.Right(otherView)) references a view that is a SubView of the referencing view — a circular/parent-to-child dependency. Layout cannot resolve a parent's position based on its own child's geometry.

Source

Thrown at Terminal.Gui/ViewBase/View.Layout.cs:1253

                if (result.Find (v => v == from) is null)
                {
                    result.Add (from);
                }

                // if 'to' is not yet added to the result, add it
                if (result.Find (v => v == to) is null)
                {
                    result.Add (to);
                }

                // remove from edge
                edges.Remove ((from, to));
            }
            else if (from != superView?.GetTopSuperView (to, from) && !ReferenceEquals (from, to))
            {
                if (ReferenceEquals (from.SuperView, to))
                {
                    throw new LayoutException ($"ComputedLayout for \"{superView}\": \"{to}\" " + $"references a SubView (\"{from}\").");
                }

                throw new LayoutException ($"ComputedLayout for \"{superView}\": \"{from}\" "
                                           + $"linked with \"{to}\" was not found. Did you forget to add it to {superView}?");
            }
        }

        // return L (a topologically sorted order)
        return result;
    } // TopologicalSort

    #endregion Topological Sort

    #region Utilities

    /// <summary>
    ///     Gets the size of the SuperView's content (nominally the same as
    ///     the SuperView's <see cref="GetContentSize ()"/>) or the screen size if there's no SuperView.

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Reverse the dependency: have the child reference the parent (child.X = Pos.Right(parent)), not vice versa.
  2. Use Pos.Absolute or Pos.Center for the parent and let children anchor to it.
  3. Review the from/to views named in the exception message to find the inverted reference.

Example fix

// before
parent.X = Pos.Left(parentChild);
// after
parent.X = Pos.Absolute(2);
parentChild.X = Pos.Right(parent);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the referenced view is NOT a SubView of the referencing view before assigning relative Pos/Dim.
if (ReferenceEquals (referenced.SuperView, referencer))
    throw new InvalidOperationException("parent must not reference its own child for position");
referencer.X = Pos.Right(referenced);

Type guard

static bool IsValidRelativeReference (View referencer, View referenced) =>
    !ReferenceEquals (referenced.SuperView, referencer);

Prevention

When it happens

Trigger: A parent view sets its X/Y/Width/Height to a PosView/DimView that points at one of its own SubViews (e.g. parent.X = Pos.Right(child)).

Common situations: Programmatically building a container and accidentally referencing a child for the container's position; copy-pasting layout code from a sibling view into its parent.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/97c8076282b63874. Report an issue: GitHub.