Skip to content

fix: pass directive file through wt step aliases - #2077

Merged
max-sixty merged 3 commits into
mainfrom
fix/issue-2075
Apr 11, 2026
Merged

max-sixty merged 3 commits into
mainfrom
fix/issue-2075

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Problem

Inside a wt step <alias> body, an inner wt switch --create prints the "shell integration not installed" warning and drops its cd directive — so alias-driven workflows like "move staged changes into a new worktree" can't actually leave the user in the new worktree. Users have to chain wt step <alias> && wt switch <branch> so the trailing wt switch runs in the parent shell with WORKTRUNK_DIRECTIVE_FILE still intact.

Root cause: run_command_streaming in src/commands/for_each.rs unconditionally scrubs WORKTRUNK_DIRECTIVE_FILE from child env, which is right for wt step for-each (arbitrary script across every worktree) but wrong for wt step <alias> (explicit, named, user-authorised command).

Solution

Give run_command_streaming an Option<&Path> directive-file parameter: None keeps the current scrub; Some(path) sets WORKTRUNK_DIRECTIVE_FILE=path on the child. step_alias reads the parent shell's WORKTRUNK_DIRECTIVE_FILE once via std::env::var_os and passes it through to each alias command. step_for_each passes None to keep its old behaviour.

Safety: aliases are user-authorised (user-config trusted; project-config requires approval), and an alias body is already arbitrary shell that can cd/rm/exec anything locally — letting it ask the parent shell to cd is strictly less powerful than what the body can already do. Inline comment at src/commands/alias.rs records this reasoning.

Testing

  • New test_alias_passes_directive_file_to_subprocess in tests/integration_tests/step_alias.rs — writes a user-config alias that invokes the test wt binary as wt switch --create alias-created, runs the outer wt step with WORKTRUNK_DIRECTIVE_FILE set, and asserts the directive file ends up containing cd '…alias-created…' and no "shell integration" warning on stderr.
  • Confirmed the test fails (empty directive file) when the passthrough is reverted, and passes with the fix in place.
  • All 21 step_alias tests, all 19 directives tests, and all 11 for_each tests pass locally.

Closes #2075

worktrunk-bot and others added 2 commits April 11, 2026 16:48
Inside a `wt step <alias>` body, an inner `wt switch --create` would
print the "shell integration not installed" hint and drop its `cd`
directive, so workflows like "move staged changes into a new worktree"
couldn't actually leave the user in the new worktree from a single
alias.

Aliases are explicit, named, user-authorised commands — user-config
aliases are trusted and project-config aliases already require approval
— so let the alias subprocess inherit `WORKTRUNK_DIRECTIVE_FILE` from
the parent shell wrapper. Inner `wt` invocations then write `cd '/path'`
into the parent's directive file, which the shell wrapper sources after
`wt` exits.

`wt step for-each` keeps scrubbing the env var: it's not the same trust
model as a named alias (it can run across every worktree) and the issue
explicitly scoped this to aliases.

Closes #2075

Co-Authored-By: Claude <[email protected]>
The Windows CI path (e.g. `D:\a\worktrunk\...\wt.exe`) contains `\a` and
`\w`, which are invalid escape sequences inside a TOML basic string.
Double the backslashes so they parse as literal characters.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner

also add a short note to the alias docs that wt switch cd-s pass through

also add a Todo re unifying hook and alias execution so they both pass through

Adds a one-line note to the `wt step` aliases docs explaining that an
inner `wt switch` inside an alias body lands the parent shell in the
new worktree, and a TODO in `step_alias` flagging that hook execution
still scrubs the directive file env var so the same passthrough should
eventually be unified across hooks and aliases.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Done in fe4f494:

  • Added a one-line note to the wt step aliases section in src/cli/mod.rs (synced to docs/content/step.md and the skill reference) explaining that an inner wt switch inside an alias body lands the parent shell in the new worktree, just like running wt switch directly.
  • Added a TODO in src/commands/alias.rs next to the existing comment block, flagging that hooks still scrub WORKTRUNK_DIRECTIVE_FILE (in process.rs and the None branch of for_each::run_command_streaming) and that the foreground pre-* paths could pass it through too — with a note that the detached post-* spawn paths need to keep scrubbing since background hooks outlive the parent shell.

Required CI is green and codecov/patch passed.

@max-sixty
max-sixty merged commit d889738 into main Apr 11, 2026
22 checks passed
@max-sixty
max-sixty deleted the fix/issue-2075 branch April 11, 2026 18:24
worktrunk-bot added a commit that referenced this pull request Apr 11, 2026
Adds three [aliases] examples to the Extending Worktrunk page that move or
copy uncommitted work into a new worktree, addressing the use case from
issue #938. Each composes wt switch --create with --execute, relying on the
directive-file passthrough landed in #2077.

Closes #938
max-sixty pushed a commit that referenced this pull request Apr 11, 2026
…2083)

## Summary

Adds a tested recipe to the Aliases section of
`docs/content/extending.md` showing three `[aliases]` entries that
compose `wt switch --create` with git's stash/diff plumbing to carry
in-progress changes into a new worktree:

- `move-changes` — stashes staged + unstaged + untracked, creates the
new worktree, and pops the stash with `--execute`. Source becomes clean.
- `copy-changes` — same push, plus `git stash apply --index --quiet` to
restore the source before the new worktree pops. Both sides end up with
identical state.
- `copy-staged` — writes `git diff --cached` to a tempfile and applies
it with `git apply --index` in the new worktree, so the source is
untouched and staged/unstaged overlap is handled.

Each alias takes the target branch via `--var to=feature-xyz`. Because
an inner `wt switch --create` inside an alias body propagates its `cd`
to the parent shell (#2077), they drop you in the new worktree directly.

The content was worked out and tested end-to-end in the issue thread
(`git status --porcelain` verified on both sides for clean /
staged+unstaged+untracked / untracked-only / empty-staged scenarios).

Closes #938.

## Test plan

- [ ] `cargo test --test integration
test_command_pages_and_skill_files_are_in_sync` — keep
`skills/worktrunk/reference/extending.md` in sync
- [ ] `pre-commit run --all-files` — lints
- [ ] Visual check of the rendered Aliases section on the docs dev
server

Co-authored-by: worktrunk-bot <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow wt step aliases to change the parent shell's directory

2 participants