diff --git a/crates/erg_common/pathutil.rs b/crates/erg_common/pathutil.rs index ffef64033..2f4f3f0f4 100644 --- a/crates/erg_common/pathutil.rs +++ b/crates/erg_common/pathutil.rs @@ -66,6 +66,10 @@ impl NormalizedPathBuf { pub fn to_path_buf(&self) -> PathBuf { self.0.clone() } + + pub fn try_read(&self) -> std::io::Result { + std::fs::read_to_string(&self.0) + } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] diff --git a/crates/erg_compiler/build_package.rs b/crates/erg_compiler/build_package.rs index 35db9a98f..ff5b5ae72 100644 --- a/crates/erg_compiler/build_package.rs +++ b/crates/erg_compiler/build_package.rs @@ -701,7 +701,7 @@ impl None => return Ok(()), }; let from_path = NormalizedPathBuf::from(cfg.input.path()); - let mut import_cfg = cfg.inherit(import_path.clone()); + let import_cfg = cfg.inherit(import_path.clone()); let import_path = NormalizedPathBuf::from(import_path.clone()); self.shared.graph.add_node_if_none(&import_path); // If we import `foo/bar`, we also need to import `foo` @@ -714,7 +714,7 @@ impl let root_import_path = root_path.and_then(|path| cfg.input.resolve_path(path, cfg)); if let Some(root_import_path) = root_import_path.map(NormalizedPathBuf::from) { if project_entry_dir_of(&root_import_path) != project_entry_dir_of(&from_path) { - let mut root_import_cfg = cfg.inherit(root_import_path.to_path_buf()); + let root_import_cfg = cfg.inherit(root_import_path.to_path_buf()); self.shared.graph.add_node_if_none(&root_import_path); let _ = self .shared @@ -725,8 +725,7 @@ impl || self.asts.contains_key(&root_import_path) { // pass - } else if let Ok(mut ast) = self.parse(cfg, &mut root_import_cfg, &root_import_path) - { + } else if let Ok(mut ast) = self.parse(&root_import_path) { let _ = self.resolve(&mut ast, &root_import_cfg); let prev = self.asts.insert(root_import_path, (__name__.clone(), ast)); debug_assert!(prev.is_none()); @@ -747,7 +746,7 @@ impl { return Ok(()); } - let Ok(mut ast) = self.parse(cfg, &mut import_cfg, &import_path) else { + let Ok(mut ast) = self.parse(&import_path) else { return Ok(()); }; if let Err(mut errs) = self.resolve(&mut ast, &import_cfg) { @@ -770,15 +769,11 @@ impl Ok(()) } - fn parse( - &mut self, - cfg: &ErgConfig, - import_cfg: &mut ErgConfig, - import_path: &NormalizedPathBuf, - ) -> Result { - let Ok(src) = import_cfg.input.try_read() else { + fn parse(&mut self, import_path: &NormalizedPathBuf) -> Result { + let Ok(src) = import_path.try_read() else { return Err(()); }; + let cfg = self.cfg.inherit(import_path.to_path_buf()); let result = if import_path.extension() == Some(OsStr::new("er")) { let mut ast_builder = DefaultASTBuilder::new(cfg.copy()); ast_builder.build_ast(src)