Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Discover only executable custom subcommands (#4565)

Maximilian Roosee9d1b823869
diff --git a/src/completion.rs b/src/completion.rs
index 0a98750d3..3d167abdf 100644
--- a/src/completion.rs
+++ b/src/completion.rs
@@ -903,7 +903,6 @@ fn discover_custom_subcommands() -> Vec<String> {
/// (without the `wt-` prefix). On Windows, executable extensions (.exe, .cmd, etc.)
/// are stripped so that `wt-sync.exe` produces the subcommand name `sync`.
fn discover_custom_subcommands_in(path_var: &OsStr) -> Vec<String> {
- let mut seen = std::collections::HashSet::new();
let mut result = Vec::new();
for dir in std::env::split_paths(path_var) {
@@ -925,25 +924,19 @@ fn discover_custom_subcommands_in(path_var: &OsStr) -> Vec<String> {
.and_then(|s| s.to_str())
.unwrap_or(subcommand);
- if subcommand.is_empty() || !seen.insert(subcommand.to_string()) {
- continue;
- }
-
- // Verify it's executable (on Unix)
- #[cfg(unix)]
+ // Use the dispatch resolver's executable rules, including symlink
+ // targets and Windows extensions. A non-executable entry must not
+ // hide a valid command in a later PATH directory.
+ if subcommand.is_empty() || which::which(dir.join(format!("wt-{subcommand}"))).is_err()
{
- use std::os::unix::fs::PermissionsExt;
- if let Ok(meta) = entry.metadata()
- && meta.permissions().mode() & 0o111 == 0
- {
- continue;
- }
+ continue;
}
result.push(subcommand.to_string());
}
}
result.sort();
+ result.dedup();
result
}
@@ -1156,6 +1149,48 @@ mod tests {
assert!(result.is_empty());
}
+ #[test]
+ fn test_discover_skips_directories() {
+ let dir = worktrunk::testing::test_tempdir();
+ std::fs::create_dir(dir.path().join("wt-directory")).unwrap();
+ assert!(discover_custom_subcommands_in(dir.path().as_os_str()).is_empty());
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn test_discover_checks_symlink_targets() {
+ use std::os::unix::fs::{PermissionsExt, symlink};
+ let dir = worktrunk::testing::test_tempdir();
+ let executable = dir.path().join("executable");
+ let non_executable = dir.path().join("data");
+ std::fs::write(&executable, "#!/bin/sh\n").unwrap();
+ std::fs::set_permissions(&executable, std::fs::Permissions::from_mode(0o755)).unwrap();
+ std::fs::write(&non_executable, "data").unwrap();
+ std::fs::set_permissions(&non_executable, std::fs::Permissions::from_mode(0o644)).unwrap();
+ symlink(&executable, dir.path().join("wt-valid")).unwrap();
+ symlink(&non_executable, dir.path().join("wt-invalid")).unwrap();
+ symlink(dir.path().join("missing"), dir.path().join("wt-broken")).unwrap();
+ assert_eq!(
+ discover_custom_subcommands_in(dir.path().as_os_str()),
+ vec!["valid"]
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn test_discover_finds_later_executable() {
+ use std::os::unix::fs::PermissionsExt;
+ let first = worktrunk::testing::test_tempdir();
+ let second = worktrunk::testing::test_tempdir();
+ for (dir, mode) in [(first.path(), 0o644), (second.path(), 0o755)] {
+ let path = dir.join("wt-visible");
+ std::fs::write(&path, "#!/bin/sh\n").unwrap();
+ std::fs::set_permissions(&path, std::fs::Permissions::from_mode(mode)).unwrap();
+ }
+ let path = std::env::join_paths([first.path(), second.path()]).unwrap();
+ assert_eq!(discover_custom_subcommands_in(&path), vec!["visible"]);
+ }
+
#[cfg(unix)]
#[test]
fn test_discover_results_are_sorted() {