Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.IO.IOException : The handle is invalid - GitHub Actions, Windows #151

Closed
eduherminio opened this issue Nov 28, 2020 · 5 comments
Closed
Labels
bug Something isn't working

Comments

@eduherminio
Copy link
Contributor

eduherminio commented Nov 28, 2020

I'm using spectre.console in a library (this file) and I have a test project that runs simple tests over it.

Everything works like a charm locally in both Windows and Linux but I have a CI pipeline configured using GitHub Actions, and tests are failing there in windows-latest agents.
This is the failed run. I also tried using pwsh as shell, but same result.

From what I understand, it's failing when invoking Console.Clear();, which I do before rendering a table:

table.AddRow(problemTitle, $"Part {part}", solution, $"[{color}]{elapsedTime}[/]");

Console.Clear();
AnsiConsole.Render(table);

PD:
I'm trying to use Console.Clear() because using AnsiConsole.Console.Clear() works for the animation of the table rows being populated, but floods stdout with partial tables when scrolling up after the program is run.
Am I missing a better way of doing it?

Final result (it can be tested in this project):
image

But when scrolling up:
image

@patriksvensson
Copy link
Contributor

patriksvensson commented Nov 28, 2020

Console.Clear() won't work in places like GitHub actions on a Windows machine, since there isn't an actual console handle to clear and will throw. AnsiConsole.Clear will use ANSI escape sequences to clear the screen, however, not sure if this is supported on GitHub and is probably why you see strange output there.

I think your best approach here is to try to detect whether or not running in an non-interactive environment and not run the clear call and animations at all.

@eduherminio
Copy link
Contributor Author

I think your best approach here is to try to detect whether or not running in an non-interactive environment and not run the clear call and animations at all.

hmm making the tests in this repo pass is relatively easy, but my real issue here is that I'm using this code as part of a NuGet package.

If I use <DefineConstants> depending on env variables + preprocessor, I'll get await with it in this repo, but other projects that will use of this package won't be able to get around the problem when using non-interactive environments, right?

Can you think of any way around it?

@patriksvensson
Copy link
Contributor

Environment.Interactive in combination with the environment variables CI servers define should be enough.

@eduherminio
Copy link
Contributor Author

hmm that's the key, I was missing the point by using the environment variables at compile time rather than at runtime.
Thanks a lot (and congrats on the project!).

eduherminio added a commit to eduherminio/AoCHelper that referenced this issue Nov 28, 2020
* Use [`Spectre.Console`](https://github.com/spectresystems/spectre.console).
* Avoid issues with `Console.Clear()` by checking `Environment.UserInteractive` (spectreconsole/spectre.console#151) and `Console.IsOutputRedirected` ([SO](https://stackoverflow.com/questions/31692714/the-handle-is-invalid-exception-in-visual-studio-2015-test-runner)).
  No special CI server environment variable will be used for now, but this may need to change in the future if issues are detected (hint for future me: revert d2e4691).
* Remove redundant tests.
@patriksvensson patriksvensson added the bug Something isn't working label Oct 4, 2021
@antibarbie
Copy link

We just hit the same problem, when running spectre output in a gitlab runner.

Maybe this could be patched/tested in spectre library directly ? so that programs would run safely whether they are running in hostile environments or not.

it feels wrongs to throw IOException just because output is redirected..
Ok I stop the rant, shall I propose a pull request if I have time ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants