From 4d4f780f8be706d3117bae58f0cd25e0817a7691 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 8 Aug 2021 15:21:41 -0700 Subject: [PATCH] pty: unix: return an error if the executable doesn't exist Without this check, something, presumably in the guts of rust itself, generates `fatal runtime error: assertion failed: output.write(&bytes).is_ok()` on the output stream of the spawned child when attempting to spawn `wsl.exe` on linux. refs: #1005 --- pty/src/cmdbuilder.rs | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/pty/src/cmdbuilder.rs b/pty/src/cmdbuilder.rs index f71b78be2..a0433888d 100644 --- a/pty/src/cmdbuilder.rs +++ b/pty/src/cmdbuilder.rs @@ -129,6 +129,28 @@ impl CommandBuilder { self.umask = mask; } + fn search_path(exe: &OsStr) -> anyhow::Result { + let exe_path: &std::path::Path = exe.as_ref(); + if exe_path.is_relative() { + if let Some(path) = std::env::var_os("PATH") { + for path in std::env::split_paths(&path) { + let candidate = path.join(&exe); + if candidate.exists() { + return Ok(candidate.into_os_string()); + } + } + } + } + if !exe_path.exists() { + anyhow::bail!( + "Unable to spawn {} because it doesn't exist on the filesystem", + exe_path.display() + ); + } + + Ok(exe.to_owned()) + } + /// Convert the CommandBuilder to a `std::process::Command` instance. pub(crate) fn as_command(&self) -> anyhow::Result { use std::os::unix::process::CommandExt; @@ -144,7 +166,9 @@ impl CommandBuilder { cmd.arg0(&format!("-{}", basename)); cmd } else { - let mut cmd = std::process::Command::new(&self.args[0]); + let resolved = Self::search_path(&self.args[0])?; + let mut cmd = std::process::Command::new(&resolved); + cmd.arg0(&self.args[0]); cmd.args(&self.args[1..]); cmd };