Fix #194: Allow --version/--help to run without TTY access
Fixes #194
Problem:
Running edit --version or edit --help failed in environments where /dev/tty was unavailable and stdin was not a TTY, because the application attempted to open /dev/tty during initial system setup.
Solution: This PR implements the approach suggested by @lhecker in the issue comments:
- Modified
sys::init()to perform only minimal setup for default stdio handles (makingsys::write_stdout()functional) without trying to open/dev/tty. - Introduced a new
sys::ensure_interactive_tty()function. This function now contains the logic to check if stdin is a TTY and, if necessary for interactive mode, opens/dev/ttyand updates the relevant system state. - Adjusted
main.rsto:- Call the modified
sys::init(). - Check for
--versionor--helparguments. If present, print the info and exit (this path now bypasses the/dev/ttylogic). - If not an early exit, call
sys::ensure_interactive_tty()before proceeding with full editor initialization.
- Call the modified
This ensures that --version and --help can execute without needing full TTY access, while interactive mode still correctly sets up the TTY.
Testing Done:
- Verified that
edit --versionandedit --helpwith piped stdin (e.g.,echo "test" | ./target/release/edit --version) now execute successfully and print the correct output. - Confirmed that normal editor operations are unaffected:
edit(opens new file)edit <filename>(opens existing file)echo "content" | ./target/release/edit(correctly ingests piped content while allowing interactive TTY control). All local tests passed.
@microsoft-github-policy-service agree
We're not sure about the approach you've taken and will need longer to decide on whether to adopt this or not. I apologize for the wait.
No worries
Please inform me if the approach is fine or do I need to try something else?
We should instead split up sys::init into two stages:
- Initialize the stdout/stdin handles (the call should never fail)
- Initialize all the rest and switch to raw mode (may fail)