Skip to content

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

Render alias `args` in -v table as shell-escaped `{{ args }}` (#2328)

Maximilian Roos38e538232cce
diff --git a/src/config/expansion.rs b/src/config/expansion.rs
index d5d06ce54..f2f40e937 100644
--- a/src/config/expansion.rs
+++ b/src/config/expansion.rs
@@ -10,7 +10,7 @@
use std::borrow::Cow;
use std::collections::{BTreeSet, HashMap};
-use std::fmt::{self, Write};
+use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
@@ -235,17 +235,27 @@ pub fn format_hook_variables(hook_type: HookType, ctx: &HashMap<String, String>)
/// Format the resolved template variables for an alias invocation.
///
/// Ordering mirrors [`format_hook_variables`]; alias scope has no operation
-/// or infrastructure vars, and `args` sits between active and repo (it's the
-/// alias analogue of hook-operation context).
+/// or infrastructure vars, and `args` lives in the exec group per the help
+/// table in `src/cli/mod.rs` (alongside `cwd`).
+///
+/// `args` is stored as a JSON-encoded `Vec<String>` per the [`ALIAS_ARGS_KEY`]
+/// contract; the table displays it space-joined and shell-escaped so it
+/// matches what `{{ args }}` substitutes in templates.
pub fn format_alias_variables(ctx: &HashMap<String, String>) -> String {
let vars: Vec<&'static str> = ACTIVE_VARS
.iter()
.copied()
- .chain(std::iter::once(ALIAS_ARGS_KEY))
.chain(REPO_VARS.iter().copied())
.chain(EXEC_BASE_VARS.iter().copied())
+ .chain(std::iter::once(ALIAS_ARGS_KEY))
.collect();
- format_variables_table(&vars, ctx)
+ let mut display_ctx = ctx.clone();
+ if let Some(json) = ctx.get(ALIAS_ARGS_KEY) {
+ let args: Vec<String> = serde_json::from_str(json)
+ .expect("ALIAS_ARGS_KEY is always serialized from a Vec<String>");
+ display_ctx.insert(ALIAS_ARGS_KEY.into(), shell_join(&args));
+ }
+ format_variables_table(&vars, &display_ctx)
}
/// Positional CLI args forwarded from `wt <alias> a b c` into the alias's
@@ -284,18 +294,20 @@ impl Object for ShellArgs {
}
fn render(self: &Arc<Self>, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- let mut first = true;
- for arg in &self.0 {
- if !first {
- f.write_char(' ')?;
- }
- first = false;
- f.write_str(&escape(Cow::Borrowed(arg)))?;
- }
- Ok(())
+ f.write_str(&shell_join(&self.0))
}
}
+/// Space-join shell-escaped args — the canonical rendering of `{{ args }}`
+/// used by both `ShellArgs::render` (template expansion) and the alias
+/// `-v` variable table.
+fn shell_join(args: &[String]) -> String {
+ args.iter()
+ .map(|a| escape(Cow::Borrowed(a)).into_owned())
+ .collect::<Vec<_>>()
+ .join(" ")
+}
+
/// Hash a string to a port in range 10000-19999.
fn string_to_port(s: &str) -> u16 {
let mut h = std::collections::hash_map::DefaultHasher::new();
@@ -1973,12 +1985,13 @@ mod tests {
ctx.insert("repo".into(), "demo".into());
ctx.insert("repo_path".into(), "/tmp/demo".into());
ctx.insert("cwd".into(), "/tmp/feature".into());
- // args is JSON-encoded per `ALIAS_ARGS_KEY` contract.
+ // args is JSON-encoded per `ALIAS_ARGS_KEY` contract; the table
+ // decodes and shell-renders it to match `{{ args }}` substitution.
ctx.insert(ALIAS_ARGS_KEY.into(), r#"["a","b c"]"#.into());
let out = format_alias_variables(&ctx);
assert!(
- out.contains(r#"args = ["a","b c"]"#),
+ out.contains("args = a 'b c'"),
"got: {out}"
);
// No hook-only keys appear in alias scope.
@@ -1986,4 +1999,15 @@ mod tests {
assert!(!out.contains("target"), "got: {out}");
assert!(!out.contains("base "), "got: {out}");
}
+
+ #[test]
+ fn test_format_alias_variables_args_empty() {
+ let mut ctx: HashMap<String, String> = HashMap::new();
+ ctx.insert(ALIAS_ARGS_KEY.into(), "[]".into());
+ let out = format_alias_variables(&ctx);
+ // Empty args render as an empty string after the `=` — distinct from
+ // `(unset)`, which means the key was absent entirely. `args` sits last
+ // in alias ordering, so the output ends with it.
+ assert!(out.ends_with("args = "), "got: {out:?}");
+ }
}
diff --git a/tests/integration_tests/step_alias.rs b/tests/integration_tests/step_alias.rs
index c9fac7a00..b4edfd1d9 100644
--- a/tests/integration_tests/step_alias.rs
+++ b/tests/integration_tests/step_alias.rs
@@ -1877,3 +1877,28 @@ greet = "echo hello {{ args }}"
assert_cmd_snapshot!("alias_verbose_variable_table", cmd);
});
}
+
+/// Multi-token args with whitespace render shell-escaped in the `-v` table,
+/// matching what `{{ args }}` actually expands to on the line below.
+#[rstest]
+fn test_alias_verbose_renders_args_shell_escaped(mut repo: TestRepo) {
+ repo.write_test_config(
+ r#"
+[aliases]
+greet = "echo hello {{ args }}"
+"#,
+ );
+ let feature_path = repo.add_worktree("feature");
+
+ let settings = setup_snapshot_settings(&repo);
+ settings.bind(|| {
+ let mut cmd = make_snapshot_cmd_with_global_flags(
+ &repo,
+ "greet",
+ &["foo", "bar baz"],
+ Some(&feature_path),
+ &["-v"],
+ );
+ assert_cmd_snapshot!("alias_verbose_args_shell_escaped", cmd);
+ });
+}
diff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_args_shell_escaped.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_args_shell_escaped.snap
new file mode 100644
index 000000000..abb72c37f
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_args_shell_escaped.snap
@@ -0,0 +1,70 @@
+---
+source: tests/integration_tests/step_alias.rs
+info:
+ program: wt
+ args:
+ - "-v"
+ - greet
+ - foo
+ - bar baz
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "500"
+ GIT_AUTHOR_DATE: "2025-01-01T00:00:00Z"
+ GIT_COMMITTER_DATE: "2025-01-01T00:00:00Z"
+ GIT_CONFIG_GLOBAL: "[TEST_GIT_CONFIG]"
+ GIT_CONFIG_SYSTEM: /dev/null
+ GIT_EDITOR: ""
+ GIT_TERMINAL_PROMPT: "0"
+ HOME: "[TEST_HOME]"
+ LANG: C
+ LC_ALL: C
+ MOCK_CONFIG_DIR: "[MOCK_CONFIG_DIR]"
+ NO_COLOR: ""
+ OPENCODE_CONFIG_DIR: "[TEST_OPENCODE_CONFIG]"
+ PATH: "[PATH]"
+ PSModulePath: ""
+ RUST_LOG: warn
+ SHELL: ""
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"
+ WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"
+ WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"
+ WORKTRUNK_TEST_EPOCH: "1735776000"
+ WORKTRUNK_TEST_NUSHELL_ENV: "0"
+ WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"
+ WORKTRUNK_TEST_POWERSHELL_ENV: "0"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+○ template variables:
+  branch = feature
+  worktree_path = _REPO_.feature
+  worktree_name = repo.feature
+  commit = 05a4a45d0b981dad5c27db59dca482836d59f89e
+  short_commit = 05a4a45
+  upstream = (unset)
+  repo = repo
+  repo_path = _REPO_
+  owner = (unset)
+  primary_worktree_path = _REPO_
+  default_branch = main
+  remote = origin
+  remote_url = ../origin.git
+  cwd = _REPO_.feature
+  args = foo 'bar baz'
+◎ Running alias greet
+○ Expanding greet
+  echo hello {{ args }}
+  →
+  echo hello foo 'bar baz'
+hello foo bar baz
diff --git a/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_variable_table.snap b/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_variable_table.snap
index 3d65c5c7c..618e83183 100644
--- a/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_variable_table.snap
+++ b/tests/snapshots/integration__integration_tests__step_alias__alias_verbose_variable_table.snap
@@ -52,7 +52,6 @@ exit_code: 0
  commit = 05a4a45d0b981dad5c27db59dca482836d59f89e
  short_commit = 05a4a45
  upstream = (unset)
-  args = ["world"]
  repo = repo
  repo_path = _REPO_
  owner = (unset)
@@ -61,6 +60,7 @@ exit_code: 0
  remote = origin
  remote_url = ../origin.git
  cwd = _REPO_.feature
+  args = world
◎ Running alias greet
○ Expanding greet
  echo hello {{ args }}