spectreconsole/spectre.console · error · InvalidOperationException
Console width must be greater than zero
Error message
Console width must be greater than zero
What it means
Thrown by the Profile.Width setter when the value is <= 0. Profile holds the console dimensions used for layout calculations. Width is normally auto-detected from the terminal, but can be explicitly overridden (e.g., for AnsiConsole.Create with a custom output, or for testing). A non-positive width would break every layout algorithm in Spectre.Console, so the setter rejects it immediately.
Source
Thrown at src/Spectre.Console/Profile.cs:80
throw new InvalidOperationException("Encoding cannot be null");
}
_out.SetEncoding(value);
_encoding = value;
}
}
/// <summary>
/// Gets or sets an explicit console width.
/// </summary>
public int Width
{
get => _width ?? _out.Width;
set
{
if (value <= 0)
{
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");
}View on GitHub (pinned to 0acc92fada)
Solutions
- Ensure the width value is positive before assigning: profile.Width = Math.Max(1, detectedWidth);
- When using AnsiConsole.Create, pass a real terminal or a correctly-sized TestConsole.
- In CI, set COLUMNS/LINES env vars or use a PTY so width detection returns a positive value.
- Do not set Width explicitly unless overriding for a known terminal size — let auto-detection handle it.
Example fix
// before var profile = new Profile(output, capabilities, encoding); profile.Width = ConsoleHelper.GetWidth(); // may return 0 // after var detected = ConsoleHelper.GetWidth(); profile.Width = detected > 0 ? detected : 80; // safe default
Defensive patterns
Strategy: validation
Validate before calling
// Clamp width to positive before setting var width = detectedWidth > 0 ? detectedWidth : 80; AnsiConsole.Profile.Width = width;
Type guard
static bool IsValidConsoleDimension(int value) => value > 0;
Prevention
- Do not set Width explicitly unless overriding — let auto-detection work.
- In CI/headless, set COLUMNS env var or use a PTY so detection returns positive.
- Clamp detected width: Math.Max(1, value) before assignment.
- Use TestConsole with correct dimensions for tests.
When it happens
Trigger: Setting AnsiConsole.Profile.Width = 0, AnsiConsole.Profile.Width = -1, or passing a non-positive width when constructing a custom console profile. Also when reading width from an environment where Console width detection returns 0 (e.g., no TTY) and that value is passed to the setter.
Common situations: CI/headless environments where the detected console width is 0 and code forwards it; testing with AnsiConsole.Create and a bad width; misconfigured AnsiConsoleSettings; custom IAnsiConsoleOutput reporting Width = 0.
Related errors
- Console height must be greater than zero
- Profile enricher of type '{enricher.GetType().FullName}' doe
- Failed to read input in non-interactive mode.
- At least one column must be specified.
- Output writer was null
AI-assisted analysis of spectreconsole/spectre.console@0acc92fada (2026-08-13).
Data as JSON: /api/errors/5f839881a705f985.
Report an issue: GitHub.