Skip to content

Commit 464dce8

Browse files
committed
[ty] Resolve python environment in Options::to_program_settings
1 parent 7638729 commit 464dce8

File tree

11 files changed

+202
-277
lines changed

11 files changed

+202
-277
lines changed

crates/ruff/tests/analyze_graph.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,6 @@ fn venv() -> Result<()> {
566566
567567
----- stderr -----
568568
ruff failed
569-
Cause: Invalid search path settings
570-
Cause: Failed to discover the site-packages directory
571569
Cause: Invalid `--python` argument `none`: does not point to a Python executable or a directory on disk
572570
Cause: No such file or directory (os error 2)
573571
");

crates/ruff_graph/src/db.rs

Lines changed: 9 additions & 5 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, PythonEnvironmentPath, PythonPlatform, PythonVersionSource,
12+
Db, Program, ProgramSettings, PythonEnvironment, PythonPlatform, PythonVersionSource,
1313
PythonVersionWithSource, SearchPathSettings, SysPrefixPathOrigin, default_lint_registry,
1414
};
1515

@@ -35,13 +35,17 @@ impl ModuleDb {
3535
python_version: PythonVersion,
3636
venv_path: Option<SystemPathBuf>,
3737
) -> Result<Self> {
38+
let db = Self::default();
3839
let mut search_paths = SearchPathSettings::new(src_roots);
39-
// TODO: Consider setting `PythonPath::Auto` if no venv_path is provided.
40+
// TODO: Consider calling `PythonEnvironment::discover` if the `venv_path` is not provided.
4041
if let Some(venv_path) = venv_path {
41-
search_paths.python_environment =
42-
PythonEnvironmentPath::explicit(venv_path, SysPrefixPathOrigin::PythonCliFlag);
42+
let environment =
43+
PythonEnvironment::new(venv_path, SysPrefixPathOrigin::PythonCliFlag, db.system())?;
44+
search_paths.site_packages_paths = environment
45+
.site_packages_paths(db.system())
46+
.context("Failed to discover the site-packages directory")?
47+
.into_vec();
4348
}
44-
let db = Self::default();
4549
let search_paths = search_paths
4650
.to_search_paths(db.system(), db.vendored())
4751
.context("Invalid search path settings")?;

crates/ty/tests/cli/python_environment.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,6 @@ fn python_cli_argument_virtual_environment() -> anyhow::Result<()> {
590590
----- stderr -----
591591
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
592592
ty failed
593-
Cause: Failed to discover the site-packages directory
594593
Cause: Invalid `--python` argument `<temp_dir>/my-venv/foo/some_other_file.txt`: does not point to a Python executable or a directory on disk
595594
");
596595

@@ -603,7 +602,6 @@ fn python_cli_argument_virtual_environment() -> anyhow::Result<()> {
603602
----- stderr -----
604603
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
605604
ty failed
606-
Cause: Failed to discover the site-packages directory
607605
Cause: Invalid `--python` argument `<temp_dir>/not-a-directory-or-executable`: does not point to a Python executable or a directory on disk
608606
Cause: No such file or directory (os error 2)
609607
");
@@ -686,7 +684,6 @@ fn config_file_broken_python_setting() -> anyhow::Result<()> {
686684
----- stderr -----
687685
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
688686
ty failed
689-
Cause: Failed to discover the site-packages directory
690687
Cause: Invalid `environment.python` setting
691688
692689
--> Invalid setting in configuration file `<temp_dir>/pyproject.toml`

crates/ty_project/src/combine.rs

Lines changed: 1 addition & 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::{PythonEnvironmentPath, PythonPlatform};
6+
use ty_python_semantic::PythonPlatform;
77

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

142142
impl_noop_combine!(SystemPathBuf);
143143
impl_noop_combine!(PythonPlatform);
144-
impl_noop_combine!(PythonEnvironmentPath);
145144
impl_noop_combine!(PythonVersion);
146145

147146
// std types

crates/ty_project/src/metadata/options.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::settings::{Override, Settings, TerminalSettings};
77
use crate::metadata::value::{
88
RangedValue, RelativeGlobPattern, RelativePathBuf, ValueSource, ValueSourceGuard,
99
};
10+
use anyhow::Context;
1011
use ordermap::OrderMap;
1112
use ruff_db::RustDoc;
1213
use ruff_db::diagnostic::{
@@ -29,9 +30,9 @@ use std::sync::Arc;
2930
use thiserror::Error;
3031
use ty_python_semantic::lint::{GetLintError, Level, LintSource, RuleSelection};
3132
use ty_python_semantic::{
32-
ProgramSettings, PythonEnvironmentPath, PythonPlatform, PythonVersionFileSource,
33+
ProgramSettings, PythonEnvironment, PythonPlatform, PythonVersionFileSource,
3334
PythonVersionSource, PythonVersionWithSource, SearchPathSettings, SearchPathValidationError,
34-
SearchPaths, SysPrefixPathOrigin,
35+
SearchPaths, SitePackagesPaths, SysPrefixPathOrigin,
3536
};
3637

3738
#[derive(
@@ -133,16 +134,45 @@ impl Options {
133134
default
134135
});
135136

136-
let search_paths = self.to_search_paths(project_root, project_name, system, vendored)?;
137+
let python_environment = if let Some(python_path) = environment.python.as_ref() {
138+
let origin = match python_path.source() {
139+
ValueSource::Cli => SysPrefixPathOrigin::PythonCliFlag,
140+
ValueSource::File(path) => {
141+
SysPrefixPathOrigin::ConfigFileSetting(path.clone(), python_path.range())
142+
}
143+
};
144+
145+
Some(PythonEnvironment::new(
146+
python_path.absolute(project_root, system),
147+
origin,
148+
system,
149+
)?)
150+
} else {
151+
PythonEnvironment::discover(project_root, system)
152+
.context("Failed to discover local Python environment")?
153+
};
154+
155+
let site_packages_paths = if let Some(python_environment) = python_environment.as_ref() {
156+
python_environment
157+
.site_packages_paths(system)
158+
.context("Failed to discover the site-packages directory")?
159+
} else {
160+
SitePackagesPaths::default()
161+
};
137162

138163
let python_version = options_python_version
139-
.or_else(|| {
140-
search_paths
141-
.try_resolve_installation_python_version()
142-
.map(Cow::into_owned)
143-
})
164+
.or_else(|| python_environment.as_ref()?.python_version().cloned())
165+
.or_else(|| site_packages_paths.try_resolve_installation_python_version())
144166
.unwrap_or_default();
145167

168+
let search_paths = self.to_search_paths(
169+
project_root,
170+
project_name,
171+
site_packages_paths,
172+
system,
173+
vendored,
174+
)?;
175+
146176
tracing::info!(
147177
"Python version: Python {python_version}, platform: {python_platform}",
148178
python_version = python_version.version
@@ -159,6 +189,7 @@ impl Options {
159189
&self,
160190
project_root: &SystemPath,
161191
project_name: &str,
192+
site_packages_paths: SitePackagesPaths,
162193
system: &dyn System,
163194
vendored: &VendoredFileSystem,
164195
) -> Result<SearchPaths, SearchPathValidationError> {
@@ -230,23 +261,7 @@ impl Options {
230261
.typeshed
231262
.as_ref()
232263
.map(|path| path.absolute(project_root, system)),
233-
python_environment: environment
234-
.python
235-
.as_ref()
236-
.map(|python_path| {
237-
let origin = match python_path.source() {
238-
ValueSource::Cli => SysPrefixPathOrigin::PythonCliFlag,
239-
ValueSource::File(path) => SysPrefixPathOrigin::ConfigFileSetting(
240-
path.clone(),
241-
python_path.range(),
242-
),
243-
};
244-
PythonEnvironmentPath::explicit(
245-
python_path.absolute(project_root, system),
246-
origin,
247-
)
248-
})
249-
.unwrap_or_else(|| PythonEnvironmentPath::Discover(project_root.to_path_buf())),
264+
site_packages_paths: site_packages_paths.into_vec(),
250265
};
251266

252267
settings.to_search_paths(system, vendored)

crates/ty_python_semantic/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ pub use module_resolver::{
1111
system_module_search_paths,
1212
};
1313
pub use program::{
14-
Program, ProgramSettings, PythonEnvironmentPath, PythonVersionFileSource, PythonVersionSource,
14+
Program, ProgramSettings, PythonVersionFileSource, PythonVersionSource,
1515
PythonVersionWithSource, SearchPathSettings,
1616
};
1717
pub use python_platform::PythonPlatform;
1818
pub use semantic_model::{HasType, SemanticModel};
19-
pub use site_packages::SysPrefixPathOrigin;
19+
pub use site_packages::{PythonEnvironment, SitePackagesPaths, SysPrefixPathOrigin};
2020
pub use util::diagnostics::add_inferred_python_version_hint_to_diagnostic;
2121

2222
pub mod ast_node_ref;

0 commit comments

Comments
 (0)