spectreconsole/spectre.console · error · InvalidOperationException

Ratio must be equal to or greater than 1

Error message

Ratio must be equal to or greater than 1

What it means

Thrown by the Layout.Ratio setter when the value is less than 1. Ratio controls how space is distributed among siblings; a zero or negative ratio is meaningless so it must be at least 1.

Source

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

    /// Gets or sets the name.
    /// </summary>
    public string? Name { get; set; }

    /// <summary>
    /// Gets or sets the ratio.
    /// </summary>
    /// <remarks>
    /// Defaults to <c>1</c>.
    /// Must be greater than <c>0</c>.
    /// </remarks>
    public int Ratio
    {
        get => _ratio;
        set
        {
            if (value < 1)
            {
                throw new InvalidOperationException("Ratio must be equal to or greater than 1");
            }

            _ratio = value;
        }
    }

    /// <summary>
    /// Gets or sets the minimum width.
    /// </summary>
    /// <remarks>
    /// Defaults to <c>1</c>.
    /// Must be greater than <c>0</c>.
    /// </remarks>
    public int MinimumSize
    {
        get => _minimumSize;
        set
        {

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Set Ratio to a value >= 1.
  2. Use Math.Max(1, ratio) when ratios are computed.
  3. To hide a layout, set IsVisible = false rather than Ratio = 0.

Example fix

// before
layout.Ratio = computedWeight; // could be 0

// after
layout.Ratio = Math.Max(1, computedWeight);
// or to hide: layout.IsVisible = false;
Defensive patterns

Strategy: validation

Validate before calling

if (ratio < 1) throw new ArgumentOutOfRangeException(nameof(ratio), "Ratio must be >= 1");
layout.Ratio = ratio;

Type guard

static bool IsValidRatio(int value) => value >= 1;

Prevention

When it happens

Trigger: Setting Layout.Ratio to 0 or a negative integer, including via object initializer or fluent builder.

Common situations: Computing ratios from weights that can be zero, or resetting a ratio to 0 intending to hide the layout (use IsVisible=false instead).

Related errors


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