Skip to content

Commit

Permalink
Pass VIRTUAL_ENV env var to uv
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jan 11, 2025
1 parent e9f3097 commit fd3d8a2
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl InstallBackend {
}
}

fn version(&self, python_path: &Path) -> Result<semver::Version> {
let mut cmd = self.make_command(python_path);
fn version(&self, python_path: &Path, venv_dir: &Path) -> Result<semver::Version> {
let mut cmd = self.make_command(python_path, venv_dir);
let output = cmd
.arg("--version")
.output()
Expand All @@ -62,12 +62,12 @@ impl InstallBackend {
}

/// check whether this install backend supports `show --files`. Returns Ok(()) if it does.
fn check_supports_show_files(&self, python_path: &Path) -> Result<()> {
fn check_supports_show_files(&self, python_path: &Path, venv_dir: &Path) -> Result<()> {
match self {
InstallBackend::Pip { .. } => Ok(()),
InstallBackend::Uv { .. } => {
// https://github.com/astral-sh/uv/releases/tag/0.4.25
let version = self.version(python_path)?;
let version = self.version(python_path, venv_dir)?;
if version < semver::Version::new(0, 4, 25) {
bail!(
"uv >= 0.4.25 is required for `show --files`. Version {} was found.",
Expand All @@ -87,7 +87,7 @@ impl InstallBackend {
}
}

fn make_command(&self, python_path: &Path) -> Command {
fn make_command(&self, python_path: &Path, venv_dir: &Path) -> Command {
match self {
InstallBackend::Pip { path } => match &path {
Some(path) => {
Expand All @@ -106,6 +106,9 @@ impl InstallBackend {
InstallBackend::Uv { path, args } => {
let mut cmd = Command::new(path);
cmd.args(args).arg("pip");
if std::env::var_os("VIRTUAL_ENV").is_none() {
cmd.env("VIRTUAL_ENV", venv_dir);
}
cmd
}
}
Expand Down Expand Up @@ -225,6 +228,7 @@ fn install_dependencies(
build_context: &BuildContext,
extras: &[String],
interpreter: &PythonInterpreter,
venv_dir: &Path,
install_backend: &InstallBackend,
) -> Result<()> {
if !build_context.metadata24.requires_dist.is_empty() {
Expand Down Expand Up @@ -256,7 +260,7 @@ fn install_dependencies(
pkg.to_string()
}));
let status = install_backend
.make_command(&interpreter.executable)
.make_command(&interpreter.executable, venv_dir)
.args(&args)
.status()
.context("Failed to run pip install")?;
Expand All @@ -275,7 +279,7 @@ fn install_wheel(
wheel_filename: &Path,
install_backend: &InstallBackend,
) -> Result<()> {
let mut cmd = install_backend.make_command(python);
let mut cmd = install_backend.make_command(python, venv_dir);
let output = cmd
.args(["install", "--no-deps", "--force-reinstall"])
.arg(dunce::simplified(wheel_filename))
Expand Down Expand Up @@ -305,7 +309,7 @@ fn install_wheel(
String::from_utf8_lossy(&output.stderr).trim(),
);
}
if let Err(err) = configure_as_editable(build_context, python, install_backend) {
if let Err(err) = configure_as_editable(build_context, python, venv_dir, install_backend) {
eprintln!("⚠️ Warning: failed to set package as editable: {}", err);
}
Ok(())
Expand All @@ -322,11 +326,12 @@ fn install_wheel(
fn configure_as_editable(
build_context: &BuildContext,
python: &Path,
venv_dir: &Path,
install_backend: &InstallBackend,
) -> Result<()> {
println!("✏️ Setting installed package as editable");
install_backend.check_supports_show_files(python)?;
let mut cmd = install_backend.make_command(python);
install_backend.check_supports_show_files(python, venv_dir)?;
let mut cmd = install_backend.make_command(python, venv_dir);
let cmd = cmd.args(["show", "--files", &build_context.metadata24.name]);
debug!("running {:?}", cmd);
let output = cmd.output()?;
Expand Down Expand Up @@ -460,7 +465,13 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> {
}
};

install_dependencies(&build_context, &extras, &interpreter, &install_backend)?;
install_dependencies(
&build_context,
&extras,
&interpreter,
venv_dir,
&install_backend,
)?;

let wheels = build_context.build_wheels()?;
if !skip_install {
Expand Down

0 comments on commit fd3d8a2

Please sign in to comment.