Skip to content

Commit 1fe62fc

Browse files
committed
fix(venv.relocatable): script entrypoints should work if symlinked to
Fixes: #8058
1 parent 8270894 commit 1fe62fc

File tree

4 files changed

+18
-6
lines changed

4 files changed

+18
-6
lines changed

crates/uv-install-wheel/src/wheel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn format_shebang(executable: impl AsRef<Path>, os_name: &str, relocatable: bool
143143
// (note: the Windows trampoline binaries natively support relative paths to executable)
144144
if shebang_length > 127 || executable.contains(' ') || relocatable {
145145
let prefix = if relocatable {
146-
r#""$(CDPATH= cd -- "$(dirname -- "$0")" && echo "$PWD")"/"#
146+
r#""$(dirname -- "$(realpath -- "$0")")"/"#
147147
} else {
148148
""
149149
};
@@ -1019,7 +1019,7 @@ mod test {
10191019
let os_name = "posix";
10201020
assert_eq!(
10211021
format_shebang(executable, os_name, true),
1022-
"#!/bin/sh\n'''exec' \"$(CDPATH= cd -- \"$(dirname -- \"$0\")\" && echo \"$PWD\")\"/'python3' \"$0\" \"$@\"\n' '''"
1022+
"#!/bin/sh\n'''exec' \"$(dirname -- \"$(realpath -- \"$0\")\")\"/'python3' \"$0\" \"$@\"\n' '''"
10231023
);
10241024

10251025
// Except on Windows...

crates/uv-virtualenv/src/virtualenv.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,7 @@ pub(crate) fn create(
298298

299299
let virtual_env_dir = match (relocatable, name.to_owned()) {
300300
(true, "activate") => {
301-
// Extremely verbose, but should cover all major POSIX shells,
302-
// as well as platforms where `readlink` does not implement `-f`.
303-
r#"'"$(dirname -- "$(CDPATH= cd -- "$(dirname -- "$SCRIPT_PATH")" > /dev/null && echo "$PWD")")"'"#
301+
r#"'"$(dirname -- "$(dirname -- "$(realpath -- "$SCRIPT_PATH")")")"'"#
304302
}
305303
(true, "activate.bat") => r"%~dp0..",
306304
(true, "activate.fish") => {

crates/uv/tests/pip_install.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6810,6 +6810,18 @@ fn install_relocatable() -> Result<()> {
68106810
.success()
68116811
.stdout(predicate::str::contains("Hello world!"));
68126812

6813+
// Relocatable entrypoint should still be usable even if symlinked.
6814+
// Only testable on POSIX since symlinks require elevated privilege on Windows
6815+
#[cfg(unix)]
6816+
{
6817+
let script_symlink_path = context.temp_dir.join("black");
6818+
std::os::unix::fs::symlink(script_path, script_symlink_path.clone())?;
6819+
Command::new(script_symlink_path.as_os_str())
6820+
.assert()
6821+
.success()
6822+
.stdout(predicate::str::contains("Hello world!"));
6823+
}
6824+
68136825
Ok(())
68146826
}
68156827

crates/uv/tests/venv.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,9 @@ fn verify_pyvenv_cfg_relocatable() {
957957

958958
let activate_sh = scripts.child("activate");
959959
activate_sh.assert(predicates::path::is_file());
960-
activate_sh.assert(predicates::str::contains(r#"VIRTUAL_ENV=''"$(dirname -- "$(CDPATH= cd -- "$(dirname -- "$SCRIPT_PATH")" > /dev/null && echo "$PWD")")"''"#));
960+
activate_sh.assert(predicates::str::contains(
961+
r#"VIRTUAL_ENV=''"$(dirname -- "$(dirname -- "$(realpath -- "$SCRIPT_PATH")")")"''"#,
962+
));
961963

962964
let activate_bat = scripts.child("activate.bat");
963965
activate_bat.assert(predicates::path::is_file());

0 commit comments

Comments
 (0)