Skip to content

Commit

Permalink
Change EnvironmentOptions::venv-path to Option<SystemPathBuf>
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jan 21, 2025
1 parent c616650 commit 0c6339c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 27 deletions.
5 changes: 1 addition & 4 deletions crates/red_knot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use red_knot_project::metadata::options::{EnvironmentOptions, Options};
use red_knot_project::watch;
use red_knot_project::watch::ProjectWatcher;
use red_knot_project::{ProjectDatabase, ProjectMetadata};
use red_knot_python_semantic::SitePackages;
use red_knot_server::run_server;
use ruff_db::diagnostic::Diagnostic;
use ruff_db::system::{OsSystem, System, SystemPath, SystemPathBuf};
Expand Down Expand Up @@ -77,9 +76,7 @@ impl Args {
venv_path: self
.venv_path
.as_ref()
.map(|venv_path| SitePackages::Derived {
venv_path: SystemPath::absolute(venv_path, cli_cwd),
}),
.map(|venv_path| SystemPath::absolute(venv_path, cli_cwd)),
typeshed: self
.typeshed
.as_ref()
Expand Down
16 changes: 6 additions & 10 deletions crates/red_knot/tests/file_watching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use red_knot_project::metadata::options::{EnvironmentOptions, Options};
use red_knot_project::metadata::pyproject::{PyProject, Tool};
use red_knot_project::watch::{directory_watcher, ChangeEvent, ProjectWatcher};
use red_knot_project::{Db, ProjectDatabase, ProjectMetadata};
use red_knot_python_semantic::{
resolve_module, ModuleName, PythonPlatform, PythonVersion, SitePackages,
};
use red_knot_python_semantic::{resolve_module, ModuleName, PythonPlatform, PythonVersion};
use ruff_db::files::{system_path_to_file, File, FileError};
use ruff_db::source::source_text;
use ruff_db::system::{OsSystem, SystemPath, SystemPathBuf};
Expand Down Expand Up @@ -324,7 +322,6 @@ where
.extra_paths
.iter()
.chain(program_settings.search_paths.typeshed.as_ref())
.chain(program_settings.search_paths.site_packages.paths())
{
std::fs::create_dir_all(path.as_std_path())
.with_context(|| format!("Failed to create search path `{path}`"))?;
Expand Down Expand Up @@ -794,7 +791,7 @@ fn search_path() -> anyhow::Result<()> {
let mut case = setup_with_options([("bar.py", "import sub.a")], |root_path, _project_path| {
Some(Options {
environment: Some(EnvironmentOptions {
venv_path: Some(SitePackages::Known(vec![root_path.join("site_packages")])),
extra_paths: Some(vec![root_path.join("site_packages")]),
..EnvironmentOptions::default()
}),
..Options::default()
Expand Down Expand Up @@ -835,7 +832,7 @@ fn add_search_path() -> anyhow::Result<()> {
// Register site-packages as a search path.
case.update_options(Options {
environment: Some(EnvironmentOptions {
venv_path: Some(SitePackages::Known(vec![site_packages.clone()])),
extra_paths: Some(vec![site_packages.clone()]),
..EnvironmentOptions::default()
}),
..Options::default()
Expand All @@ -858,7 +855,7 @@ fn remove_search_path() -> anyhow::Result<()> {
let mut case = setup_with_options([("bar.py", "import sub.a")], |root_path, _project_path| {
Some(Options {
environment: Some(EnvironmentOptions {
venv_path: Some(SitePackages::Known(vec![root_path.join("site_packages")])),
extra_paths: Some(vec![root_path.join("site_packages")]),
..EnvironmentOptions::default()
}),
..Options::default()
Expand Down Expand Up @@ -1381,9 +1378,8 @@ mod unix {
|_root, project| {
Some(Options {
environment: Some(EnvironmentOptions {
venv_path: Some(SitePackages::Known(vec![
project.join(".venv/lib/python3.12/site-packages")
])),
extra_paths: Some(vec![project.join(".venv/lib/python3.12/site-packages")]),
python_version: Some(PythonVersion::PY312),
..EnvironmentOptions::default()
}),
..Options::default()
Expand Down
6 changes: 4 additions & 2 deletions crates/red_knot_project/src/metadata/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ impl Options {
extra_paths: extra_paths.unwrap_or_default(),
src_roots,
typeshed,
site_packages: python.unwrap_or(SitePackages::Known(vec![])),
site_packages: python
.map(|venv_path| SitePackages::Derived { venv_path })
.unwrap_or(SitePackages::Known(vec![])),
}
}
}
Expand All @@ -98,7 +100,7 @@ pub struct EnvironmentOptions {

// TODO: Rename to python, see https://github.com/astral-sh/ruff/issues/15530
/// The path to the user's `site-packages` directory, where third-party packages from ``PyPI`` are installed.
pub venv_path: Option<SitePackages>,
pub venv_path: Option<SystemPathBuf>,
}

#[derive(Debug, Default, Clone, Eq, PartialEq, Combine, Serialize, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,11 @@ impl SearchPaths {
static_paths.push(stdlib_path);

let site_packages_paths = match site_packages_paths {
SitePackages::Derived { venv_path } => VirtualEnvironment::new(venv_path, system)
.and_then(|venv| venv.site_packages_directories(system))?,
SitePackages::Derived { venv_path } => {
// TODO: Warn if venv python version isn't compatible.
VirtualEnvironment::new(venv_path, system)
.and_then(|venv| venv.site_packages_directories(system))?
}
SitePackages::Known(paths) => paths
.iter()
.map(|path| canonicalize(path, system))
Expand Down
9 changes: 0 additions & 9 deletions crates/red_knot_python_semantic/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,3 @@ pub enum SitePackages {
/// Resolved site packages paths
Known(Vec<SystemPathBuf>),
}

impl SitePackages {
pub fn paths(&self) -> &[SystemPathBuf] {
match self {
SitePackages::Derived { venv_path } => std::slice::from_ref(venv_path),
SitePackages::Known(paths) => paths,
}
}
}

0 comments on commit 0c6339c

Please sign in to comment.