From ca8ca3001da66c99bfdc019f88206e65467aec67 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 13 Feb 2022 13:23:08 +0100 Subject: [PATCH] fix(render): override PATH env variable on macOS to mitigate differences in shell extension executions. Fix #966 --- espanso-render/src/extension/exec_util.rs | 104 ++++++++++++++++++++++ espanso-render/src/extension/mod.rs | 3 +- espanso-render/src/extension/shell.rs | 59 ++++++++++-- 3 files changed, 160 insertions(+), 6 deletions(-) create mode 100644 espanso-render/src/extension/exec_util.rs diff --git a/espanso-render/src/extension/exec_util.rs b/espanso-render/src/extension/exec_util.rs new file mode 100644 index 0000000..855ccc0 --- /dev/null +++ b/espanso-render/src/extension/exec_util.rs @@ -0,0 +1,104 @@ +/* + * This file is part of espanso. + * + * Copyright (C) 2019-2022 Federico Terzi + * + * espanso is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * espanso is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with espanso. If not, see . + */ + +pub enum MacShell { + Bash, + Sh, + Zsh, +} + +// Determine the PATH env variable value available inside a regular terminal session +#[cfg(target_os = "macos")] +pub fn determine_path_env_variable_override(explicit_shell: Option) -> Option { + let shell: MacShell = explicit_shell.or_else(determine_default_macos_shell)?; + + match shell { + MacShell::Bash => { + launch_command_and_get_output("bash", &["--login", "-c", "source ~/.bashrc; echo $PATH"]) + } + MacShell::Sh => launch_command_and_get_output("sh", &["--login", "-c", "echo $PATH"]), + MacShell::Zsh => { + launch_command_and_get_output("zsh", &["--login", "-c", "source ~/.zshrc; echo $PATH"]) + } + } +} + +#[cfg(not(target_os = "macos"))] +pub fn determine_path_env_variable_override(explicit_shell: Option) -> Option { + None +} + +#[cfg(target_os = "macos")] +pub fn determine_default_macos_shell() -> Option { + use regex::Regex; + use std::process::Command; + + let output = Command::new("sh") + .args(&["--login", "-c", "dscl . -read ~/ UserShell"]) + .output() + .ok()?; + + lazy_static! { + static ref EXTRACT_SHELL_REGEX: Regex = + Regex::new(r"UserShell:\s(.*)$").expect("unable to generate regex to extract default shell"); + } + + if !output.status.success() { + return None; + } + + let output_str = String::from_utf8_lossy(&output.stdout); + let captures = EXTRACT_SHELL_REGEX.captures(output_str.trim())?; + + let shell = captures.get(1)?.as_str().trim(); + + if shell.ends_with("/bash") { + Some(MacShell::Bash) + } else if shell.ends_with("/zsh") { + Some(MacShell::Zsh) + } else if shell.ends_with("/sh") { + Some(MacShell::Sh) + } else { + None + } +} + +#[cfg(not(target_os = "macos"))] +pub fn determine_default_macos_shell() -> Option { + None +} + +#[cfg(target_os = "macos")] +fn launch_command_and_get_output(command: &str, args: &[&str]) -> Option { + use std::process::Command; + + let output = Command::new(command).args(args).output().ok()?; + + if !output.status.success() { + return None; + } + + let output_str = String::from_utf8_lossy(&output.stdout); + Some(output_str.to_string()) +} + +#[cfg(not(target_os = "macos"))] +fn launch_command_and_get_output(command: &str, args: &[&str]) -> Option { + None +} diff --git a/espanso-render/src/extension/mod.rs b/espanso-render/src/extension/mod.rs index 4cc7356..1f620ef 100644 --- a/espanso-render/src/extension/mod.rs +++ b/espanso-render/src/extension/mod.rs @@ -1,7 +1,7 @@ /* * This file is part of espanso. * - * Copyright (C) 2019-2021 Federico Terzi + * Copyright (C) 2019-2022 Federico Terzi * * espanso is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,6 +21,7 @@ pub mod choice; pub mod clipboard; pub mod date; pub mod echo; +mod exec_util; pub mod form; pub mod random; pub mod script; diff --git a/espanso-render/src/extension/shell.rs b/espanso-render/src/extension/shell.rs index 4fe4250..a060b7f 100644 --- a/espanso-render/src/extension/shell.rs +++ b/espanso-render/src/extension/shell.rs @@ -1,7 +1,7 @@ /* * This file is part of espanso. * - * Copyright (C) 2019-2021 Federico Terzi + * Copyright (C) 2019-2022 Federico Terzi * * espanso is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,7 +24,7 @@ use std::{ }; use crate::{Extension, ExtensionOutput, ExtensionResult, Params, Value}; -use log::{error, info}; +use log::{debug, error, info}; use thiserror::Error; #[allow(clippy::upper_case_acronyms)] @@ -35,10 +35,16 @@ pub enum Shell { WSL2, Bash, Sh, + Zsh, } impl Shell { - fn execute_cmd(&self, cmd: &str, vars: &HashMap) -> std::io::Result { + fn execute_cmd( + &self, + cmd: &str, + vars: &HashMap, + override_path_on_macos: bool, + ) -> std::io::Result { let mut is_wsl = false; let mut command = match self { @@ -74,6 +80,11 @@ impl Shell { command.args(&["-c", cmd]); command } + Shell::Zsh => { + let mut command = Command::new("zsh"); + command.args(&["-c", cmd]); + command + } }; // Set the OS-specific flags @@ -84,6 +95,27 @@ impl Shell { command.env(key, value); } + // If Espanso is executed as an app bundle on macOS, it doesn't inherit the PATH + // environment variables that are available inside a terminal, and this can be confusing for users. + // For example, one might use "jq" inside the terminal but then it throws an error with "command not found" + // if launched through the Espanso shell extension. + // For this reason, Espanso tries to obtain the same PATH value by spawning a login shell and extracting + // the PATH after the processing. + if cfg!(target_os = "macos") && override_path_on_macos { + let supported_mac_shell = match self { + Shell::Bash => Some(super::exec_util::MacShell::Bash), + Shell::Sh => Some(super::exec_util::MacShell::Sh), + Shell::Zsh => Some(super::exec_util::MacShell::Zsh), + _ => None, + }; + if let Some(path_env_override) = + super::exec_util::determine_path_env_variable_override(supported_mac_shell) + { + debug!("overriding PATH env variable with: {}", path_env_override); + command.env("PATH", path_env_override); + } + } + // In WSL environment, we have to specify which ENV variables // should be passed to linux. // For more information: https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/ @@ -110,6 +142,7 @@ impl Shell { "wsl2" => Some(Shell::WSL2), "bash" => Some(Shell::Bash), "sh" => Some(Shell::Sh), + "zsh" => Some(Shell::Zsh), _ => None, } } @@ -120,7 +153,17 @@ impl Default for Shell { if cfg!(target_os = "windows") { Shell::Powershell } else if cfg!(target_os = "macos") { - Shell::Sh + lazy_static! { + static ref DEFAULT_MACOS_SHELL: Option = + super::exec_util::determine_default_macos_shell(); + } + + match *DEFAULT_MACOS_SHELL { + Some(super::exec_util::MacShell::Bash) => Shell::Bash, + Some(super::exec_util::MacShell::Sh) => Shell::Sh, + Some(super::exec_util::MacShell::Zsh) => Shell::Zsh, + None => Shell::Sh, + } } else if cfg!(target_os = "linux") { Shell::Bash } else { @@ -172,7 +215,13 @@ impl Extension for ShellExtension { self.config_path.to_string_lossy().to_string(), ); - match shell.execute_cmd(cmd, &env_variables) { + let macos_override_path = params + .get("macos_override_path") + .and_then(|v| v.as_bool()) + .copied() + .unwrap_or(true); + + match shell.execute_cmd(cmd, &env_variables, macos_override_path) { Ok(output) => { let output_str = String::from_utf8_lossy(&output.stdout); let error_str = String::from_utf8_lossy(&output.stderr);