Skip to main content

runner/
lib.rs

1//! # Runner (`runner-run` crate)
2//!
3//! ## Overview
4//!
5//! Universal project task runner.
6//!
7//! `runner` auto-detects your project's toolchain (package managers, task
8//! runners, version constraints) and provides a unified interface to run
9//! tasks, install dependencies, clean artifacts, and execute ad-hoc commands.
10//!
11//! ## Supported Ecosystems
12//!
13//! **Package managers/ecosystems:** [npm], [yarn], [pnpm], [bun], [cargo],
14//! [deno], [uv], [poetry], [pipenv], [go], [bundler], [composer]
15//!
16//! **Task runners:** [turbo], [nx], [make], [just], [go-task], [mise], [bacon]
17//!
18//! [npm]: https://www.npmjs.com/
19//! [yarn]: https://yarnpkg.com/
20//! [pnpm]: https://pnpm.io/
21//! [bun]: https://bun.sh/
22//! [cargo]: https://doc.rust-lang.org/cargo/
23//! [deno]: https://deno.land/
24//! [uv]: https://github.com/astral-sh/uv/
25//! [poetry]: https://python-poetry.org/
26//! [pipenv]: https://pipenv.pypa.io/
27//! [go]: https://go.dev/
28//! [bundler]: https://bundler.io/
29//! [composer]: https://getcomposer.org/
30//! [turbo]: https://turborepo.dev/
31//! [nx]: https://nx.dev/
32//! [make]: https://www.gnu.org/software/make/
33//! [just]: https://just.systems/
34//! [go-task]: https://taskfile.dev/
35//! [mise]: https://mise.jdx.dev/
36//! [bacon]: https://dystroy.org/bacon/
37//!
38//! ## Library API
39//!
40//! - [`run_from_env`] parses process args and dispatches in current dir.
41//! - [`run_from_args`] parses explicit args and dispatches in current dir.
42//! - [`run_in_dir`] parses explicit args and dispatches against a given dir.
43//!
44//! ## CLI Usage
45//!
46//! ```bash
47//! runner              # show detected project info
48//! runner <task>       # run a task (falls back to package-manager exec)
49//! run <task>          # alias binary: a same-named task wins, else the
50//!                     #   built-in default (install/clean/list/info/
51//!                     #   completions), else PM exec
52//! runner run <target> # explicit unified run: task → built-in → PM exec
53//! runner install      # ALWAYS the built-in (deps); a task named `install`
54//!                     #   is reached via `run install`
55//! runner clean        # remove caches and build artifacts (always built-in)
56//! runner list         # list available tasks from all sources (always built-in)
57//! ```
58// Generate docs with `cargo doc --document-private-items --open`.
59
60#![doc(
61    html_logo_url = "https://raw.githubusercontent.com/kjanat/runner/d876a0b9716806d92e07f5d5560b022b6158ecd5/branding/icon.svg",
62    html_favicon_url = "https://raw.githubusercontent.com/kjanat/runner/d876a0b9716806d92e07f5d5560b022b6158ecd5/branding/icon.svg"
63)]
64
65pub(crate) mod chain;
66mod cli;
67mod cmd;
68mod complete;
69mod config;
70mod detect;
71mod resolver;
72mod schema;
73mod tool;
74mod types;
75
76use std::ffi::OsString;
77use std::io::IsTerminal;
78use std::path::{Path, PathBuf};
79
80use anyhow::{Result, bail};
81use clap::{CommandFactory, FromArgMatches};
82use colored::Colorize;
83
84use resolver::ResolveError;
85
86/// JSON Schema for `runner.toml`. Built under the `schema` feature;
87/// `runner schema` renders it.
88#[cfg(feature = "schema")]
89#[must_use]
90pub fn config_schema() -> schemars::Schema {
91    schemars::schema_for!(config::RunnerConfig)
92}
93
94/// Exit code semantics:
95/// - `0` — success
96/// - `1` — generic failure (I/O, detection, child-process non-zero)
97/// - `2` — resolver could not satisfy intent (typed resolver error)
98///
99/// `main` and `bin/run.rs` use this to map an [`anyhow::Error`] to the
100/// right code: anything that downcasts to the internal resolver-error
101/// type is 2, everything else is 1. The resolver-error type itself is
102/// crate-private; only the exit-code projection is part of the
103/// library's public surface.
104#[must_use]
105pub fn exit_code_for_error(err: &anyhow::Error) -> i32 {
106    if err.downcast_ref::<ResolveError>().is_some() {
107        2
108    } else {
109        1
110    }
111}
112
113const REPOSITORY_URL: &str = env!("CARGO_PKG_REPOSITORY");
114const VERSION: &str = clap::crate_version!();
115
116/// Parse process args, detect current dir, dispatch, return exit code.
117///
118/// When the `COMPLETE` environment variable is set (e.g. `COMPLETE=zsh`),
119/// this function writes shell completions to stdout and exits without
120/// running the normal command dispatch.
121///
122/// # Errors
123///
124/// Returns an error when reading current dir fails, project detection fails,
125/// command execution fails, or writing clap output fails.
126///
127/// Argument parsing/help/version flows are rendered by clap and returned as an
128/// exit code instead of terminating the host process.
129pub fn run_from_env() -> Result<i32> {
130    let bin = bin_name_from_arg0(&std::env::args_os().next().unwrap_or_default())
131        .unwrap_or_else(|| "runner".to_string());
132    clap_complete::CompleteEnv::with_factory(move || {
133        configure_cli_command(cli::Cli::command(), true)
134            .name(bin.clone())
135            .bin_name(bin.clone())
136    })
137    .shells(complete::SHELLS)
138    .complete();
139    run_from_args(std::env::args_os())
140}
141
142/// Parse explicit args, detect current dir, dispatch, return exit code.
143///
144/// `args` must include `argv[0]` as first item.
145///
146/// # Errors
147///
148/// Returns an error when reading current dir fails, project detection fails,
149/// command execution fails, or writing clap output fails.
150///
151/// Argument parsing/help/version flows are rendered by clap and returned as an
152/// exit code instead of terminating the host process.
153pub fn run_from_args<I, T>(args: I) -> Result<i32>
154where
155    I: IntoIterator<Item = T>,
156    T: Into<OsString> + Clone,
157{
158    let cwd = std::env::current_dir()?;
159    run_in_dir(args, &cwd)
160}
161
162/// Parse explicit args and run against `dir`.
163///
164/// `args` must include `argv[0]` as first item.
165///
166/// # Errors
167///
168/// Returns an error when project detection fails, command execution fails, or
169/// writing clap output fails.
170///
171/// Argument parsing/help/version flows are rendered by clap and returned as an
172/// exit code instead of terminating the host process.
173pub fn run_in_dir<I, T>(args: I, dir: &Path) -> Result<i32>
174where
175    I: IntoIterator<Item = T>,
176    T: Into<OsString> + Clone,
177{
178    let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
179
180    if requests_version(&args) {
181        println!("{}", version_line(&args, std::io::stdout().is_terminal()));
182        return Ok(0);
183    }
184
185    let cli = match parse_cli(args) {
186        Ok(cli) => cli,
187        Err(err) => return render_clap_error(&err),
188    };
189    let project_dir = resolve_project_dir(
190        configured_project_dir(
191            cli.global.project_dir.as_deref(),
192            std::env::var_os("RUNNER_DIR").as_deref(),
193        )
194        .as_deref(),
195        dir,
196    )?;
197    dispatch(cli, &project_dir)
198}
199
200fn parse_cli<I, T>(args: I) -> Result<cli::Cli, clap::Error>
201where
202    I: IntoIterator<Item = T>,
203    T: Into<OsString> + Clone,
204{
205    let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
206
207    let mut command = configure_cli_command(cli::Cli::command(), std::io::stdout().is_terminal());
208    if let Some(bin_name) = args.first().and_then(bin_name_from_arg0) {
209        command = command.name(bin_name.clone()).bin_name(bin_name);
210    }
211    command = shorten_help_subcommand(command);
212
213    let matches = command.try_get_matches_from(args)?;
214    cli::Cli::from_arg_matches(&matches)
215}
216
217/// Replace clap's verbose default `help` subcommand description
218/// (`"Print this message or the help of the given subcommand(s)"`) with a terse
219/// one. clap only injects the implicit `help` subcommand during `Command::build`,
220/// so force the build first; the `Built` flag makes the later parse-time build a
221/// no-op. Guarded with `find_subcommand` because a flat command without
222/// subcommands (the `run` alias) never gets a `help` entry, and `mut_subcommand`
223/// panics on a missing name. Must run after `name`/`bin_name` are set, since
224/// `build` snapshots bin names.
225fn shorten_help_subcommand(mut command: clap::Command) -> clap::Command {
226    command.build();
227    if command.find_subcommand("help").is_some() {
228        command.mut_subcommand("help", |help| help.about("Print help for a subcommand"))
229    } else {
230        command
231    }
232}
233
234/// Parse process args as the `run` alias binary, detect the current dir,
235/// dispatch, and return the exit code.
236///
237/// Always treats positional arguments as a task or command (routed through
238/// `cmd::run`) — built-in subcommand names are never parsed specially, so
239/// `run clean`, `run install`, etc. run a same-named project task when one
240/// exists. When no such task exists, a bare run token naming a built-in verb
241/// (`install`/`clean`/`list`/`info`/`completions`) falls back to that
242/// built-in's default form rather than the package-manager exec path.
243///
244/// When the `COMPLETE` environment variable is set, writes shell completions
245/// to stdout and exits without running the normal command dispatch.
246///
247/// # Errors
248///
249/// Returns an error when reading current dir fails, project detection fails,
250/// command execution fails, or writing clap output fails.
251///
252/// Argument parsing/help/version flows are rendered by clap and returned as an
253/// exit code instead of terminating the host process.
254pub fn run_alias_from_env() -> Result<i32> {
255    let bin = bin_name_from_arg0(&std::env::args_os().next().unwrap_or_default())
256        .unwrap_or_else(|| "run".to_string());
257    clap_complete::CompleteEnv::with_factory(move || {
258        configure_cli_command(cli::RunAliasCli::command(), true)
259            .name(bin.clone())
260            .bin_name(bin.clone())
261    })
262    .shells(complete::SHELLS)
263    .complete();
264    run_alias_from_args(std::env::args_os())
265}
266
267/// Parse explicit args as the `run` alias binary, detect current dir,
268/// dispatch, and return the exit code. See [`run_alias_from_env`].
269///
270/// `args` must include `argv[0]` as first item.
271///
272/// # Errors
273///
274/// Returns an error when reading current dir fails, project detection fails,
275/// command execution fails, or writing clap output fails.
276pub fn run_alias_from_args<I, T>(args: I) -> Result<i32>
277where
278    I: IntoIterator<Item = T>,
279    T: Into<OsString> + Clone,
280{
281    let cwd = std::env::current_dir()?;
282    run_alias_in_dir(args, &cwd)
283}
284
285/// Parse explicit args as the `run` alias binary against `dir`.\
286/// See [`run_alias_from_env`].
287///
288/// `args` must include `argv[0]` as first item.
289///
290/// # Errors
291///
292/// Returns an error when project detection fails, command execution fails, or
293/// writing clap output fails.
294pub fn run_alias_in_dir<I, T>(args: I, dir: &Path) -> Result<i32>
295where
296    I: IntoIterator<Item = T>,
297    T: Into<OsString> + Clone,
298{
299    let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
300
301    if requests_version(&args) {
302        println!("{}", version_line(&args, std::io::stdout().is_terminal()));
303        return Ok(0);
304    }
305
306    let cli = match parse_run_alias_cli(args.clone()) {
307        Ok(cli) => cli,
308        // A `--help`/`--version` *before* any task is this binary's own:
309        // clap's built-ins are disabled and the flag is undefined, so it
310        // can't fill the hyphen-rejecting `task` positional and surfaces as
311        // `UnknownArgument`. (A *trailing* one is swallowed by `args` and
312        // forwarded instead — see `cli::RunAliasCli`.) Covers the bare
313        // `run --help` as well as `run --pm npm --help`, `run --dir … -V`.
314        Err(err) => {
315            return match alias_builtin_request(&err) {
316                Some(AliasBuiltin::Help) => print_run_alias_help(&args),
317                Some(AliasBuiltin::Version) => {
318                    println!("{}", version_line(&args, std::io::stdout().is_terminal()));
319                    Ok(0)
320                }
321                None => render_clap_error(&err),
322            };
323        }
324    };
325
326    let project_dir = resolve_project_dir(
327        configured_project_dir(
328            cli.global.project_dir.as_deref(),
329            std::env::var_os("RUNNER_DIR").as_deref(),
330        )
331        .as_deref(),
332        dir,
333    )?;
334    dispatch_run_alias(cli, &project_dir)
335}
336
337/// This binary's own help/version, requested *before* any task.
338enum AliasBuiltin {
339    Help,
340    Version,
341}
342
343/// Classify a `run`-alias parse failure as a request for this binary's own
344/// help/version, or `None` for an unrelated error to surface verbatim.
345///
346/// With clap's built-in `--help`/`--version` disabled and undefined, a
347/// leading `-h`/`--help`/`-V`/`--version` cannot fill the hyphen-rejecting
348/// `task` positional, so clap reports [`ErrorKind::UnknownArgument`] naming
349/// the offending flag. A *trailing* one never reaches here — it is captured
350/// by `args` and forwarded — so an `UnknownArgument` naming a help/version
351/// flag unambiguously means "before any task", i.e. ours to handle.
352fn alias_builtin_request(err: &clap::Error) -> Option<AliasBuiltin> {
353    use clap::error::{ContextKind, ContextValue, ErrorKind};
354
355    if err.kind() != ErrorKind::UnknownArgument {
356        return None;
357    }
358    match err.get(ContextKind::InvalidArg) {
359        Some(ContextValue::String(arg)) => match arg.as_str() {
360            "--help" | "-h" => Some(AliasBuiltin::Help),
361            "--version" | "-V" => Some(AliasBuiltin::Version),
362            _ => None,
363        },
364        _ => None,
365    }
366}
367
368/// Render the `run` alias binary's own help to stdout, returning exit 0.
369///
370/// Invoked when `-h`/`--help` precedes any task. A help flag that *follows*
371/// a task is forwarded to that task instead (see [`cli::RunAliasCli`]), so
372/// this path is only reached for `run`'s own help. The bin name is taken
373/// from `argv[0]` so the `Usage:` line reads `run`, matching how clap's
374/// built-in help rendered before it was disabled.
375fn print_run_alias_help(args: &[OsString]) -> Result<i32> {
376    let mut command =
377        configure_cli_command(cli::RunAliasCli::command(), std::io::stdout().is_terminal());
378    if let Some(bin_name) = args.first().and_then(bin_name_from_arg0) {
379        command = command.name(bin_name.clone()).bin_name(bin_name);
380    }
381    command.print_help()?;
382    Ok(0)
383}
384
385fn parse_run_alias_cli<I, T>(args: I) -> Result<cli::RunAliasCli, clap::Error>
386where
387    I: IntoIterator<Item = T>,
388    T: Into<OsString> + Clone,
389{
390    let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
391
392    let mut command =
393        configure_cli_command(cli::RunAliasCli::command(), std::io::stdout().is_terminal());
394    if let Some(bin_name) = args.first().and_then(bin_name_from_arg0) {
395        command = command.name(bin_name.clone()).bin_name(bin_name);
396    }
397
398    let matches = command.try_get_matches_from(args)?;
399    cli::RunAliasCli::from_arg_matches(&matches)
400}
401
402fn dispatch_run_alias(cli: cli::RunAliasCli, dir: &Path) -> Result<i32> {
403    let ctx = detect::detect(dir);
404    let loaded_config = config::load(dir)?;
405    let overrides = resolver::ResolutionOverrides::from_cli_and_env(
406        cli.global.pm_override.as_deref(),
407        cli.global.runner_override.as_deref(),
408        cli.global.fallback.as_deref(),
409        cli.global.on_mismatch.as_deref(),
410        resolver::DiagnosticFlags {
411            no_warnings: cli.global.no_warnings,
412            quiet: cli.global.quiet,
413            explain: cli.global.explain,
414        },
415        cli::ChainFailureFlags {
416            keep_going: cli.failure.keep_going,
417            kill_on_fail: cli.failure.kill_on_fail,
418        },
419        loaded_config.as_ref(),
420    )?;
421    match cli.task {
422        None if !cli.mode.sequential && !cli.mode.parallel => {
423            cmd::info(&ctx, &overrides, false, schema::CURRENT_VERSION)?;
424            Ok(0)
425        }
426        task => dispatch_run(&ctx, &overrides, task, cli.args, cli.mode),
427    }
428}
429
430/// Extracts the filename portion from an `argv[0]`-style `OsString`, returning it when non-empty.
431///
432/// Returns `Some(String)` with the file name if `arg0` has a non-empty file-name segment, `None` otherwise.
433///
434/// Strips a trailing `.exe` suffix (case-insensitive) so Windows builds present the
435/// same `runner` / `run` identifier in `--version`, `--help`, and the `Usage:` line
436/// as Unix builds. Without this, clap's bin-name plumbing surfaces the raw
437/// `runner.exe` from `argv[0]`, leaking the platform-specific extension into UX.
438///
439/// # Examples
440///
441/// ```rust
442/// use std::ffi::OsString;
443/// let name = runner::bin_name_from_arg0(&OsString::from("/usr/bin/runner"));
444/// assert_eq!(name.as_deref(), Some("runner"));
445///
446/// let win = runner::bin_name_from_arg0(&OsString::from("runner.exe"));
447/// assert_eq!(win.as_deref(), Some("runner"));
448/// ```
449#[must_use]
450pub fn bin_name_from_arg0(arg0: &OsString) -> Option<String> {
451    let name = Path::new(arg0)
452        .file_name()
453        .map(|segment| segment.to_string_lossy().into_owned())?;
454
455    let trimmed = strip_exe_suffix(&name);
456    (!trimmed.is_empty()).then(|| trimmed.to_string())
457}
458
459/// Strip a trailing `.exe` extension (ASCII case-insensitive) from a file name.
460///
461/// Returns the input unchanged if no such suffix is present. The match is
462/// ASCII-only because Windows treats `.EXE`, `.Exe`, `.exe` etc. as the same
463/// extension, and that case-fold is bounded to ASCII regardless of the active
464/// code page.
465fn strip_exe_suffix(name: &str) -> &str {
466    const SUFFIX: &str = ".exe";
467    if name.len() > SUFFIX.len()
468        && name.is_char_boundary(name.len() - SUFFIX.len())
469        && name[name.len() - SUFFIX.len()..].eq_ignore_ascii_case(SUFFIX)
470    {
471        &name[..name.len() - SUFFIX.len()]
472    } else {
473        name
474    }
475}
476
477/// Attaches the generated help byline to a clap command.
478///
479/// The byline text is produced by `help_byline` using `stdout_is_terminal` and is
480/// applied via `Command::before_help`.
481///
482/// # Examples
483///
484/// ```rust
485/// let cmd = clap::Command::new("app");
486/// let cmd = runner::configure_cli_command(cmd, true);
487/// assert!(cmd.get_before_help().is_some());
488/// ```
489#[must_use]
490pub fn configure_cli_command(command: clap::Command, stdout_is_terminal: bool) -> clap::Command {
491    command.before_help(help_byline(stdout_is_terminal))
492}
493
494/// Render the CLI help byline using the build-time author metadata.
495///
496/// When `stdout_is_terminal` is true and `RUNNER_AUTHOR_EMAIL` is set, the
497/// author name is wrapped in an OSC-8 `mailto:` hyperlink; otherwise the plain
498/// author name is used. The returned string is prefixed with `"by "`.
499///
500/// # Examples
501///
502/// ```rust
503/// // Without a terminal, output is plain "by <name>" using the build-time author.
504/// let s = runner::help_byline(false);
505/// assert!(s.starts_with("by "));
506///
507/// // With a terminal, the name may be wrapped in an OSC-8 mailto: hyperlink,
508/// // but the byline still begins with "by ".
509/// let t = runner::help_byline(true);
510/// assert!(t.starts_with("by "));
511/// ```
512#[must_use]
513pub fn help_byline(stdout_is_terminal: bool) -> String {
514    let name = env!("RUNNER_AUTHOR_NAME");
515    let rendered = if stdout_is_terminal {
516        option_env!("RUNNER_AUTHOR_EMAIL").map_or_else(
517            || name.to_string(),
518            |mail| osc8_link(name, &format!("mailto:{mail}")),
519        )
520    } else {
521        name.to_string()
522    };
523    format!("by {rendered}")
524}
525
526/// Detects whether the provided argv-style slice specifically requests the program version.
527///
528/// # Returns
529///
530/// `true` if `args` has exactly two elements and the second element is `--version` or `-V`, `false` otherwise.
531///
532/// # Examples
533///
534/// ```rust
535/// use std::ffi::OsString;
536///
537/// let args = vec![OsString::from("runner"), OsString::from("--version")];
538/// assert!(runner::requests_version(&args));
539///
540/// let args2 = vec![OsString::from("runner"), OsString::from("-V")];
541/// assert!(runner::requests_version(&args2));
542///
543/// let args3 = vec![OsString::from("runner")];
544/// assert!(!runner::requests_version(&args3));
545///
546/// let args4 = vec![OsString::from("runner"), OsString::from("--version"), OsString::from("extra")];
547/// assert!(!runner::requests_version(&args4));
548/// ```
549#[must_use]
550pub fn requests_version(args: &[OsString]) -> bool {
551    if args.len() != 2 {
552        return false;
553    }
554
555    let flag = args[1].to_string_lossy();
556    flag == "--version" || flag == "-V"
557}
558
559fn version_line(args: &[OsString], stdout_is_terminal: bool) -> String {
560    let bin = args
561        .first()
562        .and_then(bin_name_from_arg0)
563        .unwrap_or_else(|| "runner".to_string());
564
565    if !stdout_is_terminal {
566        return format!("{bin} {VERSION}");
567    }
568
569    format!(
570        "{} {}",
571        osc8_link(&bin, REPOSITORY_URL),
572        osc8_link(VERSION, &release_url(VERSION))
573    )
574}
575
576fn release_url(version: &str) -> String {
577    format!("{REPOSITORY_URL}releases/tag/v{version}")
578}
579
580fn osc8_link(label: &str, url: &str) -> String {
581    format!("\u{1b}]8;;{url}\u{1b}\\{label}\u{1b}]8;;\u{1b}\\")
582}
583
584fn configured_project_dir(
585    project_dir: Option<&Path>,
586    env_dir: Option<&std::ffi::OsStr>,
587) -> Option<PathBuf> {
588    project_dir
589        .map(Path::to_path_buf)
590        .or_else(|| env_dir.map(PathBuf::from))
591}
592
593fn resolve_project_dir(project_dir: Option<&Path>, cwd: &Path) -> Result<PathBuf> {
594    let dir = match project_dir {
595        Some(path) if path.is_absolute() => path.to_path_buf(),
596        Some(path) => cwd.join(path),
597        None => cwd.to_path_buf(),
598    };
599
600    if !dir.exists() {
601        bail!("project dir does not exist: {}", dir.display());
602    }
603    if !dir.is_dir() {
604        bail!("project dir is not a directory: {}", dir.display());
605    }
606
607    Ok(dir)
608}
609
610fn render_clap_error(err: &clap::Error) -> Result<i32> {
611    let exit_code = err.exit_code();
612    err.print()?;
613    Ok(exit_code)
614}
615
616fn dispatch_install_chain(
617    ctx: &types::ProjectContext,
618    overrides: &resolver::ResolutionOverrides,
619    frozen: bool,
620    tasks: &[String],
621) -> Result<i32> {
622    let mut items = vec![chain::ChainItem::install(frozen)];
623    items.extend(chain::parse::parse_task_list(tasks)?);
624    let c = chain::Chain {
625        mode: chain::ChainMode::Sequential,
626        items,
627        failure: overrides.failure_policy,
628    };
629    chain::exec::run_chain(ctx, overrides, &c)
630}
631
632fn dispatch_run(
633    ctx: &types::ProjectContext,
634    overrides: &resolver::ResolutionOverrides,
635    task: Option<String>,
636    args: Vec<String>,
637    mode: cli::ChainModeFlags,
638) -> Result<i32> {
639    if mode.sequential || mode.parallel {
640        let chain_mode = if mode.parallel {
641            chain::ChainMode::Parallel
642        } else {
643            chain::ChainMode::Sequential
644        };
645        let mut positionals: Vec<String> = Vec::new();
646        if let Some(t) = task {
647            positionals.push(t);
648        }
649        positionals.extend(args);
650        let items = chain::parse::parse_task_list(&positionals)?;
651        let c = chain::Chain {
652            mode: chain_mode,
653            items,
654            failure: overrides.failure_policy,
655        };
656        return chain::exec::run_chain(ctx, overrides, &c);
657    }
658    let Some(task) = task.as_deref() else {
659        bail!(
660            "task name required (drop -s/-p for single-task mode or supply at least one task name)"
661        );
662    };
663    if args.is_empty()
664        && let Some(code) = run_path_builtin_fallback(ctx, overrides, task)?
665    {
666        return Ok(code);
667    }
668    cmd::run(ctx, overrides, task, &args, None)
669}
670
671/// Run-path fallback for builtin verbs.
672///
673/// When a bare, arg-less `run`/`runner run` token names a built-in verb and
674/// no same-named task exists, run that built-in's default (no-flag) form —
675/// the same behavior the explicit `runner <verb>` subcommand provides. A
676/// project task of the same name takes precedence (handled by the early
677/// `has_task` return → falls through to `cmd::run`).
678///
679/// Returns `Ok(Some(code))` when the fallback handled the token, `Ok(None)`
680/// to fall through to `cmd::run` (task dispatch / PM-exec).
681///
682/// Qualified tokens (`source:verb`) carry the `source:` prefix, so they never
683/// match a bare verb arm and fall through untouched — no qualifier parsing
684/// needed here. `info` maps to a plain `list` (no deprecation warning): the
685/// deprecation is specific to the explicit `runner info` subcommand, and
686/// emitting it on the run path — where the user typed `run info` — would be
687/// misleading and would spuriously fire the GitHub Actions annotation.
688fn run_path_builtin_fallback(
689    ctx: &types::ProjectContext,
690    overrides: &resolver::ResolutionOverrides,
691    name: &str,
692) -> Result<Option<i32>> {
693    if has_task(ctx, name) {
694        return Ok(None);
695    }
696    let code = match name {
697        "install" => cmd::install(ctx, overrides, false)?,
698        "clean" => {
699            cmd::clean(ctx, false, false)?;
700            0
701        }
702        // `info` maps to a plain `list`: the deprecation warning is specific
703        // to the explicit `runner info` subcommand, not the run path.
704        "list" | "info" => {
705            cmd::list(ctx, overrides, false, false, None, schema::CURRENT_VERSION)?;
706            0
707        }
708        "completions" => {
709            cmd::completions(None, None)?;
710            0
711        }
712        _ => return Ok(None),
713    };
714    Ok(Some(code))
715}
716
717/// Resolve the effective JSON schema version for schema-aware output:
718/// explicit `--schema-version=N` wins, otherwise default to latest.
719fn resolve_schema_version(requested: Option<u32>) -> Result<u32> {
720    schema::validate_schema_version(requested.unwrap_or(schema::CURRENT_VERSION))
721}
722
723fn schema_version_for_json(json: bool, requested: Option<u32>) -> Result<u32> {
724    if json {
725        resolve_schema_version(requested)
726    } else {
727        Ok(schema::CURRENT_VERSION)
728    }
729}
730
731/// `why`-specific version resolution: `why` is at
732/// [`schema::WHY_CURRENT_VERSION`] while list remains at
733/// [`schema::CURRENT_VERSION`], so it validates against its own range
734/// and defaults to its own latest.
735fn why_schema_version_for_json(json: bool, requested: Option<u32>) -> Result<u32> {
736    if json {
737        schema::validate_why_schema_version(requested.unwrap_or(schema::WHY_CURRENT_VERSION))
738    } else {
739        Ok(schema::WHY_CURRENT_VERSION)
740    }
741}
742
743/// `doctor`-specific version resolution; see
744/// [`schema::DOCTOR_CURRENT_VERSION`].
745fn doctor_schema_version_for_json(json: bool, requested: Option<u32>) -> Result<u32> {
746    if json {
747        schema::validate_doctor_schema_version(requested.unwrap_or(schema::DOCTOR_CURRENT_VERSION))
748    } else {
749        Ok(schema::DOCTOR_CURRENT_VERSION)
750    }
751}
752
753/// Build [`resolver::ResolutionOverrides`] from a parsed CLI + loaded config.
754/// Lifted out of [`dispatch`] so the latter stays under clippy's
755/// `too_many_lines` budget; the chain-failure inputs come from whichever
756/// subcommand carries them (`Run` / `Install`), with `false` defaults for
757/// subcommands that don't.
758fn build_overrides(
759    cli: &cli::Cli,
760    loaded_config: Option<&config::LoadedConfig>,
761) -> Result<resolver::ResolutionOverrides> {
762    let (cli_keep_going, cli_kill_on_fail) = match cli.command.as_ref() {
763        Some(cli::Command::Run { failure, .. } | cli::Command::Install { failure, .. }) => {
764            (failure.keep_going, failure.kill_on_fail)
765        }
766        _ => (false, false),
767    };
768    resolver::ResolutionOverrides::from_cli_and_env(
769        cli.global.pm_override.as_deref(),
770        cli.global.runner_override.as_deref(),
771        cli.global.fallback.as_deref(),
772        cli.global.on_mismatch.as_deref(),
773        resolver::DiagnosticFlags {
774            no_warnings: cli.global.no_warnings,
775            quiet: cli.global.quiet,
776            explain: cli.global.explain,
777        },
778        cli::ChainFailureFlags {
779            keep_going: cli_keep_going,
780            kill_on_fail: cli_kill_on_fail,
781        },
782        loaded_config,
783    )
784}
785
786/// Lenient sibling of [`build_overrides`] used when strict parsing
787/// failed and the command is `doctor`: invalid env-sourced override
788/// values degrade to [`types::DetectionWarning`]s instead of killing
789/// the one command whose job is to report a broken environment.
790fn build_overrides_lenient(
791    cli: &cli::Cli,
792    loaded_config: Option<&config::LoadedConfig>,
793) -> Result<(resolver::ResolutionOverrides, Vec<types::DetectionWarning>)> {
794    let (cli_keep_going, cli_kill_on_fail) = match cli.command.as_ref() {
795        Some(cli::Command::Run { failure, .. } | cli::Command::Install { failure, .. }) => {
796            (failure.keep_going, failure.kill_on_fail)
797        }
798        _ => (false, false),
799    };
800    resolver::ResolutionOverrides::from_cli_and_env_lenient(
801        cli.global.pm_override.as_deref(),
802        cli.global.runner_override.as_deref(),
803        cli.global.fallback.as_deref(),
804        cli.global.on_mismatch.as_deref(),
805        resolver::DiagnosticFlags {
806            no_warnings: cli.global.no_warnings,
807            quiet: cli.global.quiet,
808            explain: cli.global.explain,
809        },
810        cli::ChainFailureFlags {
811            keep_going: cli_keep_going,
812            kill_on_fail: cli_kill_on_fail,
813        },
814        loaded_config,
815    )
816}
817
818/// Resolve overrides for [`dispatch`]. Strict for every command;
819/// `doctor` retries leniently on failure because it must survive the
820/// misconfigured environment it exists to diagnose — env garbage
821/// degrades to warnings appended to `ctx`, while CLI flag garbage
822/// re-raises from the lenient pass and stays fatal.
823fn dispatch_overrides(
824    cli: &cli::Cli,
825    loaded_config: Option<&config::LoadedConfig>,
826    ctx: &mut types::ProjectContext,
827) -> Result<resolver::ResolutionOverrides> {
828    match build_overrides(cli, loaded_config) {
829        Ok(overrides) => Ok(overrides),
830        Err(_) if matches!(cli.command, Some(cli::Command::Doctor { .. })) => {
831            let (overrides, env_warnings) = build_overrides_lenient(cli, loaded_config)?;
832            ctx.warnings.extend(env_warnings);
833            Ok(overrides)
834        }
835        Err(e) => Err(e),
836    }
837}
838
839fn dispatch(cli: cli::Cli, dir: &Path) -> Result<i32> {
840    let mut ctx = detect::detect(dir);
841    // A malformed `runner.toml` must not abort the `config` subcommand —
842    // `config validate`/`show` exist to inspect and repair exactly that
843    // file, and they re-load it with their own error handling. Every other
844    // command requires a clean parse here.
845    let loaded_config = match config::load(dir) {
846        Ok(loaded) => loaded,
847        Err(_) if matches!(cli.command, Some(cli::Command::Config { .. })) => None,
848        Err(e) => return Err(e),
849    };
850    let overrides = dispatch_overrides(&cli, loaded_config.as_ref(), &mut ctx)?;
851
852    match cli.command {
853        None => {
854            cmd::info(&ctx, &overrides, false, schema::CURRENT_VERSION)?;
855            Ok(0)
856        }
857        // `info` is a deprecated alias for `list`. Bare `runner` (the
858        // `None` arm above) keeps the dashboard; only the explicit verb
859        // is deprecated.
860        Some(cli::Command::Info { json }) => {
861            eprintln!(
862                "{} `runner info` is deprecated; use `runner list`",
863                "warn:".yellow().bold(),
864            );
865            // Under GitHub Actions, also emit a workflow-command
866            // annotation so the deprecation surfaces in the run summary
867            // / inline, not just buried in the step log. Kept on stderr
868            // so `runner info --json` stdout stays a clean pipe; the
869            // runner scans both streams for `::` commands.
870            if actions_rs::env::is_github_actions() {
871                eprintln!(
872                    "::warning title=Deprecation::`runner info` is deprecated; use `runner list`"
873                );
874            }
875            let schema_version = schema_version_for_json(json, cli.global.schema_version)?;
876            cmd::list(&ctx, &overrides, false, json, None, schema_version)?;
877            Ok(0)
878        }
879        Some(cli::Command::Run {
880            task, args, mode, ..
881        }) => dispatch_run(&ctx, &overrides, task, args, mode),
882        Some(cli::Command::External(args)) => {
883            if args.is_empty() {
884                cmd::info(&ctx, &overrides, false, schema::CURRENT_VERSION)?;
885                Ok(0)
886            } else {
887                cmd::run(&ctx, &overrides, &args[0], &args[1..], None)
888            }
889        }
890        Some(cli::Command::Install { frozen, tasks, .. }) if !tasks.is_empty() => {
891            dispatch_install_chain(&ctx, &overrides, frozen, &tasks)
892        }
893        Some(cli::Command::Install { frozen, .. }) => cmd::install(&ctx, &overrides, frozen),
894        Some(cli::Command::Clean {
895            yes,
896            include_framework,
897        }) => {
898            cmd::clean(&ctx, yes, include_framework)?;
899            Ok(0)
900        }
901        Some(cli::Command::List { raw, json, source }) => {
902            let schema_version = schema_version_for_json(json, cli.global.schema_version)?;
903            cmd::list(
904                &ctx,
905                &overrides,
906                raw,
907                json,
908                source.as_deref(),
909                schema_version,
910            )?;
911            Ok(0)
912        }
913        Some(cli::Command::Completions { shell, output }) => {
914            cmd::completions(shell, output.as_deref())?;
915            Ok(0)
916        }
917        #[cfg(feature = "man")]
918        Some(cli::Command::Man { output }) => dispatch_man(output.as_deref()),
919        #[cfg(feature = "schema")]
920        Some(cli::Command::Schema { all, output }) => dispatch_schema(all, output.as_deref()),
921        Some(cli::Command::Doctor { json }) => {
922            let schema_version = doctor_schema_version_for_json(json, cli.global.schema_version)?;
923            cmd::doctor(&ctx, &overrides, json, schema_version)?;
924            Ok(0)
925        }
926        Some(cli::Command::Config { action }) => cmd::config(dir, action),
927        Some(cli::Command::Why { task, json }) => {
928            let schema_version = why_schema_version_for_json(json, cli.global.schema_version)?;
929            cmd::why(&ctx, &overrides, &task, json, schema_version)?;
930            Ok(0)
931        }
932    }
933}
934
935#[cfg(feature = "man")]
936fn dispatch_man(output: Option<&Path>) -> Result<i32> {
937    match output {
938        Some(dir) => cmd::write_man_pages(dir)?,
939        None => cmd::write_runner_page_to_stdout()?,
940    }
941    Ok(0)
942}
943
944#[cfg(feature = "schema")]
945fn dispatch_schema(all: bool, output: Option<&Path>) -> Result<i32> {
946    cmd::write_schema(all, output)?;
947    Ok(0)
948}
949
950/// Whether the detected project defines a task with the given name.
951fn has_task(ctx: &types::ProjectContext, name: &str) -> bool {
952    ctx.tasks.iter().any(|task| task.name == name)
953}
954
955#[cfg(test)]
956mod tests {
957    use std::ffi::OsString;
958    use std::fs;
959    use std::path::{Path, PathBuf};
960
961    use super::{
962        AliasBuiltin, VERSION, alias_builtin_request, bin_name_from_arg0, configured_project_dir,
963        exit_code_for_error, has_task, parse_cli, parse_run_alias_cli, release_url,
964        requests_version, resolve_project_dir, run_alias_in_dir, run_in_dir, version_line,
965    };
966    use crate::cli;
967    use crate::resolver::ResolveError;
968    use crate::tool::test_support::TempDir;
969    use crate::types::{Ecosystem, ProjectContext, Task, TaskSource};
970
971    #[test]
972    fn exit_code_for_resolve_error_is_two() {
973        let err: anyhow::Error = ResolveError::NoSignalsFound {
974            ecosystem: Ecosystem::Node,
975            soft: false,
976        }
977        .into();
978
979        assert_eq!(exit_code_for_error(&err), 2);
980    }
981
982    #[test]
983    fn exit_code_for_generic_error_is_one() {
984        let err = anyhow::anyhow!("generic boom");
985
986        assert_eq!(exit_code_for_error(&err), 1);
987    }
988
989    #[test]
990    fn help_returns_zero_instead_of_exiting() {
991        let code = run_in_dir(["runner", "--help"], Path::new("."))
992            .expect("help should return an exit code");
993
994        assert_eq!(code, 0);
995    }
996
997    #[test]
998    fn invalid_args_return_non_zero_instead_of_exiting() {
999        let code = run_in_dir(["runner", "--definitely-invalid"], Path::new("."))
1000            .expect("parse errors should return an exit code");
1001
1002        assert_ne!(code, 0);
1003    }
1004
1005    #[test]
1006    fn version_returns_zero_instead_of_exiting() {
1007        let code = run_in_dir(["runner", "--version"], Path::new("."))
1008            .expect("version should return an exit code");
1009
1010        assert_eq!(code, 0);
1011    }
1012
1013    #[test]
1014    fn requests_version_detects_top_level_version_flags() {
1015        assert!(requests_version(&[
1016            OsString::from("runner"),
1017            OsString::from("--version")
1018        ]));
1019        assert!(requests_version(&[
1020            OsString::from("runner"),
1021            OsString::from("-V")
1022        ]));
1023        assert!(!requests_version(&[
1024            OsString::from("runner"),
1025            OsString::from("info"),
1026            OsString::from("--version"),
1027        ]));
1028    }
1029
1030    #[test]
1031    fn release_url_points_to_version_tag() {
1032        assert_eq!(
1033            release_url(VERSION),
1034            format!("https://github.com/kjanat/runner/releases/tag/v{VERSION}")
1035        );
1036    }
1037
1038    #[test]
1039    fn version_line_wraps_bin_and_version_with_separate_links() {
1040        let line = version_line(&[OsString::from("runner")], true);
1041
1042        assert!(line.contains(
1043            "\u{1b}]8;;https://github.com/kjanat/runner/\u{1b}\\runner\u{1b}]8;;\u{1b}\\"
1044        ));
1045        assert!(line.contains(&format!(
1046            "\u{1b}]8;;https://github.com/kjanat/runner/releases/tag/v{VERSION}\u{1b}\\{VERSION}\u{1b}]8;;\u{1b}\\"
1047        )));
1048    }
1049
1050    #[test]
1051    fn resolve_project_dir_uses_cwd_when_not_overridden() {
1052        let cwd = TempDir::new("runner-project-dir-default");
1053
1054        assert_eq!(
1055            resolve_project_dir(None, cwd.path()).expect("cwd should be accepted"),
1056            cwd.path()
1057        );
1058    }
1059
1060    #[test]
1061    fn resolve_project_dir_resolves_relative_paths_from_cwd() {
1062        let cwd = TempDir::new("runner-project-dir-cwd");
1063        fs::create_dir(cwd.path().join("child")).expect("child dir should be created");
1064
1065        let resolved = resolve_project_dir(Some(Path::new("child")), cwd.path())
1066            .expect("relative dir should resolve");
1067
1068        assert_eq!(resolved, cwd.path().join("child"));
1069    }
1070
1071    #[test]
1072    fn resolve_project_dir_rejects_missing_directories() {
1073        let cwd = TempDir::new("runner-project-dir-missing");
1074        let err = resolve_project_dir(Some(Path::new("missing")), cwd.path())
1075            .expect_err("missing dir should error");
1076
1077        assert!(err.to_string().contains("project dir does not exist"));
1078    }
1079
1080    #[test]
1081    fn configured_project_dir_prefers_flag_over_env() {
1082        let dir = configured_project_dir(
1083            Some(Path::new("flag-dir")),
1084            Some(std::ffi::OsStr::new("env-dir")),
1085        )
1086        .expect("dir should be selected");
1087
1088        assert_eq!(dir, PathBuf::from("flag-dir"));
1089    }
1090
1091    #[test]
1092    fn configured_project_dir_falls_back_to_env() {
1093        let dir = configured_project_dir(None, Some(std::ffi::OsStr::new("env-dir")))
1094            .expect("env dir should be selected");
1095
1096        assert_eq!(dir, PathBuf::from("env-dir"));
1097    }
1098
1099    #[test]
1100    fn bin_name_from_arg0_uses_path_file_name() {
1101        let name = bin_name_from_arg0(&OsString::from("/tmp/run"));
1102
1103        assert_eq!(name.as_deref(), Some("run"));
1104    }
1105
1106    #[test]
1107    fn bin_name_from_arg0_strips_windows_exe_suffix() {
1108        // Windows builds inherit `runner.exe` / `run.exe` from argv[0]; clap
1109        // pipes that straight into `--version` / `--help` / Usage unless we
1110        // normalize it here. We feed bare file names rather than full Windows
1111        // paths because `Path::file_name` is host-OS-aware and won't split on
1112        // `\` when the tests run on Unix.
1113        let runner = bin_name_from_arg0(&OsString::from("runner.exe"));
1114        assert_eq!(runner.as_deref(), Some("runner"));
1115
1116        let run = bin_name_from_arg0(&OsString::from("run.exe"));
1117        assert_eq!(run.as_deref(), Some("run"));
1118    }
1119
1120    #[test]
1121    fn bin_name_from_arg0_strips_exe_case_insensitive() {
1122        let upper = bin_name_from_arg0(&OsString::from("RUNNER.EXE"));
1123        assert_eq!(upper.as_deref(), Some("RUNNER"));
1124
1125        let mixed = bin_name_from_arg0(&OsString::from("Run.Exe"));
1126        assert_eq!(mixed.as_deref(), Some("Run"));
1127    }
1128
1129    #[test]
1130    fn bin_name_from_arg0_preserves_unrelated_extensions() {
1131        // `.exe` only — names that happen to embed those characters in other
1132        // positions, or carry different extensions, pass through unchanged.
1133        let dotted = bin_name_from_arg0(&OsString::from("/tmp/runner.exe.bak"));
1134        assert_eq!(dotted.as_deref(), Some("runner.exe.bak"));
1135
1136        let other = bin_name_from_arg0(&OsString::from("/tmp/runner.sh"));
1137        assert_eq!(other.as_deref(), Some("runner.sh"));
1138    }
1139
1140    #[test]
1141    fn bin_name_from_arg0_handles_bare_dot_exe() {
1142        // `.exe` alone shouldn't strip to an empty name; the suffix length
1143        // guard keeps the input intact.
1144        let bare = bin_name_from_arg0(&OsString::from(".exe"));
1145        assert_eq!(bare.as_deref(), Some(".exe"));
1146    }
1147
1148    fn stub_context(tasks: &[&str]) -> ProjectContext {
1149        ProjectContext {
1150            root: PathBuf::from("."),
1151            package_managers: Vec::new(),
1152            task_runners: Vec::new(),
1153            tasks: tasks
1154                .iter()
1155                .map(|name| Task {
1156                    name: (*name).to_string(),
1157                    source: TaskSource::PackageJson,
1158                    run_target: None,
1159                    description: None,
1160                    alias_of: None,
1161                    passthrough_to: None,
1162                })
1163                .collect(),
1164            node_version: None,
1165            current_node: None,
1166            is_monorepo: false,
1167            warnings: Vec::new(),
1168        }
1169    }
1170
1171    #[test]
1172    fn has_task_returns_true_for_existing_task() {
1173        let ctx = stub_context(&["clean", "install"]);
1174
1175        assert!(has_task(&ctx, "clean"));
1176        assert!(has_task(&ctx, "install"));
1177        assert!(!has_task(&ctx, "build"));
1178    }
1179
1180    #[test]
1181    fn run_alias_parses_builtin_names_as_tasks() {
1182        for name in [
1183            "clean",
1184            "install",
1185            "list",
1186            "exec",
1187            "info",
1188            "completions",
1189            "run",
1190        ] {
1191            let cli = parse_run_alias_cli(["run", name])
1192                .unwrap_or_else(|e| panic!("run {name} should parse: {e}"));
1193
1194            assert_eq!(cli.task.as_deref(), Some(name));
1195            assert!(cli.args.is_empty());
1196        }
1197    }
1198
1199    #[test]
1200    fn run_alias_forwards_trailing_args() {
1201        let cli = parse_run_alias_cli(["run", "test", "--watch", "--reporter=verbose"])
1202            .expect("run test --watch --reporter=verbose should parse");
1203
1204        assert_eq!(cli.task.as_deref(), Some("test"));
1205        assert_eq!(cli.args, vec!["--watch", "--reporter=verbose"]);
1206    }
1207
1208    #[test]
1209    fn run_alias_bare_has_no_task() {
1210        let cli = parse_run_alias_cli(["run"]).expect("bare run should parse");
1211
1212        assert!(cli.task.is_none());
1213        assert!(cli.args.is_empty());
1214    }
1215
1216    #[test]
1217    fn run_alias_honours_dir_flag() {
1218        let cli = parse_run_alias_cli(["run", "--dir=other", "build"])
1219            .expect("run --dir=other build should parse");
1220
1221        assert_eq!(cli.global.project_dir, Some(PathBuf::from("other")));
1222        assert_eq!(cli.task.as_deref(), Some("build"));
1223    }
1224
1225    #[test]
1226    fn run_alias_bare_shows_info() {
1227        let dir = TempDir::new("runner-run-bare");
1228
1229        let code =
1230            run_alias_in_dir(["run"], dir.path()).expect("bare run should succeed on empty dir");
1231
1232        assert_eq!(code, 0);
1233    }
1234
1235    #[test]
1236    fn run_alias_forwards_help_and_version_after_task() {
1237        // `run <task> --help/--version` must reach the task, not print
1238        // run's own help/version. The flag is an undefined hyphen token
1239        // after the first positional, so `args` (trailing_var_arg) keeps it.
1240        for flag in ["--help", "-h", "--version", "-V"] {
1241            let cli = parse_run_alias_cli(["run", "build", flag])
1242                .unwrap_or_else(|e| panic!("run build {flag} should parse: {e}"));
1243            assert_eq!(cli.task.as_deref(), Some("build"));
1244            assert_eq!(cli.args, vec![flag.to_string()]);
1245        }
1246    }
1247
1248    #[test]
1249    fn run_alias_forwards_interleaved_help_flag() {
1250        // A forwarded help flag keeps its position among the task's args.
1251        let cli = parse_run_alias_cli(["run", "build", "--foo", "--help", "--bar"])
1252            .expect("interleaved --help should parse and forward");
1253        assert_eq!(cli.task.as_deref(), Some("build"));
1254        assert_eq!(cli.args, vec!["--foo", "--help", "--bar"]);
1255    }
1256
1257    #[test]
1258    fn run_alias_double_dash_forwards_help_literally() {
1259        // `run <task> -- --help` keeps forwarding the literal flag (the `--`
1260        // separator itself is consumed by clap).
1261        let cli = parse_run_alias_cli(["run", "build", "--", "--help"])
1262            .expect("run build -- --help should parse");
1263        assert_eq!(cli.task.as_deref(), Some("build"));
1264        assert_eq!(cli.args, vec!["--help"]);
1265    }
1266
1267    #[test]
1268    fn run_alias_leading_builtins_classified_as_own_request() {
1269        // Before any task, a help/version flag can't fill the
1270        // hyphen-rejecting `task` positional (clap built-ins are disabled),
1271        // so it surfaces as UnknownArgument and is recognised as ours.
1272        for flag in ["--help", "-h"] {
1273            let err = parse_run_alias_cli(["run", flag])
1274                .expect_err("leading help flag should not parse as a task");
1275            assert!(
1276                matches!(alias_builtin_request(&err), Some(AliasBuiltin::Help)),
1277                "{flag} before a task should be classified as a help request",
1278            );
1279        }
1280        for flag in ["--version", "-V"] {
1281            let err = parse_run_alias_cli(["run", flag])
1282                .expect_err("leading version flag should not parse as a task");
1283            assert!(
1284                matches!(alias_builtin_request(&err), Some(AliasBuiltin::Version)),
1285                "{flag} before a task should be classified as a version request",
1286            );
1287        }
1288    }
1289
1290    #[test]
1291    fn run_alias_global_flag_before_help_still_classified_as_help() {
1292        // `run --pm npm --help`: the value-taking global flag is consumed,
1293        // then --help still lands before any task → run's own help.
1294        let err = parse_run_alias_cli(["run", "--pm", "npm", "--help"])
1295            .expect_err("--pm npm --help should not parse as a task");
1296        assert!(matches!(
1297            alias_builtin_request(&err),
1298            Some(AliasBuiltin::Help)
1299        ));
1300    }
1301
1302    #[test]
1303    fn run_alias_unknown_flag_is_not_a_builtin_request() {
1304        // A genuine unknown flag must surface as an error, never be
1305        // mistaken for a help/version request.
1306        let err = parse_run_alias_cli(["run", "--bogus"])
1307            .expect_err("unknown leading flag should not parse");
1308        assert!(alias_builtin_request(&err).is_none());
1309    }
1310
1311    #[test]
1312    fn run_alias_own_help_and_version_return_zero() {
1313        // End-to-end through dispatch: own help/version exit 0 without
1314        // needing a real project. `--pm npm --version` is len > 2 so it
1315        // bypasses the `requests_version` fast-path and exercises the
1316        // parse-error classification.
1317        let dir = TempDir::new("runner-run-builtin");
1318        assert_eq!(
1319            run_alias_in_dir(["run", "--help"], dir.path()).expect("run --help should succeed"),
1320            0,
1321        );
1322        assert_eq!(
1323            run_alias_in_dir(["run", "--pm", "npm", "--version"], dir.path())
1324                .expect("run --pm npm --version should succeed"),
1325            0,
1326        );
1327    }
1328
1329    #[test]
1330    fn runner_cli_still_parses_install_as_builtin_when_flag_set() {
1331        let cli = parse_cli(["runner", "install", "--frozen"]).expect("should parse");
1332
1333        match cli.command {
1334            Some(cli::Command::Install { frozen: true, .. }) => {}
1335            other => panic!("expected Install {{ frozen: true }}, got {other:?}"),
1336        }
1337    }
1338
1339    #[test]
1340    fn runner_cli_parses_install_frozen_short_flag() {
1341        let cli = parse_cli(["runner", "install", "-f"]).expect("should parse");
1342
1343        match cli.command {
1344            Some(cli::Command::Install { frozen: true, .. }) => {}
1345            other => panic!("expected Install {{ frozen: true }}, got {other:?}"),
1346        }
1347    }
1348
1349    #[test]
1350    fn runner_cli_parses_install_chain_flags_after_task_names() {
1351        // `runner install build test --kill-on-fail` must parse
1352        // `--kill-on-fail` as a chain-failure flag, not as a task name.
1353        // Regression for the `trailing_var_arg` consumption bug.
1354        let cli = parse_cli(["runner", "install", "build", "test", "-K"]).expect("parses");
1355        match cli.command {
1356            Some(cli::Command::Install {
1357                tasks,
1358                failure:
1359                    cli::ChainFailureFlags {
1360                        kill_on_fail: true, ..
1361                    },
1362                ..
1363            }) => assert_eq!(tasks, vec!["build".to_string(), "test".to_string()]),
1364            other => {
1365                panic!("expected Install with kill_on_fail=true and clean task list, got {other:?}")
1366            }
1367        }
1368    }
1369
1370    #[test]
1371    fn runner_cli_parses_clean_as_builtin_when_flag_set() {
1372        let cli = parse_cli(["runner", "clean", "-y"]).expect("should parse");
1373
1374        match cli.command {
1375            Some(cli::Command::Clean { yes: true, .. }) => {}
1376            other => panic!("expected Clean {{ yes: true, .. }}, got {other:?}"),
1377        }
1378    }
1379
1380    #[test]
1381    fn runner_cli_routes_unknown_name_to_external() {
1382        let cli = parse_cli(["runner", "no-such-builtin"]).expect("should parse");
1383
1384        match cli.command {
1385            Some(cli::Command::External(args)) => {
1386                assert_eq!(args, vec!["no-such-builtin"]);
1387            }
1388            other => panic!("expected External, got {other:?}"),
1389        }
1390    }
1391
1392    #[test]
1393    fn runner_cli_parses_pm_and_runner_overrides_globally() {
1394        let cli = parse_cli(["runner", "--pm", "pnpm", "--runner", "just", "run", "build"])
1395            .expect("global --pm/--runner should parse on the run subcommand");
1396
1397        assert_eq!(cli.global.pm_override.as_deref(), Some("pnpm"));
1398        assert_eq!(cli.global.runner_override.as_deref(), Some("just"));
1399        match cli.command {
1400            Some(cli::Command::Run { task, args, .. }) => {
1401                assert_eq!(task.as_deref(), Some("build"));
1402                assert!(args.is_empty());
1403            }
1404            other => panic!("expected Run, got {other:?}"),
1405        }
1406    }
1407
1408    #[test]
1409    fn run_alias_parses_pm_override() {
1410        let cli =
1411            parse_run_alias_cli(["run", "--pm=bun", "test"]).expect("--pm=bun test should parse");
1412
1413        assert_eq!(cli.global.pm_override.as_deref(), Some("bun"));
1414        assert_eq!(cli.task.as_deref(), Some("test"));
1415    }
1416
1417    #[test]
1418    fn invalid_pm_override_value_returns_error() {
1419        // Bad PM name should not crash the binary; it should surface as an
1420        // error exit code so the user sees the message from `from_cli_and_env`.
1421        let dir = TempDir::new("runner-bad-pm");
1422        let result = run_in_dir(["runner", "--pm", "zoot", "info"], dir.path());
1423
1424        let err = result.expect_err("unknown --pm should error");
1425        assert!(format!("{err}").contains("unknown package manager"));
1426    }
1427
1428    #[test]
1429    fn install_with_undetected_pm_override_exits_2() {
1430        // A cargo-only project with `--pm npm`: the override can't be
1431        // honored, so install must refuse with a ResolveError (exit 2)
1432        // before spawning anything.
1433        let dir = TempDir::new("runner-install-undetected-pm");
1434        fs::write(
1435            dir.path().join("Cargo.toml"),
1436            "[package]\nname = \"fixture\"\nversion = \"0.0.0\"\n",
1437        )
1438        .expect("write Cargo.toml");
1439
1440        let err = run_in_dir(["runner", "--pm", "npm", "install"], dir.path())
1441            .expect_err("undetected --pm should refuse the install");
1442
1443        assert_eq!(
1444            exit_code_for_error(&err),
1445            2,
1446            "ResolveError must map to exit 2"
1447        );
1448        let msg = format!("{err}");
1449        assert!(msg.contains("--pm"), "should name the source: {msg}");
1450        assert!(msg.contains("cargo"), "should list detected PMs: {msg}");
1451    }
1452
1453    #[test]
1454    fn install_chain_with_undetected_pm_override_exits_2() {
1455        // Same refusal through the chain path (`runner install <task>`).
1456        let dir = TempDir::new("runner-install-chain-undetected-pm");
1457        fs::write(
1458            dir.path().join("Cargo.toml"),
1459            "[package]\nname = \"fixture\"\nversion = \"0.0.0\"\n",
1460        )
1461        .expect("write Cargo.toml");
1462
1463        let err = run_in_dir(["runner", "--pm", "npm", "install", "build"], dir.path())
1464            .expect_err("undetected --pm should refuse the install chain");
1465
1466        assert_eq!(
1467            exit_code_for_error(&err),
1468            2,
1469            "ResolveError must map to exit 2"
1470        );
1471    }
1472
1473    #[test]
1474    fn schema_version_rejects_invalid_for_non_json_commands() {
1475        let dir = TempDir::new("runner-schema-invalid-completions");
1476
1477        let code = run_in_dir(
1478            ["runner", "--schema-version", "99", "completions", "bash"],
1479            dir.path(),
1480        )
1481        .expect("parse errors should return an exit code");
1482
1483        assert_ne!(code, 0);
1484    }
1485
1486    #[test]
1487    fn schema_version_rejects_invalid_for_run_alias_bare_info() {
1488        let dir = TempDir::new("runner-schema-invalid-run-alias");
1489
1490        let code = run_alias_in_dir(["run", "--schema-version", "99"], dir.path())
1491            .expect("parse errors should return an exit code");
1492
1493        assert_ne!(code, 0);
1494    }
1495
1496    #[test]
1497    fn schema_version_rejects_invalid_for_json_output() {
1498        let dir = TempDir::new("runner-schema-json-invalid");
1499
1500        let code = run_in_dir(
1501            ["runner", "--schema-version", "99", "info", "--json"],
1502            dir.path(),
1503        )
1504        .expect("parse errors should return an exit code");
1505
1506        assert_ne!(code, 0);
1507    }
1508
1509    #[test]
1510    fn runner_cli_parses_completions_output_long() {
1511        let cli = parse_cli(["runner", "completions", "--output", "/tmp/runner.zsh"])
1512            .expect("should parse");
1513
1514        match cli.command {
1515            Some(cli::Command::Completions {
1516                shell: None,
1517                output: Some(path),
1518            }) => assert_eq!(path, PathBuf::from("/tmp/runner.zsh")),
1519            other => panic!("expected Completions with --output long form, got {other:?}"),
1520        }
1521    }
1522
1523    #[test]
1524    fn runner_cli_parses_completions_output_short() {
1525        let cli =
1526            parse_cli(["runner", "completions", "-o", "/tmp/runner.zsh"]).expect("should parse");
1527
1528        match cli.command {
1529            Some(cli::Command::Completions {
1530                shell: None,
1531                output: Some(path),
1532            }) => assert_eq!(path, PathBuf::from("/tmp/runner.zsh")),
1533            other => panic!("expected Completions with -o short form, got {other:?}"),
1534        }
1535    }
1536
1537    #[test]
1538    fn runner_cli_parses_completions_shell_and_output() {
1539        let cli = parse_cli([
1540            "runner",
1541            "completions",
1542            "zsh",
1543            "--output",
1544            "/tmp/runner.zsh",
1545        ])
1546        .expect("should parse");
1547
1548        match cli.command {
1549            Some(cli::Command::Completions {
1550                shell: Some(_),
1551                output: Some(path),
1552            }) => assert_eq!(path, PathBuf::from("/tmp/runner.zsh")),
1553            other => panic!("expected Completions with both shell and output set, got {other:?}"),
1554        }
1555    }
1556}