fix: path resolution bug

This commit is contained in:
Shunsuke Shibayama 2023-06-29 14:54:15 +09:00
parent 1ec95a6ff9
commit bfc403af09
4 changed files with 21 additions and 13 deletions

View file

@ -9,7 +9,7 @@ use crate::consts::{ERG_MODE, EXPERIMENTAL_MODE};
use crate::env::{
erg_path, erg_py_external_lib_path, erg_pystd_path, erg_std_path, python_site_packages,
};
use crate::pathutil::add_postfix_foreach;
use crate::pathutil::{add_postfix_foreach, remove_postfix};
use crate::python_util::get_sys_path;
use crate::random::random;
use crate::stdin::GLOBAL_STDIN;
@ -579,15 +579,19 @@ impl Input {
if let Ok(path) = self.resolve_local_decl(self.dir(), path) {
return Some(path);
}
// e.g. root: lib/external/pandas.d, path: pandas/core/frame
if let Some(mut dir) = self.project_root() {
let mut path = path.iter().skip(1).collect::<PathBuf>();
if path == Path::new("") {
path.extend(dir.iter().last());
dir.pop();
}
if let Ok(path) = self.resolve_local_decl(dir, &path) {
return Some(path);
// e.g.
// root: lib/external/pandas.d, path: pandas/core/frame
// -> lib/external/pandas.d/core/frame
// root: lib/external/pandas.d, path: pandas
// -> lib/external/pandas.d
// root: lib/external/pandas.d, path: contextlib
// -> NO
if let Some((root, first)) = self.project_root().zip(path.components().next()) {
if root.ends_with(first) || remove_postfix(root.clone(), ".d").ends_with(first) {
let path = path.iter().skip(1).collect::<PathBuf>();
if let Ok(path) = self.resolve_local_decl(root, &path) {
return Some(path);
}
}
}
let py_roots = [erg_pystd_path, erg_py_external_lib_path];

View file

@ -128,6 +128,9 @@ pub fn remove_postfix_foreach<P: AsRef<Path>>(path: P, extension: &str) -> PathB
/// let path = PathBuf::from("erg.d.er");
/// let path = remove_postfix(path, ".er");
/// assert_eq!(path, PathBuf::from("erg.d"));
/// let path = PathBuf::from("erg.d/foo.d/bar.d");
/// let path = remove_postfix(path, ".d");
/// assert_eq!(path, PathBuf::from("erg.d/foo.d/bar"));
pub fn remove_postfix<P: AsRef<Path>>(path: P, extension: &str) -> PathBuf {
let string = path.as_ref().to_string_lossy();
PathBuf::from(string.trim_end_matches(extension))

View file

@ -1884,7 +1884,7 @@ impl Context {
let mod_ctx = ModuleContext::new(self.clone(), dict! {});
let mut builder = HIRBuilder::new_with_ctx(mod_ctx);
let src = Input::file(path.to_path_buf()).read();
let mode = if path.ends_with(".d.er") {
let mode = if path.to_string_lossy().ends_with(".d.er") {
"declare"
} else {
"exec"
@ -1895,7 +1895,7 @@ impl Context {
Err(art) => art.object,
};
let ctx = builder.pop_mod_ctx().unwrap();
let cache = if path.ends_with("d.er") {
let cache = if path.to_string_lossy().ends_with("d.er") {
&self.shared().py_mod_cache
} else {
&self.shared().mod_cache

View file

@ -22,6 +22,7 @@ pub struct ModuleGraph(Graph<PathBuf, ()>);
impl fmt::Display for ModuleGraph {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "ModuleGraph {{")?;
for node in self.0.iter() {
writeln!(f, "{} depends on {{", node.id.display())?;
for dep in node.depends_on.iter() {
@ -29,7 +30,7 @@ impl fmt::Display for ModuleGraph {
}
writeln!(f, "}}, ")?;
}
Ok(())
write!(f, "}}")
}
}