runner-run 0.11.0

Universal project task runner
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! `runner run <target>` — resolve a task name to the right tool and execute
//! it. When no task matches, fall back to executing the target as an
//! arbitrary command through the detected package manager (formerly `runner
//! exec`).
//!
//! # Module layout
//!
//! - [`qualify`] — `source:task` parsing, reversed-qualifier detection,
//!   and the side-effect-free [`qualify::precheck_task`] used by chain
//!   mode to bail before any sibling task runs.
//! - [`select`] — picking the best [`crate::types::Task`] candidate when
//!   a name matches multiple sources. The ranking key (priority, depth,
//!   display order, alias-ness) is split into individual `pub(crate)`
//!   helpers so [`crate::cmd::why`] can render the same key the dispatcher
//!   used.
//! - [`dispatch`] — turning a task token into a fully-configured
//!   [`std::process::Command`]: warning emission, the resolver chain,
//!   bun-test special case, PM-exec fallback, and per-source `run_cmd`
//!   selection.
//!
//! This file owns only the public entry points ([`run`] for inherited
//! stdio, [`dispatch_task_piped`] for the parallel chain executor) and
//! the test module.

use anyhow::Result;

mod dispatch;
mod qualify;
mod select;

pub(crate) use qualify::{allowed_runner_sources, precheck_task, runner_constraint_error};
pub(crate) use select::{select_task_entry, source_depth, source_priority};

use crate::resolver::ResolutionOverrides;
use crate::types::ProjectContext;

/// Resolve `task` and run it with inherited stdio, returning the exit
/// code. Bun special case: when `task == "test"` and no package-manifest
/// `test` script exists, falls back to `bun test`. PM-exec fallback for
/// unqualified misses runs the target through `npx`/`bunx`/`pnpm exec`/
/// `deno x`/`uvx`, plus `go run` for Go module/path-shaped targets;
/// otherwise spawns the binary directly from `PATH`.
pub(crate) fn run(
    ctx: &ProjectContext,
    overrides: &ResolutionOverrides,
    task: &str,
    args: &[String],
    sink: super::WarningSink<'_>,
) -> Result<i32> {
    let mut cmd = dispatch::resolve_dispatch(ctx, overrides, task, args, sink)?;
    Ok(super::exit_code(cmd.status()?))
}

/// Resolve `task` and spawn it with piped stdout/stderr (so the caller
/// can multiplex output) and `Stdio::null()` stdin (so parallel
/// siblings don't compete for the parent TTY or interfere with each
/// other's terminal modes). Used by the parallel chain executor
/// (`chain::exec::run_parallel`).
pub(crate) fn dispatch_task_piped(
    ctx: &ProjectContext,
    overrides: &ResolutionOverrides,
    task: &str,
    args: &[String],
    sink: super::WarningSink<'_>,
) -> Result<std::process::Child> {
    use std::process::Stdio;

    let mut cmd = dispatch::resolve_dispatch(ctx, overrides, task, args, sink)?;
    cmd.stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());
    Ok(cmd.spawn()?)
}
#[cfg(test)]
mod tests {
    use std::fs;
    use std::path::PathBuf;

    use super::dispatch::should_use_bun_test_fallback;
    use super::qualify::{detect_reversed_qualifier, parse_qualified_task};
    use super::{precheck_task, select_task_entry};
    use crate::resolver::ResolutionOverrides;
    use crate::tool::test_support::TempDir;
    use crate::types::{PackageManager, ProjectContext, Task, TaskRunner, TaskSource};

    #[test]
    fn parse_qualified_task_splits_source_and_name() {
        let (source, name) = parse_qualified_task("justfile:fmt");
        assert_eq!(source, Some(TaskSource::Justfile));
        assert_eq!(name, "fmt");
    }

    #[test]
    fn parse_qualified_task_returns_bare_name() {
        let (source, name) = parse_qualified_task("build");
        assert_eq!(source, None);
        assert_eq!(name, "build");
    }

    #[test]
    fn parse_qualified_task_handles_unknown_source() {
        let (source, name) = parse_qualified_task("unknown:build");
        assert_eq!(source, None);
        assert_eq!(name, "unknown:build");
    }

    #[test]
    fn parse_qualified_task_with_colons_in_task_name() {
        let (source, name) = parse_qualified_task("package.json:helix:sync");
        assert_eq!(source, Some(TaskSource::PackageJson));
        assert_eq!(name, "helix:sync");
    }

    #[test]
    fn parse_qualified_task_preserves_colons_in_bare_name() {
        let (source, name) = parse_qualified_task("helix:sync");
        assert_eq!(source, None);
        assert_eq!(name, "helix:sync");
    }

    #[test]
    fn parse_qualified_task_accepts_turbo_jsonc_qualifier() {
        let (source, name) = parse_qualified_task("turbo.jsonc:build");
        assert_eq!(source, Some(TaskSource::TurboJson));
        assert_eq!(name, "build");
    }

    #[test]
    fn parse_qualified_task_accepts_deno_jsonc_qualifier() {
        let (source, name) = parse_qualified_task("deno.jsonc:test");
        assert_eq!(source, Some(TaskSource::DenoJson));
        assert_eq!(name, "test");
    }

    #[test]
    fn parse_qualified_task_accepts_bacon_toml_qualifier() {
        let (source, name) = parse_qualified_task("bacon.toml:check");
        assert_eq!(source, Some(TaskSource::BaconToml));
        assert_eq!(name, "check");
    }

    #[test]
    fn detect_reversed_qualifier_catches_task_colon_source() {
        // `lint:cargo` has the qualifier inverted — caller should bail
        // with `did you mean "cargo:lint"?` instead of falling through
        // to PM-exec and spawning a binary named `lint:cargo`.
        let got = detect_reversed_qualifier("lint:cargo");
        assert_eq!(got, Some((TaskSource::CargoAliases, "lint")));
    }

    #[test]
    fn detect_reversed_qualifier_returns_none_for_correct_syntax() {
        // Correct ordering — the prefix branch (`parse_qualified_task`)
        // handles this; the reversed-detector must not fire.
        assert!(detect_reversed_qualifier("cargo:lint").is_none());
        // Plain name, no colon.
        assert!(detect_reversed_qualifier("lint").is_none());
        // Suffix that is not a known source.
        assert!(detect_reversed_qualifier("lint:zoot").is_none());
    }

    #[test]
    fn detect_reversed_qualifier_matches_last_colon() {
        // Multi-colon with a recognized suffix still fires: hint the
        // user toward the canonical ordering. Anything else (suffix not
        // a source label) returns None and falls through to the
        // existing PM-exec / not-found path.
        let got = detect_reversed_qualifier("foo:bar:cargo");
        assert_eq!(got, Some((TaskSource::CargoAliases, "foo:bar")));
        assert!(detect_reversed_qualifier("lint:cargo:extra").is_none());
    }

    #[test]
    fn precheck_reversed_qualifier_beats_runner_constraint() {
        let ctx = context(vec![], vec![]);
        let overrides = ResolutionOverrides {
            prefer_runners: vec![TaskRunner::Just],
            ..ResolutionOverrides::default()
        };

        let err = precheck_task(&ctx, &overrides, "lint:cargo")
            .expect_err("reversed qualifier should fail precheck");

        assert!(format!("{err:#}").contains("cargo:lint"));
    }

    #[test]
    fn reversed_qualifier_fast_fail_does_not_block_real_tasks() {
        // The fast-fail in `resolve_dispatch` is gated by
        // `restricted.is_empty()` — a real task whose name happens to
        // match the `task:source` shape must still dispatch.
        //
        // We mirror the dispatch lookup directly: `parse_qualified_task`
        // returns `(None, original)` for an unknown prefix, then the
        // filter on `ctx.tasks` runs. If that filter is non-empty,
        // `resolve_dispatch` skips the empty-branch entirely and
        // `detect_reversed_qualifier` is never reached.
        let ctx = ProjectContext {
            root: PathBuf::from("/tmp/has-quirky-task-name"),
            package_managers: Vec::new(),
            task_runners: Vec::new(),
            tasks: vec![Task {
                name: "lint:cargo".to_string(),
                source: TaskSource::Justfile,
                run_target: None,
                description: None,
                alias_of: None,
                passthrough_to: None,
            }],
            node_version: None,
            current_node: None,
            is_monorepo: false,
            warnings: Vec::new(),
        };

        let (qualifier, task_name) = parse_qualified_task("lint:cargo");
        assert_eq!(qualifier, None);
        assert_eq!(task_name, "lint:cargo");

        let found: Vec<_> = ctx.tasks.iter().filter(|t| t.name == task_name).collect();
        assert_eq!(
            found.len(),
            1,
            "real task named `lint:cargo` must be reachable; \
             fast-fail only fires when the filter is empty",
        );
        assert_eq!(found[0].source, TaskSource::Justfile);
    }

    #[test]
    fn bun_test_fallback_enabled_when_resolved_to_bun() {
        let ctx = context(vec![PackageManager::Bun], vec![]);

        // The resolver would return Bun via Lockfile for ctx=[Bun].
        assert!(should_use_bun_test_fallback(
            &ctx,
            Some(PackageManager::Bun),
            "test"
        ));
    }

    #[test]
    fn bun_test_fallback_disabled_when_test_script_exists() {
        let ctx = context(
            vec![PackageManager::Bun],
            vec![Task {
                name: "test".to_string(),
                source: TaskSource::PackageJson,
                run_target: None,
                description: None,
                alias_of: None,
                passthrough_to: None,
            }],
        );

        assert!(!should_use_bun_test_fallback(
            &ctx,
            Some(PackageManager::Bun),
            "test"
        ));
    }

    #[test]
    fn bun_test_fallback_disabled_for_other_package_managers() {
        let ctx = context(vec![PackageManager::Npm], vec![]);

        assert!(!should_use_bun_test_fallback(
            &ctx,
            Some(PackageManager::Npm),
            "test"
        ));
    }

    #[test]
    fn bun_test_fallback_disabled_for_non_test_task() {
        let ctx = context(vec![PackageManager::Bun], vec![]);

        assert!(!should_use_bun_test_fallback(
            &ctx,
            Some(PackageManager::Bun),
            "build"
        ));
    }

    #[test]
    fn bun_test_fallback_suppressed_when_resolver_returns_non_bun() {
        // Models `--pm npm` against a Bun-detected project: the
        // resolver returns Npm (override wins), so the fallback must
        // not fire. The previous-shape "user intent wins" test now
        // collapses to a simpler assertion about the resolved verdict.
        let ctx = context(vec![PackageManager::Bun], vec![]);

        assert!(!should_use_bun_test_fallback(
            &ctx,
            Some(PackageManager::Npm),
            "test"
        ));
    }

    #[test]
    fn bun_test_fallback_disabled_when_resolver_returns_none() {
        // Resolver errored (--fallback=error with no signal) → no
        // fallback. Even though ctx says Bun, the caller already
        // collapsed the error to None.
        let ctx = context(vec![PackageManager::Bun], vec![]);

        assert!(!should_use_bun_test_fallback(&ctx, None, "test"));
    }

    #[test]
    fn bun_test_fallback_enabled_when_resolver_picks_bun_with_no_lockfile() {
        // Models `--pm bun` against an empty ctx — resolver returns
        // Bun even though ctx has no detected PM. Fallback fires.
        let ctx = context(vec![], vec![]);

        assert!(should_use_bun_test_fallback(
            &ctx,
            Some(PackageManager::Bun),
            "test"
        ));
    }

    #[test]
    fn source_depth_walks_upward_for_non_node_sources() {
        // Generalization landed in the same change: depth-aware tiebreak
        // used to require a custom upward walker per source. Now every
        // source consults `tool::files::find_first_upwards`, so a
        // Makefile two levels up still resolves with a finite depth (and
        // therefore beats a hypothetical sibling resolved at MAX).
        let dir = TempDir::new("source-depth-upward");
        let nested = dir.path().join("apps").join("api");
        fs::create_dir_all(&nested).expect("nested dir should be created");
        fs::write(dir.path().join("Makefile"), "build:\n\techo build\n")
            .expect("root Makefile should be written");

        let ctx = ProjectContext {
            root: nested,
            package_managers: Vec::new(),
            task_runners: Vec::new(),
            tasks: Vec::new(),
            node_version: None,
            current_node: None,
            is_monorepo: false,
            warnings: Vec::new(),
        };

        let depth = super::source_depth(&ctx, TaskSource::Makefile);
        assert_ne!(depth, usize::MAX, "Makefile two levels up should resolve");
    }

    #[test]
    fn source_depth_treats_subdirectory_config_as_depth_zero() {
        // `.cargo/config.toml` sits *inside* root (parent dir is
        // `<root>/.cargo`), not as an ancestor. The ancestors() walk
        // never matches it, so without the subdir-fallback the depth
        // would collapse to `usize::MAX` and any root-level source
        // (`bacon.toml`, `Makefile`, …) would win every tiebreak by
        // default — robbing `display_order` of the tie-break it was
        // designed to perform.
        let dir = TempDir::new("source-depth-subdirectory");
        let cargo_dir = dir.path().join(".cargo");
        fs::create_dir_all(&cargo_dir).expect(".cargo dir should be created");
        fs::write(
            cargo_dir.join("config.toml"),
            "[alias]\nlint = \"clippy\"\n",
        )
        .expect("config.toml should be written");

        let ctx = ProjectContext {
            root: dir.path().to_path_buf(),
            package_managers: Vec::new(),
            task_runners: Vec::new(),
            tasks: Vec::new(),
            node_version: None,
            current_node: None,
            is_monorepo: false,
            warnings: Vec::new(),
        };

        let depth = super::source_depth(&ctx, TaskSource::CargoAliases);
        assert_eq!(
            depth, 0,
            ".cargo/config.toml is a subdir of root → treat as depth 0",
        );
    }

    #[test]
    fn cargo_aliases_beats_bacon_toml_for_same_name_task() {
        // Once both sources resolve to depth 0 (cargo via the subdir
        // fallback, bacon via root-level match), the `display_order`
        // tiebreak should pick cargo (6) over bacon (7). This is what
        // the user expected when their `.cargo/config.toml` alias for
        // `lint` was being silently overridden by `bacon.toml`'s
        // `[jobs.lint]` + `default_job = "lint"`.
        let dir = TempDir::new("priority-cargo-vs-bacon");
        let cargo_dir = dir.path().join(".cargo");
        fs::create_dir_all(&cargo_dir).expect(".cargo dir should be created");
        fs::write(
            cargo_dir.join("config.toml"),
            "[alias]\nlint = \"clippy\"\n",
        )
        .expect("config.toml should be written");
        fs::write(
            dir.path().join("bacon.toml"),
            "[jobs.lint]\ncommand = [\"cargo\", \"clippy\"]\n",
        )
        .expect("bacon.toml should be written");

        let tasks = vec![
            Task {
                name: "lint".to_string(),
                source: TaskSource::BaconToml,
                run_target: None,
                description: None,
                alias_of: None,
                passthrough_to: None,
            },
            Task {
                name: "lint".to_string(),
                source: TaskSource::CargoAliases,
                run_target: None,
                description: None,
                alias_of: None,
                passthrough_to: None,
            },
        ];
        let ctx = ProjectContext {
            root: dir.path().to_path_buf(),
            package_managers: Vec::new(),
            task_runners: Vec::new(),
            tasks,
            node_version: None,
            current_node: None,
            is_monorepo: false,
            warnings: Vec::new(),
        };

        let candidates: Vec<&Task> = ctx.tasks.iter().collect();
        let entry = select_task_entry(&ctx, &ResolutionOverrides::default(), &candidates);
        assert_eq!(
            entry.source,
            TaskSource::CargoAliases,
            "display_order should pick CargoAliases over BaconToml once both hit depth 0",
        );
    }

    #[test]
    fn select_task_entry_prefers_package_json_over_deno_json() {
        let dir = TempDir::new("run-deno-nearest");
        let nested = dir.path().join("apps").join("site").join("src");
        fs::create_dir_all(&nested).expect("nested dir should be created");
        fs::write(
            dir.path().join("deno.jsonc"),
            r#"{ tasks: { build: "deno task build" } }"#,
        )
        .expect("root deno.jsonc should be written");
        fs::write(
            dir.path().join("apps").join("site").join("package.json"),
            r#"{ "scripts": { "build": "deno task build" } }"#,
        )
        .expect("member package.json should be written");
        let ctx = ProjectContext {
            root: nested,
            package_managers: vec![PackageManager::Deno],
            task_runners: Vec::new(),
            tasks: vec![
                Task {
                    name: "build".to_string(),
                    source: TaskSource::DenoJson,
                    run_target: None,
                    description: None,
                    alias_of: None,
                    passthrough_to: None,
                },
                Task {
                    name: "build".to_string(),
                    source: TaskSource::PackageJson,
                    run_target: None,
                    description: None,
                    alias_of: None,
                    passthrough_to: None,
                },
            ],
            node_version: None,
            current_node: None,
            is_monorepo: false,
            warnings: Vec::new(),
        };

        let found: Vec<_> = ctx.tasks.iter().collect();
        let overrides = ResolutionOverrides::default();
        let entry = select_task_entry(&ctx, &overrides, &found);

        assert_eq!(entry.source, TaskSource::PackageJson);
    }

    fn context(package_managers: Vec<PackageManager>, tasks: Vec<Task>) -> ProjectContext {
        ProjectContext {
            root: PathBuf::from("."),
            package_managers,
            task_runners: Vec::new(),
            tasks,
            node_version: None,
            current_node: None,
            is_monorepo: false,
            warnings: Vec::new(),
        }
    }

    fn task(name: &str, source: TaskSource) -> Task {
        Task {
            name: name.to_string(),
            source,
            run_target: None,
            description: None,
            alias_of: None,
            passthrough_to: None,
        }
    }

    #[test]
    fn prefer_runners_reorders_default_tier() {
        // Default priority would pick TurboJson first; `prefer = [just]`
        // promotes the Justfile candidate above it.
        let ctx = context(
            vec![],
            vec![
                task("build", TaskSource::TurboJson),
                task("build", TaskSource::Justfile),
            ],
        );
        let found: Vec<_> = ctx.tasks.iter().collect();
        let overrides = ResolutionOverrides {
            prefer_runners: vec![TaskRunner::Just],
            ..ResolutionOverrides::default()
        };
        let entry = select_task_entry(&ctx, &overrides, &found);

        assert_eq!(entry.source, TaskSource::Justfile);
    }

    #[test]
    fn runner_override_promotes_just_over_turbo() {
        // `--runner just` restricts candidates; `select_task_entry` is
        // called after `run()` filters by the constraint, but with no
        // constraint helper here we exercise the priority directly.
        let ctx = context(
            vec![],
            vec![
                task("build", TaskSource::TurboJson),
                task("build", TaskSource::Justfile),
            ],
        );
        // Only the Justfile candidate survives the constraint.
        let found: Vec<&Task> = ctx
            .tasks
            .iter()
            .filter(|t| t.source == TaskSource::Justfile)
            .collect();
        let overrides = ResolutionOverrides::default();
        let entry = select_task_entry(&ctx, &overrides, &found);

        assert_eq!(entry.source, TaskSource::Justfile);
    }
}