Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(alias): forward `--args=X` and `--vars=X` instead of dropping them (#4391)
Worktrunk Bot0ca9278c654d
diff --git a/src/commands/alias.rs b/src/commands/alias.rsindex bf64f5f3b..7b9687c36 100644--- a/src/commands/alias.rs+++ b/src/commands/alias.rs@@ -44,7 +44,7 @@ use anyhow::{Context, bail};use color_print::cformat;use worktrunk::config::{ALIAS_ARGS_KEY, CommandConfig, ProjectConfig, UserConfig, VarScope, alias_context_filter,- format_alias_variables, referenced_vars_for_config,+ binds_cli_var, format_alias_variables, referenced_vars_for_config,};use worktrunk::git::{CommandError, Repository, WorktrunkError};use worktrunk::styling::{@@ -242,7 +242,7 @@ impl AliasOptions {bail!("invalid KEY=VALUE: key cannot be empty");}let canon = key.replace('-', "_");- if referenced_vars.contains(&canon) {+ if binds_cli_var(&canon, referenced_vars) {vars.push((canon, value.to_string()));} else {positional_args.push(arg.clone());@@ -256,7 +256,7 @@ impl AliasOptions {// At end of args, forward `--KEY` alone.let canon = rest.replace('-', "_");if let Some(next) = args.get(i + 1) {- if referenced_vars.contains(&canon) {+ if binds_cli_var(&canon, referenced_vars) {// Warn on the footgun case: `--KEY --VALUE` with KEY// referenced binds VALUE as the value. Almost always// a typo — the user probably meant `--KEY=--VALUE`.@@ -1316,6 +1316,26 @@ cmd = ["#);}+ #[test]+ fn test_parse_reserved_keys_forward_even_when_referenced() {+ use insta::assert_debug_snapshot;+ // `args` and `vars` are set by the runtime after CLI bindings, so a+ // binding would be overwritten and the token lost. Both forms forward+ // into `{{ args }}` instead, even though the template references them.+ assert_debug_snapshot!(parse_with(&["deploy", "--args=x", "--vars", "y", "z"], &["args", "vars"]).unwrap(), @r#"+ AliasOptions {+ name: "deploy",+ vars: [],+ positional_args: [+ "--args=x",+ "--vars",+ "y",+ "z",+ ],+ }+ "#);+ }+#[test]fn test_parse_literal_forward_escape() {use insta::assert_debug_snapshot;diff --git a/src/commands/hook_commands.rs b/src/commands/hook_commands.rsindex 91b4639b6..fd1cf8048 100644--- a/src/commands/hook_commands.rs+++ b/src/commands/hook_commands.rs@@ -11,7 +11,8 @@ use color_print::cformat;use strum::IntoEnumIterator;use worktrunk::HookType;use worktrunk::config::{- ALIAS_ARGS_KEY, Approvals, CommandConfig, ProjectConfig, UserConfig, referenced_vars_for_config,+ ALIAS_ARGS_KEY, Approvals, CommandConfig, ProjectConfig, UserConfig, binds_cli_var,+ referenced_vars_for_config,};use worktrunk::git::Repository;use worktrunk::path::format_path_for_display;@@ -259,7 +260,7 @@ pub fn run_hook(let mut args: Vec<String> = Vec::new();for raw in shorthand_vars {let (canon_key, orig_key, value) = parse_shorthand_token(raw)?;- if referenced.contains(&canon_key) {+ if binds_cli_var(&canon_key, &referenced) {bindings.push((canon_key, value));} else {args.push(format!("--{orig_key}={value}"));diff --git a/src/config/expansion.rs b/src/config/expansion.rsindex 3f0363b7d..fdaaeae31 100644--- a/src/config/expansion.rs+++ b/src/config/expansion.rs@@ -85,6 +85,18 @@ pub fn base_vars() -> Vec<&'static str> {/// indexing, iteration, and `length` behave like a sequence.pub const ALIAS_ARGS_KEY: &str = "args";+/// Whether a `--KEY=VALUE` token on an alias or `wt hook` command line binds+/// to `{{ KEY }}` rather than forwarding into `{{ args }}`.+///+/// `key` is the canonical (underscored) name and `referenced` the template's+/// top-level variables. `args` and `vars` are never bindable even when+/// referenced: the runtime always sets both (the positional list, the+/// per-branch vars object) after CLI bindings, so a bound value would be+/// overwritten and the token lost. Forwarding keeps it in `{{ args }}`.+pub fn binds_cli_var(key: &str, referenced: &BTreeSet<String>) -> bool {+ key != ALIAS_ARGS_KEY && key != "vars" && referenced.contains(key)+}+/// Variables available in `wt list` custom-column templates (plus `vars.*`).////// Deliberately narrower than [`base_vars`]: column values are computed perdiff --git a/src/config/mod.rs b/src/config/mod.rsindex 124c468ab..836931f0c 100644--- a/src/config/mod.rs+++ b/src/config/mod.rs@@ -211,11 +211,12 @@ pub use deprecation::{pub use deprecation::{DeprecationKind, Deprecations};pub use expansion::{ACTIVE_VARS, ALIAS_ARGS_KEY, EXEC_BASE_VARS, REPO_VARS, TemplateContext, TemplateExpandError,- ValidationScope, VarScope, VarsMode, alias_context_filter, base_vars, expand_template,- format_alias_variables, format_base_variables, format_hook_variables, redact_credentials,- referenced_vars_for_config, referenced_vars_for_templates, sanitize_branch_name, sanitize_db,- short_hash, template_environment, template_references_var, validate_list_column_template,- validate_template, validate_template_syntax, vars_available_in, vars_map_to_value,+ ValidationScope, VarScope, VarsMode, alias_context_filter, base_vars, binds_cli_var,+ expand_template, format_alias_variables, format_base_variables, format_hook_variables,+ redact_credentials, referenced_vars_for_config, referenced_vars_for_templates,+ sanitize_branch_name, sanitize_db, short_hash, template_environment, template_references_var,+ validate_list_column_template, validate_template, validate_template_syntax, vars_available_in,+ vars_map_to_value,};pub use hooks::HooksConfig;pub use project::{diff --git a/tests/integration_tests/step_alias.rs b/tests/integration_tests/step_alias.rsindex a291df7b2..0cdd6e826 100644--- a/tests/integration_tests/step_alias.rs+++ b/tests/integration_tests/step_alias.rs@@ -198,6 +198,37 @@ run = "echo got {{ args }}"));}+/// `--args=VALUE` can't bind `{{ args }}` — the alias's own positional list+/// fills that slot — so the token forwards into it rather than vanishing.+#[rstest]+fn test_step_alias_args_flag_forwards_to_args(mut repo: TestRepo) {+ repo.write_project_config(+ r#"+[aliases]+run = "echo got {{ args }}"+"#,+ );+ repo.commit("Add alias config");+ let feature_path = repo.add_worktree("feature");++ let output = repo+ .wt_command()+ .args(["-y", "run", "--args=x", "y"])+ .current_dir(&feature_path)+ .output()+ .unwrap();+ assert!(+ output.status.success(),+ "alias should succeed: {}",+ String::from_utf8_lossy(&output.stderr)+ );+ let stdout = String::from_utf8_lossy(&output.stdout);+ assert!(+ stdout.contains("got --args=x y"),+ "--args=x should reach {{{{ args }}}}, got: {stdout}"+ );+}+/// `--` is a literal-forward escape: every later token goes to `{{ args }}`,/// so flag-shaped values that would normally bind are passed through verbatim.#[rstest]diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rsindex 0534ebf39..6814aa498 100644--- a/tests/integration_tests/user_hooks.rs+++ b/tests/integration_tests/user_hooks.rs@@ -2909,6 +2909,35 @@ test = "echo '{{ branch }}' > shorthand_output.txt");}+/// `--args=VALUE` forwards into `{{ args }}` rather than binding it, since the+/// forwarded positional list would overwrite the binding and drop the token.+#[rstest]+fn test_var_shorthand_args_key_forwards(repo: TestRepo) {+ repo.write_test_config(+ r#"[pre-start]+test = "echo '{{ args }}' > args_output.txt"+"#,+ );++ let output = repo+ .wt_command()+ .args(["hook", "pre-start", "--yes", "--args=KEPT"])+ .output()+ .expect("Failed to run wt hook");++ assert!(+ output.status.success(),+ "Hook should succeed, stderr: {}",+ String::from_utf8_lossy(&output.stderr)+ );++ let contents = fs::read_to_string(repo.root_path().join("args_output.txt")).unwrap();+ assert!(+ contents.contains("--args=KEPT"),+ "--args=KEPT should forward into args, got: {contents}"+ );+}+#[rstest]fn test_var_shorthand_mixed_with_long_form(repo: TestRepo) {// Shorthand and `--var` forms coexist in the same invocation.