Skip to content
Merged
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
33 changes: 14 additions & 19 deletions crates/forge_config/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,29 @@ impl ConfigReader {

/// Returns the base directory for all Forge config files.
///
/// If the `FORGE_CONFIG` environment variable is set, its value is used
/// directly as the base path. Otherwise defaults to `~/.forge`.
/// Falls back to the legacy `~/forge` path if it exists, even if `~/.forge`
/// also exists. This prevents tools that eagerly create `~/.forge` (such as
/// the shell plugin's config-edit action) from silently switching the
/// active base path while the user's credentials and config still live
/// in `~/forge`. Once the user runs `forge config migrate` the
/// `~/forge` directory is removed, so this fallback naturally stops
/// applying.
/// Resolution order:
/// 1. `FORGE_CONFIG` environment variable, if set.
/// 2. `~/.forge`, if that directory exists.
/// 3. `~/forge` (legacy path) as a fallback, so users who have not yet run
/// `forge config migrate` continue to read from their existing directory
/// without disruption.
pub fn base_path() -> PathBuf {
if let Ok(path) = std::env::var("FORGE_CONFIG") {
return PathBuf::from(path);
}

let home = dirs::home_dir().unwrap_or(PathBuf::from("."));
let path = home.join(".forge");
let legacy_path = home.join("forge");

// Prefer the legacy ~/forge path while it still exists so that an
// empty ~/.forge directory (e.g. created by `mkdir -p` in the shell
// plugin) does not cause the base path to flip before migration.
if legacy_path.exists() {
tracing::info!("Using legacy path");
return legacy_path;

// Prefer ~/.forge when it exists; fall back to ~/forge for users who
// have not yet migrated.
if path.exists() {
tracing::info!("Using new path");
return path;
}

tracing::info!("Using new path");
path
tracing::info!("Using legacy path");
home.join("forge")
}

/// Adds the provided TOML string as a config source without touching the
Expand Down
Loading