spectreconsole/spectre.console · error · InvalidOperationException

Size must be equal to or greater than 1

Error message

Size must be equal to or greater than 1

What it means

Thrown by the Layout.Size setter when the nullable value, when set, is less than 1. Size fixes an exact rendered width; a non-null value below 1 is invalid (null means auto-size).

Source

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

            _minimumSize = value;
        }
    }

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

            _size = value;
        }
    }

    /// <summary>
    /// Gets or sets a value indicating whether or not the layout should
    /// be visible or not.
    /// </summary>
    /// <remarks>Defaults to <c>true</c>.</remarks>
    public bool IsVisible { get; set; } = true;

    /// <summary>
    /// Gets the splitter used for this layout.
    /// </summary>
    internal LayoutSplitter Splitter => _splitter;

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Set Size to null for automatic sizing.
  2. Set Size to a positive integer when a fixed width is required.
  3. Clamp computed sizes with value <= 0 ? null : value.

Example fix

// before
layout.Size = computedWidth; // could be 0

// after
layout.Size = computedWidth > 0 ? computedWidth : null;
Defensive patterns

Strategy: validation

Validate before calling

layout.Size = computedWidth > 0 ? computedWidth : null;

Type guard

static int? SanitizeSize(int? value) => value.HasValue && value.Value < 1 ? null : value;

Prevention

When it happens

Trigger: Setting Layout.Size to a non-null int that is 0 or negative.

Common situations: Assigning 0 to mean 'automatic' (use null instead), or computing a fixed width that underflows to 0 in a narrow terminal.

Related errors


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