Skip to content

Commit 0d06193

Browse files
committed
Only canonicalize executable path if it has relative directories
Otherwise symbolic links may also accidentally be resolved which may lead to unexpected results in the case of 'coreutils', a binary that depends on the executable name being a symbolic link. This means a path like /bin/echo being canonicalized to /bin/coreutils will loose all information about the desired functionality. Test failures will occur if 'echo' is resolved that way and it's not trivial to find the cause of it in the provided error messages.
1 parent 7fbbf4e commit 0d06193

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

crates/cargo-util/src/paths.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,19 @@ pub fn resolve_executable(exec: &Path) -> Result<PathBuf> {
125125
if candidate.is_file() {
126126
// PATH may have a component like "." in it, so we still need to
127127
// canonicalize.
128-
return Ok(candidate.canonicalize()?);
128+
// Only do so if there are relative path components, otherwise symlinks to 'echo' may be resolved to their
129+
// root program like 'coreutils' which relies on the executable name for proper function.
130+
let has_relative_path_components = candidate.components().any(|c| {
131+
matches!(
132+
c,
133+
std::path::Component::ParentDir | std::path::Component::CurDir
134+
)
135+
});
136+
return Ok(if has_relative_path_components {
137+
candidate.canonicalize()?
138+
} else {
139+
candidate
140+
});
129141
}
130142
}
131143

0 commit comments

Comments
 (0)