From 72da1779169ce6bb3d1ec811ef968cd68caaaa72 Mon Sep 17 00:00:00 2001 From: leostelon Date: Mon, 31 Aug 2026 16:00:43 +0530 Subject: [PATCH] fix: canonicalize executable path before resolving resource root On macOS, std::env::current_exe() returns the symlink path (~/.chief/bin/chief) instead of the resolved target (~/.chief/versions//bin/chief). The grandparent walk from the symlink path landed on ~/.chief/ instead of the version directory, so the resources/ directory was never found. Canonicalize the executable path before computing the resource root so the walk starts from the real binary and lands on the correct version directory. --- .../crates/host-primitives/src/install.rs | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/apps/chiefd/crates/host-primitives/src/install.rs b/apps/chiefd/crates/host-primitives/src/install.rs index 0a6b8be..add8269 100644 --- a/apps/chiefd/crates/host-primitives/src/install.rs +++ b/apps/chiefd/crates/host-primitives/src/install.rs @@ -78,7 +78,11 @@ pub const RESOURCES_DIR: &str = "resources"; /// tools and nobody found out for a day. pub fn resource_root_from_exe() -> Option { let exe = std::env::current_exe().ok()?; - resource_root_beside(&exe) + // Canonicalize so a symlink at ~/.chief/bin/chief resolves to the real + // binary under versions//bin/chief, and the grandparent walk lands on + // the correct version directory rather than on ~/.chief. + let resolved = exe.canonicalize().ok().unwrap_or(exe); + resource_root_beside(&resolved) } /// [`resource_root_from_exe`], against a named executable path. @@ -207,4 +211,36 @@ mod tests { "/checkout/../versions/2.0.7/resources" ))); } + + /// A binary invoked through a symlink (e.g. `~/.chief/bin/chief` -> + /// `~/.chief/versions/0.5.0/bin/chief`) must resolve resources beside the + /// REAL binary, not beside the symlink. The caller canonicalizes the + /// executable path before handing it here; this test proves that a + /// canonicalized path to a real binary finds the resources correctly. + #[test] + fn resources_are_found_when_the_binary_is_reached_through_a_symlink() { + let root = tempfile::tempdir().expect("tempdir"); + // Simulate the install layout: ~/.chief/versions/0.5.0/bin/chief + let version_dir = root.path().join("versions/v2.0.7"); + let bin = version_dir.join("bin"); + std::fs::create_dir_all(&bin).expect("bin"); + let resources = version_dir.join("resources"); + std::fs::create_dir_all(&resources).expect("resources"); + // The real binary must exist for canonicalize to resolve the symlink. + let real_binary = bin.join("chief"); + std::fs::write(&real_binary, b"fake binary").expect("write binary"); + + // The symlink at ~/.chief/bin/chief + let symlink_dir = root.path().join("bin"); + std::fs::create_dir_all(&symlink_dir).expect("bin-dir"); + let symlink = symlink_dir.join("chief"); + std::os::unix::fs::symlink(&real_binary, &symlink).expect("symlink"); + + // The caller canonicalizes the symlink, resolving it to the real path. + let resolved = symlink.canonicalize().expect("canonicalize"); + // On macOS /var is a symlink to /private/var, so canonicalize both + // sides of the comparison. + let expected = resources.canonicalize().expect("canonicalize resources"); + assert_eq!(resource_root_beside(&resolved), Some(expected)); + } }