mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-25 19:22:01 +03:00
Merge pull request #896 from AleoHQ/feature-relative-imports
[CLI] Makes import path relative to program directory
This commit is contained in:
commit
b6f6f051f0
@ -229,7 +229,11 @@ impl<'a, F: PrimeField, G: GroupType<F>> Compiler<'a, F, G> {
|
||||
tracing::debug!("Program parsing complete\n{:#?}", self.program);
|
||||
|
||||
// Create a new symbol table from the program, imported_programs, and program_input.
|
||||
let asg = Asg::new(self.context, &self.program, &mut leo_imports::ImportParser::default())?;
|
||||
let asg = Asg::new(
|
||||
self.context,
|
||||
&self.program,
|
||||
&mut leo_imports::ImportParser::new(self.main_file_path.clone()),
|
||||
)?;
|
||||
|
||||
tracing::debug!("ASG generation complete");
|
||||
|
||||
|
@ -18,7 +18,7 @@ use crate::errors::ImportParserError;
|
||||
use leo_asg::{AsgContext, AsgConvertError, ImportResolver, Program, Span};
|
||||
|
||||
use indexmap::{IndexMap, IndexSet};
|
||||
use std::env::current_dir;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Stores imported packages.
|
||||
///
|
||||
@ -26,11 +26,21 @@ use std::env::current_dir;
|
||||
/// directory, foreign in the imports directory, or part of the core package list.
|
||||
#[derive(Clone, Default)]
|
||||
pub struct ImportParser<'a> {
|
||||
program_path: PathBuf,
|
||||
partial_imports: IndexSet<String>,
|
||||
imports: IndexMap<String, Program<'a>>,
|
||||
}
|
||||
|
||||
//todo: handle relative imports relative to file...
|
||||
impl<'a> ImportParser<'a> {
|
||||
pub fn new(program_path: PathBuf) -> Self {
|
||||
ImportParser {
|
||||
program_path,
|
||||
partial_imports: Default::default(),
|
||||
imports: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ImportResolver<'a> for ImportParser<'a> {
|
||||
fn resolve_package(
|
||||
&mut self,
|
||||
@ -46,8 +56,7 @@ impl<'a> ImportResolver<'a> for ImportParser<'a> {
|
||||
return Ok(Some(program.clone()));
|
||||
}
|
||||
let mut imports = Self::default();
|
||||
let path =
|
||||
current_dir().map_err(|x| -> AsgConvertError { ImportParserError::current_directory_error(x).into() })?;
|
||||
let path = self.program_path.clone();
|
||||
|
||||
self.partial_imports.insert(full_path.clone());
|
||||
let program = imports
|
||||
|
@ -361,13 +361,12 @@ mod cli_tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore] // ignore until imports path is fixed #875
|
||||
fn test_sudoku() {
|
||||
let path = &Some(PathBuf::from("examples/silly-sudoku"));
|
||||
|
||||
assert!(run_cmd("leo build", path).is_ok());
|
||||
assert!(run_cmd("leo test", path).is_ok());
|
||||
assert!(run_cmd("leo test -f src/lib.leo", path).is_ok());
|
||||
assert!(run_cmd("leo test -f src/main.leo", path).is_ok());
|
||||
assert!(run_cmd("leo test -f examples/silly-sudoku/src/lib.leo", path).is_ok());
|
||||
assert!(run_cmd("leo test -f examples/silly-sudoku/src/main.leo", path).is_ok());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user