From 8096fde8d98cdde4e110865ebb15ad034b92ff27 Mon Sep 17 00:00:00 2001 From: aur-build Date: Thu, 19 Nov 2026 14:15:23 +0000 Subject: config: the root is the directory config.json lives in MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `--root` names a directory and `init` writes config.json inside it, so the root was already named by where the config lived. The `root` field in the file was treated as a second source of truth instead: a relative one was re-joined onto the config's own directory, and `init` saved whatever it had been handed. So `--root runtime` wrote `"root": "runtime"`, and every command after `init` resolved it to `runtime/runtime/` — a second tree with its own empty database, while `init` had created and migrated the first one. The user-visible half was a path like `runtime/runtime/repo/aur-build/os/x86_64/…` in an error message. Compounding it, the CLI fell back to a default config whenever loading failed, so a typo'd `--root` produced a fresh empty server that answered every command as though it were the one you meant. `load` now sets the root to the directory it found the config in, absolutized, and a missing config is an error for every command except `init` — the only one that runs before one exists. --- src/config.rs | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index bda99e9..00ac3e4 100644 --- a/src/config.rs +++ b/src/config.rs @@ -167,8 +167,19 @@ impl Config { /// `anyhow::Result` here meant two error vocabularies in one crate, and the /// one thing a caller can actually branch on — "there is no config yet, run /// init" — arrived as an opaque string. + /// + /// **The root is the directory the config file was found in.** The `root` + /// field inside the file is written for whoever is looking at the tree, not + /// as a second source of truth: `load` is only ever handed the directory it + /// then reads the config from, so a field that disagreed with its own + /// location could only ever contradict it. Honouring a relative field by + /// re-joining it onto the config's directory is what turned `--root runtime` + /// (which `init` used to save verbatim) into `runtime/runtime/…` — every + /// command after `init` ran against a second tree with its own database, + /// while `init` had created and migrated the first one. pub fn load(root: &Path) -> crate::error::Result { - let p = root.join("config.json"); + let dir = absolute(root)?; + let p = dir.join("config.json"); if !p.exists() { return Err(crate::error::Error::not_found(format!( "no config at {}; run `aur-build init`", @@ -177,19 +188,20 @@ impl Config { } let s = std::fs::read_to_string(&p)?; let mut cfg: Config = serde_json::from_str(&s)?; - // root inside the config file is relative to the config's directory - // unless it is absolute; that keeps a moved tree usable. - if !cfg.root.is_absolute() { - cfg.root = root.join(&cfg.root); - } + cfg.root = dir; Ok(cfg) } pub fn save(&self) -> crate::error::Result<()> { std::fs::create_dir_all(&self.root)?; + let mut out = self.clone(); + // Absolutize on the way out, so a config written by a process that was + // handed a relative `--root` can never be misread — by the next command, + // or by a person editing it by hand. + out.root = absolute(&self.root)?; std::fs::write( self.root.join("config.json"), - serde_json::to_string_pretty(self)? + "\n", + serde_json::to_string_pretty(&out)? + "\n", )?; Ok(()) } @@ -319,6 +331,13 @@ impl Config { } } +/// Absolutize a path without resolving symlinks and without requiring it to +/// exist, both of which `canonicalize` insists on — and `init` saves its config +/// before the tree under it is complete. +fn absolute(p: &Path) -> crate::error::Result { + Ok(std::path::absolute(p)?) +} + /// Filesystem-safe slug for a URL, used in cache filenames. pub fn slug(url: &str) -> String { url.chars() -- cgit v1.2.3