From cce19713fb970df1a96be7e76e3217e4e50efc42 Mon Sep 17 00:00:00 2001 From: gavynriebau Date: Mon, 16 Jan 2023 15:13:48 +0800 Subject: [PATCH] Fix for lost clipboard contents (#5424) (#5426) * Fix for lost clipboard contents (#5424) * PR feedback: Call "setsid" for all unix systems * PR Feedback: Only install libc for unix targets --- Cargo.lock | 1 + helix-view/Cargo.toml | 3 +++ helix-view/src/clipboard.rs | 21 ++++++++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 69ba84449..ede4ae0d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1274,6 +1274,7 @@ dependencies = [ "helix-lsp", "helix-tui", "helix-vcs", + "libc", "log", "once_cell", "serde", diff --git a/helix-view/Cargo.toml b/helix-view/Cargo.toml index e7a20496d..7d130317e 100644 --- a/helix-view/Cargo.toml +++ b/helix-view/Cargo.toml @@ -48,5 +48,8 @@ which = "4.2" [target.'cfg(windows)'.dependencies] clipboard-win = { version = "4.5", features = ["std"] } +[target.'cfg(unix)'.dependencies] +libc = "0.2" + [dev-dependencies] helix-tui = { path = "../helix-tui" } diff --git a/helix-view/src/clipboard.rs b/helix-view/src/clipboard.rs index 4f83fb4dc..3c620c146 100644 --- a/helix-view/src/clipboard.rs +++ b/helix-view/src/clipboard.rs @@ -276,12 +276,27 @@ pub mod provider { let stdin = input.map(|_| Stdio::piped()).unwrap_or_else(Stdio::null); let stdout = pipe_output.then(Stdio::piped).unwrap_or_else(Stdio::null); - let mut child = Command::new(self.prg) + let mut command: Command = Command::new(self.prg); + + let mut command_mut: &mut Command = command .args(self.args) .stdin(stdin) .stdout(stdout) - .stderr(Stdio::null()) - .spawn()?; + .stderr(Stdio::null()); + + // Fix for https://github.com/helix-editor/helix/issues/5424 + if cfg!(unix) { + use std::os::unix::process::CommandExt; + + unsafe { + command_mut = command_mut.pre_exec(|| match libc::setsid() { + -1 => Err(std::io::Error::last_os_error()), + _ => Ok(()), + }); + } + } + + let mut child = command_mut.spawn()?; if let Some(input) = input { let mut stdin = child.stdin.take().context("stdin is missing")?;