Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
67 changes: 59 additions & 8 deletions crates/uv-python/src/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -919,27 +919,75 @@ fn executable_path_from_base(
}
}

/// The Windows launcher subsystem to generate when linking to a Python executable.
///
/// On Windows, a generated trampoline must declare itself as either a console or windowed
/// subsystem binary. Console launchers (e.g. `python.exe`) attach to a terminal; windowed
/// launchers (e.g. `pythonw.exe`) do not.
///
/// The variant is ignored on non-Windows platforms, where [`create_link_to_executable`] and
/// [`replace_link_to_executable`] use a symlink and the subsystem is irrelevant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LauncherSubsystem {
/// A console subsystem launcher, e.g. `python.exe`.
Console,
/// A windowed subsystem launcher, e.g. `pythonw.exe`.
Windowed,
}

/// A target for [`create_link_to_executable`] and [`replace_link_to_executable`].
///
/// Bundles the path of the executable to link to with the [`LauncherSubsystem`] the caller
/// wants the resulting Windows trampoline to use. The caller is the source of truth for
/// the subsystem because it knows which logical executable is being created (e.g. `python`
/// vs `pythonw`); the subsystem is ignored on non-Windows.
#[derive(Debug, Clone)]
pub struct LauncherTarget {
pub path: PathBuf,
pub subsystem: LauncherSubsystem,
}

impl LauncherTarget {
/// Create a console-subsystem launcher target (e.g. for `python.exe`).
pub fn console(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
subsystem: LauncherSubsystem::Console,
}
}

/// Create a windowed-subsystem launcher target (e.g. for `pythonw.exe`).
pub fn windowed(path: impl Into<PathBuf>) -> Self {
Self {
path: path.into(),
subsystem: LauncherSubsystem::Windowed,
}
}
}

/// Create a link to a managed Python executable.
///
/// If the file already exists at the link path, an error will be returned.
pub fn create_link_to_executable(link: &Path, executable: &Path) -> Result<(), Error> {
pub fn create_link_to_executable(link: &Path, executable: &LauncherTarget) -> Result<(), Error> {
let link_parent = link.parent().ok_or(Error::NoExecutableDirectory)?;
fs_err::create_dir_all(link_parent).map_err(Error::ExecutableDirectory)?;

if cfg!(unix) {
// Note this will never copy on Unix — we use it here to allow compilation on Windows
match symlink_or_copy_file(executable, link) {
match symlink_or_copy_file(&executable.path, link) {
Ok(()) => Ok(()),
Err(err) if err.kind() == io::ErrorKind::NotFound => {
Err(Error::MissingExecutable(executable.to_path_buf()))
Err(Error::MissingExecutable(executable.path.clone()))
}
Err(err) => Err(Error::LinkExecutable(err)),
}
} else if cfg!(windows) {
use uv_trampoline_builder::windows_python_launcher;

// TODO(zanieb): Install GUI launchers as well
let launcher = windows_python_launcher(executable, false)?;
let launcher = windows_python_launcher(
&executable.path,
matches!(executable.subsystem, LauncherSubsystem::Windowed),
)?;

// OK to use `std::fs` here, `fs_err` does not support `File::create_new` and we attach
// error context anyway
Expand All @@ -959,16 +1007,19 @@ pub fn create_link_to_executable(link: &Path, executable: &Path) -> Result<(), E
/// If a file already exists at the link path, it will be atomically replaced.
///
/// See [`create_link_to_executable`] for a variant that errors if the link already exists.
pub fn replace_link_to_executable(link: &Path, executable: &Path) -> Result<(), Error> {
pub fn replace_link_to_executable(link: &Path, executable: &LauncherTarget) -> Result<(), Error> {
let link_parent = link.parent().ok_or(Error::NoExecutableDirectory)?;
fs_err::create_dir_all(link_parent).map_err(Error::ExecutableDirectory)?;

if cfg!(unix) {
replace_symlink(executable, link).map_err(Error::LinkExecutable)
replace_symlink(&executable.path, link).map_err(Error::LinkExecutable)
} else if cfg!(windows) {
use uv_trampoline_builder::windows_python_launcher;

let launcher = windows_python_launcher(executable, false)?;
let launcher = windows_python_launcher(
&executable.path,
matches!(executable.subsystem, LauncherSubsystem::Windowed),
)?;

uv_fs::write_atomic_sync(link, &*launcher).map_err(Error::LinkExecutable)
} else {
Expand Down
37 changes: 26 additions & 11 deletions crates/uv-virtualenv/src/virtualenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use uv_fs::{CWD, Simplified, cachedir};
use uv_platform_tags::Os;
use uv_pypi_types::Scheme;
use uv_python::managed::{
ManagedPythonInstallation, PythonMinorVersionLink, replace_link_to_executable,
LauncherTarget, ManagedPythonInstallation, PythonMinorVersionLink, replace_link_to_executable,
};
use uv_python::{Interpreter, VirtualEnvironment};
use uv_shell::escape_posix_for_single_quotes;
Expand Down Expand Up @@ -298,25 +298,40 @@ pub(crate) fn create(
if cfg!(windows) {
if using_minor_version_link {
let target = scripts.join(WindowsExecutable::Python.exe(interpreter));
replace_link_to_executable(target.as_path(), &executable_target)
.map_err(Error::Python)?;
replace_link_to_executable(
target.as_path(),
&LauncherTarget::console(executable_target.clone()),
)
.map_err(Error::Python)?;
let targetw = scripts.join(WindowsExecutable::Pythonw.exe(interpreter));
replace_link_to_executable(targetw.as_path(), &executable_target)
.map_err(Error::Python)?;
replace_link_to_executable(
targetw.as_path(),
&LauncherTarget::windowed(executable_target.clone()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

executable_target is created (via PythonMinorVersionLink::from_installation) with installation.executable(false) which means it's the base python interpreter and not pythonw. Won't using it here still lead to a console window being created?

)
.map_err(Error::Python)?;
if interpreter.gil_disabled() {
let targett = scripts.join(WindowsExecutable::PythonMajorMinort.exe(interpreter));
replace_link_to_executable(targett.as_path(), &executable_target)
.map_err(Error::Python)?;
replace_link_to_executable(
targett.as_path(),
&LauncherTarget::console(executable_target.clone()),
)
.map_err(Error::Python)?;
let targetwt = scripts.join(WindowsExecutable::PythonwMajorMinort.exe(interpreter));
replace_link_to_executable(targetwt.as_path(), &executable_target)
.map_err(Error::Python)?;
replace_link_to_executable(
targetwt.as_path(),
&LauncherTarget::windowed(executable_target.clone()),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

)
.map_err(Error::Python)?;
}
} else if matches!(interpreter.platform().os(), Os::Pyodide { .. }) {
// For Pyodide, link only `python.exe`.
// This should not be copied as `python.exe` is a wrapper that launches Pyodide.
let target = scripts.join(WindowsExecutable::Python.exe(interpreter));
replace_link_to_executable(target.as_path(), &executable_target)
.map_err(Error::Python)?;
replace_link_to_executable(
target.as_path(),
&LauncherTarget::console(executable_target.clone()),
)
.map_err(Error::Python)?;
} else {
// Always copy `python.exe`.
copy_launcher_windows(
Expand Down
8 changes: 5 additions & 3 deletions crates/uv/src/commands/python/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use uv_python::downloads::{
PythonDownloadRequest,
};
use uv_python::managed::{
ManagedPythonInstallation, ManagedPythonInstallations, PythonMinorVersionLink,
LauncherTarget, ManagedPythonInstallation, ManagedPythonInstallations, PythonMinorVersionLink,
compare_build_versions, create_link_to_executable, python_executable_dir,
};
use uv_python::{
Expand Down Expand Up @@ -1013,7 +1013,7 @@ fn create_bin_links(
installation.executable(false)
};

match create_link_to_executable(&target, &executable) {
match create_link_to_executable(&target, &LauncherTarget::console(&executable)) {
Ok(()) => {
debug!(
"Installed executable at `{}` for {}",
Expand Down Expand Up @@ -1170,7 +1170,9 @@ fn create_bin_links(
.remove(&target);
}

if let Err(err) = create_link_to_executable(&target, &executable) {
if let Err(err) =
create_link_to_executable(&target, &LauncherTarget::console(&executable))
{
errors.push((
InstallErrorKind::Bin,
installation.key().clone(),
Expand Down
Loading