Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Simplify shell integration detection to known setup lines (#4595)
Maximilian Roosbb25aea3efff
diff --git a/docs/src/content/docs/config.md b/docs/src/content/docs/config.mdindex c063264ef..175f2762e 100644--- a/docs/src/content/docs/config.md+++ b/docs/src/content/docs/config.md@@ -918,9 +918,7 @@ $ wt config shell uninstall --yes### Version tolerance-Uninstall removes every worktrunk-managed integration it finds, regardless of:-- Binary name it was installed under (`wt`, `git-wt`, …)-- Minor syntax variations between versions+Uninstall recognizes the standalone setup lines written by Worktrunk or shown in `wt config shell init --help`, including older generated forms and custom binary names. Customized initialization lines must be removed manually.### Command referencediff --git a/docs/src/content/docs/faq.md b/docs/src/content/docs/faq.mdindex 57357b4dd..c75d4f167 100644--- a/docs/src/content/docs/faq.md+++ b/docs/src/content/docs/faq.md@@ -217,7 +217,7 @@ A branch checked out in a second worktree is retained regardless, `-D` included.- `wt remove` — also stops the removed worktree's `git fsmonitor--daemon` (under `core.fsmonitor=true`), and resumes deleting `.git/wt/trash/` entries an interrupted removal left behind, along with fsmonitor daemons whose worktree no longer exists- `wt config state clear` — clears config keys, caches, markers, hints, variables, logs, and stale trash- `wt config shell install` — replaces an existing fish or Nushell wrapper file whole, and removes one an older version installed at a previous location (fish `conf.d/wt.fish`, Nushell `<config-dir>/vendor/autoload/wt.nu`), printing each removal; [Files created](/shell-integration/#files-created) names the file each shell gets-- `wt config shell uninstall` — removes the lines that run `wt config shell init` from bash/zsh/PowerShell rc files, and deletes wrapper and completion files carrying worktrunk's content markers (fish `functions/`, `conf.d/`, and `completions/`; nushell `vendor/autoload`). Every removed line is printed+- `wt config shell uninstall` — removes recognized initialization lines from bash/zsh/PowerShell rc files, and deletes wrapper and completion files carrying worktrunk's content markers (fish `functions/`, `conf.d/`, and `completions/`; nushell `vendor/autoload`). Customized initialization lines must be removed manually. Every removed line is printed- `wt config plugins opencode uninstall` / `wt config plugins pi uninstall` / `wt config plugins omp uninstall` — deletes that agent's `worktrunk.ts` plugin file. Only worktrunk's own file is touched; the rest of the agent's plugin directory is left aloneSee [What files does Worktrunk create?](#what-files-does-worktrunk-create) for details.diff --git a/docs/src/content/docs/shell-integration.md b/docs/src/content/docs/shell-integration.mdindex cd4753998..6a6ac0f1e 100644--- a/docs/src/content/docs/shell-integration.md+++ b/docs/src/content/docs/shell-integration.md@@ -14,6 +14,8 @@ A subprocess can't change its parent shell's directory, so `wt` installs a shellSee [`wt config shell install`](/config/#wt-config-shell-install) for automatic installation and [`wt config shell init`](/config/#wt-config-shell-init) for manual setup in each supported shell.+Keep each initialization statement on its own line for automatic detection and removal. Customized initialization lines must be managed manually.+## Files created[`wt config shell install`](/config/#wt-config-shell-install) appends a line to the bash, zsh, and PowerShell files, and writes the fish and Nushell files whole:diff --git a/plugins/worktrunk/skills/worktrunk/reference/config.md b/plugins/worktrunk/skills/worktrunk/reference/config.mdindex 390a3cd53..e813d7a37 100644--- a/plugins/worktrunk/skills/worktrunk/reference/config.md+++ b/plugins/worktrunk/skills/worktrunk/reference/config.md@@ -910,9 +910,7 @@ $ wt config shell uninstall --yes### Version tolerance-Uninstall removes every worktrunk-managed integration it finds, regardless of:-- Binary name it was installed under (`wt`, `git-wt`, …)-- Minor syntax variations between versions+Uninstall recognizes the standalone setup lines written by Worktrunk or shown in `wt config shell init --help`, including older generated forms and custom binary names. Customized initialization lines must be removed manually.### Command referencediff --git a/plugins/worktrunk/skills/worktrunk/reference/faq.md b/plugins/worktrunk/skills/worktrunk/reference/faq.mdindex c1697b578..2ff5e248d 100644--- a/plugins/worktrunk/skills/worktrunk/reference/faq.md+++ b/plugins/worktrunk/skills/worktrunk/reference/faq.md@@ -213,7 +213,7 @@ A branch checked out in a second worktree is retained regardless, `-D` included.- `wt remove` — also stops the removed worktree's `git fsmonitor--daemon` (under `core.fsmonitor=true`), and resumes deleting `.git/wt/trash/` entries an interrupted removal left behind, along with fsmonitor daemons whose worktree no longer exists- `wt config state clear` — clears config keys, caches, markers, hints, variables, logs, and stale trash- `wt config shell install` — replaces an existing fish or Nushell wrapper file whole, and removes one an older version installed at a previous location (fish `conf.d/wt.fish`, Nushell `<config-dir>/vendor/autoload/wt.nu`), printing each removal; [Files created](https://worktrunk.dev/shell-integration/#files-created) names the file each shell gets-- `wt config shell uninstall` — removes the lines that run `wt config shell init` from bash/zsh/PowerShell rc files, and deletes wrapper and completion files carrying worktrunk's content markers (fish `functions/`, `conf.d/`, and `completions/`; nushell `vendor/autoload`). Every removed line is printed+- `wt config shell uninstall` — removes recognized initialization lines from bash/zsh/PowerShell rc files, and deletes wrapper and completion files carrying worktrunk's content markers (fish `functions/`, `conf.d/`, and `completions/`; nushell `vendor/autoload`). Customized initialization lines must be removed manually. Every removed line is printed- `wt config plugins opencode uninstall` / `wt config plugins pi uninstall` / `wt config plugins omp uninstall` — deletes that agent's `worktrunk.ts` plugin file. Only worktrunk's own file is touched; the rest of the agent's plugin directory is left aloneSee [What files does Worktrunk create?](#what-files-does-worktrunk-create) for details.diff --git a/plugins/worktrunk/skills/worktrunk/reference/shell-integration.md b/plugins/worktrunk/skills/worktrunk/reference/shell-integration.mdindex 8b8c4abf0..b30f7e99e 100644--- a/plugins/worktrunk/skills/worktrunk/reference/shell-integration.md+++ b/plugins/worktrunk/skills/worktrunk/reference/shell-integration.md@@ -10,6 +10,8 @@ A subprocess can't change its parent shell's directory, so `wt` installs a shellSee [`wt config shell install`](https://worktrunk.dev/config/#wt-config-shell-install) for automatic installation and [`wt config shell init`](https://worktrunk.dev/config/#wt-config-shell-init) for manual setup in each supported shell.+Keep each initialization statement on its own line for automatic detection and removal. Customized initialization lines must be managed manually.+## Files created[`wt config shell install`](https://worktrunk.dev/config/#wt-config-shell-install) appends a line to the bash, zsh, and PowerShell files, and writes the fish and Nushell files whole:diff --git a/skills/worktrunk/reference/config.md b/skills/worktrunk/reference/config.mdindex 390a3cd53..e813d7a37 100644--- a/skills/worktrunk/reference/config.md+++ b/skills/worktrunk/reference/config.md@@ -910,9 +910,7 @@ $ wt config shell uninstall --yes### Version tolerance-Uninstall removes every worktrunk-managed integration it finds, regardless of:-- Binary name it was installed under (`wt`, `git-wt`, …)-- Minor syntax variations between versions+Uninstall recognizes the standalone setup lines written by Worktrunk or shown in `wt config shell init --help`, including older generated forms and custom binary names. Customized initialization lines must be removed manually.### Command referencediff --git a/skills/worktrunk/reference/faq.md b/skills/worktrunk/reference/faq.mdindex c1697b578..2ff5e248d 100644--- a/skills/worktrunk/reference/faq.md+++ b/skills/worktrunk/reference/faq.md@@ -213,7 +213,7 @@ A branch checked out in a second worktree is retained regardless, `-D` included.- `wt remove` — also stops the removed worktree's `git fsmonitor--daemon` (under `core.fsmonitor=true`), and resumes deleting `.git/wt/trash/` entries an interrupted removal left behind, along with fsmonitor daemons whose worktree no longer exists- `wt config state clear` — clears config keys, caches, markers, hints, variables, logs, and stale trash- `wt config shell install` — replaces an existing fish or Nushell wrapper file whole, and removes one an older version installed at a previous location (fish `conf.d/wt.fish`, Nushell `<config-dir>/vendor/autoload/wt.nu`), printing each removal; [Files created](https://worktrunk.dev/shell-integration/#files-created) names the file each shell gets-- `wt config shell uninstall` — removes the lines that run `wt config shell init` from bash/zsh/PowerShell rc files, and deletes wrapper and completion files carrying worktrunk's content markers (fish `functions/`, `conf.d/`, and `completions/`; nushell `vendor/autoload`). Every removed line is printed+- `wt config shell uninstall` — removes recognized initialization lines from bash/zsh/PowerShell rc files, and deletes wrapper and completion files carrying worktrunk's content markers (fish `functions/`, `conf.d/`, and `completions/`; nushell `vendor/autoload`). Customized initialization lines must be removed manually. Every removed line is printed- `wt config plugins opencode uninstall` / `wt config plugins pi uninstall` / `wt config plugins omp uninstall` — deletes that agent's `worktrunk.ts` plugin file. Only worktrunk's own file is touched; the rest of the agent's plugin directory is left aloneSee [What files does Worktrunk create?](#what-files-does-worktrunk-create) for details.diff --git a/skills/worktrunk/reference/shell-integration.md b/skills/worktrunk/reference/shell-integration.mdindex 8b8c4abf0..b30f7e99e 100644--- a/skills/worktrunk/reference/shell-integration.md+++ b/skills/worktrunk/reference/shell-integration.md@@ -10,6 +10,8 @@ A subprocess can't change its parent shell's directory, so `wt` installs a shellSee [`wt config shell install`](https://worktrunk.dev/config/#wt-config-shell-install) for automatic installation and [`wt config shell init`](https://worktrunk.dev/config/#wt-config-shell-init) for manual setup in each supported shell.+Keep each initialization statement on its own line for automatic detection and removal. Customized initialization lines must be managed manually.+## Files created[`wt config shell install`](https://worktrunk.dev/config/#wt-config-shell-install) appends a line to the bash, zsh, and PowerShell files, and writes the fish and Nushell files whole:diff --git a/src/cli/config.rs b/src/cli/config.rsindex e399116c4..64caf9ac5 100644--- a/src/cli/config.rs+++ b/src/cli/config.rs@@ -134,9 +134,7 @@ $ wt config shell uninstall --yes## Version tolerance-Uninstall removes every worktrunk-managed integration it finds, regardless of:-- Binary name it was installed under (`wt`, `git-wt`, …)-- Minor syntax variations between versions"#+Uninstall recognizes the standalone setup lines written by Worktrunk or shown in `wt config shell init --help`, including older generated forms and custom binary names. Customized initialization lines must be removed manually."#)]Uninstall {/// Shell to uninstall (default: all)diff --git a/src/commands/config/show.rs b/src/commands/config/show.rsindex e34a6ae81..f1ac254fd 100644--- a/src/commands/config/show.rs+++ b/src/commands/config/show.rs@@ -1550,7 +1550,7 @@ fn render_shell_status(out: &mut String) -> anyhow::Result<()> {)?;}- // Show potential false negatives (lines containing cmd but not detected)+ // Show references to the command outside recognized initialization lines.// Skip files that have valid integration detected (matched_lines) - those are fine,// and the other lines containing cmd are just part of the integration script.// Also skip files already confirmed as integration by scan_shell_configs (e.g., Nushell/Fish@@ -1584,7 +1584,7 @@ fn render_shell_status(out: &mut String) -> anyhow::Result<()> {out,"{}",warning_message(cformat!(- "Found <bold>{cmd}</> in <bold>{location}</> but not detected as integration:"+ "Found <bold>{cmd}</> outside a recognized initialization line @ <bold>{location}</>:")))?;for detected in &detection.unmatched_candidates {@@ -1634,52 +1634,12 @@ fn render_shell_status(out: &mut String) -> anyhow::Result<()> {}}- // Whenever there are unmatched candidates, offer the false-negative report link.- // A real detector miss in one config file is worth reporting even if another shell- // is correctly detected — the `confirmed_paths` filter already excludes wrapper files,- // and the narrow phrasing ("meant to load") steers non-integration lines (plain aliases)- // away from reporting themselves as bugs.if has_any_unmatched {- let unmatched_summary: Vec<_> = detection_results- .iter()- .filter(|r| {- !r.unmatched_candidates.is_empty()- && r.matched_lines.is_empty()- && !confirmed_paths.contains(r.path.as_path())- })- .flat_map(|r| {- r.unmatched_candidates- .iter()- .map(|d| d.content.trim().to_string())- })- .collect();- let body = format!(- "Shell integration not detected despite config containing `{cmd}`.\n\n\- **Unmatched lines:**\n```\n{}\n```\n\n\- **Expected behavior:** These lines should be detected as shell integration.",- unmatched_summary.join("\n")- );- let issue_url = format!(- "https://github.com/max-sixty/worktrunk/issues/new?title={}&body={}",- urlencoding::encode("Shell integration detection false negative"),- urlencoding::encode(&body)- );-- // Quote a short version of the unmatched content in the hint- let quoted = if unmatched_summary.len() == 1 {- format!("`{}`", unmatched_summary[0])- } else {- format!(- "`{}` (and {} more)",- unmatched_summary[0],- unmatched_summary.len() - 1- )- };writeln!(out,"{}",- hint_message(format!(- "If {quoted} is meant to load Worktrunk shell integration, report a false negative: {issue_url}"+ hint_message(cformat!(+ "To see recognized initialization lines, run <underline>{cmd} config shell init --help</>")))?;}diff --git a/src/commands/configure_shell.rs b/src/commands/configure_shell.rsindex ba02857b7..f800c3f2b 100644--- a/src/commands/configure_shell.rs+++ b/src/commands/configure_shell.rs@@ -896,20 +896,13 @@ fn contains_shell_integration(format_path_for_display(path))})?;- if is_install_shell_integration_line(line.trim_end_matches('\r'), shell, cmd) {+ if shell::shell_integration(line, cmd) == Some(shell) {return Ok(true);}}Ok(false)}-fn is_install_shell_integration_line(line: &str, shell: Shell, cmd: &str) -> bool {- shell::is_shell_integration_line(line, cmd)- && line- .to_ascii_lowercase()- .contains(&format!("config shell init {shell}"))-}-/// Extract non-comment, non-blank lines from fish/nushell source.////// Install compares wrapper code lines so it detects existing installations even@@ -1816,53 +1809,6 @@ mod tests {assert_eq!(ConfigAction::WouldCreate.symbol(), INFO_SYMBOL);}- #[test]- fn test_is_shell_integration_line() {- // Valid integration lines for "wt"- assert!(shell::is_shell_integration_line(- "eval \"$(wt config shell init bash)\"",- "wt"- ));- assert!(shell::is_shell_integration_line(- " eval \"$(wt config shell init zsh)\" ",- "wt"- ));- assert!(shell::is_shell_integration_line(- "if command -v wt; then eval \"$(wt config shell init bash)\"; fi",- "wt"- ));- assert!(shell::is_shell_integration_line(- "source <(wt config shell init fish)",- "wt"- ));-- // Valid integration lines for "git-wt"- assert!(shell::is_shell_integration_line(- "eval \"$(git-wt config shell init bash)\"",- "git-wt"- ));- assert!(!shell::is_shell_integration_line(- "eval \"$(wt config shell init bash)\"",- "git-wt"- ));-- // Not integration lines (comments)- assert!(!shell::is_shell_integration_line(- "# eval \"$(wt config shell init bash)\"",- "wt"- ));-- // Not integration lines (no eval/source/if)- assert!(!shell::is_shell_integration_line(- "wt config shell init bash",- "wt"- ));- assert!(!shell::is_shell_integration_line(- "echo wt config shell init bash",- "wt"- ));- }-#[test]fn test_fish_completion_content() {insta::assert_snapshot!(fish_completion_content("wt"));diff --git a/src/shell/detection.rs b/src/shell/detection.rsindex 710abfd87..5400969dd 100644--- a/src/shell/detection.rs+++ b/src/shell/detection.rs@@ -11,345 +11,120 @@ use std::path::PathBuf;use super::paths::{home_dir_required, powershell_profile_paths, zsh_config_dir};-/// Detect if a line contains shell integration for a specific command.-///-/// # Detection Goal-///-/// We need to answer: "Is shell integration configured for THIS binary?"-///-/// When running as `wt`, we should detect `wt` integration but NOT `git-wt` integration-/// (and vice versa). This prevents misleading "restart shell to activate" messages when-/// the user has integration for a different command name.-///-/// # Command Name Patterns-///-/// Users invoke worktrunk in several ways, each creating different command names:-///-/// | Invocation | Binary name | Function created |-/// |-------------------------|-------------|------------------|-/// | `wt` | `wt` | `wt()` |-/// | `git wt` (subcommand) | `git-wt` | `git-wt()` |-/// | `git-wt` (direct) | `git-wt` | `git-wt()` |-///-/// Note: `git wt` dispatches to the `git-wt` binary, so both create the same function.-///-/// # Detection Strategy-///-/// We detect shell integration by looking for eval/source lines in shell config files.-///-/// Lines like `eval "$(wt config shell init bash)"` in `.bashrc`/`.zshrc`.-///-/// **Challenge:** `wt config shell init` is a substring of `git wt config shell init`.-///-/// **Solution:** Use negative lookbehind to exclude `git ` and `git-` prefixes:-/// - For `wt`: match `wt config shell init` NOT preceded by `git ` or `git-`-/// - For `git-wt`: match `git-wt config shell init` OR `git wt config shell init`-///-/// # Pattern Details-///-/// **Eval line patterns** (for `wt`):-/// ```text-/// eval "$(wt config shell init bash)" ✓ matches-/// eval "$(command wt config shell init bash)" ✓ matches-/// eval "$(git wt config shell init bash)" ✗ no match (git- prefix)-/// eval "$(git-wt config shell init bash)" ✗ no match (git- prefix)-/// source <(wt config shell init zsh) ✓ matches-/// ```-///-/// # Edge Cases Handled-///-/// - Quoted command names: `eval "$('wt' config shell init bash)"` - rare but matched-/// - Comment lines: `# eval "$(wt config shell init bash)"` - skipped-/// - Partial matches: `newt config shell init` - not matched (word boundary)-///-/// # Usage-///-/// Used by:-/// - `wt config show` - render the matched integration line, and report a line-/// that names the command but did not match as a possible false negative-/// - `contains_shell_integration` - the install scan's idempotency check, which-/// decides whether an rc file already carries this shell's line. It runs-/// under the installation scanner, so these consumers read it: `wt config shell-/// install` skips the append when it answers yes, and `wt config show`'s-/// per-shell rows, shell-integration warnings, and the first-run integration-/// offer read the same answer without writing-///-/// Not by uninstall, which reads-/// [`is_shell_integration_line_for_uninstall_any_cmd`] instead: it asks only-/// whether a line is worktrunk's, whatever binary name it was installed under.-///-/// # Impact of False Negatives-///-/// The messaging cases are benign, but they are not all gated the same way.-/// The shell-integration warnings ask only when shell integration is-/// NOT active (i.e., user ran the binary directly without the shell wrapper);-/// once the wrapper is active, `WORKTRUNK_DIRECTIVE_CD_FILE` is set and they-/// stop asking.-///-/// **When binary is run directly (wrapper not active):**-/// - If detection finds integration → "installed but not active" (restart hint)-/// - If detection misses (false negative) → "shell integration not installed"-///-/// **When wrapper is active:** none of those warnings are shown, regardless of-/// detection.-///-/// `wt config show` is not gated that way — it scans on every run, so a line-/// that names the command but doesn't match is reported as a possible false-/// negative whether or not the wrapper is active. That is still messaging, but-/// it is a warning the user sees rather than silence.-///-/// Install is the case that isn't benign: a miss there reads an-/// already-configured rc file as unconfigured and appends a second copy of the-/// line. `wt switch`'s offer reaches that same write by a longer route — a miss-/// makes it offer an install the user doesn't need, and accepting appends the-/// duplicate. Uninstall removes every line it matches, so the duplicate is not-/// stranded — but this is a write to a user-owned file rather than a message,-/// which is why the bar for a change here is higher than the messaging cases-/// alone would set it.-pub fn is_shell_integration_line(line: &str, cmd: &str) -> bool {- has_init_invocation(line_code_portion(line), cmd)-}--/// The code portion of a config line: empty for comment lines, otherwise the-/// text before any trailing comment — a comment can mention an integration line-/// without running it, so detection reads only the code.-///-/// A `#` opens a comment only at a word start (line start or after whitespace);-/// `$#` and PowerShell's `<#` stay code. Quote-blind by design: no integration-/// form worktrunk writes contains `#`.-fn line_code_portion(line: &str) -> &str {- let trimmed = line.trim();- // Comment lines (# for POSIX shells, <# #> for PowerShell block comments)- if trimmed.starts_with('#') || trimmed.starts_with("<#") {- return "";- }- let end = trimmed- .char_indices()- .find(|&(i, c)| c == '#' && trimmed[..i].ends_with(char::is_whitespace))- .map_or(trimmed.len(), |(i, _)| i);- &trimmed[..end]+/// Recognize complete lines written by Worktrunk or shown in manual setup.+///+/// This is a template match, not a shell parser. Customized expressions remain+/// user-managed: install may append its standard line, and uninstall leaves+/// them alone rather than deleting other commands sharing the line.+/// Returns the shell so installation need not parse the matched text again.+pub fn shell_integration(line: &str, cmd: &str) -> Option<super::Shell> {+ let integration = match_integration_line(line)?;+ let name = integration+ .cmd+ .strip_suffix(".exe")+ .unwrap_or(integration.cmd);+ (integration.active+ && (integration.cmd == cmd || name == cmd || (cmd == "git-wt" && name == "git wt")))+ .then_some(integration.shell)}-/// The invariant part of every integration line. Only the binary name in front-/// of it varies, so it anchors both the cmd-specific and cmd-agnostic searches.-const INIT_MARKER: &str = " config shell init";--/// Cmd-agnostic line detection: matches an integration line whatever binary name-/// it was installed under.-///-/// The whole rule is one question — does this line *invoke*-/// `<name> config shell init`? — answered by `command_invocation_precedes`.-/// Where the cmd-specific detectors search for a known name, this reads the name-/// off the line.-///-/// It deliberately asks nothing about how the output is consumed. A detector-/// that also scanned for `eval`/`source`/`save` would be reading English words-/// out of the whole line, matching them inside quoted strings as readily as in-/// code, and it turns out to reject nothing this doesn't: a line that merely-/// mentions the command has it in argument position, which is already the-/// disqualifier. What is left over is the narrow case of a line that runs-/// `<name> config shell init` and discards the output, which is a worktrunk-/// invocation in an rc file either way.-///-/// Unlike the cmd-specific detectors this deliberately does not exclude a `git `-/// prefix: `git wt config shell init` is worktrunk's own integration too, and-/// uninstall asks only whether a line is ours, not which install it belongs to.-///-/// Used by `wt config shell uninstall`, which deletes the lines it matches. That-/// makes a false positive destructive — it takes a line the user wrote — while a-/// false negative only strands a line they can delete by hand. So where the two-/// trade off, this errs toward leaving the line, and `wt config shell uninstall`-/// shows every line it matched before removing it.+/// Uninstall also recognizes old PowerShell lines without `Out-String`, which+/// no longer activate integration, and installations under other binary names.pub fn is_shell_integration_line_for_uninstall_any_cmd(line: &str) -> bool {- let code = line_code_portion(line);- code.match_indices(INIT_MARKER)- .any(|(pos, _)| command_invocation_precedes(code, pos))-}--/// Does a command *invocation* end at `pos`?-///-/// Two things must hold: the word ending at `pos` names a command, and it sits-/// in command position — it starts a command rather than sitting inside one as-/// an argument.-///-/// The word is read backwards over everything that is neither whitespace nor-/// the shell punctuation wrapping an invocation (`"$(`, `<(`, backticks), so a-/// path to the binary stays whole and its basename is the name. Holding that-/// name to [`validate_shell_command_name`](super::validate_shell_command_name)-/// — the same rule governing what worktrunk is willing to write into shell code-/// — keeps detection and generation from drifting apart. A bare `$VAR` fails it,-/// so `eval "$($WORKTRUNK_BIN config shell init bash)"` is not claimed by-/// either detector: which binary it names isn't knowable from the line.-///-/// Command position is the whole of what separates worktrunk's own line from a-/// user's line that quotes one: in-/// `alias setup='echo "run: wt config shell init fish | source"'` the name is an-/// argument to `echo`, and without that check uninstall deletes the alias.-fn command_invocation_precedes(line: &str, pos: usize) -> bool {- let before = &line[..pos];- let word = trailing_word(before);- if word.is_empty() {- // The marker stands alone with nothing invoking it.- return false;- }- let name = word.rsplit_once(['/', '\\']).map_or(word, |(_, base)| base);- super::validate_shell_command_name(name).is_ok()- && starts_command(&before[..before.len() - word.len()])-}--/// The word ending at the end of `s`: everything back to the nearest whitespace-/// or shell punctuation. Empty when `s` ends in one of those.-fn trailing_word(s: &str) -> &str {- s.char_indices()- .rev()- .take_while(|&(_, c)| {- !c.is_whitespace()- && !matches!(- c,- ';' | '&' | '|' | '(' | ')' | '{' | '}' | '<' | '>' | '`' | '"' | '\''- )- })- .map(|(i, _)| i)- .last()- .map_or("", |start| &s[start..])-}--/// Words that hand command position to whatever follows them: the two that-/// prefix a worktrunk integration line in practice. `command` is what every rc-/// line worktrunk writes uses; `git` covers the hand-written `git wt` dispatch-/// form. A shell has other such words (`then`, `exec`, `do`), but no integration-/// line puts one directly in front of the binary, and admitting one it doesn't-/// need only widens what uninstall will delete.-const COMMAND_MODIFIERS: [&str; 2] = ["command", "git"];--/// Does a command start where `before` ends?-///-/// True at the start of the code and after the punctuation that separates or-/// opens one — `;`, `&`, `|`, a group `{`, and the substitution forms wrapping-/// an integration line (`$(`, `<(`, `=(`, backticks, PowerShell's `&` call-/// operator). A bare word in front means the following word is that word's-/// *argument*, unless the word is a [`COMMAND_MODIFIERS`] entry.-fn starts_command(before: &str) -> bool {- let before = before.trim_end_matches([' ', '\t']);- match before.chars().last() {- None => true,- Some(';' | '&' | '|' | '(' | '{' | '`') => true,- Some(_) => COMMAND_MODIFIERS.contains(&trailing_word(before)),- }+ match_integration_line(line).is_some()}-/// Does the line run the command's output, rather than merely mention it?-///-/// A property of the whole line, not of where the command sits within it: an-/// exec keyword anywhere satisfies it, including inside a quoted string. That-/// makes it too loose to decide a deletion on, so it is confined to the-/// cmd-specific detectors, whose answer only picks a message.-///-/// What it buys there is a PowerShell line lacking `| Out-String`, which does-/// not work — `Invoke-Expression` fails with "Cannot convert-/// 'System.Object[]'" (issue #885). Reporting such a line as *not* configured-/// is what gets `wt config shell install` to rewrite it.-fn is_execution_context(line: &str) -> bool {- let line_lower = line.to_lowercase();- if line_lower.contains("invoke-expression") || line_lower.contains("iex") {- return line_lower.contains("out-string");- }-- // POSIX shells (bash, zsh, fish) and nushell- line.contains("eval")- || line.contains("source")- || line.contains(". <(") // POSIX dot command with process substitution- || line.contains(". =(") // zsh dot command with =() substitution- || line.contains("save") // nushell pipe to save+struct IntegrationLine<'a> {+ shell: super::Shell,+ cmd: &'a str,+ active: bool,}-/// Check if line contains `{cmd} config shell init` as a command invocation.-///-/// For `wt`: matches `wt config shell init` but NOT `git wt` or `git-wt`.-/// For `git-wt`: matches `git-wt config shell init` OR `git wt config shell init`.-fn has_init_invocation(line: &str, cmd: &str) -> bool {- // For git-wt, we need to match both "git-wt config shell init" AND "git wt config shell init"- // because users invoke it both ways (and git dispatches "git wt" to "git-wt")- if cmd == "git-wt" {- // Match either form, with boundary check for "git" in "git wt" form- return has_init_pattern_with_prefix_check(line, "git-wt")- || has_init_pattern_with_prefix_check(line, "git wt");- }-- // For other commands, use normal matching with prefix exclusion- has_init_pattern_with_prefix_check(line, cmd)-}+fn match_integration_line(line: &str) -> Option<IntegrationLine<'_>> {+ use super::Shell;-/// Check if line has the init pattern, with prefix exclusion for non-git-wt commands.-///-/// Handles Windows `.exe` suffix: searches for both `{cmd} config shell init` and-/// `{cmd}.exe config shell init` to match lines like:-/// ```text-/// eval "$(git-wt.exe config shell init bash)"-/// ```-fn has_init_pattern_with_prefix_check(line: &str, cmd: &str) -> bool {- if !is_execution_context(line) {- return false;+ // None of our templates contains '#'. Accept an appended comment without+ // interpreting quoting or comment rules in arbitrary shell expressions.+ let line = line.trim();+ if !line.contains(" config shell init ") {+ return None;}-- // Search for both plain command and .exe variant (Windows Git Bash)- for cmd_in_line in [cmd.to_string(), format!("{cmd}.exe")] {- let init_pattern = format!("{cmd_in_line}{INIT_MARKER}");-- let mut search_start = 0;- while let Some(pos) = line[search_start..].find(&init_pattern) {- let absolute_pos = search_start + pos;-- // Check what precedes the match- if is_valid_command_position(line, absolute_pos, &cmd_in_line) {- return true;+ let line = line+ .split_once(" #")+ .map_or(line, |(code, _)| code)+ .trim_end();+ for shell in Shell::all() {+ let generated = shell.config_line(COMMAND_PLACEHOLDER);+ let manual = match shell {+ Shell::Bash | Shell::Zsh => {+ format!("eval \"$({COMMAND_PLACEHOLDER} config shell init {shell})\"")+ }+ Shell::Fish => format!("{COMMAND_PLACEHOLDER} config shell init fish | source"),+ Shell::Nushell => format!(+ "{COMMAND_PLACEHOLDER} config shell init nu | save -f ($nu.vendor-autoload-dirs | last | path join {COMMAND_PLACEHOLDER}.nu)"+ ),+ Shell::PowerShell => format!(+ "Invoke-Expression (& {COMMAND_PLACEHOLDER} config shell init powershell | Out-String)"+ ),+ };+ let mut templates = vec![generated];+ if matches!(shell, Shell::Bash | Shell::Zsh | Shell::Fish) {+ templates.push(manual.replace(+ COMMAND_PLACEHOLDER,+ &format!("command {COMMAND_PLACEHOLDER}"),+ ));+ }+ templates.push(manual);+ for template in templates {+ if let Some(cmd) = template_command(line, &template) {+ return Some(IntegrationLine {+ shell,+ cmd,+ active: true,+ });+ }+ // The installer used to pass --cmd explicitly. PowerShell also+ // used to omit Out-String (the broken form repaired in #888).+ let old = if shell == Shell::PowerShell {+ template.replace(" | Out-String", "")+ } else {+ template.clone()+ };+ let with_cmd = old.replace(+ &format!("config shell init {shell}"),+ &format!("config shell init {shell} --cmd={COMMAND_PLACEHOLDER}"),+ );+ for template in [old, with_cmd] {+ if let Some(cmd) = template_command(line, &template) {+ return Some(IntegrationLine {+ shell,+ cmd,+ active: shell != Shell::PowerShell,+ });+ }}-- // Continue searching after this match- search_start = absolute_pos + 1;}}-- false+ None}-/// Check if the command at `pos` is a valid standalone command, not part of another command.-///-/// For `wt` at position `pos`:-/// - Valid: start of line, after `$(`, after whitespace, after `command `-/// - Invalid: after `git ` (would be `git wt`), after `git-` (would be `git-wt`)-///-/// For `git-wt`: must not be preceded by alphanumeric (Unicode-aware), underscore, or hyphen-/// (e.g., `my-git-wt` should NOT match)-fn is_valid_command_position(line: &str, pos: usize, cmd: &str) -> bool {- if pos == 0 {- return true; // Start of line- }+const COMMAND_PLACEHOLDER: &str = "WORKTRUNK_COMMAND";- let before = &line[..pos];-- // For git-wt (and git-wt.exe), just check it's not part of a longer identifier- // e.g., `my-git-wt` should not match- if cmd == "git-wt" || cmd == "git-wt.exe" {- let last_char = before.chars().last().unwrap();- return !last_char.is_alphanumeric() && last_char != '_' && last_char != '-';- }-- // For other commands (like `wt`), check for git prefix- // This handles: `git wt config...` and `git-wt config...`- if before.ends_with("git ") || before.ends_with("git-") {- return false;+/// Recover the template's command slot, then compare the entire line including+/// repeated slots. A command-looking substring alone never establishes ownership.+fn template_command<'a>(line: &'a str, template: &str) -> Option<&'a str> {+ let (prefix, rest) = template.split_once(COMMAND_PLACEHOLDER)?;+ let delimiter = rest.split(COMMAND_PLACEHOLDER).next()?;+ let (cmd, _) = line.strip_prefix(prefix)?.split_once(delimiter)?;+ if cmd != "git wt" && super::validate_shell_command_name(cmd).is_err() {+ return None;}-- // Valid if preceded by: whitespace, $(, (, ", ', `, or / (for absolute paths)- let last_char = before.chars().last().unwrap();- matches!(last_char, ' ' | '\t' | '$' | '(' | '"' | '\'' | '`' | '/')+ (line == template.replace(COMMAND_PLACEHOLDER, cmd)).then_some(cmd)}/// Check if a line contains the command name at a word boundary.///-/// Used to identify potential false negatives - lines that contain the command-/// but weren't detected as integration lines.+/// Used to show custom setup lines that are not recognized as integration.fn contains_cmd_at_word_boundary(line: &str, cmd: &str) -> bool {let mut search_start = 0;while let Some(pos) = line[search_start..].find(cmd) {@@ -396,7 +171,7 @@ pub struct FileDetectionResult {/// Lines that matched as shell integration (detected).pub matched_lines: Vec<DetectedLine>,/// Lines containing the command at word boundary but NOT detected.- /// These are potential false negatives.+ /// These may be customized integration or unrelated uses of the command.pub unmatched_candidates: Vec<DetectedLine>,/// Aliases that bypass shell integration by pointing to a binary path./// e.g., `alias gwt="/usr/bin/wt"` or `alias wt="wt.exe"`@@ -416,7 +191,7 @@ pub struct BypassAlias {pub content: String,}-/// Scan a single file for shell integration lines and potential false negatives.+/// Scan a file for recognized setup lines and other references to the command.fn scan_file(path: &std::path::Path, cmd: &str) -> Option<FileDetectionResult> {let file = fs::File::open(path).ok()?;let reader = BufReader::new(file);@@ -440,7 +215,7 @@ fn scan_file(path: &std::path::Path, cmd: &str) -> Option<FileDetectionResult> {continue;}- if is_shell_integration_line(&line, cmd) {+ if shell_integration(&line, cmd).is_some() {matched_lines.push(DetectedLine {line_number,content: line.clone(),@@ -554,7 +329,7 @@ fn detect_bypass_alias(line: &str, cmd: &str, line_number: usize) -> Option<Bypa////// Returns information about:/// - Which lines matched as shell integration-/// - Which lines contain the command but didn't match (potential false negatives)+/// - Which lines contain the command but are not recognized setup lines////// Used by `wt config show` to provide debugging output.pub fn scan_for_detection_details(cmd: &str) -> Result<Vec<FileDetectionResult>, std::io::Error> {@@ -607,608 +382,78 @@ mod tests {use super::*;use rstest::rstest;- // ==========================================================================- // Detection tests: eval/source lines- // ==========================================================================-- /// Basic eval patterns that SHOULD match for `wt`#[rstest]- #[case::basic_eval(r#"eval "$(wt config shell init bash)""#)]- #[case::with_command(r#"eval "$(command wt config shell init bash)""#)]- #[case::source_process_sub(r#"source <(wt config shell init zsh)"#)]- #[case::fish_source(r#"wt config shell init fish | source"#)]- #[case::with_if_check(- r#"if command -v wt >/dev/null; then eval "$(wt config shell init bash)"; fi"#+ #[case(r#"eval "$(wt config shell init bash)""#, crate::shell::Shell::Bash)]+ #[case(+ r#"eval "$(command wt config shell init zsh)""#,+ crate::shell::Shell::Zsh)]- #[case::single_quotes(r#"eval '$( wt config shell init bash )'"#)]- fn test_wt_eval_patterns_match(#[case] line: &str) {- assert!(- is_shell_integration_line(line, "wt"),- "Should match for 'wt': {line}"- );- }-- /// Patterns that should NOT match for `wt` (they're for git-wt)- #[rstest]- #[case::git_space_wt(r#"eval "$(git wt config shell init bash)""#)]- #[case::git_hyphen_wt(r#"eval "$(git-wt config shell init bash)""#)]- #[case::command_git_wt(r#"eval "$(command git wt config shell init bash)""#)]- #[case::command_git_hyphen_wt(r#"eval "$(command git-wt config shell init bash)""#)]- fn test_git_wt_patterns_dont_match_wt(#[case] line: &str) {- assert!(- !is_shell_integration_line(line, "wt"),- "Should NOT match for 'wt' (this is git-wt integration): {line}"- );- }-- /// Patterns that SHOULD match for `git-wt`- #[rstest]- #[case::git_hyphen_wt(r#"eval "$(git-wt config shell init bash)""#)]- #[case::git_space_wt(r#"eval "$(git wt config shell init bash)""#)]- #[case::command_git_wt(r#"eval "$(command git wt config shell init bash)""#)]- fn test_git_wt_eval_patterns_match(#[case] line: &str) {- assert!(- is_shell_integration_line(line, "git-wt"),- "Should match for 'git-wt': {line}"- );- }-- /// Comment lines should never match- #[rstest]- #[case::bash_comment(r#"# eval "$(wt config shell init bash)""#)]- #[case::indented_comment(r#" # eval "$(wt config shell init bash)""#)]- fn test_comments_dont_match(#[case] line: &str) {- assert!(- !is_shell_integration_line(line, "wt"),- "Comment should not match: {line}"- );- }-- /// Lines without execution context should not match- #[rstest]- #[case::just_command("wt config shell init bash")]- #[case::echo(r#"echo "wt config shell init bash""#)]- fn test_no_execution_context_doesnt_match(#[case] line: &str) {- assert!(- !is_shell_integration_line(line, "wt"),- "Without eval/source should not match: {line}"- );- }-- // ==========================================================================- // Edge cases and real-world patterns- // ==========================================================================-- /// Real-world patterns from user dotfiles- #[rstest]- #[case::chezmoi_style(- r#"if command -v wt &>/dev/null; then eval "$(wt config shell init bash)"; fi"#,- "wt",- true- )]- #[case::nikiforov_style(r#"eval "$(command git wt config shell init bash)""#, "git-wt", true)]- #[case::nikiforov_not_wt(r#"eval "$(command git wt config shell init bash)""#, "wt", false)]- fn test_real_world_patterns(#[case] line: &str, #[case] cmd: &str, #[case] should_match: bool) {- assert_eq!(- is_shell_integration_line(line, cmd),- should_match,- "Line: {line}\nCommand: {cmd}\nExpected: {should_match}"- );- }-- // ==========================================================================- // Windows .exe suffix tests (Issue #348)- // ==========================================================================-- /// Windows Git Bash users may have .exe in their config lines.- /// Detection should match both `git-wt config shell init` and `git-wt.exe config shell init`.- #[rstest]- #[case::wt_exe_basic(r#"eval "$(wt.exe config shell init bash)""#, "wt", true)]- #[case::wt_exe_with_command(r#"eval "$(command wt.exe config shell init bash)""#, "wt", true)]- #[case::git_wt_exe_basic(r#"eval "$(git-wt.exe config shell init bash)""#, "git-wt", true)]- #[case::git_wt_exe_with_command(- r#"eval "$(command git-wt.exe config shell init bash)""#,- "git-wt",- true+ #[case(+ r#" eval "$(wt config shell init zsh)" # worktrunk"#,+ crate::shell::Shell::Zsh)]- #[case::git_wt_exe_with_if(- r#"if command -v git-wt.exe &> /dev/null; then eval "$(command git-wt.exe config shell init bash)"; fi"#,- "git-wt",- true+ #[case("wt config shell init fish | source", crate::shell::Shell::Fish)]+ #[case(+ "wt config shell init nu | save -f ($nu.vendor-autoload-dirs | last | path join wt.nu)",+ crate::shell::Shell::Nushell)]- // Issue #348: exact pattern from user's dotfiles- #[case::issue_348_exact(- r#"eval "$(command git-wt.exe config shell init bash)""#,- "git-wt",- true+ #[case(+ "Invoke-Expression (& wt config shell init powershell | Out-String)",+ crate::shell::Shell::PowerShell)]- fn test_windows_exe_suffix(#[case] line: &str, #[case] cmd: &str, #[case] should_match: bool) {- assert_eq!(- is_shell_integration_line(line, cmd),- should_match,- "Windows .exe test failed\nLine: {line}\nCommand: {cmd}\nExpected: {should_match}"- );+ fn documented_lines_match(#[case] line: &str, #[case] shell: crate::shell::Shell) {+ assert_eq!(shell_integration(line, "wt"), Some(shell));+ assert!(is_shell_integration_line_for_uninstall_any_cmd(line));}- /// .exe should NOT cause false positives for different commands- #[rstest]- #[case::wt_exe_not_git_wt(r#"eval "$(wt.exe config shell init bash)""#, "git-wt", false)]- #[case::git_wt_exe_not_wt(r#"eval "$(git-wt.exe config shell init bash)""#, "wt", false)]- // Prefixed command with .exe should not match- #[case::my_git_wt_exe_not_git_wt(- r#"eval "$(my-git-wt.exe config shell init bash)""#,- "git-wt",- false- )]- fn test_windows_exe_no_false_positives(- #[case] line: &str,- #[case] cmd: &str,- #[case] should_match: bool,- ) {- assert_eq!(- is_shell_integration_line(line, cmd),- should_match,- "Windows .exe false positive check failed\nLine: {line}\nCommand: {cmd}\nExpected: {should_match}"- );- }-- /// Word boundary: `newt` should not match `wt`#[test]- fn test_word_boundary_newt() {- let line = r#"eval "$(newt config shell init bash)""#;- // This line contains "wt config shell init" as a substring- // but the command is "newt", not "wt"- assert!(- !is_shell_integration_line(line, "wt"),- "newt should not match wt"- );- }-- /// Partial command names should not match- #[test]- fn test_partial_command_no_match() {- // "swt" contains "wt" but is not "wt"- let line = r#"eval "$(swt config shell init bash)""#;- assert!(- !is_shell_integration_line(line, "wt"),- "swt should not match wt"- );- }-- // ==========================================================================- // ADVERSARIAL FALSE NEGATIVE TESTS- // These test cases attempt to find patterns that SHOULD be detected but ARE NOT- // ==========================================================================-- /// Helper to test false negatives - if this panics, we found one- fn assert_detects(line: &str, cmd: &str, description: &str) {- assert!(- is_shell_integration_line(line, cmd),- "FALSE NEGATIVE: {} not detected for cmd={}\nLine: {}",- description,- cmd,- line- );- }-- /// Helper to verify non-detection (expected behavior)- fn assert_not_detects(line: &str, cmd: &str, description: &str) {- assert!(- !is_shell_integration_line(line, cmd),- "UNEXPECTED MATCH: {} matched for cmd={}\nLine: {}",- description,- cmd,- line- );- }-- // ------------------------------------------------------------------------- // FALSE NEGATIVE: dot (.) command as source equivalent- // -------------------------------------------------------------------------- /// The `.` command is POSIX-equivalent to `source` - now detected- #[test]- fn test_dot_command_process_substitution() {- // . <(wt config shell init bash) is equivalent to source <(...)- // This is a common POSIX pattern- assert_detects(- ". <(wt config shell init bash)",- "wt",- "dot command with process substitution",- );- }-- #[test]- fn test_dot_command_zsh_equals() {- // . =(wt config shell init zsh) is zsh-specific- assert_detects(- ". =(wt config shell init zsh)",- "wt",- "dot command with zsh =() substitution",- );- }-- // ------------------------------------------------------------------------- // FALSE NEGATIVE: PowerShell iex alias- // -------------------------------------------------------------------------- /// iex is PowerShell's alias for Invoke-Expression - now detected- /// Must include | Out-String to be detected (issue #885)- #[test]- fn test_powershell_iex_alias() {- // Common in PowerShell profiles - must have | Out-String- assert_detects(- "iex (wt config shell init powershell | Out-String)",- "wt",- "PowerShell iex alias",- );- }-- #[test]- fn test_powershell_iex_with_ampersand() {- assert_detects(- "iex (& wt config shell init powershell | Out-String)",- "wt",- "PowerShell iex with &",- );- }-- /// PowerShell lines without | Out-String should NOT be detected (strict mode)- /// This ensures old configs are treated as "not installed" so users get the fix- #[test]- fn test_powershell_without_out_string_not_detected() {- assert_not_detects(- "iex (wt config shell init powershell)",- "wt",- "PowerShell without Out-String (outdated config)",- );- assert_not_detects(- "Invoke-Expression (& wt config shell init powershell)",- "wt",- "Invoke-Expression without Out-String (outdated config)",- );- // This is the exact old canonical PowerShell line that users have- assert_not_detects(- "if (Get-Command wt -ErrorAction SilentlyContinue) { Invoke-Expression (& wt config shell init powershell) }",- "wt",- "exact old canonical PowerShell line (must not detect)",- );- }-- /// A trailing comment can mention an integration line without running it;- /// detection reads only the code portion of the line.- #[test]- fn test_trailing_comment_is_not_code() {- // The comment mentions the init call; the code runs something else.- // Deleting this line would take the user's direnv hook with it.- let line = r#"eval "$(direnv hook bash)" # see: wt config shell init bash"#;- assert_not_detects(line, "wt", "mention in trailing comment");- assert!(!is_shell_integration_line_for_uninstall_any_cmd(line));-- // A real integration line keeps matching with a comment after it,- // and `$#` does not open a comment.- for line in [- r#"eval "$(wt config shell init bash)" # worktrunk"#,- r#"if [ $# -eq 0 ]; then :; fi; eval "$(wt config shell init bash)""#,+ fn historical_installations_match() {+ for shell in [+ crate::shell::Shell::Bash,+ crate::shell::Shell::Zsh,+ crate::shell::Shell::Fish,+ crate::shell::Shell::PowerShell,] {- assert_detects(line, "wt", "code before trailing comment");- assert!(is_shell_integration_line_for_uninstall_any_cmd(line));- }- }-- #[test]- fn test_any_cmd_detects_managed_line_regardless_of_binary_name() {- // The name is read off the line, so every form worktrunk writes matches,- // as does the `git wt` dispatch form a cmd-specific detector rejects.- for line in [- r#"eval "$(wt config shell init bash)""#,- r#"eval "$(command git-wt config shell init zsh)""#,- r#"eval "$(git wt config shell init bash)""#,- r#"eval "$(git-wt.exe config shell init bash)""#,- r#"eval "$(/opt/homebrew/bin/my.tool_1 config shell init bash)""#,- "if type -q wt; command wt config shell init fish | source; end",- "iex (wt config shell init powershell | Out-String)",- // Old PowerShell forms without `| Out-String`, which uninstall- // must still remove even though they do not work.- "iex (wt config shell init powershell)",- "if (Get-Command wt -ErrorAction SilentlyContinue) { Invoke-Expression (& wt config shell init powershell) }",- // A bare invocation, with nothing consuming the output. Matched- // because uninstall asks only whether the line invokes the command:- // this is a worktrunk line whichever way it got there.- "wt config shell init bash",- ] {- assert!(- is_shell_integration_line_for_uninstall_any_cmd(line),- "should detect: {line}"- );- }-- // Not managed: a mention that never runs, a commented-out line, and a- // marker with no command in front of it.- for line in [- "echo wt config shell init bash",- r#"# eval "$(wt config shell init bash)""#,- r#"eval "$( config shell init bash)""#,- ] {- assert!(- !is_shell_integration_line_for_uninstall_any_cmd(line),- "should not detect: {line}"- );- }- }-- /// Uninstall deletes what it matches, so a line that merely quotes an- /// integration command must not match — the exec keyword satisfying- /// `is_execution_context` can sit inside the quoted string itself, leaving- /// the command's own position as the only thing telling the two apart.- #[test]- fn test_any_cmd_rejects_quoted_mention_alongside_exec_keyword() {- for line in [- // The user's own alias: `source` is inside the echoed string, and- // `wt` is an argument to `echo`, not a command.- r#"alias setup='echo "run: wt config shell init fish | source"'"#,- // Same shape without the alias wrapper.- r#"echo "wt config shell init bash | source""#,- // A real integration line elsewhere on the line does not license- // deleting a mention that sits in argument position.- r#"eval "$(direnv hook bash)"; echo "see wt config shell init bash""#,- ] {- assert!(- !is_shell_integration_line_for_uninstall_any_cmd(line),- "quoted mention should not be claimed for deletion: {line}"- );- }- }-- /// Every line worktrunk writes, and the documented hand-written forms, put- /// the command in a position the deletion check accepts. Regressions here- /// strand a working install.- #[test]- fn test_any_cmd_detects_every_generated_config_line() {- for shell in crate::shell::Shell::all() {- for cmd in ["wt", "git-wt"] {- let line = shell.config_line(cmd);- assert!(- is_shell_integration_line_for_uninstall_any_cmd(&line),- "uninstall must match its own generated line for {shell}/{cmd}: {line}"+ let line = shell+ .config_line("git-wt")+ .replace(" | Out-String", "")+ .replace(+ &format!("config shell init {shell}"),+ &format!("config shell init {shell} --cmd=git-wt"),);- }+ assert!(is_shell_integration_line_for_uninstall_any_cmd(&line));+ assert_eq!(+ shell_integration(&line, "git-wt").is_some(),+ shell != crate::shell::Shell::PowerShell+ );}- }-- // ------------------------------------------------------------------------- // FALSE NEGATIVE: PowerShell block comments- // Note: This is actually a FALSE POSITIVE risk (comments matching)- // -------------------------------------------------------------------------- #[test]- fn test_powershell_block_comment() {- // PowerShell block comments <# #> should NOT match - now correctly skipped- let line = "<# Invoke-Expression (wt config shell init powershell) #>";- assert_not_detects(line, "wt", "PowerShell block comment should not match");- }-- // ------------------------------------------------------------------------- // FALSE NEGATIVE: zsh =() process substitution without source/eval- // -------------------------------------------------------------------------- /// Zsh allows sourcing with just =() which creates a temp file - now detected- #[test]- fn test_zsh_bare_equals_substitution() {- // Some zsh configs might use: . =(command)- // Already covered above, but this is a variant- assert_detects(- ". =(command wt config shell init zsh)",- "wt",- "dot with command prefix",- );- }-- // ------------------------------------------------------------------------- // EDGE CASE: Backtick command substitution- // -------------------------------------------------------------------------- /// Backticks (older syntax) should work - they DO- #[test]- fn test_backtick_substitution() {- assert_detects(- "eval \"`wt config shell init bash`\"",- "wt",- "backtick substitution",- );- }-- /// Backticks without quotes- #[test]- fn test_backtick_no_outer_quotes() {- assert_detects(- "eval `wt config shell init bash`",- "wt",- "backtick without outer quotes",- );- }-- // ------------------------------------------------------------------------- // Path prefixes to binary- // Paths like /usr/local/bin/wt are detected because '/' is in the allowed- // set of preceding characters (alongside ' ', '\t', '$', etc.) checked by- // is_valid_command_position.- // -------------------------------------------------------------------------- #[test]- fn test_absolute_path() {- // Path-prefixed binary invocation - now detected with '/' in allowed chars- assert_detects(- r#"eval "$(/usr/local/bin/wt config shell init bash)""#,- "wt",- "absolute path to binary",- );- }-- #[test]- fn test_home_path() {- assert_detects(- r#"eval "$(~/.cargo/bin/wt config shell init bash)""#,- "wt",- "home-relative path",- );- }-- #[test]- fn test_env_var_path() {- assert_detects(- r#"eval "$($HOME/.cargo/bin/wt config shell init bash)""#,- "wt",- "env var in path",- );- }-- // ------------------------------------------------------------------------- // EDGE CASE: WORKTRUNK_BIN fallback variations- // -------------------------------------------------------------------------- #[test]- fn test_worktrunk_bin_only() {- // Using only WORKTRUNK_BIN without default- assert_not_detects(- r#"eval "$($WORKTRUNK_BIN config shell init bash)""#,- "wt",- "WORKTRUNK_BIN without default (expected: no match - cant tell which cmd)",- );- }-- // ------------------------------------------------------------------------- // EDGE CASE: git wt spacing variations- // -------------------------------------------------------------------------- #[test]- fn test_git_wt_double_space() {- // Extra space between git and wt- assert_not_detects(- r#"eval "$(git wt config shell init bash)""#,- "git-wt",- "double space (expected: no match due to pattern)",- );- }-- #[test]- fn test_git_wt_tab_separator() {- // Tab between git and wt- let line = "eval \"$(git\twt config shell init bash)\"";- assert_not_detects(- line,- "git-wt",- "tab separator (expected: no match - only single space matched)",- );- }-- // ------------------------------------------------------------------------- // FALSE NEGATIVE: fish without explicit source/eval keyword- // The fish pattern wt config shell init fish | source works because "source" is detected- // -------------------------------------------------------------------------- #[test]- fn test_fish_standard() {- assert_detects(- "wt config shell init fish | source",- "wt",- "standard fish pattern",- );- }-- #[test]- fn test_fish_with_command() {- assert_detects(- "command wt config shell init fish | source",- "wt",- "fish with command prefix",- );- }-- // ------------------------------------------------------------------------- // Nushell detection- // -------------------------------------------------------------------------- #[test]- fn test_nushell_save_pattern() {- // Nushell's config_line uses `save --force` (detected via "save" keyword)- let line = "if (which wt | is-not-empty) { wt config shell init nu | save --force ($nu.vendor-autoload-dirs | last | path join wt.nu) }";- assert_detects(line, "wt", "nushell save pattern (actual config line)");- }-- #[test]- fn test_nushell_source_pattern() {- // Alternative nushell pattern using source (for manual setups)- let line = "wt config shell init nu | source";- assert_detects(line, "wt", "nushell source pattern");- }-- // ------------------------------------------------------------------------- // Verify comment handling edge cases- // -------------------------------------------------------------------------- #[test]- fn test_inline_comment() {- // The line starts with actual code, not a comment- assert_detects(- r#"eval "$(wt config shell init bash)" # setup wt"#,- "wt",- "inline comment after code",- );- }-- #[test]- fn test_commented_in_middle() {- // Line starts with #- assert_not_detects(- r#"#eval "$(wt config shell init bash)""#,- "wt",- "line starting with # (expected: no match)",+ let old_powershell = "Invoke-Expression (& wt config shell init powershell)";+ assert!(is_shell_integration_line_for_uninstall_any_cmd(+ old_powershell+ ));+ assert_eq!(shell_integration(old_powershell, "wt"), None);+ let dispatch = r#"eval "$(git wt config shell init zsh)""#;+ assert_eq!(+ shell_integration(dispatch, "git-wt"),+ Some(crate::shell::Shell::Zsh));+ assert_eq!(shell_integration(dispatch, "wt"), None);+ assert!(is_shell_integration_line_for_uninstall_any_cmd(dispatch));}- // ------------------------------------------------------------------------- // Multiple commands on one line- // -------------------------------------------------------------------------- #[test]- fn test_multiple_evals() {- // Both wt and git-wt on same line- let line =- r#"eval "$(wt config shell init bash)"; eval "$(git-wt config shell init bash)""#;- assert_detects(line, "wt", "wt in multi-command line");- assert_detects(line, "git-wt", "git-wt in multi-command line");- }-- // ==========================================================================- // WORD BOUNDARY TESTS - Bugs fixed in adversarial testing rounds 3-4- // ==========================================================================-- /// Prefixed git-wt commands should NOT match git-wt- #[rstest]- #[case::my_git_wt(r#"eval "$(my-git-wt config shell init bash)""#)]- #[case::test_git_wt(r#"eval "$(test-git-wt config shell init bash)""#)]- #[case::underscore_git_wt(r#"eval "$(_git-wt config shell init bash)""#)]- #[case::x_git_wt(r#"eval "$(x-git-wt config shell init bash)""#)]- fn test_prefixed_git_wt_no_match(#[case] line: &str) {- assert_not_detects(line, "git-wt", "prefixed git-wt command should NOT match");- }-- /// Prefixed "git wt" (space form) should NOT match git-wt#[rstest]- #[case::agit_wt(r#"eval "$(agit wt config shell init bash)""#)]- #[case::xgit_wt(r#"eval "$(xgit wt config shell init bash)""#)]- #[case::mygit_wt(r#"eval "$(mygit wt config shell init bash)""#)]- fn test_prefixed_git_space_wt_no_match(#[case] line: &str) {- assert_not_detects(line, "git-wt", "prefixed 'git wt' should NOT match git-wt");- }-- /// Unicode alphanumerics before command should NOT match (is_alphanumeric is Unicode-aware)- #[rstest]- #[case::greek(r#"eval "$(αgit-wt config shell init bash)""#, "git-wt")]- #[case::cyrillic(r#"eval "$(яwt config shell init bash)""#, "wt")]- fn test_unicode_alphanumerics_no_match(#[case] line: &str, #[case] cmd: &str) {- assert_not_detects(line, cmd, "Unicode alphanumeric before command");+ #[case(r#"# eval "$(wt config shell init bash)""#)]+ #[case(r#"alias setup='eval "$(wt config shell init bash)"'"#)]+ #[case(r#"eval "$(wt config shell init bash)"; echo keep-me"#)]+ #[case(r#"eval "$(direnv hook bash)" # wt config shell init bash"#)]+ #[case(r#"echo "wt config shell init fish | source""#)]+ #[case(r#"eval "$($WORKTRUNK_BIN config shell init bash)""#)]+ #[case(r#"eval "$(/opt/bin/wt config shell init bash)""#)]+ #[case(r#"eval "$(wt config shell init bash-extra)""#)]+ #[case(r#"if command -v other >/dev/null 2>&1; then eval "$(command wt config shell init bash)"; fi"#)]+ fn custom_lines_are_not_claimed(#[case] line: &str) {+ assert_eq!(shell_integration(line, "wt"), None);+ assert!(!is_shell_integration_line_for_uninstall_any_cmd(line));}// ==========================================================================diff --git a/src/shell/mod.rs b/src/shell/mod.rsindex 005593323..1cc75f6c9 100644--- a/src/shell/mod.rs+++ b/src/shell/mod.rs@@ -13,8 +13,8 @@ use askama::Template;// Re-export public types and functionspub use detection::{- BypassAlias, DetectedLine, FileDetectionResult, is_shell_integration_line,- is_shell_integration_line_for_uninstall_any_cmd, scan_for_detection_details,+ BypassAlias, DetectedLine, FileDetectionResult,+ is_shell_integration_line_for_uninstall_any_cmd, scan_for_detection_details, shell_integration,};pub use paths::{fish_completion_path, fish_config_dir, home_dir_required, line_based_config_paths,@@ -535,13 +535,11 @@ mod tests {}/// Verify that `config_line()` generates lines that- /// `is_shell_integration_line()` can detect.+ /// `shell_integration()` can detect.////// This prevents install and detection from drifting out of sync.- /// Note: .exe variants are not included because `binary_name()` strips- /// the .exe suffix on Windows (MSYS2/Git Bash handles the resolution).#[rstest]- fn test_config_line_detected_by_is_shell_integration_line(+ fn test_config_line_detected_by_shell_integration(#[values(Shell::Bash,Shell::Zsh,@@ -550,13 +548,16 @@ mod tests {Shell::PowerShell)]shell: Shell,- #[values("wt", "git-wt")] prefix: &str,+ #[values("wt", "git-wt", "my.tool_1", "git-wt.exe")] prefix: &str,) {let line = shell.config_line(prefix);- assert!(- is_shell_integration_line(&line, prefix),- "{shell} config_line({prefix:?}) not detected:\n {line}"+ assert_eq!(+ shell_integration(&line, prefix.trim_end_matches(".exe")),+ Some(shell));+ assert_eq!(shell_integration(&line, prefix), Some(shell));+ assert!(is_shell_integration_line_for_uninstall_any_cmd(&line));+ assert_eq!(shell_integration(&line, "other"), None);}// PowerShell config_line evaluation test is in tests/integration_tests/shell_powershell.rsdiff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rsindex 62e6bae79..1d216fc18 100644--- a/tests/integration_tests/config_show.rs+++ b/tests/integration_tests/config_show.rs@@ -934,8 +934,8 @@ fn test_config_show_fish_without_completions(/// Test that config show displays "Outdated" when fish wrapper exists but has different code.////// The wrapper file contains `function wt` which matches the command name at a word boundary,-/// but `is_shell_integration_line()` won't match it (it looks for eval/source patterns).-/// This should NOT trigger a "Found wt ... but not detected as integration" warning because+/// but `shell_integration()` won't match it (it recognizes initialization statements).+/// This should NOT trigger an unrecognized-line warning because/// the wrapper file IS the integration — `scan_shell_configs` already identified it (as/// outdated). Only the "Outdated shell extension" warning should appear.#[rstest]@@ -971,7 +971,7 @@ fn test_config_show_fish_outdated_wrapper(mut repo: TestRepo, temp_home: TempDir/// Same false-positive suppression as the fish variant: the wrapper file contains `def --wrapped wt`/// which matches the command name, but `scan_shell_configs` already recognized the file as/// integration (outdated). Only the "Outdated" warning should appear, not the generic-/// "not detected as integration" warning.+/// unrecognized-line warning.#[rstest]fn test_config_show_nushell_outdated_wrapper(mut repo: TestRepo, temp_home: TempDir) {repo.setup_mock_ci_tools_unauthenticated();@@ -2026,9 +2026,7 @@ fn test_config_show_no_user_config(mut repo: TestRepo, temp_home: TempDir) {});}-///-/// When a shell config contains `wt` at a word boundary but it's NOT detected as-/// shell integration, show a warning with file:line format to help debug detection.+/// Customized initialization is identified by location and left for manual management.#[rstest]fn test_config_show_unmatched_candidate_warning(mut repo: TestRepo, temp_home: TempDir) {// Setup mock gh/glab for deterministic BINARIES output@@ -2039,13 +2037,12 @@ fn test_config_show_unmatched_candidate_warning(mut repo: TestRepo, temp_home: Tfs::create_dir_all(&global_config_dir).unwrap();fs::write(global_config_dir.join("config.toml"), "").unwrap();- // Create .bashrc with a line containing `wt` but NOT a valid integration pattern- // This should trigger the "unmatched candidate" warning+ // This runs Worktrunk's initialization but is not a standard setup statement.fs::write(temp_home.path().join(".bashrc"),r#"# Some bash configexport PATH="$HOME/bin:$PATH"-alias wt="git worktree"+source <(wt config shell init bash)"#,).unwrap();@@ -2124,7 +2121,7 @@ fn test_config_show_unmatched_candidate_not_suppressed_by_wrapper(fs::create_dir_all(&global_config_dir).unwrap();fs::write(global_config_dir.join("config.toml"), "").unwrap();- // Fish wrapper (outdated) — should NOT trigger "not detected" warning+ // The outdated fish wrapper is already recognized as integration.let functions = temp_home.path().join(".config/fish/functions");fs::create_dir_all(&functions).unwrap();fs::write(@@ -2133,7 +2130,7 @@ fn test_config_show_unmatched_candidate_not_suppressed_by_wrapper().unwrap();- // Bash alias — SHOULD trigger "not detected" warning+ // An unrelated bash alias still appears as an unrecognized reference.fs::write(temp_home.path().join(".bashrc"),r#"# Some bash configdiff --git a/tests/integration_tests/configure_shell.rs b/tests/integration_tests/configure_shell.rsindex d5f697e57..4b45391e9 100644--- a/tests/integration_tests/configure_shell.rs+++ b/tests/integration_tests/configure_shell.rs@@ -1596,7 +1596,11 @@ fn test_install_uninstall_roundtrip(repo: TestRepo, temp_home: TempDir) {}#[rstest]-fn test_uninstall_scan_removes_all_worktrunk_zsh_lines(repo: TestRepo, temp_home: TempDir) {+fn test_uninstall_scan_removes_all_worktrunk_zsh_lines(+ repo: TestRepo,+ temp_home: TempDir,+ #[values("git-wt", "git-wt.exe")] command_name: &str,+) {let zshrc_path = temp_home.path().join(".zshrc");fs::write(&zshrc_path,@@ -1611,7 +1615,13 @@ fn test_uninstall_scan_removes_all_worktrunk_zsh_lines(repo: TestRepo, temp_homeinstall_cmd.env("WORKTRUNK_TEST_COMPINIT_CONFIGURED", "1");install_cmd.args([- "config", "shell", "install", "zsh", "--yes", "--cmd", "git-wt",+ "config",+ "shell",+ "install",+ "zsh",+ "--yes",+ "--cmd",+ command_name,]).current_dir(repo.root_path());@@ -1622,7 +1632,9 @@ fn test_uninstall_scan_removes_all_worktrunk_zsh_lines(repo: TestRepo, temp_homeString::from_utf8_lossy(&install_output.stderr));let installed = fs::read_to_string(&zshrc_path).unwrap();- assert!(installed.contains("git-wt config shell init zsh"));+ assert!(installed.contains(&format!("{command_name} config shell init zsh")));+ assert!(install_cmd.output().unwrap().status.success());+ assert_eq!(fs::read_to_string(&zshrc_path).unwrap(), installed);// A hand-written line in the `git wt` dispatch form, which install never// emits: uninstall recognizes worktrunk's integration by whatever name it@@ -1650,7 +1662,7 @@ fn test_uninstall_scan_removes_all_worktrunk_zsh_lines(repo: TestRepo, temp_homelet content = fs::read_to_string(&zshrc_path).unwrap();assert!(- !content.contains("git-wt config shell init zsh"),+ !content.contains(&format!("{command_name} config shell init zsh")),"Custom command integration should be removed");assert!(@@ -1671,11 +1683,12 @@ fn test_uninstall_preserves_user_lines_quoting_the_init_command(repo: TestRepo,temp_home: TempDir,) {- // Each survivor puts `wt config shell init` in argument position while an- // exec keyword sits elsewhere on the line — the shape that makes the- // whole-line execution-context check alone insufficient.+ // Mentioning or wrapping an init invocation does not make the user's+ // entire statement Worktrunk-owned.let survivors = [r#"alias setup='echo "run: wt config shell init fish | source"'"#,+ r#"alias setup='eval "$(wt config shell init zsh)"'"#,+ r#"eval "$(wt config shell init zsh)"; echo keep-me"#,r#"echo "wt config shell init bash | source""#,];let zshrc_path = temp_home.path().join(".zshrc");diff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_exe_emits_extra_hint.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_exe_emits_extra_hint.snapindex 23d2abe9a..446e136cc 100644--- a/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_exe_emits_extra_hint.snap+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_exe_emits_extra_hint.snap@@ -71,10 +71,10 @@ exit_code: 0[107m [0m Invoked as: [1m[PROJECT_ROOT]/target/[BUILD_MODE]/wt[22m[2m○[22m [1mbash[22m: Not configured shell extension & completions-[33m▲[39m [33mFound [1mwt[22m in [1m~/.bashrc:2[22m but not detected as integration:[39m+[33m▲[39m [33mFound [1mwt[22m outside a recognized initialization line @ [1m~/.bashrc:2[22m:[39m[107m [0m [2m[0m[2m[35mexport[0m[2m WT_BIN=[0m[2m[32m"/c/Program Files/wt"[0m[2m↳[22m [2mNote: [1mwt[22m creates shell function [1mwt[22m. Aliases should use [4mwt[24m, not [4mwt[24m[22m-[2m↳[22m [2mIf `export WT_BIN="/c/Program Files/wt"` is meant to load Worktrunk shell integration, report a false negative: https://github.com/max-sixty/worktrunk/issues/new?title=Shell%20integration%20detection%20false%20negative&body=Shell%20integration%20not%20detected%20despite%20config%20containing%20%60wt%60.%0A%0A%2A%2AUnmatched%20lines%3A%2A%2A%0A%60%60%60%0Aexport%20WT_BIN%3D%22%2Fc%2FProgram%20Files%2Fwt%22%0A%60%60%60%0A%0A%2A%2AExpected%20behavior%3A%2A%2A%20These%20lines%20should%20be%20detected%20as%20shell%20integration.[22m+[2m↳[22m [2mTo see recognized initialization lines, run [4mwt config shell init --help[24m[22m[36mOTHER[39m[2m○[22m wt: [1m[VERSION][22mdiff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_not_suppressed_by_wrapper.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_not_suppressed_by_wrapper.snapindex 5e7f50bfe..392e7eff8 100644--- a/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_not_suppressed_by_wrapper.snap+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_not_suppressed_by_wrapper.snap@@ -72,9 +72,9 @@ exit_code: 0[2m○[22m [1mbash[22m: Not configured shell extension & completions[33m▲[39m [33m[1mfish[22m: Outdated shell extension @ [1m~/.config/fish/functions/wt.fish[22m[39m[2m↳[22m [2mTo configure, run [4mwt config shell install[24m[22m-[33m▲[39m [33mFound [1mwt[22m in [1m~/.bashrc:2[22m but not detected as integration:[39m+[33m▲[39m [33mFound [1mwt[22m outside a recognized initialization line @ [1m~/.bashrc:2[22m:[39m[107m [0m [2m[0m[2m[34malias[0m[2m wt=[0m[2m[32m"git worktree"[0m-[2m↳[22m [2mIf `alias wt="git worktree"` is meant to load Worktrunk shell integration, report a false negative: https://github.com/max-sixty/worktrunk/issues/new?title=Shell%20integration%20detection%20false%20negative&body=Shell%20integration%20not%20detected%20despite%20config%20containing%20%60wt%60.%0A%0A%2A%2AUnmatched%20lines%3A%2A%2A%0A%60%60%60%0Aalias%20wt%3D%22git%20worktree%22%0A%60%60%60%0A%0A%2A%2AExpected%20behavior%3A%2A%2A%20These%20lines%20should%20be%20detected%20as%20shell%20integration.[22m+[2m↳[22m [2mTo see recognized initialization lines, run [4mwt config shell init --help[24m[22m[36mOTHER[39m[2m○[22m wt: [1m[VERSION][22mdiff --git a/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_warning.snap b/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_warning.snapindex 6d2ed6f7c..a8a4bcba0 100644--- a/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_warning.snap+++ b/tests/snapshots/integration__integration_tests__config_show__config_show_unmatched_candidate_warning.snap@@ -71,9 +71,9 @@ exit_code: 0[107m [0m Invoked as: [1m[PROJECT_ROOT]/target/[BUILD_MODE]/wt[22m[2m○[22m [1mbash[22m: Not configured shell extension & completions-[33m▲[39m [33mFound [1mwt[22m in [1m~/.bashrc:3[22m but not detected as integration:[39m-[107m [0m [2m[0m[2m[34malias[0m[2m wt=[0m[2m[32m"git worktree"[0m-[2m↳[22m [2mIf `alias wt="git worktree"` is meant to load Worktrunk shell integration, report a false negative: https://github.com/max-sixty/worktrunk/issues/new?title=Shell%20integration%20detection%20false%20negative&body=Shell%20integration%20not%20detected%20despite%20config%20containing%20%60wt%60.%0A%0A%2A%2AUnmatched%20lines%3A%2A%2A%0A%60%60%60%0Aalias%20wt%3D%22git%20worktree%22%0A%60%60%60%0A%0A%2A%2AExpected%20behavior%3A%2A%2A%20These%20lines%20should%20be%20detected%20as%20shell%20integration.[22m+[33m▲[39m [33mFound [1mwt[22m outside a recognized initialization line @ [1m~/.bashrc:3[22m:[39m+[107m [0m [2m[0m[2m[34msource[0m[2m <([0m[2m[34mwt[0m[2m config shell init bash)[0m+[2m↳[22m [2mTo see recognized initialization lines, run [4mwt config shell init --help[24m[22m[36mOTHER[39m[2m○[22m wt: [1m[VERSION][22m