How to allow `Process` to receive user input when run as part of an executable (e.g. to enabled `sudo` commands)

This is likely happens because Swift's Process uses posix_spawn(2), which has no support for setting terminal process group TPGID, at least on Darwin.

An easy fix would be to call tcsetpgrp(3) after process.run() as follows:

tcsetpgrp(STDIN_FILENO, process.processIdentifier)

See macos - Process cannot read from the standard input in Swift - Stack Overflow for more details.

2 Likes