@@ -7,6 +7,7 @@ use super::settings::{Override, Settings, TerminalSettings};
7
7
use crate :: metadata:: value:: {
8
8
RangedValue , RelativeGlobPattern , RelativePathBuf , ValueSource , ValueSourceGuard ,
9
9
} ;
10
+ use anyhow:: Context ;
10
11
use ordermap:: OrderMap ;
11
12
use ruff_db:: RustDoc ;
12
13
use ruff_db:: diagnostic:: {
@@ -29,9 +30,9 @@ use std::sync::Arc;
29
30
use thiserror:: Error ;
30
31
use ty_python_semantic:: lint:: { GetLintError , Level , LintSource , RuleSelection } ;
31
32
use ty_python_semantic:: {
32
- ProgramSettings , PythonEnvironmentPath , PythonPlatform , PythonVersionFileSource ,
33
+ ProgramSettings , PythonEnvironment , PythonPlatform , PythonVersionFileSource ,
33
34
PythonVersionSource , PythonVersionWithSource , SearchPathSettings , SearchPathValidationError ,
34
- SearchPaths , SysPrefixPathOrigin ,
35
+ SearchPaths , SitePackagesPaths , SysPrefixPathOrigin ,
35
36
} ;
36
37
37
38
#[ derive(
@@ -133,16 +134,45 @@ impl Options {
133
134
default
134
135
} ) ;
135
136
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
+ } ;
137
162
138
163
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 ( ) )
144
166
. unwrap_or_default ( ) ;
145
167
168
+ let search_paths = self . to_search_paths (
169
+ project_root,
170
+ project_name,
171
+ site_packages_paths,
172
+ system,
173
+ vendored,
174
+ ) ?;
175
+
146
176
tracing:: info!(
147
177
"Python version: Python {python_version}, platform: {python_platform}" ,
148
178
python_version = python_version. version
@@ -159,6 +189,7 @@ impl Options {
159
189
& self ,
160
190
project_root : & SystemPath ,
161
191
project_name : & str ,
192
+ site_packages_paths : SitePackagesPaths ,
162
193
system : & dyn System ,
163
194
vendored : & VendoredFileSystem ,
164
195
) -> Result < SearchPaths , SearchPathValidationError > {
@@ -230,23 +261,7 @@ impl Options {
230
261
. typeshed
231
262
. as_ref ( )
232
263
. 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 ( ) ,
250
265
} ;
251
266
252
267
settings. to_search_paths ( system, vendored)
0 commit comments