Fix python resolving

This commit is contained in:
Shunsuke Shibayama 2022-12-14 11:24:24 +09:00
parent 3d69353bf6
commit 481761e50b
2 changed files with 33 additions and 14 deletions

View file

@ -167,6 +167,23 @@ impl Input {
dir.canonicalize() dir.canonicalize()
}) })
} }
pub fn local_py_resolve(&self, path: &Path) -> Result<PathBuf, std::io::Error> {
let mut dir = if let Self::File(mut path) = self.clone() {
path.pop();
path
} else {
PathBuf::new()
};
dir.push(path);
dir.set_extension("py");
dir.canonicalize().or_else(|_| {
dir.pop();
dir.push(path);
dir.push("__init__.py"); // {path}/__init__.er
dir.canonicalize()
})
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -1293,12 +1293,13 @@ impl Context {
Ok(path) Ok(path)
} }
None => { None => {
if let Ok(path) = self.cfg.input.local_py_resolve(Path::new(&__name__[..])) {
// pylyzer is a static analysis tool for Python. // pylyzer is a static analysis tool for Python.
// It can convert a Python script to an Erg AST for code analysis. // It can convert a Python script to an Erg AST for code analysis.
// There is also an option to output the analysis result as `d.er`. Use this if the system have pylyzer installed. // There is also an option to output the analysis result as `d.er`. Use this if the system have pylyzer installed.
match Command::new("pylyzer") match Command::new("pylyzer")
.arg("--dump-decl") .arg("--dump-decl")
.arg(format!("{__name__}.py")) .arg(path.to_str().unwrap())
.output() .output()
{ {
Ok(out) if out.status.success() => { Ok(out) if out.status.success() => {
@ -1310,6 +1311,7 @@ impl Context {
} }
_ => {} _ => {}
} }
}
let err = TyCheckError::import_error( let err = TyCheckError::import_error(
self.cfg.input.clone(), self.cfg.input.clone(),
line!() as usize, line!() as usize,