term: Fix editor closing `stderr` stream

This commit is contained in:
Alexis Sellier 2023-05-15 15:03:03 +02:00
parent 94cd7658b1
commit 0c41de1433
No known key found for this signature in database
3 changed files with 12 additions and 1 deletions

1
Cargo.lock generated
View File

@ -2141,6 +2141,7 @@ dependencies = [
"anyhow",
"inquire",
"is-terminal",
"libc",
"once_cell",
"pretty_assertions",
"tempfile",

View File

@ -10,6 +10,7 @@ anyhow = { version = "1" }
anstyle-query = { version = "1.0.0" }
is-terminal = { version = "0.4.4" }
inquire = { version = "0.6", default-features = false, features = ["termion", "editor"] }
libc = { version = "0.2" }
once_cell = { version = "1.13" }
termion = { version = "2" }
unicode-width = { version = "0.1.10", default-features = false }

View File

@ -67,8 +67,17 @@ impl Editor {
)
);
};
// We duplicate the stderr file descriptor to pass it to the child process, otherwise, if
// we simply pass the `RawFd` of our stderr, `Command` will close our stderr when the
// child exits.
let stderr = io::stderr().as_raw_fd();
let stderr = unsafe { libc::dup(stderr) };
process::Command::new(cmd)
.stdout(unsafe { process::Stdio::from_raw_fd(io::stderr().as_raw_fd()) })
.stdout(unsafe { process::Stdio::from_raw_fd(stderr) })
.stderr(process::Stdio::inherit())
.stdin(process::Stdio::inherit())
.arg(&self.path)
.spawn()?
.wait()?;