vendored_typeshed_versions should use db.vendored (#13434)

This commit is contained in:
Micha Reiser 2024-09-21 16:35:06 +02:00 committed by GitHub
parent 3018303c87
commit 8921fbb54c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 49 deletions

View file

@ -1,20 +1,21 @@
use rustc_hash::{FxBuildHasher, FxHashSet};
use std::borrow::Cow; use std::borrow::Cow;
use std::iter::FusedIterator; use std::iter::FusedIterator;
use std::ops::Deref;
use rustc_hash::{FxBuildHasher, FxHashSet};
use ruff_db::files::{File, FilePath, FileRootKind}; use ruff_db::files::{File, FilePath, FileRootKind};
use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf}; use ruff_db::system::{DirectoryEntry, System, SystemPath, SystemPathBuf};
use ruff_db::vendored::{VendoredFileSystem, VendoredPath}; use ruff_db::vendored::{VendoredFileSystem, VendoredPath};
use super::module::{Module, ModuleKind};
use super::path::{ModulePath, SearchPath, SearchPathValidationError};
use crate::db::Db; use crate::db::Db;
use crate::module_name::ModuleName; use crate::module_name::ModuleName;
use crate::module_resolver::typeshed::{vendored_typeshed_versions, TypeshedVersions}; use crate::module_resolver::typeshed::{vendored_typeshed_versions, TypeshedVersions};
use crate::site_packages::VirtualEnvironment; use crate::site_packages::VirtualEnvironment;
use crate::{Program, PythonVersion, SearchPathSettings, SitePackages}; use crate::{Program, PythonVersion, SearchPathSettings, SitePackages};
use super::module::{Module, ModuleKind};
use super::path::{ModulePath, SearchPath, SearchPathValidationError};
/// Resolves a module name to a module. /// Resolves a module name to a module.
pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option<Module> { pub fn resolve_module(db: &dyn Db, module_name: ModuleName) -> Option<Module> {
let interned_name = ModuleNameIngredient::new(db, module_name); let interned_name = ModuleNameIngredient::new(db, module_name);
@ -136,7 +137,7 @@ pub(crate) struct SearchPaths {
/// for the first `site-packages` path /// for the first `site-packages` path
site_packages: Vec<SearchPath>, site_packages: Vec<SearchPath>,
typeshed_versions: ResolvedTypeshedVersions, typeshed_versions: TypeshedVersions,
} }
impl SearchPaths { impl SearchPaths {
@ -202,11 +203,11 @@ impl SearchPaths {
let search_path = SearchPath::custom_stdlib(db, &custom_typeshed)?; let search_path = SearchPath::custom_stdlib(db, &custom_typeshed)?;
(ResolvedTypeshedVersions::Custom(parsed), search_path) (parsed, search_path)
} else { } else {
tracing::debug!("Using vendored stdlib"); tracing::debug!("Using vendored stdlib");
( (
ResolvedTypeshedVersions::Vendored(vendored_typeshed_versions()), vendored_typeshed_versions(db),
SearchPath::vendored_stdlib(), SearchPath::vendored_stdlib(),
) )
}; };
@ -279,23 +280,6 @@ impl SearchPaths {
} }
} }
#[derive(Debug, PartialEq, Eq)]
enum ResolvedTypeshedVersions {
Vendored(&'static TypeshedVersions),
Custom(TypeshedVersions),
}
impl Deref for ResolvedTypeshedVersions {
type Target = TypeshedVersions;
fn deref(&self) -> &Self::Target {
match self {
ResolvedTypeshedVersions::Vendored(versions) => versions,
ResolvedTypeshedVersions::Custom(versions) => versions,
}
}
}
/// Collect all dynamic search paths. For each `site-packages` path: /// Collect all dynamic search paths. For each `site-packages` path:
/// - Collect that `site-packages` path /// - Collect that `site-packages` path
/// - Collect any search paths listed in `.pth` files in that `site-packages` directory /// - Collect any search paths listed in `.pth` files in that `site-packages` directory

View file

@ -4,25 +4,19 @@ use std::num::{NonZeroU16, NonZeroUsize};
use std::ops::{RangeFrom, RangeInclusive}; use std::ops::{RangeFrom, RangeInclusive};
use std::str::FromStr; use std::str::FromStr;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use super::vendored::vendored_typeshed_stubs;
use crate::db::Db; use crate::db::Db;
use crate::module_name::ModuleName; use crate::module_name::ModuleName;
use crate::{Program, PythonVersion}; use crate::{Program, PythonVersion};
static VENDORED_VERSIONS: Lazy<TypeshedVersions> = Lazy::new(|| { pub(in crate::module_resolver) fn vendored_typeshed_versions(db: &dyn Db) -> TypeshedVersions {
TypeshedVersions::from_str( TypeshedVersions::from_str(
&vendored_typeshed_stubs() &db.vendored()
.read_to_string("stdlib/VERSIONS") .read_to_string("stdlib/VERSIONS")
.unwrap(), .expect("The vendored typeshed stubs should contain a VERSIONS file"),
) )
.unwrap() .expect("The VERSIONS file in the vendored typeshed stubs should be well-formed")
});
pub(crate) fn vendored_typeshed_versions() -> &'static TypeshedVersions {
&VENDORED_VERSIONS
} }
pub(crate) fn typeshed_versions(db: &dyn Db) -> &TypeshedVersions { pub(crate) fn typeshed_versions(db: &dyn Db) -> &TypeshedVersions {
@ -332,6 +326,8 @@ mod tests {
use insta::assert_snapshot; use insta::assert_snapshot;
use crate::db::tests::TestDb;
use super::*; use super::*;
const TYPESHED_STDLIB_DIR: &str = "stdlib"; const TYPESHED_STDLIB_DIR: &str = "stdlib";
@ -353,12 +349,9 @@ mod tests {
#[test] #[test]
fn can_parse_vendored_versions_file() { fn can_parse_vendored_versions_file() {
let versions_data = include_str!(concat!( let db = TestDb::new();
env!("CARGO_MANIFEST_DIR"),
"/vendor/typeshed/stdlib/VERSIONS"
));
let versions = TypeshedVersions::from_str(versions_data).unwrap(); let versions = vendored_typeshed_versions(&db);
assert!(versions.len() > 100); assert!(versions.len() > 100);
assert!(versions.len() < 1000); assert!(versions.len() < 1000);
@ -395,9 +388,9 @@ mod tests {
#[test] #[test]
fn typeshed_versions_consistent_with_vendored_stubs() { fn typeshed_versions_consistent_with_vendored_stubs() {
const VERSIONS_DATA: &str = include_str!("../../../vendor/typeshed/stdlib/VERSIONS"); let db = TestDb::new();
let vendored_typeshed_versions = vendored_typeshed_versions(&db);
let vendored_typeshed_dir = Path::new("vendor/typeshed").canonicalize().unwrap(); let vendored_typeshed_dir = Path::new("vendor/typeshed").canonicalize().unwrap();
let vendored_typeshed_versions = TypeshedVersions::from_str(VERSIONS_DATA).unwrap();
let mut empty_iterator = true; let mut empty_iterator = true;

View file

@ -1,5 +1,7 @@
use anyhow::Result; use anyhow::Result;
use red_knot_python_semantic::{Db, Program, ProgramSettings, PythonVersion, SearchPathSettings}; use red_knot_python_semantic::{
vendored_typeshed_stubs, Db, Program, ProgramSettings, PythonVersion, SearchPathSettings,
};
use ruff_db::files::{File, Files}; use ruff_db::files::{File, Files};
use ruff_db::system::{OsSystem, System, SystemPathBuf}; use ruff_db::system::{OsSystem, System, SystemPathBuf};
use ruff_db::vendored::VendoredFileSystem; use ruff_db::vendored::VendoredFileSystem;
@ -11,7 +13,6 @@ pub struct ModuleDb {
storage: salsa::Storage<Self>, storage: salsa::Storage<Self>,
files: Files, files: Files,
system: OsSystem, system: OsSystem,
vendored: VendoredFileSystem,
} }
impl ModuleDb { impl ModuleDb {
@ -26,12 +27,10 @@ impl ModuleDb {
.next() .next()
.ok_or_else(|| anyhow::anyhow!("No source roots provided"))?; .ok_or_else(|| anyhow::anyhow!("No source roots provided"))?;
let mut search_paths = SearchPathSettings::new(src_root.to_path_buf()); let mut search_paths = SearchPathSettings::new(src_root);
// Add the remaining source roots as extra paths. // Add the remaining source roots as extra paths.
for src_root in src_roots { search_paths.extra_paths.extend(src_roots);
search_paths.extra_paths.push(src_root.to_path_buf());
}
search_paths search_paths
}; };
@ -54,7 +53,6 @@ impl ModuleDb {
Self { Self {
storage: self.storage.clone(), storage: self.storage.clone(),
system: self.system.clone(), system: self.system.clone(),
vendored: self.vendored.clone(),
files: self.files.snapshot(), files: self.files.snapshot(),
} }
} }
@ -72,7 +70,7 @@ impl Upcast<dyn SourceDb> for ModuleDb {
#[salsa::db] #[salsa::db]
impl SourceDb for ModuleDb { impl SourceDb for ModuleDb {
fn vendored(&self) -> &VendoredFileSystem { fn vendored(&self) -> &VendoredFileSystem {
&self.vendored vendored_typeshed_stubs()
} }
fn system(&self) -> &dyn System { fn system(&self) -> &dyn System {