@@ -30,9 +30,9 @@ use thiserror::Error;
3030use ty_combine:: Combine ;
3131use ty_python_semantic:: lint:: { Level , LintSource , RuleSelection } ;
3232use ty_python_semantic:: {
33- ProgramSettings , PythonEnvironment , PythonPlatform , PythonVersionFileSource ,
34- PythonVersionSource , PythonVersionWithSource , SearchPathSettings , SearchPathValidationError ,
35- SearchPaths , SitePackagesPaths , SysPrefixPathOrigin ,
33+ MisconfigurationMode , ProgramSettings , PythonEnvironment , PythonPlatform ,
34+ PythonVersionFileSource , PythonVersionSource , PythonVersionWithSource , SearchPathSettings ,
35+ SearchPathValidationError , SearchPaths , SitePackagesPaths , SysPrefixPathOrigin ,
3636} ;
3737use ty_static:: EnvVars ;
3838
@@ -117,6 +117,7 @@ impl Options {
117117 project_name : & str ,
118118 system : & dyn System ,
119119 vendored : & VendoredFileSystem ,
120+ misconfiguration_mode : MisconfigurationMode ,
120121 ) -> anyhow:: Result < ProgramSettings > {
121122 let environment = self . environment . or_default ( ) ;
122123
@@ -154,14 +155,25 @@ impl Options {
154155 ValueSource :: Editor => SysPrefixPathOrigin :: Editor ,
155156 } ;
156157
157- Some ( PythonEnvironment :: new (
158- python_path. absolute ( project_root, system) ,
159- origin,
160- system,
161- ) ?)
158+ PythonEnvironment :: new ( python_path. absolute ( project_root, system) , origin, system)
159+ . map_err ( anyhow:: Error :: from)
160+ . map ( Some )
162161 } else {
163162 PythonEnvironment :: discover ( project_root, system)
164- . context ( "Failed to discover local Python environment" ) ?
163+ . context ( "Failed to discover local Python environment" )
164+ } ;
165+
166+ // If in safe-mode, fallback to None if this fails instead of erroring.
167+ let python_environment = match python_environment {
168+ Ok ( python_environment) => python_environment,
169+ Err ( err) => {
170+ if misconfiguration_mode == MisconfigurationMode :: UseDefault {
171+ tracing:: debug!( "Default settings failed to discover local Python environment" ) ;
172+ None
173+ } else {
174+ return Err ( err) ;
175+ }
176+ }
165177 } ;
166178
167179 let self_site_packages = self_environment_search_paths (
@@ -174,11 +186,23 @@ impl Options {
174186 . unwrap_or_default ( ) ;
175187
176188 let site_packages_paths = if let Some ( python_environment) = python_environment. as_ref ( ) {
177- self_site_packages. concatenate (
178- python_environment
179- . site_packages_paths ( system)
180- . context ( "Failed to discover the site-packages directory" ) ?,
181- )
189+ let site_packages_paths = python_environment
190+ . site_packages_paths ( system)
191+ . context ( "Failed to discover the site-packages directory" ) ;
192+ let site_packages_paths = match site_packages_paths {
193+ Ok ( paths) => paths,
194+ Err ( err) => {
195+ if misconfiguration_mode == MisconfigurationMode :: UseDefault {
196+ tracing:: debug!(
197+ "Default settings failed to discover site-packages directory"
198+ ) ;
199+ SitePackagesPaths :: default ( )
200+ } else {
201+ return Err ( err) ;
202+ }
203+ }
204+ } ;
205+ self_site_packages. concatenate ( site_packages_paths)
182206 } else {
183207 tracing:: debug!( "No virtual environment found" ) ;
184208 self_site_packages
@@ -201,13 +225,15 @@ impl Options {
201225 . or_else ( || site_packages_paths. python_version_from_layout ( ) )
202226 . unwrap_or_default ( ) ;
203227
228+ // Safe mode is handled inside this function, so we just assume this can't fail
204229 let search_paths = self . to_search_paths (
205230 project_root,
206231 project_name,
207232 site_packages_paths,
208233 real_stdlib_path,
209234 system,
210235 vendored,
236+ misconfiguration_mode,
211237 ) ?;
212238
213239 tracing:: info!(
@@ -222,6 +248,7 @@ impl Options {
222248 } )
223249 }
224250
251+ #[ expect( clippy:: too_many_arguments) ]
225252 fn to_search_paths (
226253 & self ,
227254 project_root : & SystemPath ,
@@ -230,6 +257,7 @@ impl Options {
230257 real_stdlib_path : Option < SystemPathBuf > ,
231258 system : & dyn System ,
232259 vendored : & VendoredFileSystem ,
260+ misconfiguration_mode : MisconfigurationMode ,
233261 ) -> Result < SearchPaths , SearchPathValidationError > {
234262 let environment = self . environment . or_default ( ) ;
235263 let src = self . src . or_default ( ) ;
@@ -344,6 +372,7 @@ impl Options {
344372 . map ( |path| path. absolute ( project_root, system) ) ,
345373 site_packages_paths : site_packages_paths. into_vec ( ) ,
346374 real_stdlib_path,
375+ misconfiguration_mode,
347376 } ;
348377
349378 settings. to_search_paths ( system, vendored)
0 commit comments