Skip to content

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

fix: preserve TOML insertion order for user config hooks (#741)

Maximilian Roosbdc1cf1b64f3
diff --git a/Cargo.lock b/Cargo.lock
index 5737b2c90..e900a32a0 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -205,6 +205,9 @@ name = "bitflags"
version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3"
+dependencies = [
+ "serde_core",
+]
[[package]]
name = "block-buffer"
@@ -401,8 +404,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b30fa8254caad766fc03cb0ccae691e14bf3bd72bfff27f72802ce729551b3d6"
dependencies = [
"convert_case 0.6.0",
+ "indexmap",
"pathdiff",
+ "ron",
"serde_core",
+ "serde_json",
"toml",
"winnow",
]
@@ -1807,6 +1813,21 @@ version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba39f3699c378cd8970968dcbff9c43159ea4cfbd88d43c00b22f2ef10a435d2"
+[[package]]
+name = "ron"
+version = "0.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fd490c5b18261893f14449cbd28cb9c0b637aebf161cd77900bfdedaff21ec32"
+dependencies = [
+ "bitflags 2.10.0",
+ "indexmap",
+ "once_cell",
+ "serde",
+ "serde_derive",
+ "typeid",
+ "unicode-ident",
+]
+
[[package]]
name = "rstest"
version = "0.26.1"
@@ -2471,6 +2492,12 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4013970217383f67b18aef68f6fb2e8d409bc5755227092d32efb0422ba24b8"
+[[package]]
+name = "typeid"
+version = "1.0.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bc7d623258602320d5c55d1bc22793b57daff0ec7efc270ea7d55ce1d5f5471c"
+
[[package]]
name = "typenum"
version = "1.19.0"
diff --git a/Cargo.toml b/Cargo.toml
index 10751c50b..84dad84de 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -72,7 +72,7 @@ chrono = "0.4"
clap = { version = "4.5", features = ["derive", "unstable-ext", "wrap_help"] }
clap_complete = { version = "4.5", features = ["unstable-dynamic"] }
# Only enable TOML format - saves ~250KB by excluding yaml, ron, json5, ini parsers
-config = { version = "0.15", default-features = false, features = ["toml", "convert-case"] }
+config = { version = "0.15", default-features = false, features = ["toml", "convert-case", "preserve_order"] }
crossbeam-channel = "0.5"
crossterm = "0.29"
env_logger = "0.11"
diff --git a/src/config/user.rs b/src/config/user.rs
index d554c491d..2bedc355d 100644
--- a/src/config/user.rs
+++ b/src/config/user.rs
@@ -418,6 +418,9 @@ impl WorktrunkConfig {
.convert_case(Case::Kebab),
);
+ // The config crate's `preserve_order` feature ensures TOML insertion order
+ // is preserved (uses IndexMap instead of HashMap internally).
+ // See: https://github.com/max-sixty/worktrunk/issues/737
let config: Self = builder.build()?.try_deserialize()?;
// Validate worktree path (only if explicitly set - default is always valid)
diff --git a/tests/integration_tests/user_hooks.rs b/tests/integration_tests/user_hooks.rs
index 78b3766a2..1ce2d5029 100644
--- a/tests/integration_tests/user_hooks.rs
+++ b/tests/integration_tests/user_hooks.rs
@@ -1359,3 +1359,40 @@ test = "echo '{{ main_worktree }}' > alias_output.txt"
"Deprecated alias should be overridden, got: {contents}"
);
}
+
+// ============================================================================
+// Hook Order Preservation Tests (Issue #737)
+// ============================================================================
+
+/// Test that user hooks execute in TOML insertion order, not alphabetical
+/// See: https://github.com/max-sixty/worktrunk/issues/737
+#[rstest]
+fn test_user_hooks_preserve_toml_order(repo: TestRepo) {
+ // Write user config with hooks in specific order (NOT alphabetical: vscode, claude, copy, submodule)
+ // If order were alphabetical, it would be: claude, copy, submodule, vscode
+ repo.write_test_config(
+ r#"[post-create]
+vscode = "echo '1' >> hook_order.txt"
+claude = "echo '2' >> hook_order.txt"
+copy = "echo '3' >> hook_order.txt"
+submodule = "echo '4' >> hook_order.txt"
+"#,
+ );
+
+ snapshot_switch("user_hooks_preserve_order", &repo, &["--create", "feature"]);
+
+ // Verify execution order by reading the output file
+ let worktree_path = repo.root_path().parent().unwrap().join("repo.feature");
+ let order_file = worktree_path.join("hook_order.txt");
+ assert!(order_file.exists(), "hook_order.txt should be created");
+
+ let contents = fs::read_to_string(&order_file).unwrap();
+ let lines: Vec<&str> = contents.lines().collect();
+
+ // Hooks should execute in TOML order: 1, 2, 3, 4
+ assert_eq!(
+ lines,
+ vec!["1", "2", "3", "4"],
+ "Hooks should execute in TOML insertion order (vscode, claude, copy, submodule)"
+ );
+}
diff --git a/tests/snapshots/integration__integration_tests__user_hooks__user_hooks_preserve_order.snap b/tests/snapshots/integration__integration_tests__user_hooks__user_hooks_preserve_order.snap
new file mode 100644
index 000000000..c7796c810
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__user_hooks__user_hooks_preserve_order.snap
@@ -0,0 +1,50 @@
+---
+source: tests/integration_tests/user_hooks.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - feature
+ 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]"
+ PATH: "[PATH]"
+ RUST_LOG: warn
+ SHELL: ""
+ SOURCE_DATE_EPOCH: "1735776000"
+ TERM: alacritty
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"
+ WT_TEST_DELAYED_STREAM_MS: "999999"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: true
+exit_code: 0
+----- stdout -----
+
+----- stderr -----
+◎ Running post-create user:vscode @ _REPO_.feature:
+  echo '1' >> hook_order.txt
+◎ Running post-create user:claude @ _REPO_.feature:
+  echo '2' >> hook_order.txt
+◎ Running post-create user:copy @ _REPO_.feature:
+  echo '3' >> hook_order.txt
+◎ Running post-create user:submodule @ _REPO_.feature:
+  echo '4' >> hook_order.txt
+✓ Created branch feature from main and worktree @ _REPO_.feature
+↳ To customize worktree locations, run wt config create
+▲ Cannot change directory — shell integration not installed
+↳ To enable automatic cd, run wt config shell install