diff --git a/src/main.rs b/src/main.rs index 6396d61..ce5897d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, ¤t_dir); + } if let Some(tag) = &cli.tag { anyhow::bail!( "No meta config found for worktree '{}', so --tag '{}' cannot select its repos", @@ -952,16 +956,7 @@ fn handle_command_dispatch( let absolute_path = match find_meta_config(¤t_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(¤t_dir)); - std::process::exit(1); - } + None => config_not_found(cli, ¤t_dir), }; let meta_dir = absolute_path.parent().unwrap_or(std::path::Path::new(".")); @@ -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], diff --git a/src/subprocess_plugins.rs b/src/subprocess_plugins.rs index 671ba08..7d09e91 100644 --- a/src/subprocess_plugins.rs +++ b/src/subprocess_plugins.rs @@ -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, diff --git a/tests/json_errors.rs b/tests/json_errors.rs new file mode 100644 index 0000000..09b8481 --- /dev/null +++ b/tests/json_errors.rs @@ -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")); +} diff --git a/tests/plugin_discovery.rs b/tests/plugin_discovery.rs index 0d6d1ca..db9b3ea 100644 --- a/tests/plugin_discovery.rs +++ b/tests/plugin_discovery.rs @@ -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] diff --git a/tests/plugin_routing.rs b/tests/plugin_routing.rs index 2603aaa..46108ca 100644 --- a/tests/plugin_routing.rs +++ b/tests/plugin_routing.rs @@ -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")) @@ -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()); +}