diff --git a/compiler/src/constraints/import/import_symbol.rs b/compiler/src/constraints/import/import_symbol.rs index 4011df354c..da457f59c0 100644 --- a/compiler/src/constraints/import/import_symbol.rs +++ b/compiler/src/constraints/import/import_symbol.rs @@ -10,6 +10,8 @@ use leo_types::{ImportSymbol, Program, Span}; use snarkos_models::curves::{Field, PrimeField}; use std::fs::DirEntry; +static LIBRARY_FILE: &str = "src/lib.leo"; + fn parse_import_file(entry: &DirEntry, span: &Span) -> Result { // make sure the given entry is file let file_type = entry @@ -20,14 +22,18 @@ fn parse_import_file(entry: &DirEntry, span: &Span) -> Result> ConstrainedProgram { pub fn enforce_package_access( @@ -54,26 +54,33 @@ impl> ConstrainedProgram { }); // search for package name in imports directory - // let mut source_directory = path.clone(); - // source_directory.push(IMPORTS_DIRECTORY_NAME); - // - // let entries = fs::read_dir(source_directory) - // .map_err(|error| ImportError::directory_error(error, package_name.span.clone()))? - // .into_iter() - // .collect::, std::io::Error>>() - // .map_err(|error| ImportError::directory_error(error, package_name.span.clone()))?; - // - // let matched_import_entry = entries.into_iter().find(|entry| { - // entry.file_name().eq(&package_name.name) - // }); + let mut imports_directory = path.clone(); + imports_directory.push(IMPORTS_DIRECTORY_NAME); - // todo: return error if package name is present in both directories + if imports_directory.exists() { + let entries = fs::read_dir(imports_directory) + .map_err(|error| ImportError::directory_error(error, package_name.span.clone()))? + .into_iter() + .collect::, std::io::Error>>() + .map_err(|error| ImportError::directory_error(error, package_name.span.clone()))?; - // Enforce package access - if let Some(entry) = matched_source_entry { - self.enforce_package_access(scope, &entry, package.access) + let matched_import_entry = entries + .into_iter() + .find(|entry| entry.file_name().into_string().unwrap().eq(&package_name.name)); + + // Enforce package access and potential collisions + match (matched_source_entry, matched_import_entry) { + (Some(_), Some(_)) => Err(ImportError::conflicting_imports(package_name)), + (Some(source_entry), None) => self.enforce_package_access(scope, &source_entry, package.access), + (None, Some(import_entry)) => self.enforce_package_access(scope, &import_entry, package.access), + (None, None) => Err(ImportError::unknown_package(package_name)), + } } else { - Err(ImportError::unknown_package(package_name)) + // Enforce local package access with no found imports directory + match matched_source_entry { + Some(source_entry) => self.enforce_package_access(scope, &source_entry, package.access), + None => Err(ImportError::unknown_package(package_name)), + } } } } diff --git a/compiler/src/errors/constraints/import.rs b/compiler/src/errors/constraints/import.rs index 4b5bf200f3..d9f7ed2406 100644 --- a/compiler/src/errors/constraints/import.rs +++ b/compiler/src/errors/constraints/import.rs @@ -17,6 +17,12 @@ impl ImportError { ImportError::Error(FormattedError::new_from_span(message, span)) } + pub fn conflicting_imports(identifier: Identifier) -> Self { + let message = format!("conflicting imports found for `{}`", identifier.name); + + Self::new_from_span(message, identifier.span) + } + pub fn convert_os_string(span: Span) -> Self { let message = format!("failed to convert file string name, maybe an illegal character?");