diff --git a/src/builtins/status.rs b/src/builtins/status.rs index b35a94c84..5dee7f243 100644 --- a/src/builtins/status.rs +++ b/src/builtins/status.rs @@ -1,7 +1,8 @@ use std::os::unix::prelude::*; use super::prelude::*; -use crate::common::{PROGRAM_NAME, bytes2wcstring, get_fish_path}; +use crate::common::{PROGRAM_NAME, bytes2wcstring}; +use crate::env::config_paths::get_fish_path; use crate::future_feature_flags::{self as features, feature_test}; use crate::proc::{ JobControl, get_job_control_mode, get_login, is_interactive_session, set_job_control_mode, diff --git a/src/common.rs b/src/common.rs index 57fbc30c8..26fd88836 100644 --- a/src/common.rs +++ b/src/common.rs @@ -21,11 +21,10 @@ use once_cell::sync::OnceCell; use std::cell::{Cell, RefCell}; use std::env; -use std::ffi::{CStr, CString, OsStr, OsString}; +use std::ffi::{CStr, CString, OsString}; use std::mem; use std::ops::{Deref, DerefMut}; use std::os::unix::prelude::*; -use std::path::PathBuf; use std::sync::atomic::{AtomicI32, AtomicU32, Ordering}; use std::sync::{Arc, MutexGuard}; use std::time; @@ -1577,32 +1576,6 @@ pub fn valid_var_name(s: &wstr) -> bool { !s.is_empty() && s.chars().all(valid_var_name_char) } -/// Get the absolute path to the fish executable itself -pub fn get_fish_path() -> PathBuf { - let Ok(path) = std::env::current_exe() else { - assert!(PROGRAM_NAME.get().unwrap() == "fish"); - return PathBuf::from("fish"); - }; - if path.exists() { - return path; - } - - // When /proc/self/exe points to a file that was deleted (or overwritten on update!) - // then linux adds a " (deleted)" suffix. - // If that's not a valid path, let's remove that awkward suffix. - if path.as_os_str().as_bytes().ends_with(b" (deleted)") { - return path; - } - - if let (Some(filename), Some(parent)) = (path.file_name(), path.parent()) { - if let Some(filename) = filename.to_str() { - let corrected_filename = OsStr::new(filename.strip_suffix(" (deleted)").unwrap()); - return parent.join(corrected_filename); - } - } - path -} - /// A wrapper around Cell which supports modifying the contents, scoped to a region of code. /// This provides a somewhat nicer API than ScopedRefCell because you can directly modify the value, /// instead of requiring an accessor function which returns a mutable reference to a field. diff --git a/src/env/config_paths.rs b/src/env/config_paths.rs index c5178ccba..5062648b3 100644 --- a/src/env/config_paths.rs +++ b/src/env/config_paths.rs @@ -1,6 +1,8 @@ -use crate::common::{BUILD_DIR, get_fish_path}; +use crate::common::{BUILD_DIR, PROGRAM_NAME}; use crate::{FLOG, FLOGF}; use fish_build_helper::workspace_root; +use std::ffi::OsStr; +use std::os::unix::ffi::OsStrExt; use std::path::PathBuf; /// A struct of configuration directories, determined in main() that fish will optionally pass to @@ -141,3 +143,29 @@ fn from_exec_path(unresolved_exec_path: PathBuf) -> Self { Self::static_paths() } } + +/// Get the absolute path to the fish executable itself +pub fn get_fish_path() -> PathBuf { + let Ok(path) = std::env::current_exe() else { + assert!(PROGRAM_NAME.get().unwrap() == "fish"); + return PathBuf::from("fish"); + }; + if path.exists() { + return path; + } + + // When /proc/self/exe points to a file that was deleted (or overwritten on update!) + // then linux adds a " (deleted)" suffix. + // If that's not a valid path, let's remove that awkward suffix. + if path.as_os_str().as_bytes().ends_with(b" (deleted)") { + return path; + } + + if let (Some(filename), Some(parent)) = (path.file_name(), path.parent()) { + if let Some(filename) = filename.to_str() { + let corrected_filename = OsStr::new(filename.strip_suffix(" (deleted)").unwrap()); + return parent.join(corrected_filename); + } + } + path +}