{"record":{"id":"5f839881a705f985","repo":"spectreconsole/spectre.console","slug":"console-width-must-be-greater-than-zero","errorCode":null,"errorMessage":"Console width must be greater than zero","messagePattern":"Console width must be greater than zero","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Spectre.Console/Profile.cs","lineNumber":80,"sourceCode":"                throw new InvalidOperationException(\"Encoding cannot be null\");\n            }\n\n            _out.SetEncoding(value);\n            _encoding = value;\n        }\n    }\n\n    /// <summary>\n    /// Gets or sets an explicit console width.\n    /// </summary>\n    public int Width\n    {\n        get => _width ?? _out.Width;\n        set\n        {\n            if (value <= 0)\n            {\n                throw new InvalidOperationException(\"Console width must be greater than zero\");\n            }\n\n            _width = value;\n        }\n    }\n\n    /// <summary>\n    /// Gets or sets an explicit console height.\n    /// </summary>\n    public int Height\n    {\n        get => _height ?? _out.Height;\n        set\n        {\n            if (value <= 0)\n            {\n                throw new InvalidOperationException(\"Console height must be greater than zero\");\n            }","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/spectreconsole/spectre.console/blob/0acc92fada6c42f13984e79c2b5f3d993bdfb099/src/Spectre.Console/Profile.cs#L62-L98","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar profile = new Profile(output, capabilities, encoding);\nprofile.Width = ConsoleHelper.GetWidth(); // may return 0\n\n// after\nvar detected = ConsoleHelper.GetWidth();\nprofile.Width = detected > 0 ? detected : 80; // safe default","handlingStrategy":"validation","validationCode":"// Clamp width to positive before setting\nvar width = detectedWidth > 0 ? detectedWidth : 80;\nAnsiConsole.Profile.Width = width;","typeGuard":"static bool IsValidConsoleDimension(int value) => value > 0;","tryCatchPattern":null,"preventionTips":["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."],"tags":["invalidoperationexception","profile","console-width","configuration","ci"],"backgroundTag":null,"analyzedSha":"0acc92fada6c42f13984e79c2b5f3d993bdfb099","analyzedAt":"2026-08-13T18:27:21.983Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}