Repository: casey/just
Pull request #1759 (2023-12-12 21:45:38)

Chooser: Pass justfile path to preview command for default chooser
The default chooser is fzf, and we pass it a command to generate the preview with `just --show` from the recipe name. This has been working well as long as we rely on the default justfile available in the directory, given that the preview command can find it too. However, passing `--chose` alongside `--justfile` to select a specific file results in the preview command not being able to find the right justfile to process.

To address this issue, we turn the const string defining the preview command into a function that takes the internal representation of the justfile as an argument, and updates the preview command with the right file.

Fixes: https://github.com/casey/just/issues/1638

Qeole: I'm getting a failure on the CI test this time, `justfile` has been replaced with `/tmp/just-test-tempdirWEynpr/justfile`. I can probably use a wildcard to address this in the test.

```
---- choose::invoke_error_function stdout ----
thread 'choose::invoke_error_function' panicked at tests/test.rs:244:9:
Stderr regex mismatch:
"error: Chooser `/ -cu fzf --multi --preview 'just --unstable --color always --justfile /tmp/just-test-tempdirWEynpr/justfile --show {}'` invocation failed: Permission denied (os error 13)\n"
!~=
/Regex("^error: Chooser `/ -cu fzf --multi --preview 'just --unstable --color always --justfile justfile --show \\{\\}'` invocation failed: .*\\n$")/
```

Patch:
diff --git a/completions/just.elvish b/completions/just.elvish
index 51176476..57ad502e 100644
--- a/completions/just.elvish
+++ b/completions/just.elvish
@@ -51,7 +51,7 @@ edit:completion:arg-completer[just] = [@words]{
             cand -v 'Use verbose output'
             cand --verbose 'Use verbose output'
             cand --changelog 'Print changelog'
-            cand --choose 'Select one or more recipes to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`'
+            cand --choose 'Select one or more recipes to run using a binary chooser. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`'
             cand --dump 'Print justfile'
             cand -e 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`'
             cand --edit 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`'
diff --git a/completions/just.fish b/completions/just.fish
index aa126bdd..aa28dbd9 100644
--- a/completions/just.fish
+++ b/completions/just.fish
@@ -64,7 +64,7 @@ complete -c just -n "__fish_use_subcommand" -s u -l unsorted -d 'Return list and
 complete -c just -n "__fish_use_subcommand" -l unstable -d 'Enable unstable features'
 complete -c just -n "__fish_use_subcommand" -s v -l verbose -d 'Use verbose output'
 complete -c just -n "__fish_use_subcommand" -l changelog -d 'Print changelog'
-complete -c just -n "__fish_use_subcommand" -l choose -d 'Select one or more recipes to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`'
+complete -c just -n "__fish_use_subcommand" -l choose -d 'Select one or more recipes to run using a binary chooser. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`'
 complete -c just -n "__fish_use_subcommand" -l dump -d 'Print justfile'
 complete -c just -n "__fish_use_subcommand" -s e -l edit -d 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`'
 complete -c just -n "__fish_use_subcommand" -l evaluate -d 'Evaluate and print all variables. If a variable name is given as an argument, only print that variable\'s value.'
diff --git a/completions/just.powershell b/completions/just.powershell
index d2907e3c..92439dc2 100644
--- a/completions/just.powershell
+++ b/completions/just.powershell
@@ -56,7 +56,7 @@ Register-ArgumentCompleter -Native -CommandName 'just' -ScriptBlock {
             [CompletionResult]::new('-v', 'v', [CompletionResultType]::ParameterName, 'Use verbose output')
             [CompletionResult]::new('--verbose', 'verbose', [CompletionResultType]::ParameterName, 'Use verbose output')
             [CompletionResult]::new('--changelog', 'changelog', [CompletionResultType]::ParameterName, 'Print changelog')
-            [CompletionResult]::new('--choose', 'choose', [CompletionResultType]::ParameterName, 'Select one or more recipes to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`')
+            [CompletionResult]::new('--choose', 'choose', [CompletionResultType]::ParameterName, 'Select one or more recipes to run using a binary chooser. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`')
             [CompletionResult]::new('--dump', 'dump', [CompletionResultType]::ParameterName, 'Print justfile')
             [CompletionResult]::new('-e', 'e', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`')
             [CompletionResult]::new('--edit', 'edit', [CompletionResultType]::ParameterName, 'Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`')
diff --git a/completions/just.zsh b/completions/just.zsh
index b2ee3d65..46f35569 100644
--- a/completions/just.zsh
+++ b/completions/just.zsh
@@ -52,7 +52,7 @@ _just() {
 '*-v[Use verbose output]' \
 '*--verbose[Use verbose output]' \
 '--changelog[Print changelog]' \
-'--choose[Select one or more recipes to run using a binary. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`]' \
+'--choose[Select one or more recipes to run using a binary chooser. If `--chooser` is not passed the chooser defaults to the value of $JUST_CHOOSER, falling back to `fzf`]' \
 '--dump[Print justfile]' \
 '-e[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \
 '--edit[Edit justfile with editor given by $VISUAL or $EDITOR, falling back to `vim`]' \
diff --git a/src/config.rs b/src/config.rs
index b22e9190..ec611606 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -3,13 +3,19 @@ use {
   clap::{App, AppSettings, Arg, ArgGroup, ArgMatches, ArgSettings},
 };
 
-// These three strings should be kept in sync:
-pub(crate) const CHOOSER_DEFAULT: &str =
-  "fzf --multi --preview 'just --unstable --color always --show {}'";
 pub(crate) const CHOOSER_ENVIRONMENT_KEY: &str = "JUST_CHOOSER";
-pub(crate) const CHOOSE_HELP: &str = "Select one or more recipes to run using a binary. If \
-                                      `--chooser` is not passed the chooser defaults to the value \
-                                      of $JUST_CHOOSER, falling back to `fzf`";
+
+pub(crate) const CHOOSE_HELP: &str = "Select one or more recipes to run using a binary chooser. \
+                                      If `--chooser` is not passed the chooser defaults to the \
+                                      value of $JUST_CHOOSER, falling back to `fzf`";
+
+pub(crate) fn chooser_default(justfile: &Path) -> OsString {
+  let mut chooser = OsString::new();
+  chooser.push("fzf --multi --preview 'just --unstable --color always --justfile \"");
+  chooser.push(justfile);
+  chooser.push("\" --show {}'");
+  chooser
+}
 
 #[derive(Debug, PartialEq)]
 #[allow(clippy::struct_excessive_bools)]
diff --git a/src/subcommand.rs b/src/subcommand.rs
index 99dacec4..294b0c13 100644
--- a/src/subcommand.rs
+++ b/src/subcommand.rs
@@ -216,7 +216,7 @@ impl Subcommand {
     let chooser = chooser
       .map(OsString::from)
       .or_else(|| env::var_os(config::CHOOSER_ENVIRONMENT_KEY))
-      .unwrap_or_else(|| OsString::from(config::CHOOSER_DEFAULT));
+      .unwrap_or_else(|| config::chooser_default(&search.justfile));
 
     let result = justfile
       .settings


Test patch:
diff --git a/tests/choose.rs b/tests/choose.rs
index 940fab0e..892045cf 100644
--- a/tests/choose.rs
+++ b/tests/choose.rs
@@ -152,7 +152,7 @@ fn invoke_error_function() {
       ",
     )
     .stderr_regex(
-      r"error: Chooser `/ -cu fzf --multi --preview 'just --unstable --color always --show \{\}'` invocation failed: .*\n",
+      r#"error: Chooser `/ -cu fzf --multi --preview 'just --unstable --color always --justfile ".*justfile" --show \{\}'` invocation failed: .*\n"#,
     )
     .status(EXIT_FAILURE)
     .shell(false)