From b5708d8723ae6d55bcd2547c7263fe7f1b20fae1 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 13 Oct 2026 18:37:29 -0700 Subject: [PATCH] Pass justfile to default chooser via environment variable --- src/subcommand.rs | 13 +++++-------- tests/choose.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/subcommand.rs b/src/subcommand.rs index e38972df61..0aa691ff13 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -272,19 +272,16 @@ impl Subcommand { return Err(Error::NoChoosableRecipes); } + let mut command = justfile.settings.shell_command(config); + let chooser = if let Some(chooser) = chooser { chooser.as_str().into() } else { - let mut chooser = String::new(); - chooser.push_str("fzf --multi --preview 'just --unstable --color always --justfile \""); - chooser.push_str(search.justfile.as_str()); - chooser.push_str("\" --show {}'"); - chooser + command.env("JUST_JUSTFILE", &search.justfile); + "fzf --multi --preview 'just --unstable --color always --show {}'".into() }; - let result = justfile - .settings - .shell_command(config) + let result = command .shell_arg(&chooser) .current_dir(&search.working_directory) .stdin(Stdio::piped()) diff --git a/tests/choose.rs b/tests/choose.rs index f397fb58d6..e283f1265e 100644 --- a/tests/choose.rs +++ b/tests/choose.rs @@ -171,7 +171,7 @@ fn invoke_error_function() { ", ) .stderr_regex( - r#"error: chooser `/ -cu fzf --multi --preview 'just --unstable --color always --justfile ".*justfile" --show \{\}'` invocation failed: .*\n"#, + r"error: chooser `/ -cu fzf --multi --preview 'just --unstable --color always --show \{\}'` invocation failed: .*\n", ) .shell(false) .args(["--shell", "/", "--choose"]) @@ -374,6 +374,50 @@ fn visit_modules_in_alphabetical_order() { .success(); } +#[cfg(unix)] +#[test] +fn default_chooser_preview_finds_justfile() { + let tmp = tempdir(); + + fs::write(tmp.path().join("foo.just"), "foo:\n echo foo\n").unwrap(); + + let fzf = tmp.path().join("fzf"); + fs::write( + &fzf, + format!( + "#!/usr/bin/env bash\neval \"${{3//\\{{\\}}/foo}}\" > '{}' 2>&1\necho foo\n", + tmp.path().join("output").display(), + ), + ) + .unwrap(); + fs::set_permissions(&fzf, std::os::unix::fs::PermissionsExt::from_mode(0o700)).unwrap(); + + let path = env::join_paths( + [ + tmp.path().to_owned(), + Path::new(JUST).parent().unwrap().to_owned(), + ] + .into_iter() + .chain(env::split_paths(&env::var_os("PATH").unwrap())), + ) + .unwrap(); + + let output = Command::new(JUST) + .arg("--choose") + .arg("--justfile=foo.just") + .current_dir(tmp.path()) + .env("PATH", path) + .output() + .unwrap(); + + assert_stdout(&output, "foo\n"); + + assert_eq!( + fs::read_to_string(tmp.path().join("output")).unwrap(), + "foo:\n echo foo\n" + ); +} + #[cfg(unix)] #[test] fn chooser_signal_exit_code_is_propagated() {