Skip to content

InvalidOperationException in SegmentShape.Calculate when rendering an empty Table with hidden headers #2015

@rodrigomfvn

Description

@rodrigomfvn

Information

  • OS: Windows
  • Version: 0.54.0
  • Terminal: Windows Terminal / Any

Describe the bug
A System.InvalidOperationException: Sequence contains no elements is thrown during the rendering process when a Table has no rows and has ShowHeaders set to false (either by directly setting the property or calling HideHeaders()). This happens because SegmentShape.Calculate attempts to call Enumerable.Max on an empty collection of renderable lines.

To Reproduce

using Spectre.Console;

// 1. Create a table with no content and hidden headers
var table = new Table().HideHeaders();

// 2. Attempt to render it via Live display
AnsiConsole.Live(table).Start(ctx =>
{
    // 3. This triggers the crash
    ctx.Refresh();
});

Expected behavior
The console should render nothing (or empty space) without throwing an exception. SegmentShape.Calculate should return a width of 0 if no segments are generated.

Stack Trace

System.InvalidOperationException: Sequence contains no elements
   at System.Linq.ThrowHelper.ThrowNoElementsException()
   at System.Linq.Enumerable.MaxInteger[TSource,TResult](IEnumerable`1 source, Func`2 selector)
   at Spectre.Console.Rendering.SegmentShape.Calculate(RenderOptions options, List`1 lines) in /_/src/Spectre.Console/Rendering/SegmentShape.cs:line 22
   at Spectre.Console.Rendering.LiveRenderable.Render(RenderOptions options, Int32 maxWidth)+MoveNext() in /_/src/Spectre.Console/Live/LiveRenderable.cs:line 89
   at System.Collections.Generic.List`1.AddRange(IEnumerable`1 collection)
   at Spectre.Console.RenderableExtensions.GetSegments(IAnsiConsole console, RenderOptions options, IEnumerable`1 renderables) in /_/src/Spectre.Console/Extensions/RenderableExtensions.cs:line 37
   at Spectre.Console.RenderableExtensions.GetSegments(IRenderable renderable, IAnsiConsole console) in /_/src/Spectre.Console/Extensions/RenderableExtensions.cs:line 29
   at Spectre.Console.AnsiBuilder.Build(IAnsiConsole console, IRenderable renderable) in /_/src/Spectre.Console/Internal/Backends/Ansi/AnsiBuilder.cs:line 17
   at Spectre.Console.AnsiConsoleBackend.Write(IRenderable renderable) in /_/src/Spectre.Console/Internal/Backends/Ansi/AnsiConsoleBackend.cs:line 30
   at Spectre.Console.AnsiConsoleFacade.Write(IRenderable renderable) in /_/src/Spectre.Console/Internal/Backends/AnsiConsoleFacade.cs:line 40
   at Spectre.Console.LiveDisplayContext.Refresh() in /_/src/Spectre.Console/Live/LiveDisplayContext.cs:line 41
   at CertificateManager.Program.<>c.<Run>b__1_0(LiveDisplayContext ctx) in E:\Visual Studio\CertificateManager\CertificateManager\Program.cs:line 72
   at Spectre.Console.LiveDisplay.<>c__DisplayClass15_0.<Start>b__0(LiveDisplayContext ctx) in /_/src/Spectre.Console/Live/LiveDisplay.cs:line 47
   at Spectre.Console.LiveDisplay.<>c__DisplayClass17_0.<<StartAsync>b__0>d.MoveNext() in /_/src/Spectre.Console/Live/LiveDisplay.cs:line 80
--- End of stack trace from previous location ---
   at Spectre.Console.LiveDisplay.<>c__DisplayClass18_0`1.<<StartAsync>b__0>d.MoveNext() in /_/src/Spectre.Console/Live/LiveDisplay.cs:line 110
--- End of stack trace from previous location ---
   at Spectre.Console.Internal.DefaultExclusivityMode.RunAsync[T](Func`1 func) in /_/src/Spectre.Console/Internal/DefaultExclusivityMode.cs:line 40
   at Spectre.Console.LiveDisplay.StartAsync[T](Func`2 func) in /_/src/Spectre.Console/Live/LiveDisplay.cs:line 98
   at Spectre.Console.LiveDisplay.StartAsync(Func`2 func) in /_/src/Spectre.Console/Live/LiveDisplay.cs:line 78
   at Spectre.Console.LiveDisplay.Start(Action`1 action) in /_/src/Spectre.Console/Live/LiveDisplay.cs:line 51

Additional context
The issue is located in src/Spectre.Console/Rendering/SegmentShape.cs inside the Calculate method.

var width = lines.Max(l => Segment.CellCount(l));

This line fails when lines is an empty list.

Proposed Fix
Change the line to:

var width = lines.Count > 0 ? lines.Max(l => Segment.CellCount(l)) : 0;

Please upvote 👍 this issue if you are interested in it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds triageSomeone needs to take a look at this

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions