Skip to content

Commit

Permalink
Query the interpreter for virtual environments
Browse files Browse the repository at this point in the history
When a virtual environment is created, `sys.path` is modified. Instead
of reusing the base interpreter's `sys.path`, query the interpreter
again to get the new `sys.path` values.
  • Loading branch information
pradyunsg committed Jan 13, 2025
1 parent 1858d4c commit b8d0038
Show file tree
Hide file tree
Showing 13 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/uv-build-frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ doctest = false
workspace = true

[dependencies]
uv-cache = { workspace = true }
uv-configuration = { workspace = true }
uv-distribution = { workspace = true }
uv-distribution-types = { workspace = true }
Expand Down
3 changes: 3 additions & 0 deletions crates/uv-build-frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use tokio::process::Command;
use tokio::sync::{Mutex, Semaphore};
use tracing::{debug, info_span, instrument, Instrument};

use uv_cache::Cache;
use uv_configuration::{BuildKind, BuildOutput, ConfigSettings, LowerBound, SourceStrategy};
use uv_distribution::BuildRequires;
use uv_distribution_types::{IndexLocations, Resolution};
Expand Down Expand Up @@ -260,6 +261,7 @@ impl SourceBuild {
mut environment_variables: FxHashMap<OsString, OsString>,
level: BuildOutput,
concurrent_builds: usize,
cache: &Cache,
) -> Result<Self, Error> {
let temp_dir = build_context.cache().venv_dir()?;

Expand Down Expand Up @@ -302,6 +304,7 @@ impl SourceBuild {
temp_dir.path(),
interpreter.clone(),
uv_virtualenv::Prompt::None,
cache,
false,
false,
false,
Expand Down
1 change: 1 addition & 0 deletions crates/uv-dispatch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ impl BuildContext for BuildDispatch<'_> {
self.build_extra_env_vars.clone(),
build_output,
self.concurrency.builds,
self.cache,
)
.boxed_local()
.await?;
Expand Down
2 changes: 2 additions & 0 deletions crates/uv-tool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ impl InstalledTools {
&self,
name: &PackageName,
interpreter: Interpreter,
cache: &Cache,
) -> Result<PythonEnvironment, Error> {
let environment_path = self.tool_dir(name);

Expand All @@ -276,6 +277,7 @@ impl InstalledTools {
&environment_path,
interpreter,
uv_virtualenv::Prompt::None,
cache,
false,
false,
false,
Expand Down
1 change: 1 addition & 0 deletions crates/uv-virtualenv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ doctest = false
workspace = true

[dependencies]
uv-cache = { workspace = true }
uv-fs = { workspace = true }
uv-platform-tags = { workspace = true }
uv-pypi-types = { workspace = true }
Expand Down
6 changes: 5 additions & 1 deletion crates/uv-virtualenv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::Path;

use thiserror::Error;

use uv_cache::Cache;
use uv_platform_tags::PlatformError;
use uv_python::{Interpreter, PythonEnvironment};

Expand All @@ -12,6 +13,8 @@ mod virtualenv;
pub enum Error {
#[error(transparent)]
Io(#[from] io::Error),
#[error(transparent)]
InterpreterQuery(#[from] uv_python::InterpreterError),
#[error("Failed to determine Python interpreter to use")]
Discovery(#[from] uv_python::DiscoveryError),
#[error("Failed to determine Python interpreter to use")]
Expand Down Expand Up @@ -51,6 +54,7 @@ pub fn create_venv(
location: &Path,
interpreter: Interpreter,
prompt: Prompt,
cache: &Cache,
system_site_packages: bool,
allow_existing: bool,
relocatable: bool,
Expand All @@ -68,6 +72,6 @@ pub fn create_venv(
)?;

// Create the corresponding `PythonEnvironment`.
let interpreter = interpreter.with_virtualenv(virtualenv);
let interpreter = Interpreter::query(virtualenv.executable, cache)?;
Ok(PythonEnvironment::from_interpreter(interpreter))
}
1 change: 1 addition & 0 deletions crates/uv/src/commands/project/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ impl CachedEnvironment {
temp_dir.path(),
interpreter,
uv_virtualenv::Prompt::None,
cache,
false,
false,
true,
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/commands/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,7 @@ pub(crate) async fn get_or_init_environment(
&venv,
interpreter,
prompt,
cache,
false,
false,
false,
Expand Down
4 changes: 4 additions & 0 deletions crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ pub(crate) async fn run(
temp_dir.path(),
interpreter,
uv_virtualenv::Prompt::None,
cache,
false,
false,
false,
Expand Down Expand Up @@ -648,6 +649,7 @@ pub(crate) async fn run(
temp_dir.path(),
interpreter,
uv_virtualenv::Prompt::None,
cache,
false,
false,
false,
Expand Down Expand Up @@ -881,6 +883,7 @@ pub(crate) async fn run(
temp_dir.path(),
interpreter,
uv_virtualenv::Prompt::None,
cache,
false,
false,
false,
Expand Down Expand Up @@ -931,6 +934,7 @@ pub(crate) async fn run(
temp_dir.path(),
base_interpreter.clone(),
uv_virtualenv::Prompt::None,
cache,
false,
false,
false,
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/tool/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ pub(crate) async fn install(
},
};

let environment = installed_tools.create_environment(&from.name, interpreter)?;
let environment = installed_tools.create_environment(&from.name, interpreter, &cache)?;

// At this point, we removed any existing environment, so we should remove any of its
// executables.
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/tool/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ async fn upgrade_tool(
)
.await?;

let environment = installed_tools.create_environment(name, interpreter.clone())?;
let environment = installed_tools.create_environment(name, interpreter.clone(), &cache)?;

let environment = sync_environment(
environment,
Expand Down
1 change: 1 addition & 0 deletions crates/uv/src/commands/venv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ async fn venv_impl(
&path,
interpreter,
prompt,
cache,
system_site_packages,
allow_existing,
relocatable,
Expand Down

0 comments on commit b8d0038

Please sign in to comment.