chore: resolve decl package path

This commit is contained in:
Shunsuke Shibayama 2024-02-10 01:20:31 +09:00
parent 027009680a
commit 14bf431c57
5 changed files with 36 additions and 14 deletions

View file

@ -499,7 +499,7 @@ impl Input {
pub fn resolve_path(&self, path: &Path, cfg: &ErgConfig) -> Option<PathBuf> {
self.resolve_real_path(path, cfg)
.or_else(|| self.resolve_decl_path(path))
.or_else(|| self.resolve_decl_path(path, cfg))
}
/// resolution order:
@ -507,7 +507,7 @@ impl Input {
/// 2. `./{path/to}/__init__.er`
/// 3. `std/{path/to}.er`
/// 4. `std/{path/to}/__init__.er`
/// 5. `pkgs/{path/to}/lib.er`
/// 5. `pkgs/{path/to}/src/lib.er`
pub fn resolve_real_path(&self, path: &Path, cfg: &ErgConfig) -> Option<PathBuf> {
if let Ok(path) = self.resolve_local(path) {
Some(path)
@ -522,7 +522,7 @@ impl Input {
.canonicalize()
{
Some(normalize_path(path))
} else if let Some(pkg) = self.resolve_project_dep_path(path, cfg) {
} else if let Some(pkg) = self.resolve_project_dep_path(path, cfg, false) {
Some(normalize_path(pkg))
} else if path == Path::new("unsound") {
Some(PathBuf::from("unsound"))
@ -531,7 +531,12 @@ impl Input {
}
}
fn resolve_project_dep_path(&self, path: &Path, cfg: &ErgConfig) -> Option<PathBuf> {
fn resolve_project_dep_path(
&self,
path: &Path,
cfg: &ErgConfig,
decl: bool,
) -> Option<PathBuf> {
let name = format!("{}", path.display());
let pkg = cfg.packages.iter().find(|p| p.as_name == name)?;
let path = if let Some(path) = pkg.path {
@ -539,7 +544,12 @@ impl Input {
} else {
erg_pkgs_path().join(pkg.name).join(pkg.version)
};
Some(path.join("src").join("lib.er"))
let path = if decl {
path.join("src").join("lib.d.er")
} else {
path.join("src").join("lib.er")
};
path.canonicalize().ok()
}
/// resolution order:
@ -552,9 +562,10 @@ impl Input {
/// (and repeat for the project root)
/// 7. `std/{path/to}.d.er`
/// 8. `std/{path/to}/__init__.d.er`
/// 9. `site-packages/{path}/__pycache__/{to}.d.er`
/// 10. `site-packages/{path/to}/__pycache__/__init__.d.er`
pub fn resolve_decl_path(&self, path: &Path) -> Option<PathBuf> {
/// 9. `pkgs/{path/to}/src/lib.d.er`
/// 10. `site-packages/{path}/__pycache__/{to}.d.er`
/// 11. `site-packages/{path/to}/__pycache__/__init__.d.er`
pub fn resolve_decl_path(&self, path: &Path, cfg: &ErgConfig) -> Option<PathBuf> {
if let Ok(path) = self.resolve_local_decl(self.dir(), path) {
return Some(path);
}
@ -579,6 +590,9 @@ impl Input {
return Some(path);
}
}
if let Some(pkg) = self.resolve_project_dep_path(path, cfg, true) {
return Some(normalize_path(pkg));
}
for site_packages in python_site_packages() {
if let Some(path) = Self::resolve_site_pkgs_decl_path(site_packages, path) {
return Some(path);

View file

@ -13,7 +13,7 @@ use std::time::{Duration, SystemTime};
use erg_common::config::ErgMode;
use erg_common::config::ErgConfig;
use erg_common::consts::ELS;
use erg_common::consts::{DEBUG_MODE, ELS};
use erg_common::debug_power_assert;
use erg_common::dict::Dict;
use erg_common::env::is_std_decl_path;
@ -525,7 +525,11 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
.spawn()
.and_then(|mut child| child.wait())
{
if let Some(path) = self.cfg.input.resolve_decl_path(Path::new(&__name__[..])) {
if let Some(path) = self
.cfg
.input
.resolve_decl_path(Path::new(&__name__[..]), &self.cfg)
{
let size = metadata(&path).unwrap().len();
// if pylyzer crashed
if !status.success() && size == 0 {
@ -728,7 +732,7 @@ impl<ASTBuilder: ASTBuildable, HIRBuilder: Buildable>
// return;
} else if let Some(inliner) = self.inlines.get(path).cloned() {
self.build_deps_and_module(&inliner, graph);
} else {
} else if DEBUG_MODE {
todo!("{path} is not found in self.inlines and self.asts");
}
}

View file

@ -1445,7 +1445,7 @@ pub(crate) fn resolve_decl_path_func(
return Err(type_mismatch("Str", other, "Path"));
}
};
let Some(path) = ctx.cfg.input.resolve_decl_path(path) else {
let Some(path) = ctx.cfg.input.resolve_decl_path(path, &ctx.cfg) else {
return Err(ErrorCore::new(
vec![SubMessage::only_loc(Location::Unknown)],
format!("Path {} is not found", path.display()),

View file

@ -2262,7 +2262,11 @@ impl Context {
}
fn get_decl_path(&self, __name__: &Str, loc: &impl Locational) -> CompileResult<PathBuf> {
match self.cfg.input.resolve_decl_path(Path::new(&__name__[..])) {
match self
.cfg
.input
.resolve_decl_path(Path::new(&__name__[..]), &self.cfg)
{
Some(path) => {
if self.cfg.input.decl_file_is(&path) {
return Ok(path);

View file

@ -483,7 +483,7 @@ impl<'a> HIRLinker<'a> {
let mod_path = self
.cfg
.input
.resolve_decl_path(Path::new(&mod_name_str[..]))
.resolve_decl_path(Path::new(&mod_name_str[..]), self.cfg)
.unwrap();
if !mod_path
.canonicalize()