Skip to content
This repository was archived by the owner on Nov 3, 2026. It is now read-only.
40 changes: 29 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,11 @@ fn handle_command_dispatch(
return Ok(());
}

// No config found — degraded legacy path with warning
// No config found. A plugin command needs one, as outside a set;
// `meta exec` still runs in the set's checkouts.
if !is_explicit_exec && plugins.claims(&command_args) {
config_not_found(cli, &current_dir);
}
if let Some(tag) = &cli.tag {
anyhow::bail!(
"No meta config found for worktree '{}', so --tag '{}' cannot select its repos",
Expand Down Expand Up @@ -952,16 +956,7 @@ fn handle_command_dispatch(

let absolute_path = match find_meta_config(&current_dir, cli.config.as_ref())? {
Some(path) => path,
None => {
let config_name = cli
.config
.as_ref()
.map(|p| escape_path(p))
.unwrap_or_else(|| config::META_CONFIG_NAME.to_string());
eprintln!("Error: Could not find meta config file '{config_name}'");
eprintln!("Searched from {} up to root", escape_path(&current_dir));
std::process::exit(1);
}
None => config_not_found(cli, &current_dir),
};

let meta_dir = absolute_path.parent().unwrap_or(std::path::Path::new("."));
Expand Down Expand Up @@ -1477,6 +1472,29 @@ fn select_repos(
repo_filter::select(&include, &exclude, &dirs, root, &names())
}

/// Report that no meta config was found from `dir`, then exit 1. A JSON
/// caller also gets the error as JSON on stdout, so it can tell a missing
/// config from any other failure.
fn config_not_found(cli: &Cli, dir: &std::path::Path) -> ! {
let config_name = cli
.config
.as_ref()
.map(|p| escape_path(p))
.unwrap_or_else(|| config::META_CONFIG_NAME.to_string());
let message = format!(
"Could not find meta config file '{config_name}'\nSearched from {} up to root",
escape_path(dir)
);
if cli.json {
let error = serde_json::json!({
"error": {"code": "config_not_found", "message": message}
});
println!("{error}");
}
eprintln!("Error: {message}");
std::process::exit(1);
}

/// Print unrecognized command error with suggestion and help, then exit.
fn unrecognized_command_error(
command_args: &[String],
Expand Down
5 changes: 5 additions & 0 deletions src/subprocess_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ impl SubprocessPluginManager {
})
}

/// Whether a plugin claims this command.
pub fn claims(&self, args: &[String]) -> bool {
self.route(args).is_some()
}

/// Execute a command via the plugin that owns it; false if none does.
pub fn execute(
&self,
Expand Down
61 changes: 61 additions & 0 deletions tests/json_errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//! A `--json` caller can tell a missing meta config from any other failure.

use std::path::Path;
use std::process::{Command, Output};

fn meta(cwd: &Path, home: &Path, args: &[&str]) -> Output {
Command::new(env!("CARGO_BIN_EXE_meta"))
.args(args)
.current_dir(cwd)
.env("HOME", home)
.env("USERPROFILE", home)
.env("XDG_CONFIG_HOME", home.join("xdg"))
.env("XDG_DATA_HOME", home.join("xdg/data"))
.env("GIT_CONFIG_GLOBAL", home.join("gitconfig"))
.env("META_DATA_DIR", home.join("data"))
.output()
.unwrap()
}

fn json_error_code(output: &Output) -> serde_json::Value {
assert_eq!(output.status.code(), Some(1));
let value: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
let message = value["error"]["message"].as_str().unwrap();
assert!(
message.contains("Could not find meta config file"),
"{message}"
);
assert!(String::from_utf8_lossy(&output.stderr).contains(message));
value["error"]["code"].clone()
}

#[test]
fn json_callers_without_a_config_get_a_json_error() {
let temp = tempfile::tempdir().unwrap();
let home = temp.path().join("home");
let outside = temp.path().join("not-a-workspace");
std::fs::create_dir_all(&outside).unwrap();
for args in [
vec!["project", "list", "--recursive", "--json"],
vec!["--json", "git", "worktree", "list"],
vec!["--json", "exec", "--", "true"],
] {
let output = meta(&outside, &home, &args);
assert_eq!(json_error_code(&output), "config_not_found", "{args:?}");
}
// An explicit --config that doesn't exist is the same error inside a workspace
let workspace = temp.path().join("workspace");
std::fs::create_dir_all(&workspace).unwrap();
std::fs::write(workspace.join(".meta.yaml"), "projects: {}\n").unwrap();
let output = meta(
&workspace,
&home,
&["--config", "absent.yaml", "project", "list", "--json"],
);
assert_eq!(json_error_code(&output), "config_not_found");
// Any other failure is not reported as a missing config
std::fs::write(workspace.join(".meta.yaml"), "projects: [unclosed\n").unwrap();
let output = meta(&workspace, &home, &["project", "list", "--json"]);
assert!(!output.status.success());
assert!(!String::from_utf8_lossy(&output.stdout).contains("config_not_found"));
}
1 change: 1 addition & 0 deletions tests/plugin_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ fn missing_config_errors_are_escaped() {
let output = f.run(&["exec", "--", "true"]);
let err = String::from_utf8_lossy(&output.stderr);
assert!(err.contains("'.meta.yaml'"), "{err}");
assert!(output.stdout.is_empty());
}

#[test]
Expand Down
48 changes: 47 additions & 1 deletion tests/plugin_routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ fn meta(root: &Path, args: &[&str]) {
}

fn run_meta(root: &Path, args: &[&str]) -> std::process::Output {
run_meta_in(root, root, args)
}

fn run_meta_in(root: &Path, cwd: &Path, args: &[&str]) -> std::process::Output {
std::process::Command::new(env!("CARGO_BIN_EXE_meta"))
.args(args)
.current_dir(root)
.current_dir(cwd)
.env("HOME", root)
.env("XDG_CONFIG_HOME", root.join("xdg"))
.env("GIT_CONFIG_GLOBAL", root.join("gitconfig"))
Expand Down Expand Up @@ -127,3 +131,45 @@ esac
assert!(stderr.contains("upgrade the plugin"), "{stderr}");
assert!(stderr.contains(&plugin.display().to_string()), "{stderr}");
}

#[test]
fn a_plugin_command_in_a_plain_git_worktree_needs_a_config() {
let temp = tempdir().unwrap();
let root = temp.path().canonicalize().unwrap();
workspace(&root);
fs::remove_file(root.join(".meta.yaml")).unwrap();
// A plain Git worktree under `.worktrees`, with no meta config anywhere
let repo = root.join("plain");
fs::create_dir_all(&repo).unwrap();
for args in [
&["init", "-q"][..],
&["commit", "-q", "--allow-empty", "-m", "init"],
&["worktree", "add", "-q", ".worktrees/x"],
] {
let status = std::process::Command::new("git")
.args(["-c", "user.name=t", "-c", "user.email=t@example.invalid"])
.args(["-c", "commit.gpgSign=false"])
.args(args)
.current_dir(&repo)
.env("GIT_CONFIG_GLOBAL", root.join("gitconfig"))
.env("GIT_CONFIG_NOSYSTEM", "1")
.env_remove("GIT_DIR")
.env_remove("GIT_WORK_TREE")
.status()
.unwrap();
assert!(status.success(), "git {args:?}");
}
let set = repo.join(".worktrees/x");

let output = run_meta_in(&root, &set, &["--json", "touch", "file"]);
assert_eq!(output.status.code(), Some(1));
let value: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
assert_eq!(value["error"]["code"], "config_not_found");
assert!(!set.join("file").exists(), "ran the plugin's command raw");
assert!(!root.join("plugin.log").exists());

// `meta exec` still runs in the set's checkout
let output = run_meta_in(&root, &set, &["exec", "--", "touch", "ran"]);
assert!(output.status.success());
assert!(set.join("ran").exists());
}
Loading