From faddf945d7def29b9d11f90f432688ebdd1b1050 Mon Sep 17 00:00:00 2001 From: infosave2007 Date: Sat, 8 Aug 2026 17:57:45 +0300 Subject: [PATCH] fix(config): a foreign project must not capture the config path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit default_config_path treated any directory holding Cargo.toml plus a config/ folder as a gateway checkout, so running cortiq-gateway inside an unrelated project died with "cannot read config config/gateway.toml" instead of using the per-user home. Now ./config/gateway.toml counts only when the file is actually readable, the repo-checkout heuristic requires Cargo.toml to be cortiq-gateway's own, and everything else falls through to $CORTIQ_GATEWAY_HOME / ~/.cortiq-gateway — typing cortiq-gateway in any directory picks up the same configuration. --- src/config.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 75cc5d0..6293ec8 100644 --- a/src/config.rs +++ b/src/config.rs @@ -924,14 +924,23 @@ impl Default for Config { } /// Where the config lives when `--config` is not given. -/// Repo checkout (`config/gateway.toml` exists or `config/` dir exists) → keep it there; -/// installed binary (no repo) → `$data_dir/gateway.toml`. +/// A readable `./config/gateway.toml` (a real gateway checkout / install dir) +/// wins; otherwise the per-user home (`$CORTIQ_GATEWAY_HOME` or +/// `~/.cortiq-gateway`) — so `cortiq-gateway` typed in any directory picks up +/// the same configuration it was set up with. A foreign project that merely +/// has `Cargo.toml` and a `config/` folder must NOT capture the config +/// (that used to shadow the user's real config with a broken path). pub fn default_config_path() -> String { let repo_path = "config/gateway.toml"; - if std::path::Path::new(repo_path).exists() { + // the file itself, readable — not just an entry that exists() + if std::fs::read_to_string(repo_path).is_ok() { return repo_path.to_string(); } - if std::path::Path::new("Cargo.toml").exists() && std::path::Path::new("config").exists() { + // our own repo checkout before first run: config/ exists, file not yet + let ours = std::fs::read_to_string("Cargo.toml") + .map(|t| t.contains("name = \"cortiq-gateway\"")) + .unwrap_or(false); + if ours && std::path::Path::new("config").is_dir() { return repo_path.to_string(); } data_dir()