spectreconsole / spectre.console

A .NET library that makes it easier to create beautiful console applications.
https://spectreconsole.net
MIT License
9.14k stars 471 forks source link

Error debugging a console app in VSCode when running `AnsiConsole.Live(table)` #1393

Open eduherminio opened 9 months ago

eduherminio commented 9 months ago

Information

Describe the bug

Unable to debug in in VSCode a console app that renders a table via AnsiConsole.Live(table).

Works as expected running directly from pwsh inside VSCode, and debugging from VS.

Stacktrace:

System.IO.IOException: 'The handle is invalid'
   at System.ConsolePal.set_CursorVisible(Boolean value)
   at Spectre.Console.LegacyConsoleCursor.Show(Boolean show) in /_/src/Spectre.Console/Internal/Backends/Legacy/LegacyConsoleCursor.cs:line 7
   at Spectre.Console.CursorExtensions.Hide(IAnsiConsoleCursor cursor) in /_/src/Spectre.Console/Extensions/CursorExtensions.cs:line 33
   at Spectre.Console.LiveDisplayRenderer.Started() in /_/src/Spectre.Console/Live/LiveDisplayRenderer.cs:line 16
   at Spectre.Console.LiveDisplay.<>c__DisplayClass18_0`1.<<StartAsync>b__0>d.MoveNext() in /_/src/Spectre.Console/Live/LiveDisplay.cs:line 104
--- 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 AoCHelper.Solver.SolveAll(Action`1 options) in C:\dev\_Competitive_Programming\AoCHelper\src\AoCHelper\Solver.cs:line 227
   at Program.<Main>$(String[] args) in C:\dev\_Competitive_Programming\AoCHelper\src\AoCHelper.PoC\Program.cs:line 4
   at Program.<Main>(String[] args)

To Reproduce

Simplest code to reproduce it (debug from VSCode):

using Spectre.Console;

var table = new Table()
    .AddColumns("1", "2");

await AnsiConsole.Live(table)
    .StartAsync(async ctx =>
    {
    });

return;

Real project:

git clone https://github.com/eduherminio/AoCHelper
cd AoCHelper/src/AoCHelper.PoC
code .
<debug from VSCode>

Expected behavior No exception is thrown

Screenshots

From someone consuming the library that has the spectre.console dependency via NuGet (https://github.com/eduherminio/AoCHelper/issues/183) image

From my PoC project (second repro above) that uses the library which spectre.console dependency via project reference image


Please upvote :+1: this issue if you are interested in it.

rafaelsc commented 9 months ago

This error happens in more situations:

patriksvensson commented 9 months ago

Hmm, since stdout is redirected, you shouldn't be able to do live output at all (by design). We will need to investigate this.

eduherminio commented 9 months ago

Just FYI I do check Environment.UserInteractive && !Console.IsOutputRedirected as a requirement to calling AnsiConsole.Clear(), after having issues with CI runners (https://github.com/spectreconsole/spectre.console/issues/151), but never found an issue there with AnsiConsole.Live(table).