config_paths: use early return

This commit is contained in:
Johannes Altmanninger
2025-09-13 12:18:25 +02:00
parent cef60fe585
commit cacb9f50b8

View File

@@ -59,6 +59,19 @@ pub struct ConfigPaths {
const DOC_DIR: &str = env!("DOCDIR");
impl ConfigPaths {
#[cfg(not(feature = "embed-data"))]
fn static_paths() -> Self {
// Fall back to what got compiled in.
FLOG!(config, "Using compiled in paths:");
Self {
data: Some(PathBuf::from(env!("DATADIR")).join("fish")),
sysconf: PathBuf::from(SYSCONF_DIR).join("fish"),
doc: DOC_DIR.into(),
bin: Some(PathBuf::from(env!("BINDIR"))),
locale: Some(PathBuf::from(env!("LOCALEDIR"))),
}
}
#[cfg(feature = "embed-data")]
fn new(_argv0: &Path, exec_path: PathBuf) -> Self {
FLOG!(config, "embed-data feature is active, ignoring data paths");
@@ -73,60 +86,53 @@ fn new(_argv0: &Path, exec_path: PathBuf) -> Self {
#[cfg(not(feature = "embed-data"))]
fn new(argv0: &Path, exec_path: PathBuf) -> Self {
if let Ok(exec_path) = exec_path.canonicalize() {
let Ok(exec_path) = exec_path.canonicalize() else {
return Self::static_paths();
};
FLOG!(
config,
format!("exec_path: {:?}, argv[0]: {:?}", exec_path, &argv0)
);
// TODO: we should determine program_name from argv0 somewhere in this file
// If we're in Cargo's target directory or CMake's build directory, use the source files.
if exec_path.starts_with(env!("FISH_BUILD_DIR")) {
let workspace_root = fish_build_helper::workspace_root();
FLOG!(
config,
format!("exec_path: {:?}, argv[0]: {:?}", exec_path, &argv0)
"Running out of target directory, using paths relative to workspace root:\n",
workspace_root.display()
);
// TODO: we should determine program_name from argv0 somewhere in this file
return ConfigPaths {
data: Some(workspace_root.join("share")),
sysconf: workspace_root.join("etc"),
doc: workspace_root.join("user_doc/html"),
bin: Some(exec_path.parent().unwrap().to_owned()),
locale: Some(workspace_root.join("share/locale")),
};
}
// If we're in Cargo's target directory or CMake's build directory, use the source files.
if exec_path.starts_with(env!("FISH_BUILD_DIR")) {
let workspace_root = fish_build_helper::workspace_root();
FLOG!(
config,
"Running out of target directory, using paths relative to workspace root:\n",
workspace_root.display()
);
return ConfigPaths {
data: Some(workspace_root.join("share")),
sysconf: workspace_root.join("etc"),
doc: workspace_root.join("user_doc/html"),
bin: Some(exec_path.parent().unwrap().to_owned()),
locale: Some(workspace_root.join("share/locale")),
};
}
let mut paths = ConfigPaths::default();
// The next check is that we are in a relocatable directory tree
if exec_path.ends_with("bin/fish") {
let base_path = exec_path.parent().unwrap().parent().unwrap();
paths = ConfigPaths {
data: Some(base_path.join("share/fish")),
sysconf: base_path.join("etc/fish"),
doc: base_path.join("share/doc/fish"),
bin: Some(base_path.join("bin")),
locale: Some(base_path.join("share/locale")),
}
}
if paths.data.clone().is_some_and(|x| x.exists()) && paths.sysconf.exists() {
// The docs dir may not exist; in that case fall back to the compiled in path.
if !paths.doc.exists() {
paths.doc = PathBuf::from(DOC_DIR);
}
return paths;
let mut paths = ConfigPaths::default();
// The next check is that we are in a relocatable directory tree
if exec_path.ends_with("bin/fish") {
let base_path = exec_path.parent().unwrap().parent().unwrap();
paths = ConfigPaths {
data: Some(base_path.join("share/fish")),
sysconf: base_path.join("etc/fish"),
doc: base_path.join("share/doc/fish"),
bin: Some(base_path.join("bin")),
locale: Some(base_path.join("share/locale")),
}
}
// Fall back to what got compiled in.
FLOG!(config, "Using compiled in paths:");
ConfigPaths {
data: Some(PathBuf::from(env!("DATADIR")).join("fish")),
sysconf: PathBuf::from(SYSCONF_DIR).join("fish"),
doc: DOC_DIR.into(),
bin: Some(PathBuf::from(env!("BINDIR"))),
locale: Some(PathBuf::from(env!("LOCALEDIR"))),
if paths.data.clone().is_some_and(|x| x.exists()) && paths.sysconf.exists() {
// The docs dir may not exist; in that case fall back to the compiled in path.
if !paths.doc.exists() {
paths.doc = PathBuf::from(DOC_DIR);
}
return paths;
}
Self::static_paths()
}
}