Skip to content

Commit 7638729

Browse files
[ty] Move venv and conda env discovery to SearchPath::from_settings (#18938)
Co-authored-by: Alex Waygood <[email protected]>
1 parent d04e63a commit 7638729

File tree

14 files changed

+146
-107
lines changed

14 files changed

+146
-107
lines changed

crates/ruff/tests/analyze_graph.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fn command() -> Command {
1717
command.arg("analyze");
1818
command.arg("graph");
1919
command.arg("--preview");
20+
command.env_clear();
2021
command
2122
}
2223

crates/ruff_benchmark/benches/ty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,7 @@ impl<'a> ProjectBenchmark<'a> {
429429
metadata.apply_options(Options {
430430
environment: Some(EnvironmentOptions {
431431
python_version: Some(RangedValue::cli(self.project.config.python_version)),
432-
python: (!self.project.config().dependencies.is_empty())
433-
.then_some(RelativePathBuf::cli(SystemPath::new(".venv"))),
432+
python: Some(RelativePathBuf::cli(SystemPath::new(".venv"))),
434433
..EnvironmentOptions::default()
435434
}),
436435
..Options::default()

crates/ruff_benchmark/benches/ty_walltime.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ impl<'a> Benchmark<'a> {
3636
metadata.apply_options(Options {
3737
environment: Some(EnvironmentOptions {
3838
python_version: Some(RangedValue::cli(self.project.config.python_version)),
39-
python: (!self.project.config().dependencies.is_empty())
40-
.then_some(RelativePathBuf::cli(SystemPath::new(".venv"))),
39+
python: Some(RelativePathBuf::cli(SystemPath::new(".venv"))),
4140
..EnvironmentOptions::default()
4241
}),
4342
..Options::default()

crates/ruff_benchmark/src/real_world_projects.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,17 @@ impl<'a> RealWorldProject<'a> {
7474
};
7575

7676
// Install dependencies if specified
77-
if !checkout.project().dependencies.is_empty() {
78-
tracing::debug!(
79-
"Installing {} dependencies for project '{}'...",
80-
checkout.project().dependencies.len(),
81-
checkout.project().name
82-
);
83-
let start = std::time::Instant::now();
84-
install_dependencies(&checkout)?;
85-
tracing::debug!(
86-
"Dependency installation completed in {:.2}s",
87-
start.elapsed().as_secs_f64()
88-
);
89-
}
77+
tracing::debug!(
78+
"Installing {} dependencies for project '{}'...",
79+
checkout.project().dependencies.len(),
80+
checkout.project().name
81+
);
82+
let start_install = std::time::Instant::now();
83+
install_dependencies(&checkout)?;
84+
tracing::debug!(
85+
"Dependency installation completed in {:.2}s",
86+
start_install.elapsed().as_secs_f64()
87+
);
9088

9189
tracing::debug!("Project setup took: {:.2}s", start.elapsed().as_secs_f64());
9290

@@ -281,6 +279,14 @@ fn install_dependencies(checkout: &Checkout) -> Result<()> {
281279
String::from_utf8_lossy(&output.stderr)
282280
);
283281

282+
if checkout.project().dependencies.is_empty() {
283+
tracing::debug!(
284+
"No dependencies to install for project '{}'",
285+
checkout.project().name
286+
);
287+
return Ok(());
288+
}
289+
284290
// Install dependencies with date constraint in the isolated environment
285291
let mut cmd = Command::new("uv");
286292
cmd.args([

crates/ruff_db/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ filetime = { workspace = true }
3030
glob = { workspace = true }
3131
ignore = { workspace = true, optional = true }
3232
matchit = { workspace = true }
33+
path-slash = { workspace = true }
34+
rustc-hash = { workspace = true }
3335
salsa = { workspace = true }
3436
schemars = { workspace = true, optional = true }
3537
serde = { workspace = true, optional = true }
36-
path-slash = { workspace = true }
3738
thiserror = { workspace = true }
3839
tracing = { workspace = true }
3940
tracing-subscriber = { workspace = true, optional = true }
40-
rustc-hash = { workspace = true }
4141
zip = { workspace = true }
4242

4343
[target.'cfg(target_arch="wasm32")'.dependencies]

crates/ruff_graph/src/db.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ruff_db::{Db as SourceDb, Upcast};
99
use ruff_python_ast::PythonVersion;
1010
use ty_python_semantic::lint::{LintRegistry, RuleSelection};
1111
use ty_python_semantic::{
12-
Db, Program, ProgramSettings, PythonPath, PythonPlatform, PythonVersionSource,
12+
Db, Program, ProgramSettings, PythonEnvironmentPath, PythonPlatform, PythonVersionSource,
1313
PythonVersionWithSource, SearchPathSettings, SysPrefixPathOrigin, default_lint_registry,
1414
};
1515

@@ -36,11 +36,11 @@ impl ModuleDb {
3636
venv_path: Option<SystemPathBuf>,
3737
) -> Result<Self> {
3838
let mut search_paths = SearchPathSettings::new(src_roots);
39+
// TODO: Consider setting `PythonPath::Auto` if no venv_path is provided.
3940
if let Some(venv_path) = venv_path {
40-
search_paths.python_path =
41-
PythonPath::sys_prefix(venv_path, SysPrefixPathOrigin::PythonCliFlag);
41+
search_paths.python_environment =
42+
PythonEnvironmentPath::explicit(venv_path, SysPrefixPathOrigin::PythonCliFlag);
4243
}
43-
4444
let db = Self::default();
4545
let search_paths = search_paths
4646
.to_search_paths(db.system(), db.vendored())

crates/ty/tests/cli/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -708,9 +708,8 @@ impl CliTest {
708708
let mut command = Command::new(get_cargo_bin("ty"));
709709
command.current_dir(&self.project_dir).arg("check");
710710

711-
// Unset environment variables that can affect test behavior
712-
command.env_remove("VIRTUAL_ENV");
713-
command.env_remove("CONDA_PREFIX");
711+
// Unset all environment variables because they can affect test behavior.
712+
command.env_clear();
714713

715714
command
716715
}

crates/ty_project/src/combine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::{collections::HashMap, hash::BuildHasher};
33
use ordermap::OrderMap;
44
use ruff_db::system::SystemPathBuf;
55
use ruff_python_ast::PythonVersion;
6-
use ty_python_semantic::{PythonPath, PythonPlatform};
6+
use ty_python_semantic::{PythonEnvironmentPath, PythonPlatform};
77

88
/// Combine two values, preferring the values in `self`.
99
///
@@ -141,7 +141,7 @@ macro_rules! impl_noop_combine {
141141

142142
impl_noop_combine!(SystemPathBuf);
143143
impl_noop_combine!(PythonPlatform);
144-
impl_noop_combine!(PythonPath);
144+
impl_noop_combine!(PythonEnvironmentPath);
145145
impl_noop_combine!(PythonVersion);
146146

147147
// std types

crates/ty_project/src/metadata/options.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use crate::Db;
22
use crate::combine::Combine;
33
use crate::glob::{ExcludeFilter, IncludeExcludeFilter, IncludeFilter, PortableGlobKind};
44
use crate::metadata::settings::{OverrideSettings, SrcSettings};
5+
6+
use super::settings::{Override, Settings, TerminalSettings};
57
use crate::metadata::value::{
68
RangedValue, RelativeGlobPattern, RelativePathBuf, ValueSource, ValueSourceGuard,
79
};
8-
910
use ordermap::OrderMap;
1011
use ruff_db::RustDoc;
1112
use ruff_db::diagnostic::{
@@ -28,13 +29,11 @@ use std::sync::Arc;
2829
use thiserror::Error;
2930
use ty_python_semantic::lint::{GetLintError, Level, LintSource, RuleSelection};
3031
use ty_python_semantic::{
31-
ProgramSettings, PythonPath, PythonPlatform, PythonVersionFileSource, PythonVersionSource,
32-
PythonVersionWithSource, SearchPathSettings, SearchPathValidationError, SearchPaths,
33-
SysPrefixPathOrigin,
32+
ProgramSettings, PythonEnvironmentPath, PythonPlatform, PythonVersionFileSource,
33+
PythonVersionSource, PythonVersionWithSource, SearchPathSettings, SearchPathValidationError,
34+
SearchPaths, SysPrefixPathOrigin,
3435
};
3536

36-
use super::settings::{Override, Settings, TerminalSettings};
37-
3837
#[derive(
3938
Debug, Default, Clone, PartialEq, Eq, Combine, Serialize, Deserialize, OptionsMetadata,
4039
)]
@@ -231,7 +230,7 @@ impl Options {
231230
.typeshed
232231
.as_ref()
233232
.map(|path| path.absolute(project_root, system)),
234-
python_path: environment
233+
python_environment: environment
235234
.python
236235
.as_ref()
237236
.map(|python_path| {
@@ -242,24 +241,12 @@ impl Options {
242241
python_path.range(),
243242
),
244243
};
245-
PythonPath::sys_prefix(python_path.absolute(project_root, system), origin)
246-
})
247-
.or_else(|| {
248-
system.env_var("VIRTUAL_ENV").ok().map(|virtual_env| {
249-
PythonPath::sys_prefix(virtual_env, SysPrefixPathOrigin::VirtualEnvVar)
250-
})
251-
})
252-
.or_else(|| {
253-
system.env_var("CONDA_PREFIX").ok().map(|path| {
254-
PythonPath::sys_prefix(path, SysPrefixPathOrigin::CondaPrefixVar)
255-
})
256-
})
257-
.unwrap_or_else(|| {
258-
PythonPath::sys_prefix(
259-
project_root.to_path_buf(),
260-
SysPrefixPathOrigin::LocalVenv,
244+
PythonEnvironmentPath::explicit(
245+
python_path.absolute(project_root, system),
246+
origin,
261247
)
262-
}),
248+
})
249+
.unwrap_or_else(|| PythonEnvironmentPath::Discover(project_root.to_path_buf())),
263250
};
264251

265252
settings.to_search_paths(system, vendored)

crates/ty_python_semantic/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use module_resolver::{
1111
system_module_search_paths,
1212
};
1313
pub use program::{
14-
Program, ProgramSettings, PythonPath, PythonVersionFileSource, PythonVersionSource,
14+
Program, ProgramSettings, PythonEnvironmentPath, PythonVersionFileSource, PythonVersionSource,
1515
PythonVersionWithSource, SearchPathSettings,
1616
};
1717
pub use python_platform::PythonPlatform;

0 commit comments

Comments
 (0)