From 7f50fd94b5bbf0c5c5b4aab660954ce9fb485111 Mon Sep 17 00:00:00 2001 From: Sebastian Gieseler Date: Fri, 6 Nov 2026 12:54:46 +0100 Subject: [PATCH] fix: devstack finds the stack file from any folder of the repository Without -f, every command looks for devstack.toml in the current folder, then in each folder above it up to the repository root (the first folder holding .git). Run from an app's folder, status, logs and lane list answered "no daemon is running" although the stack ran. Where nothing is found, a command says where it looked; a missing -f file is named. Closes #467 Bug-Class: a default path resolved against the caller's working directory Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 13 +++++ crates/devstack/src/main.rs | 112 ++++++++++++++++++++++++++++++++++-- crates/devstack/src/mcp.rs | 6 ++ docs/stack-file.md | 5 ++ skills/devstack/SKILL.md | 2 +- 5 files changed, 132 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5fe2e86..353b2358 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,19 @@ and listed with their assets on the ## [Unreleased] +### Changed + +- **devstack finds the stack file from any folder of the repository.** + Without `-f`, a command looks for `devstack.toml` in the current folder, + then in each folder above it up to the repository root (the first folder + holding `.git`), as git finds `.git`. Run from an app's folder, `devstack + status`, `logs` and `lane list` used to answer "no daemon is running" + although the stack ran; they now mean the stack around it. Where no stack + file is found, a command says where it looked instead (`no devstack.toml + in or above it up to the repository root`), and a missing `-f` + file is named as such. `-f` is taken as given, and `devstack init` still + writes into the current folder. + ## [0.41.0] - 2026-11-05 A stack's daemon now ends with its last tree (ADR-0095): every tree, diff --git a/crates/devstack/src/main.rs b/crates/devstack/src/main.rs index e4b2eb93..14d8e506 100644 --- a/crates/devstack/src/main.rs +++ b/crates/devstack/src/main.rs @@ -26,9 +26,10 @@ mod update; #[derive(Parser)] #[command(version, about = "Local development orchestrator")] struct Cli { - /// Stack file. - #[arg(long, short = 'f', global = true, default_value = "devstack.toml")] - file: PathBuf, + /// Stack file [default: devstack.toml in this directory or the nearest + /// one above it, up to the repository root]. + #[arg(long, short = 'f', global = true)] + file: Option, #[command(subcommand)] cmd: Cmd, } @@ -945,6 +946,58 @@ fn repo_of(file: &Path) -> Result { Ok(abs.parent().map(Path::to_path_buf).unwrap_or(abs)) } +const STACK_FILE: &str = "devstack.toml"; + +/// The stack file without `-f`: in `dir`, else in the nearest directory +/// above it, like git finds `.git`, so that a command run from an app's +/// folder means the stack around it (#467). The search ends at the +/// repository root (the first directory holding `.git`, a folder in the +/// main tree, a file in a worktree): a repository nested in another +/// never reaches the outer one's stack. Found in `dir` itself, the path +/// stays relative, as it was before the search. +fn find_stack_file(dir: &Path) -> Option { + if dir.join(STACK_FILE).is_file() { + return Some(PathBuf::from(STACK_FILE)); + } + let mut at = dir; + while !at.join(".git").exists() { + at = at.parent()?; + let file = at.join(STACK_FILE); + if file.is_file() { + return Some(file); + } + } + None +} + +/// The commands that cannot do without the stack file: without one they +/// say where devstack looked, not "no daemon is running" (#467). `init` +/// writes one; `doctor` and `mcp` report its absence themselves; `down` +/// still clears what a stack left behind; `tunnel` sets up the machine's +/// tunnel too; the rest belong to the machine. +fn needs_stack_file(cmd: &Cmd) -> bool { + matches!( + cmd, + Cmd::Check { .. } + | Cmd::Build { .. } + | Cmd::Up { .. } + | Cmd::Status + | Cmd::Journal { .. } + | Cmd::Env { .. } + | Cmd::Start { .. } + | Cmd::Stop { .. } + | Cmd::Restart { .. } + | Cmd::HotReload { .. } + | Cmd::Promote { .. } + | Cmd::Rollback { .. } + | Cmd::Logs { .. } + | Cmd::Builds { .. } + | Cmd::Top { .. } + | Cmd::Run { .. } + | Cmd::Lane { .. } + ) +} + /// Every command goes to the area that handles it ([`dispatch`]). /// `devstack hook`: the agent's input on stdin, the texts on stdout; /// nothing on any error, so that a prompt is never held up. @@ -966,6 +1019,29 @@ fn main() -> Result<()> { // limit up to the hard one). let _ = devstack_proc::raise_open_files(); let Cli { file, cmd } = Cli::parse(); + let file = match file { + Some(f) => { + if needs_stack_file(&cmd) && !f.is_file() { + anyhow::bail!("the stack file {} does not exist", f.display()); + } + f + } + None => match std::env::current_dir() + .ok() + .and_then(|d| find_stack_file(&d)) + { + Some(found) => found, + None => { + if needs_stack_file(&cmd) { + anyhow::bail!( + "no {STACK_FILE} in {} or above it up to the repository root (run it from the stack or pass -f)", + std::env::current_dir()?.display() + ); + } + PathBuf::from(STACK_FILE) + } + }, + }; match cmd { cmd @ (Cmd::Check { .. } | Cmd::Build { .. } @@ -1036,15 +1112,41 @@ mod tests { #[test] fn the_stack_file_defaults_and_is_global() { - assert_eq!(parse(&["status"]).file, PathBuf::from("devstack.toml")); + assert_eq!(parse(&["status"]).file, None, "found by the search"); let cli = parse(&["restart", "api", "-f", "other.toml", "--rebuild"]); - assert_eq!(cli.file, PathBuf::from("other.toml")); + assert_eq!(cli.file, Some(PathBuf::from("other.toml"))); assert!(matches!( cli.cmd, Cmd::Restart { service, rebuild: true } if service == "api" )); } + /// From a folder below the stack, the stack around it (#467); never + /// past the repository root, never past a nested repository's. + #[test] + fn the_stack_file_is_found_above_up_to_the_repository_root() { + let root = tempfile::tempdir().unwrap(); + let outer = root.path().join("outer"); + let app = outer.join("apps").join("web").join("src"); + std::fs::create_dir_all(&app).unwrap(); + std::fs::create_dir_all(outer.join(".git")).unwrap(); + std::fs::write(outer.join(STACK_FILE), "").unwrap(); + // Found in the folder itself: as before, relative. + assert_eq!(find_stack_file(&outer), Some(PathBuf::from(STACK_FILE))); + assert_eq!(find_stack_file(&app), Some(outer.join(STACK_FILE))); + + // A worktree marks its root with a `.git` file. + let nested = app.join("vendor"); + std::fs::create_dir_all(nested.join("pkg")).unwrap(); + std::fs::write(nested.join(".git"), "gitdir: elsewhere").unwrap(); + assert_eq!(find_stack_file(&nested.join("pkg")), None); + + // Outside any repository the search ends at the filesystem's root. + let loose = root.path().join("loose").join("deep"); + std::fs::create_dir_all(&loose).unwrap(); + assert_eq!(find_stack_file(&loose), None); + } + #[test] fn up_takes_its_lane_options() { let Cmd::Up { diff --git a/crates/devstack/src/mcp.rs b/crates/devstack/src/mcp.rs index 23cb5b01..aa252176 100644 --- a/crates/devstack/src/mcp.rs +++ b/crates/devstack/src/mcp.rs @@ -572,6 +572,12 @@ fn name_arg<'a>(args: &'a Value, key: &str) -> Result<&'a str> { } fn connect(file: &Path) -> Result { + if !file.is_file() { + anyhow::bail!( + "no stack file {}: the MCP server was started outside a stack", + std::path::absolute(file)?.display() + ); + } // A daemon that runs but refuses or hangs says so: `up` would not help. client::Client::probe(&client::state_dir(file)?)? .ok_or_else(|| anyhow::anyhow!("no daemon is running for this stack; call `up` first")) diff --git a/docs/stack-file.md b/docs/stack-file.md index 1612477c..d478151d 100644 --- a/docs/stack-file.md +++ b/docs/stack-file.md @@ -4,6 +4,11 @@ One file in the repository root describes the local stack: the containers it borrows from Compose, the native processes devstack builds and runs, and the commands people and agents can trigger. Schema version 1 ([ADR-0017](adr/0017-stack-file-is-toml.md)). +- **Any folder of the repository will do.** Without `-f`, every command looks + for `devstack.toml` in the current folder, then in each folder above it up + to the repository root (the first folder holding `.git`), so a command run + from an app's folder means the stack around it. `-f ` is taken as + given, relative to the current folder. - **Every table rejects unknown keys.** A typo is an error, never a silently ignored setting. - **Validate after every edit:** `devstack check` (add `--compose` to let diff --git a/skills/devstack/SKILL.md b/skills/devstack/SKILL.md index 7a3a672f..8a62feaa 100644 --- a/skills/devstack/SKILL.md +++ b/skills/devstack/SKILL.md @@ -43,7 +43,7 @@ PATH of an already running agent process: then use the full path ## 2. Operate -Run from the repository root (devstack looks for `{{devstack:stack_file}}` there; `-f` otherwise). +Run from anywhere in the repository: without `-f`, devstack looks for `devstack.toml` in the current folder and the folders above it, up to the repository root (`{{devstack:stack_file}}` under another name needs `-f`). | Command | Effect | | --- | --- |