spectreconsole/spectre.console · error · InvalidOperationException

Console height must be greater than zero

Error message

Console height must be greater than zero

What it means

Thrown by the Profile.Height setter when the value is <= 0. The same logic as the Width setter: Height is auto-detected from the terminal or explicitly overridden, and a non-positive value would corrupt layout (page breaks, progress bar positioning). The setter enforces > 0.

Source

Thrown at src/Spectre.Console/Profile.cs:97

            {
                throw new InvalidOperationException("Console width must be greater than zero");
            }

            _width = value;
        }
    }

    /// <summary>
    /// Gets or sets an explicit console height.
    /// </summary>
    public int Height
    {
        get => _height ?? _out.Height;
        set
        {
            if (value <= 0)
            {
                throw new InvalidOperationException("Console height must be greater than zero");
            }

            _height = value;
        }
    }

    /// <summary>
    /// Gets or sets the capabilities of the profile.
    /// </summary>
    public Capabilities Capabilities
    {
        get => _capabilities;
        set
        {
            _capabilities = value ?? throw new InvalidOperationException("Profile capabilities cannot be null");
        }
    }

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Clamp height to a minimum positive value: profile.Height = Math.Max(1, detectedHeight);
  2. Use AnsiConsole.Testing.TestConsole with proper dimensions for tests.
  3. In CI, set LINES env var or use a PTY so height detection returns positive.
  4. Avoid setting Height explicitly when the terminal is available.

Example fix

// before
profile.Height = ConsoleHelper.GetHeight(); // returns 0 headless

// after
var detected = ConsoleHelper.GetHeight();
profile.Height = detected > 0 ? detected : 24; // safe default
Defensive patterns

Strategy: validation

Validate before calling

// Clamp height to positive before setting
var height = detectedHeight > 0 ? detectedHeight : 24;
AnsiConsole.Profile.Height = height;

Type guard

static bool IsValidConsoleDimension(int value) => value > 0;

Prevention

When it happens

Trigger: Setting AnsiConsole.Profile.Height = 0 or a negative value. Also hit when height auto-detection returns 0 in a headless/CI environment and that value is passed to the setter.

Common situations: Running in CI without a TTY where terminal height detection returns 0; custom IAnsiConsoleOutput with Height = 0; misconfigured test harness passing zero height; redirecting stdout to a non-terminal.

Related errors


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