fix(render): override PATH env variable on macOS to mitigate differences in shell extension executions. Fix #966
This commit is contained in:
parent
115e2f2138
commit
ca8ca3001d
104
espanso-render/src/extension/exec_util.rs
Normal file
104
espanso-render/src/extension/exec_util.rs
Normal file
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<MacShell>) -> Option<String> {
|
||||||
|
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<MacShell>) -> Option<String> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
pub fn determine_default_macos_shell() -> Option<MacShell> {
|
||||||
|
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<MacShell> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
fn launch_command_and_get_output(command: &str, args: &[&str]) -> Option<String> {
|
||||||
|
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<String> {
|
||||||
|
None
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* This file is part of espanso.
|
* 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
|
* espanso is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* 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 clipboard;
|
||||||
pub mod date;
|
pub mod date;
|
||||||
pub mod echo;
|
pub mod echo;
|
||||||
|
mod exec_util;
|
||||||
pub mod form;
|
pub mod form;
|
||||||
pub mod random;
|
pub mod random;
|
||||||
pub mod script;
|
pub mod script;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* This file is part of espanso.
|
* 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
|
* espanso is free software: you can redistribute it and/or modify
|
||||||
* it under the terms of the GNU General Public License as published by
|
* 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 crate::{Extension, ExtensionOutput, ExtensionResult, Params, Value};
|
||||||
use log::{error, info};
|
use log::{debug, error, info};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
#[allow(clippy::upper_case_acronyms)]
|
#[allow(clippy::upper_case_acronyms)]
|
||||||
|
@ -35,10 +35,16 @@ pub enum Shell {
|
||||||
WSL2,
|
WSL2,
|
||||||
Bash,
|
Bash,
|
||||||
Sh,
|
Sh,
|
||||||
|
Zsh,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Shell {
|
impl Shell {
|
||||||
fn execute_cmd(&self, cmd: &str, vars: &HashMap<String, String>) -> std::io::Result<Output> {
|
fn execute_cmd(
|
||||||
|
&self,
|
||||||
|
cmd: &str,
|
||||||
|
vars: &HashMap<String, String>,
|
||||||
|
override_path_on_macos: bool,
|
||||||
|
) -> std::io::Result<Output> {
|
||||||
let mut is_wsl = false;
|
let mut is_wsl = false;
|
||||||
|
|
||||||
let mut command = match self {
|
let mut command = match self {
|
||||||
|
@ -74,6 +80,11 @@ impl Shell {
|
||||||
command.args(&["-c", cmd]);
|
command.args(&["-c", cmd]);
|
||||||
command
|
command
|
||||||
}
|
}
|
||||||
|
Shell::Zsh => {
|
||||||
|
let mut command = Command::new("zsh");
|
||||||
|
command.args(&["-c", cmd]);
|
||||||
|
command
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set the OS-specific flags
|
// Set the OS-specific flags
|
||||||
|
@ -84,6 +95,27 @@ impl Shell {
|
||||||
command.env(key, value);
|
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
|
// In WSL environment, we have to specify which ENV variables
|
||||||
// should be passed to linux.
|
// should be passed to linux.
|
||||||
// For more information: https://devblogs.microsoft.com/commandline/share-environment-vars-between-wsl-and-windows/
|
// 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),
|
"wsl2" => Some(Shell::WSL2),
|
||||||
"bash" => Some(Shell::Bash),
|
"bash" => Some(Shell::Bash),
|
||||||
"sh" => Some(Shell::Sh),
|
"sh" => Some(Shell::Sh),
|
||||||
|
"zsh" => Some(Shell::Zsh),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,7 +153,17 @@ impl Default for Shell {
|
||||||
if cfg!(target_os = "windows") {
|
if cfg!(target_os = "windows") {
|
||||||
Shell::Powershell
|
Shell::Powershell
|
||||||
} else if cfg!(target_os = "macos") {
|
} else if cfg!(target_os = "macos") {
|
||||||
Shell::Sh
|
lazy_static! {
|
||||||
|
static ref DEFAULT_MACOS_SHELL: Option<super::exec_util::MacShell> =
|
||||||
|
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") {
|
} else if cfg!(target_os = "linux") {
|
||||||
Shell::Bash
|
Shell::Bash
|
||||||
} else {
|
} else {
|
||||||
|
@ -172,7 +215,13 @@ impl Extension for ShellExtension {
|
||||||
self.config_path.to_string_lossy().to_string(),
|
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) => {
|
Ok(output) => {
|
||||||
let output_str = String::from_utf8_lossy(&output.stdout);
|
let output_str = String::from_utf8_lossy(&output.stdout);
|
||||||
let error_str = String::from_utf8_lossy(&output.stderr);
|
let error_str = String::from_utf8_lossy(&output.stderr);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user