Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(venv): make relocatable activation scripts support ksh #5640

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion crates/uv-shell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum Shell {
Nushell,
/// C SHell (csh)
Csh,
/// Korn SHell (ksh)
Ksh,
}

impl Shell {
Expand All @@ -43,6 +45,8 @@ impl Shell {
Some(Shell::Bash)
} else if std::env::var_os("ZSH_VERSION").is_some() {
Some(Shell::Zsh)
} else if std::env::var_os("KSH_VERSION").is_some() {
Some(Shell::Ksh)
} else if let Some(env_shell) = std::env::var_os("SHELL") {
Shell::from_shell_path(env_shell)
} else if cfg!(windows) {
Expand Down Expand Up @@ -99,6 +103,10 @@ impl Shell {
home_dir.join(".bashrc"),
]
}
Shell::Ksh => {
// On Ksh it's standard POSIX `.profile` for login shells, and `.kshrc` for non-login.
vec![home_dir.join(".profile"), home_dir.join(".kshrc")]
}
Shell::Zsh => {
// On Zsh, we only need to update `.zshenv`. This file is sourced for both login and
// non-login shells. However, we match rustup's logic for determining _which_
Expand Down Expand Up @@ -174,7 +182,7 @@ impl Shell {
pub fn prepend_path(self, path: &Path) -> Option<String> {
match self {
Shell::Nushell => None,
Shell::Bash | Shell::Zsh => Some(format!(
Shell::Bash | Shell::Zsh | Shell::Ksh => Some(format!(
"export PATH=\"{}:$PATH\"",
backslash_escape(&path.simplified_display().to_string()),
)),
Expand Down Expand Up @@ -208,6 +216,7 @@ impl std::fmt::Display for Shell {
Shell::Zsh => write!(f, "Zsh"),
Shell::Nushell => write!(f, "Nushell"),
Shell::Csh => write!(f, "Csh"),
Shell::Ksh => write!(f, "Ksh"),
}
}
}
Expand All @@ -220,6 +229,7 @@ fn parse_shell_from_path(path: &Path) -> Option<Shell> {
"zsh" => Some(Shell::Zsh),
"fish" => Some(Shell::Fish),
"csh" => Some(Shell::Csh),
"ksh" => Some(Shell::Ksh),
"powershell" | "powershell_ise" => Some(Shell::Powershell),
_ => None,
}
Expand Down
16 changes: 12 additions & 4 deletions crates/uv-virtualenv/src/activator/activate
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly


if [ "${BASH_SOURCE-}" = "$0" ]; then
echo "You must source this script: \$ source $0" >&2
exit 33
# Get script path (only used if environment is relocatable).
if [ -n "${BASH_VERSION:+x}" ] ; then
SCRIPT_PATH="${BASH_SOURCE[0]}"
if [ "$SCRIPT_PATH" = "$0" ]; then
# Only bash has a reasonably robust check for source'dness.
echo "You must source this script: \$ source $0" >&2
exit 33
fi
elif [ -n "${ZSH_VERSION:+x}" ] ; then
SCRIPT_PATH="${(%):-%x}"
elif [ -n "${KSH_VERSION:+x}" ] ; then
SCRIPT_PATH="${.sh.file}"
fi

deactivate () {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-virtualenv/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ pub(crate) fn create(
(true, "activate") => {
// Extremely verbose, but should cover all major POSIX shells,
// as well as platforms where `readlink` does not implement `-f`.
r#"'"$(dirname -- "$(CDPATH= cd -- "$(dirname -- ${BASH_SOURCE[0]:-${(%):-%x}})" && echo "$PWD")")"'"#
r#"'"$(dirname -- "$(CDPATH= cd -- "$(dirname -- "$SCRIPT_PATH")" > /dev/null && echo "$PWD")")"'"#
}
(true, "activate.bat") => r"%~dp0..",
(true, "activate.fish") => {
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ async fn venv_impl(
// Determine the appropriate activation command.
let activation = match Shell::from_env() {
None => None,
Some(Shell::Bash | Shell::Zsh) => Some(format!(
Some(Shell::Bash | Shell::Zsh | Shell::Ksh) => Some(format!(
"source {}",
shlex_posix(venv.scripts().join("activate"))
)),
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ fn verify_pyvenv_cfg_relocatable() {

let activate_sh = scripts.child("activate");
activate_sh.assert(predicates::path::is_file());
activate_sh.assert(predicates::str::contains(r#"VIRTUAL_ENV=''"$(dirname -- "$(CDPATH= cd -- "$(dirname -- ${BASH_SOURCE[0]:-${(%):-%x}})" && echo "$PWD")")"''"#));
activate_sh.assert(predicates::str::contains(r#"VIRTUAL_ENV=''"$(dirname -- "$(CDPATH= cd -- "$(dirname -- "$SCRIPT_PATH")" > /dev/null && echo "$PWD")")"''"#));

let activate_bat = scripts.child("activate.bat");
activate_bat.assert(predicates::path::is_file());
Expand Down
Loading