Skip to content

armgit / xerj

Public Imported from GitHub
0

AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.

fix(home): fall back to USERPROFILE when HOME is unset (native Windows)

xerj-org2cd117d142d7
diff --git a/engine/crates/xerj-autoindex/src/init.rs b/engine/crates/xerj-autoindex/src/init.rs
index c9fb411fd..668227aae 100644
--- a/engine/crates/xerj-autoindex/src/init.rs
+++ b/engine/crates/xerj-autoindex/src/init.rs
@@ -490,8 +490,9 @@ pub(crate) fn run_init_in(cwd: &Path, url: &str, dry: bool) -> Result<Vec<String
Ok(out)
}
+/// `HOME`, else `USERPROFILE` (native Windows usually has no `HOME`).
fn dirs_home() -> Option<PathBuf> {
- std::env::var_os("HOME").map(PathBuf::from)
+ xerj_common::home::home_dir()
}
#[cfg(test)]
diff --git a/engine/crates/xerj-common/src/home.rs b/engine/crates/xerj-common/src/home.rs
new file mode 100644
index 000000000..056cbfa18
--- /dev/null
+++ b/engine/crates/xerj-common/src/home.rs
@@ -0,0 +1,64 @@
+//! The user's home directory, resolved the same way on every platform.
+//!
+//! `$HOME` is the Unix convention and is usually **unset on native Windows**,
+//! where the profile directory is `%USERPROFILE%`. Reading `HOME` alone sent
+//! Windows users' hub cache and agent config to `./.xerj/...` (the current
+//! directory) or skipped them entirely. This is the one rule: a non-empty
+//! `HOME`, else a non-empty `USERPROFILE`, else `None`.
+
+use std::ffi::OsString;
+use std::path::PathBuf;
+
+/// The home directory from the process environment. See [`home_dir_from`].
+pub fn home_dir() -> Option<PathBuf> {
+ home_dir_from(&|key| std::env::var_os(key))
+}
+
+/// [`home_dir`] over an injectable environment, so the rule is testable without
+/// mutating the process-global one (tests run in parallel).
+pub fn home_dir_from(env: &dyn Fn(&str) -> Option<OsString>) -> Option<PathBuf> {
+ ["HOME", "USERPROFILE"]
+ .into_iter()
+ .find_map(|key| env(key).filter(|v| !v.is_empty()))
+ .map(PathBuf::from)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn env<'a>(vars: &'a [(&'a str, &'a str)]) -> impl Fn(&str) -> Option<OsString> + 'a {
+ move |key| {
+ vars.iter()
+ .find(|(k, _)| *k == key)
+ .map(|(_, v)| OsString::from(v))
+ }
+ }
+
+ #[test]
+ fn home_wins_over_userprofile() {
+ let e = env(&[("HOME", "/home/u"), ("USERPROFILE", r"C:\Users\u")]);
+ assert_eq!(home_dir_from(&e), Some(PathBuf::from("/home/u")));
+ }
+
+ #[test]
+ fn userprofile_is_the_windows_fallback() {
+ let e = env(&[("USERPROFILE", r"C:\Users\u")]);
+ assert_eq!(home_dir_from(&e), Some(PathBuf::from(r"C:\Users\u")));
+ }
+
+ #[test]
+ fn empty_home_falls_through_to_userprofile() {
+ let e = env(&[("HOME", ""), ("USERPROFILE", r"C:\Users\u")]);
+ assert_eq!(home_dir_from(&e), Some(PathBuf::from(r"C:\Users\u")));
+ }
+
+ #[test]
+ fn neither_set_is_none() {
+ assert_eq!(home_dir_from(&env(&[])), None);
+ assert_eq!(
+ home_dir_from(&env(&[("HOME", ""), ("USERPROFILE", "")])),
+ None
+ );
+ }
+}
diff --git a/engine/crates/xerj-common/src/lib.rs b/engine/crates/xerj-common/src/lib.rs
index 8867ec4ff..f78dfedfd 100644
--- a/engine/crates/xerj-common/src/lib.rs
+++ b/engine/crates/xerj-common/src/lib.rs
@@ -25,6 +25,7 @@
//! - [`metrics`] — Prometheus counters, histograms, and gauges
//! - [`net`] — Network trust primitives (trusted-proxy CIDR matching)
//! - [`resource`] — The machine-resource policy: cores, memory budget, thread priority
+//! - [`home`] — the home directory: `HOME`, else `USERPROFILE` (native Windows)
//! - [`localauth`] — Loopback guard + `<data_dir>/admin.key` discovery for
//! same-machine CLI/MCP commands (`xerj mcp`, `xerj autoindex`, `xerj init`)
//! - [`xccode`] — the reference-coding semantics shared by `xerj code`,
@@ -38,6 +39,7 @@ pub mod error;
pub mod feedback;
pub mod field_coercion;
pub mod fsio;
+pub mod home;
pub mod hub;
pub mod localauth;
pub mod metrics;
diff --git a/engine/crates/xerj-hub/src/cli.rs b/engine/crates/xerj-hub/src/cli.rs
index 247fc9512..c723d9513 100644
--- a/engine/crates/xerj-hub/src/cli.rs
+++ b/engine/crates/xerj-hub/src/cli.rs
@@ -1366,13 +1366,9 @@ fn emit_answer(answer: &Answer, as_json: bool) {
/// `~/.xerj/hub`, overridable by `XERJ_HUB_HOME` (tests). Never `/tmp`: the
/// pack cache and catalogue must survive a reboot (same rule as `code_root`).
pub(crate) fn hub_home() -> PathBuf {
- if let Ok(h) = std::env::var("XERJ_HUB_HOME") {
- if !h.is_empty() {
- return PathBuf::from(h);
- }
- }
- let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
- Path::new(&home).join(".xerj").join("hub")
+ // One rule with `lockfile::default_hub_root` (incl. the Windows
+ // `USERPROFILE` fallback), so `status` and `pull` never disagree.
+ lockfile::default_hub_root()
}
/// The hub's ed25519 public key: `XERJ_HUB_PUBKEY` (tests) else the compiled-in
diff --git a/engine/crates/xerj-hub/src/lockfile.rs b/engine/crates/xerj-hub/src/lockfile.rs
index d07f386ff..901698549 100644
--- a/engine/crates/xerj-hub/src/lockfile.rs
+++ b/engine/crates/xerj-hub/src/lockfile.rs
@@ -265,9 +265,8 @@ pub(crate) fn hub_root_from(env: &dyn Fn(&str) -> Option<std::ffi::OsString>) ->
if let Some(h) = env("XERJ_HUB_HOME").filter(|h| !h.is_empty()) {
return PathBuf::from(h);
}
- let home = env("HOME")
- .map(PathBuf::from)
- .unwrap_or_else(|| PathBuf::from("."));
+ // `HOME`, else `USERPROFILE` — native Windows usually has no `HOME`.
+ let home = xerj_common::home::home_dir_from(env).unwrap_or_else(|| PathBuf::from("."));
home.join(".xerj").join("hub")
}
@@ -884,6 +883,11 @@ mod tests {
hub_root_from(&env(&[("HOME", "/home/u")])),
PathBuf::from("/home/u/.xerj/hub")
);
+ // Native Windows: no HOME, so the profile dir is the home.
+ assert_eq!(
+ hub_root_from(&env(&[("USERPROFILE", "/Users/u")])),
+ PathBuf::from("/Users/u/.xerj/hub")
+ );
// No HOME at all degrades to a relative path, never a panic.
assert_eq!(hub_root_from(&env(&[])), PathBuf::from("./.xerj/hub"));
}