spectreconsole/spectre.console · error · ArgumentException

Must be > 1

Error message

Must be > 1

What it means

Thrown by the Canvas constructor when the width argument is less than 1. The message text 'Must be > 1' is misleading: the guard is 'width < 1', so the true minimum is 1 (i.e. >= 1). The library needs a positive width to allocate the internal _pixels Color?[,] array.

Source

Thrown at src/Spectre.Console/Widgets/Canvas.cs:51

    /// </summary>
    public bool Scale { get; set; } = true;

    /// <summary>
    /// Gets or sets the pixel width.
    /// </summary>
    [Obsolete("Not used anymore. Will be removed in future update.")]
    public int PixelWidth { get; set; } = 2;

    /// <summary>
    /// Initializes a new instance of the <see cref="Canvas"/> class.
    /// </summary>
    /// <param name="width">The canvas width.</param>
    /// <param name="height">The canvas height.</param>
    public Canvas(int width, int height)
    {
        if (width < 1)
        {
            throw new ArgumentException("Must be > 1", nameof(width));
        }

        if (height < 1)
        {
            throw new ArgumentException("Must be > 1", nameof(height));
        }

        Width = width;
        Height = height;

        _pixels = new Color?[Width, Height];
    }

    /// <summary>
    /// Sets a pixel with the specified color in the canvas at the specified location.
    /// </summary>
    /// <param name="x">The X coordinate for the pixel.</param>
    /// <param name="y">The Y coordinate for the pixel.</param>

View on GitHub (pinned to 0acc92fada)

Solutions

  1. Ensure the width argument is >= 1 before constructing the Canvas.
  2. Clamp computed width values to a minimum of 1 using Math.Max(1, width).
  3. Validate input from config/UI before passing to the Canvas constructor.

Example fix

// before
var canvas = new Canvas(consoleWidth, height); // consoleWidth could be 0

// after
var canvas = new Canvas(Math.Max(1, consoleWidth), height);
Defensive patterns

Strategy: validation

Validate before calling

if (width < 1) throw new ArgumentOutOfRangeException(nameof(width), "Canvas width must be >= 1");
var canvas = new Canvas(width, height);

Type guard

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

Prevention

When it happens

Trigger: Instantiating 'new Canvas(width, height)' where width is 0, negative, or derived from an uninitialized/zeroed variable.

Common situations: Reading canvas dimensions from config or user input that defaults to 0, calculating width from a console size that returned 0, or passing a computed width that underflows.

Related errors


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