laravel / prompts

Beautiful and user-friendly forms for your command-line PHP applications.
https://laravel.com/docs/prompts
MIT License
532 stars 94 forks source link

[ BugFix ] Handle Failed Terminal Read Gracefully #164

Closed ProjektGopher closed 1 month ago

ProjektGopher commented 1 month ago

In Terminal::read() if the result of fread(STDIN) is false, indicating an error, we return an empty string.

This can be a problem. While this issue is rare, it does appear to be temporary (maybe some sort of race condition?)

The problem is that Concerns/TypedValue::trackTypedValue() expects the string $key passed to the 'on key' listener to not be empty as it expects to be able to read $key[0]. By checking that $key is not empty we can eliminate a repeated E_WARNING: Uninitialized string offset 0 from overwhelming the terminal output when encountered inside of a loop.

I've also added a safeguard in the prompt looping mechanism to continue onto the next iteration if this is encountered.

I haven't found a good way to make a test actually fail when a warning is encountered, but this configuration change shows the warning pretty clearly. And at least it is covered by a test, even if it's not returning a non-zero exitcode.