Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ ratatui = "0.26"
crossterm = "0.28"
terminal-colorsaurus = "1.0"

# Home directory resolution
home = "0.5"

# Error handling
miette = { version = "7", features = ["fancy"] }
thiserror = "2"
Expand Down
14 changes: 9 additions & 5 deletions crates/openshell-cli/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,11 @@ fn package_managed_tls_dirs() -> Vec<PathBuf> {

let state_dir = std::env::var_os("XDG_STATE_HOME")
.map(PathBuf::from)
.or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".local/state")));
.or_else(|| {
openshell_core::paths::home_dir()
.ok()
.map(|home| home.join(".local/state"))
});
if let Some(state_dir) = state_dir {
dirs.push(state_dir.join("openshell/tls"));
}
Expand Down Expand Up @@ -4351,10 +4355,10 @@ fn read_gcloud_adc() -> Result<(String, String, String)> {
{
PathBuf::from(config_dir).join("application_default_credentials.json")
} else {
let home = std::env::var("HOME")
.map_err(|_| miette::miette!("HOME is not set; cannot locate gcloud ADC file"))?;
PathBuf::from(home)
.join(".config")
let home = openshell_core::paths::home_dir().map_err(|_| {
miette::miette!("could not determine home directory; cannot locate gcloud ADC file")
})?;
home.join(".config")
.join("gcloud")
.join("application_default_credentials.json")
};
Expand Down
6 changes: 2 additions & 4 deletions crates/openshell-cli/src/ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1454,10 +1454,8 @@ fn openshell_ssh_config_path() -> Result<PathBuf> {
}

fn user_ssh_config_path() -> Result<PathBuf> {
let home = std::env::var("HOME")
.into_diagnostic()
.wrap_err("HOME is not set")?;
Ok(PathBuf::from(home).join(".ssh").join("config"))
let home = openshell_core::paths::home_dir()?;
Ok(home.join(".ssh").join("config"))
}

fn render_include_line(path: &Path) -> String {
Expand Down
1 change: 1 addition & 0 deletions crates/openshell-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tonic = { workspace = true, features = ["channel", "tls-native-roots"] }
tonic-prost = { workspace = true }
tokio = { workspace = true }
thiserror = { workspace = true }
home = { workspace = true }
miette = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/openshell-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn podman_socket_candidates() -> Vec<PathBuf> {
podman_socket_candidates_from_env(
socket,
std::env::var_os("XDG_RUNTIME_DIR").map(PathBuf::from),
std::env::var_os("HOME").map(PathBuf::from),
crate::paths::home_dir().ok(),
)
}

Expand Down Expand Up @@ -216,8 +216,8 @@ fn docker_socket_candidates() -> Vec<PathBuf> {

candidates.push(PathBuf::from("/var/run/docker.sock"));

if let Some(home) = std::env::var_os("HOME") {
candidates.push(PathBuf::from(home).join(".docker/run/docker.sock"));
if let Ok(home) = crate::paths::home_dir() {
candidates.push(home.join(".docker/run/docker.sock"));
}

if let Some(runtime_dir) = std::env::var_os("XDG_RUNTIME_DIR") {
Expand Down
37 changes: 25 additions & 12 deletions crates/openshell-core/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,24 @@
use miette::{IntoDiagnostic, Result, WrapErr};
use std::path::{Path, PathBuf};

/// Resolve the user's home directory in a cross-platform way.
///
/// Uses the `home` crate, which reads `HOME` on Unix and `USERPROFILE`
/// (falling back to `HOMEDRIVE`+`HOMEPATH`) on Windows.
pub fn home_dir() -> Result<PathBuf> {
home::home_dir().ok_or_else(|| miette::miette!("could not determine home directory"))
}

/// Resolve the XDG config base directory.
///
/// Returns `$XDG_CONFIG_HOME` if set, otherwise `$HOME/.config`.
pub fn xdg_config_dir() -> Result<PathBuf> {
if let Ok(path) = std::env::var("XDG_CONFIG_HOME") {
return Ok(PathBuf::from(path));
}
let home = std::env::var("HOME")
.into_diagnostic()
.wrap_err("HOME is not set")?;
Ok(PathBuf::from(home).join(".config"))
Ok(home_dir()
.wrap_err("cannot resolve XDG config directory")?
.join(".config"))
}

/// The top-level `OpenShell` config directory: `$XDG_CONFIG_HOME/openshell/`.
Expand All @@ -38,10 +45,10 @@ pub fn xdg_state_dir() -> Result<PathBuf> {
if let Ok(path) = std::env::var("XDG_STATE_HOME") {
return Ok(PathBuf::from(path));
}
let home = std::env::var("HOME")
.into_diagnostic()
.wrap_err("HOME is not set")?;
Ok(PathBuf::from(home).join(".local").join("state"))
Ok(home_dir()
.wrap_err("cannot resolve XDG state directory")?
.join(".local")
.join("state"))
}

/// The top-level `OpenShell` state directory: `$XDG_STATE_HOME/openshell/`.
Expand All @@ -56,10 +63,10 @@ pub fn xdg_data_dir() -> Result<PathBuf> {
if let Ok(path) = std::env::var("XDG_DATA_HOME") {
return Ok(PathBuf::from(path));
}
let home = std::env::var("HOME")
.into_diagnostic()
.wrap_err("HOME is not set")?;
Ok(PathBuf::from(home).join(".local").join("share"))
Ok(home_dir()
.wrap_err("cannot resolve XDG data directory")?
.join(".local")
.join("share"))
}

/// Create a directory (and parents) with owner-only permissions (`0o700`) on
Expand Down Expand Up @@ -159,6 +166,12 @@ pub fn normalize_path(path: &str) -> String {
mod tests {
use super::*;

#[test]
fn home_dir_returns_ok() {
let dir = home_dir().unwrap();
assert!(dir.is_absolute(), "expected absolute path, got: {dir:?}");
}

#[test]
fn xdg_config_dir_respects_env() {
// This test checks the logic — actual env var mutation is unsafe so
Expand Down
1 change: 1 addition & 0 deletions crates/openshell-driver-podman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rustix = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
thiserror = { workspace = true }
home = { workspace = true }
miette = { workspace = true }

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions crates/openshell-driver-podman/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ impl PodmanComputeConfig {
pub fn default_socket_path() -> PathBuf {
#[cfg(target_os = "macos")]
{
let home = std::env::var("HOME").expect("HOME must be set on macOS");
PathBuf::from(home).join(".local/share/containers/podman/machine/podman.sock")
let home = home::home_dir().expect("home directory must be resolvable on macOS");
home.join(".local/share/containers/podman/machine/podman.sock")
}
#[cfg(target_os = "linux")]
{
Expand Down
1 change: 1 addition & 0 deletions crates/openshell-driver-vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ nix = { workspace = true }
clap = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
home = { workspace = true }
miette = { workspace = true }
url = { workspace = true }
serde = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/openshell-driver-vm/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3229,8 +3229,8 @@ async fn connect_local_container_engine() -> Option<Docker> {
fn podman_socket_path() -> PathBuf {
#[cfg(target_os = "macos")]
{
let home = std::env::var("HOME").unwrap_or_default();
PathBuf::from(home).join(".local/share/containers/podman/machine/podman.sock")
let home = home::home_dir().unwrap_or_default();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the vm driver we use openshell_core::paths::home_dir() instead of the external crate. Is that intentional?

home.join(".local/share/containers/podman/machine/podman.sock")
}
#[cfg(target_os = "linux")]
{
Expand Down
4 changes: 2 additions & 2 deletions crates/openshell-providers/src/providers/opencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ fn opencode_config_path() -> Option<PathBuf> {
.ok()
.map(PathBuf::from)
.or_else(|| {
std::env::var("HOME")
openshell_core::paths::home_dir()
.ok()
.map(|h| PathBuf::from(h).join(".config"))
.map(|h| h.join(".config"))
})?;
Some(config_home.join("opencode").join("opencode.json"))
}
Expand Down
6 changes: 3 additions & 3 deletions crates/openshell-server/src/compute/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ fn resolve_driver_search_dirs(vm_config: &VmComputeConfig) -> Vec<PathBuf> {
push_unique_path(&mut dirs, prefix.join("libexec"));
push_unique_path(&mut dirs, prefix.join("libexec").join("openshell"));
}
for dir in VmComputeConfig::default_driver_search_dirs(
std::env::var_os("HOME").map(PathBuf::from),
) {
for dir in
VmComputeConfig::default_driver_search_dirs(openshell_core::paths::home_dir().ok())
{
push_unique_path(&mut dirs, dir);
}
dirs
Expand Down
Loading