Skip to content

Commit 787a445

Browse files
worktrunk-botclaude
andcommitted
refactor(alias)!: source-tag steps and decide EXEC per-step, not per-pipeline
Previously, alias dispatch merged user and project alias bodies through `append_aliases` and decided EXEC passthrough by checking whether the project config defined a same-name alias (`project_aliases_contain`). The lookup was name-keyed and the decision was per-pipeline, so a same-name collision scrubbed EXEC for the whole merged body — including the user's own steps. Mirror the hook resolution shape: - `SourcedStep` becomes the shared shape for both alias and hook dispatch (`hook_type` is now `Option<HookType>` so aliases can leave it `None`). - `ForegroundStep` carries its own `DirectivePassthrough`, so `execute_pipeline_foreground` no longer takes a per-pipeline `directives` argument. - `sourced_steps_to_foreground(steps, &PipelineKind)` is the single resolver: hook steps and project-source alias steps get `inherit_from_env()` (CD only); user-source alias steps get `inherit_from_env_with_exec()`. - `prepare_alias_sourced_steps` builds the alias side from per-source `CommandConfig` lookups, dropping the merged-execution path and the `project_aliases_contain` name search. Net behavior change: when both user and project define the same alias, the user's steps now keep the EXEC relaxation while the project's steps still scrub. Strictly more permissive than the previous per-pipeline scrub, which was a conservative side effect of name-keyed merging. Updated `test_user_and_project_alias_collision_scrubs_exec_directive` → `test_user_and_project_alias_collision_scrubs_only_project_step` to assert the new per-step behavior end-to-end. Co-Authored-By: Claude Opus 4.7 <[email protected]>
1 parent 3648926 commit 787a445

4 files changed

Lines changed: 209 additions & 111 deletions

File tree

‎src/commands/alias.rs‎

Lines changed: 76 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
//!
2626
//! User-config aliases are trusted (skip approval). Project-config aliases
2727
//! require command approval. When both define the same alias, both run — user
28-
//! first, then project. The CD directive file is passed through to child
29-
//! processes so inner `wt` invocations can redirect the parent shell's cwd;
30-
//! the EXEC directive file is scrubbed so alias bodies cannot inject
31-
//! arbitrary shell into the interactive session.
28+
//! first, then project. Steps are tagged with their source (`HookSource::User`
29+
//! / `Project`), and `sourced_steps_to_foreground` applies per-step
30+
//! `DirectivePassthrough`: the CD directive file is passed through to every
31+
//! step so inner `wt` invocations can redirect the parent shell's cwd; the
32+
//! EXEC directive file passes through user-source steps but is scrubbed for
33+
//! project-source steps. In a merged user+project body, the user's own steps
34+
//! still get the EXEC relaxation. See issue #2101.
3235
3336
use std::collections::{BTreeMap, BTreeSet};
3437

@@ -46,12 +49,14 @@ use worktrunk::styling::{
4649

4750
use crate::commands::command_approval::approve_alias_commands;
4851
use crate::commands::command_executor::{
49-
AnnouncePolicy, CommandContext, FailureStrategy, ForegroundStep, PreparedCommand, PreparedStep,
50-
alias_error_wrapper, build_hook_context, execute_pipeline_foreground,
52+
CommandContext, FailureStrategy, PipelineKind, PreparedCommand, PreparedStep,
53+
build_hook_context, execute_pipeline_foreground,
54+
};
55+
use crate::commands::hooks::{
56+
HookSource, SourcedStep, format_pipeline_summary_from_names, sourced_steps_to_foreground,
57+
step_names_from_config,
5158
};
52-
use crate::commands::hooks::{format_pipeline_summary_from_names, step_names_from_config};
5359
use crate::commands::{build_invalid_subcommand_error, similar_subcommands};
54-
use crate::output::DirectivePassthrough;
5560

5661
/// Built-in `wt step` subcommand names. Aliases with these names are
5762
/// reachable via `wt <name>` (top-level) but shadowed via `wt step <name>`.
@@ -278,16 +283,6 @@ fn alias_needs_approval(
278283
.cloned()
279284
}
280285

281-
/// Whether the project config defines an alias by this name.
282-
///
283-
/// When the same name appears in both user and project configs, the merged
284-
/// alias appends both sources' steps (user first, then project) — see
285-
/// `append_aliases`. Returning `true` here scrubs EXEC for the entire
286-
/// pipeline, since we can't selectively pass it to user steps only.
287-
fn project_aliases_contain(project_config: Option<&ProjectConfig>, alias_name: &str) -> bool {
288-
project_config.is_some_and(|pc| pc.aliases.contains_key(alias_name))
289-
}
290-
291286
/// Synthesize clap's native `InvalidSubcommand` error for `wt step <name>`
292287
/// and return it via `enhance_clap_error`, so the output matches what
293288
/// `wt <typo>` produces at the top level. Suggestion candidates include both
@@ -546,52 +541,80 @@ fn run_alias(
546541
);
547542
}
548543

549-
// User-source aliases get the EXEC directive file passed through so a
550-
// nested `wt switch --execute …` inside the body works the same as the
551-
// user typing it at the top level. Project aliases keep the conservative
552-
// scrub: the body lives in shared config and could inject arbitrary
553-
// shell into the parent session. See issue #2101.
554-
let directives = if project_aliases_contain(project_config.as_ref(), &opts.name) {
555-
DirectivePassthrough::inherit_from_env()
556-
} else {
557-
DirectivePassthrough::inherit_from_env_with_exec()
558-
};
544+
// Build source-tagged steps from per-source CommandConfig lookups instead
545+
// of the merged `cmd_config`. Same shape `prepare_sourced_steps` produces
546+
// for hooks, so both flow through `sourced_steps_to_foreground` for the
547+
// EXEC-passthrough policy: user-source steps relax (EXEC passes through),
548+
// project-source steps stay conservative (scrub). When both configs
549+
// define the alias, the user steps still get the relaxation — the
550+
// decision is per-step now, not per-pipeline. See issue #2101.
551+
let user_alias = user_config
552+
.aliases(repo.project_identifier().ok().as_deref())
553+
.get(&opts.name)
554+
.cloned();
555+
let project_alias = project_config
556+
.as_ref()
557+
.and_then(|pc| pc.aliases.get(&opts.name).cloned());
559558

560-
// Build ForegroundSteps: all alias commands use lazy expansion so vars.*
561-
// references resolved from git config at execution time are visible to
562-
// later steps that set vars via `wt config state vars set`.
563559
let alias_name = opts.name.clone();
564-
let foreground_steps: Vec<ForegroundStep> = cmd_config
565-
.steps()
566-
.iter()
567-
.map(|step| {
560+
let sourced_steps = prepare_alias_sourced_steps(
561+
user_alias.as_ref(),
562+
project_alias.as_ref(),
563+
&context_json,
564+
&alias_name,
565+
);
566+
let foreground_steps =
567+
sourced_steps_to_foreground(sourced_steps, &PipelineKind::Alias { name: alias_name });
568+
569+
execute_pipeline_foreground(
570+
&foreground_steps,
571+
&repo,
572+
&wt_path,
573+
FailureStrategy::FailFast,
574+
)
575+
}
576+
577+
/// Build source-tagged steps for an alias from per-source `CommandConfig`s.
578+
///
579+
/// User steps appear first, then project steps — matching runtime order from
580+
/// the previous `append_aliases` merge. Both sources use the same
581+
/// `context_json` and `alias_name` (the variable bindings and label are
582+
/// pipeline-wide, not per-source).
583+
fn prepare_alias_sourced_steps(
584+
user_alias: Option<&CommandConfig>,
585+
project_alias: Option<&CommandConfig>,
586+
context_json: &str,
587+
alias_name: &str,
588+
) -> Vec<SourcedStep> {
589+
let mut result = Vec::new();
590+
for (source, config) in [
591+
(HookSource::User, user_alias),
592+
(HookSource::Project, project_alias),
593+
] {
594+
let Some(cfg) = config else { continue };
595+
for step in cfg.steps() {
568596
let prepared = match step {
569597
HookStep::Single(cmd) => {
570-
PreparedStep::Single(alias_prepared_command(cmd, &context_json, &alias_name))
598+
PreparedStep::Single(alias_prepared_command(cmd, context_json, alias_name))
571599
}
572600
HookStep::Concurrent(cmds) => PreparedStep::Concurrent(
573601
cmds.iter()
574-
.map(|cmd| alias_prepared_command(cmd, &context_json, &alias_name))
602+
.map(|cmd| alias_prepared_command(cmd, context_json, alias_name))
575603
.collect(),
576604
),
577605
};
578-
ForegroundStep {
606+
result.push(SourcedStep {
579607
step: prepared,
580-
concurrent: true,
581-
announce: AnnouncePolicy::None,
582-
pipe_stdin: false,
583-
error_wrapper: alias_error_wrapper(alias_name.clone()),
584-
}
585-
})
586-
.collect();
587-
588-
execute_pipeline_foreground(
589-
&foreground_steps,
590-
&repo,
591-
&wt_path,
592-
&directives,
593-
FailureStrategy::FailFast,
594-
)
608+
source,
609+
hook_type: None,
610+
display_path: None,
611+
// Aliases have no deprecated single-table form, so concurrent
612+
// commands always run concurrently.
613+
is_pipeline: true,
614+
});
615+
}
616+
}
617+
result
595618
}
596619

597620
/// Build a PreparedCommand for an alias, deferring template expansion to execution time.

‎src/commands/command_executor.rs‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ pub enum AnnouncePolicy {
7878
/// `Send + Sync` bounds are required.
7979
pub type ErrorWrapper = Box<dyn Fn(&PreparedCommand, String, Option<i32>) -> anyhow::Error>;
8080

81+
/// What kind of pipeline a sourced step belongs to.
82+
///
83+
/// Supplied at conversion time (`sourced_steps_to_foreground`) so a single
84+
/// `SourcedStep` shape can be produced by both alias and hook resolution.
85+
/// Drives the per-step trust model (EXEC passthrough), announce policy,
86+
/// stdin handling, and error wrapping.
87+
pub enum PipelineKind {
88+
Hook,
89+
Alias { name: String },
90+
}
91+
8192
/// A pipeline step ready for foreground execution, with rendering / error policy.
8293
pub struct ForegroundStep {
8394
pub step: PreparedStep,
@@ -92,6 +103,12 @@ pub struct ForegroundStep {
92103
pub pipe_stdin: bool,
93104
/// Wraps a per-command failure into the final error returned to the caller.
94105
pub error_wrapper: ErrorWrapper,
106+
/// Per-step directive passthrough. Trust differs by source — user-source
107+
/// alias steps pass EXEC through (the body is the user's own config),
108+
/// while project-source steps and all hook steps scrub it. Per-step rather
109+
/// than per-pipeline so a merged user+project alias relaxes the user's
110+
/// own steps without leaking the project's body into the parent shell.
111+
pub directives: DirectivePassthrough,
95112
}
96113

97114
/// Controls how foreground execution responds to command failures.
@@ -341,28 +358,20 @@ pub fn execute_pipeline_foreground(
341358
steps: &[ForegroundStep],
342359
repo: &Repository,
343360
wt_path: &Path,
344-
directives: &DirectivePassthrough,
345361
failure_strategy: FailureStrategy,
346362
) -> anyhow::Result<()> {
347363
for fg_step in steps {
348364
match &fg_step.step {
349365
PreparedStep::Single(cmd) => {
350-
run_one_command(cmd, fg_step, repo, wt_path, directives, failure_strategy)?;
366+
run_one_command(cmd, fg_step, repo, wt_path, failure_strategy)?;
351367
}
352368
PreparedStep::Concurrent(cmds) => {
353369
if !fg_step.concurrent {
354370
for cmd in cmds {
355-
run_one_command(cmd, fg_step, repo, wt_path, directives, failure_strategy)?;
371+
run_one_command(cmd, fg_step, repo, wt_path, failure_strategy)?;
356372
}
357373
} else {
358-
run_concurrent_group(
359-
cmds,
360-
fg_step,
361-
repo,
362-
wt_path,
363-
directives,
364-
failure_strategy,
365-
)?;
374+
run_concurrent_group(cmds, fg_step, repo, wt_path, failure_strategy)?;
366375
}
367376
}
368377
}
@@ -383,9 +392,9 @@ fn run_concurrent_group(
383392
fg_step: &ForegroundStep,
384393
repo: &Repository,
385394
wt_path: &Path,
386-
directives: &DirectivePassthrough,
387395
failure_strategy: FailureStrategy,
388396
) -> anyhow::Result<()> {
397+
let directives = &fg_step.directives;
389398
for cmd in cmds {
390399
announce_command(cmd, &fg_step.announce);
391400
}
@@ -454,9 +463,9 @@ fn run_one_command(
454463
fg_step: &ForegroundStep,
455464
repo: &Repository,
456465
wt_path: &Path,
457-
directives: &DirectivePassthrough,
458466
failure_strategy: FailureStrategy,
459467
) -> anyhow::Result<()> {
468+
let directives = &fg_step.directives;
460469
announce_command(cmd, &fg_step.announce);
461470

462471
let lazy_expanded;

0 commit comments

Comments
 (0)