@@ -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,52 @@ 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
+ tracing:: debug!( "No virtual environment found" ) ;
161
+
162
+ SitePackagesPaths :: default ( )
163
+ } ;
137
164
138
165
let python_version = options_python_version
139
166
. or_else ( || {
140
- search_paths
141
- . try_resolve_installation_python_version ( )
142
- . map ( Cow :: into_owned)
167
+ python_environment
168
+ . as_ref ( ) ?
169
+ . python_version_from_metadata ( )
170
+ . cloned ( )
143
171
} )
172
+ . or_else ( || site_packages_paths. python_version_from_layout ( ) )
144
173
. unwrap_or_default ( ) ;
145
174
175
+ let search_paths = self . to_search_paths (
176
+ project_root,
177
+ project_name,
178
+ site_packages_paths,
179
+ system,
180
+ vendored,
181
+ ) ?;
182
+
146
183
tracing:: info!(
147
184
"Python version: Python {python_version}, platform: {python_platform}" ,
148
185
python_version = python_version. version
@@ -159,6 +196,7 @@ impl Options {
159
196
& self ,
160
197
project_root : & SystemPath ,
161
198
project_name : & str ,
199
+ site_packages_paths : SitePackagesPaths ,
162
200
system : & dyn System ,
163
201
vendored : & VendoredFileSystem ,
164
202
) -> Result < SearchPaths , SearchPathValidationError > {
@@ -230,23 +268,7 @@ impl Options {
230
268
. typeshed
231
269
. as_ref ( )
232
270
. 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 ( ) ) ) ,
271
+ site_packages_paths : site_packages_paths. into_vec ( ) ,
250
272
} ;
251
273
252
274
settings. to_search_paths ( system, vendored)
0 commit comments