ty_module_resolver/settings.rs
1//! Search path configuration settings.
2
3use ruff_db::system::{System, SystemPathBuf};
4use ruff_db::vendored::VendoredFileSystem;
5
6use crate::path::SearchPathError;
7use crate::resolve::SearchPaths;
8use crate::strategy::MisconfigurationStrategy;
9use crate::typeshed::TypeshedVersionsParseError;
10
11/// Configures the search paths for module resolution.
12#[derive(Eq, PartialEq, Debug, Clone)]
13pub struct SearchPathSettings {
14 /// List of user-provided paths that should take first priority in the module resolution.
15 /// Examples in other type checkers are mypy's MYPYPATH environment variable,
16 /// or pyright's stubPath configuration setting.
17 pub extra_paths: Vec<SystemPathBuf>,
18
19 /// The root of the project, used for finding first-party modules.
20 pub src_roots: Vec<SystemPathBuf>,
21
22 /// Optional path to a "custom typeshed" directory on disk for us to use for standard-library types.
23 /// If this is not provided, we will fallback to our vendored typeshed stubs for the stdlib,
24 /// bundled as a zip file in the binary
25 pub custom_typeshed: Option<SystemPathBuf>,
26
27 /// List of site packages paths to use.
28 pub site_packages_paths: Vec<SystemPathBuf>,
29
30 /// Option path to the real stdlib on the system, and not some instance of typeshed.
31 ///
32 /// We should ideally only ever use this for things like goto-definition,
33 /// where typeshed isn't the right answer.
34 pub real_stdlib_path: Option<SystemPathBuf>,
35}
36
37impl SearchPathSettings {
38 pub fn new(src_roots: Vec<SystemPathBuf>) -> Self {
39 Self {
40 src_roots,
41 ..SearchPathSettings::empty()
42 }
43 }
44
45 pub fn empty() -> Self {
46 SearchPathSettings {
47 src_roots: vec![],
48 extra_paths: vec![],
49 custom_typeshed: None,
50 site_packages_paths: vec![],
51 real_stdlib_path: None,
52 }
53 }
54
55 pub fn to_search_paths<Strategy: MisconfigurationStrategy>(
56 &self,
57 system: &dyn System,
58 vendored: &VendoredFileSystem,
59 strategy: &Strategy,
60 ) -> Result<SearchPaths, Strategy::Error<SearchPathSettingsError>> {
61 SearchPaths::from_settings(self, system, vendored, strategy)
62 }
63}
64
65/// Enumeration describing the various ways in which validation of the search paths options might fail.
66///
67/// If validation fails for a search path derived from the user settings,
68/// a message must be displayed to the user,
69/// as type checking cannot be done reliably in these circumstances.
70#[derive(Debug, thiserror::Error)]
71pub enum SearchPathSettingsError {
72 #[error(transparent)]
73 InvalidSearchPath(#[from] SearchPathError),
74
75 /// The typeshed path provided by the user is a directory,
76 /// but `stdlib/VERSIONS` could not be read.
77 /// (This is only relevant for stdlib search paths.)
78 #[error("Failed to read the custom typeshed versions file '{path}'")]
79 FailedToReadVersionsFile {
80 path: SystemPathBuf,
81 #[source]
82 error: std::io::Error,
83 },
84
85 /// The path provided by the user is a directory,
86 /// and a `stdlib/VERSIONS` file exists, but it fails to parse.
87 /// (This is only relevant for stdlib search paths.)
88 #[error(transparent)]
89 VersionsParseError(#[from] TypeshedVersionsParseError),
90}