edit icon indicating copy to clipboard operation
edit copied to clipboard

Fix #194: Allow --version/--help to run without TTY access

Open Nihal-Pandey-2302 opened this issue 6 months ago • 5 comments

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 (making sys::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/tty and updates the relevant system state.
  • Adjusted main.rs to:
    1. Call the modified sys::init().
    2. Check for --version or --help arguments. If present, print the info and exit (this path now bypasses the /dev/tty logic).
    3. If not an early exit, call sys::ensure_interactive_tty() before proceeding with full editor initialization.

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 --version and edit --help with 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.

Nihal-Pandey-2302 avatar May 27 '25 19:05 Nihal-Pandey-2302

@microsoft-github-policy-service agree

Nihal-Pandey-2302 avatar May 27 '25 19:05 Nihal-Pandey-2302

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.

lhecker avatar May 29 '25 22:05 lhecker

No worries

Nihal-Pandey-2302 avatar May 30 '25 03:05 Nihal-Pandey-2302

Please inform me if the approach is fine or do I need to try something else?

Nihal-Pandey-2302 avatar Jun 25 '25 03:06 Nihal-Pandey-2302

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)

lhecker avatar Jun 30 '25 13:06 lhecker