diff --git a/PROJECT/dep/dep/foo.zok b/PROJECT/dep/dep/foo.zok deleted file mode 100644 index 4498c0fd..00000000 --- a/PROJECT/dep/dep/foo.zok +++ /dev/null @@ -1,2 +0,0 @@ -def foo() -> (field): - return 1 \ No newline at end of file diff --git a/PROJECT/dep/foo.zok b/PROJECT/dep/foo.zok deleted file mode 100644 index 3f862718..00000000 --- a/PROJECT/dep/foo.zok +++ /dev/null @@ -1,4 +0,0 @@ -from "./dep/foo.zok" import foo as bar - -def foo() -> (field): - return 2 + bar() \ No newline at end of file diff --git a/PROJECT/main.zok b/PROJECT/main.zok deleted file mode 100644 index 2c412c84..00000000 --- a/PROJECT/main.zok +++ /dev/null @@ -1,4 +0,0 @@ -from "./dep/foo.zok" import foo - -def main() -> (field): - return foo() \ No newline at end of file diff --git a/zokrates_cli/src/bin.rs b/zokrates_cli/src/bin.rs index ef2ae034..ed7125cc 100644 --- a/zokrates_cli/src/bin.rs +++ b/zokrates_cli/src/bin.rs @@ -13,7 +13,7 @@ use std::io::{stdin, BufReader, BufWriter, Read, Write}; use std::path::{Path, PathBuf}; use std::string::String; use zokrates_abi::Encode; -use zokrates_core::compile::{compile, CompilationArtifacts}; +use zokrates_core::compile::{compile, CompilationArtifacts, CompileError}; use zokrates_core::ir; use zokrates_core::proof_system::*; use zokrates_core::typed_absy::abi::Abi; @@ -287,9 +287,29 @@ fn cli() -> Result<(), String> { .read_to_string(&mut source) .map_err(|why| format!("couldn't open input file {}: {}", path.display(), why))?; + let fmt_error = |e: &CompileError| { + format!( + "{}:{}", + e.file() + .canonicalize() + .unwrap() + .strip_prefix(std::env::current_dir().unwrap()) + .unwrap() + .display(), + e.value() + ) + }; + let artifacts: CompilationArtifacts = - compile(source, path, Some(&fs_resolve)) - .map_err(|e| format!("Compilation failed:\n\n {}", e))?; + compile(source, path, Some(&fs_resolve)).map_err(|e| { + format!( + "Compilation failed:\n\n{}", + e.0.iter() + .map(|e| fmt_error(e)) + .collect::>() + .join("\n\n") + ) + })?; let program_flattened = artifacts.prog(); diff --git a/zokrates_core/src/compile.rs b/zokrates_core/src/compile.rs index 5f9624c8..f503becc 100644 --- a/zokrates_core/src/compile.rs +++ b/zokrates_core/src/compile.rs @@ -44,20 +44,6 @@ impl From for CompileErrors { } } -impl fmt::Display for CompileErrors { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}", - self.0 - .iter() - .map(|e| format!("{}", e)) - .collect::>() - .join("\n\n") - ) - } -} - #[derive(Debug)] pub enum CompileErrorInner { ParserError(pest::Error), @@ -81,6 +67,16 @@ pub struct CompileError { value: CompileErrorInner, } +impl CompileError { + pub fn file(&self) -> &PathBuf { + &self.file + } + + pub fn value(&self) -> &CompileErrorInner { + &self.value + } +} + impl CompileErrors { pub fn with_context(self, file: PathBuf) -> Self { CompileErrors( @@ -95,22 +91,6 @@ impl CompileErrors { } } -impl fmt::Display for CompileError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "{}:{}", - self.file - .canonicalize() - .unwrap() - .strip_prefix(std::env::current_dir().unwrap()) - .unwrap() - .display(), - self.value - ) - } -} - impl From for CompileErrorInner { fn from(error: pest::Error) -> Self { CompileErrorInner::ParserError(error) @@ -140,13 +120,12 @@ impl From for CompileError { impl fmt::Display for CompileErrorInner { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let res = match *self { - CompileErrorInner::ParserError(ref e) => format!("{}", e), - CompileErrorInner::SemanticError(ref e) => format!("{}", e), - CompileErrorInner::ReadError(ref e) => format!("{}", e), - CompileErrorInner::ImportError(ref e) => format!("{}", e), - }; - write!(f, "{}", res) + match *self { + CompileErrorInner::ParserError(ref e) => write!(f, "{}", e), + CompileErrorInner::SemanticError(ref e) => write!(f, "{}", e), + CompileErrorInner::ReadError(ref e) => write!(f, "{}", e), + CompileErrorInner::ImportError(ref e) => write!(f, "{}", e), + } } } diff --git a/zokrates_core/src/imports.rs b/zokrates_core/src/imports.rs index c1dac9be..1f9bc4c9 100644 --- a/zokrates_core/src/imports.rs +++ b/zokrates_core/src/imports.rs @@ -180,7 +180,6 @@ impl Importer { } } else { // to resolve imports, we need a resolver - let folder = location.clone().parent().unwrap(); match resolve_option { Some(resolve) => match resolve(location.clone(), import.source.to_path_buf()) { Ok((source, new_location)) => { diff --git a/zokrates_fs_resolver/src/lib.rs b/zokrates_fs_resolver/src/lib.rs index 2beb62ac..6dfb72ed 100644 --- a/zokrates_fs_resolver/src/lib.rs +++ b/zokrates_fs_resolver/src/lib.rs @@ -14,12 +14,6 @@ pub fn resolve<'a>( current_location: CurrentLocation, import_location: ImportLocation<'a>, ) -> Result<(SourceCode, CurrentLocation), io::Error> { - println!( - "get file {} {}", - current_location.display(), - import_location.display() - ); - let source = Path::new(&import_location); // paths starting with `./` or `../` are interpreted relative to the current file diff --git a/zokrates_js/src/lib.rs b/zokrates_js/src/lib.rs index 686f9642..81fd8338 100644 --- a/zokrates_js/src/lib.rs +++ b/zokrates_js/src/lib.rs @@ -1,9 +1,10 @@ use bincode::{deserialize, serialize}; use serde::{Deserialize, Serialize}; use serde_json::to_string_pretty; +use std::path::PathBuf; use wasm_bindgen::prelude::*; use zokrates_abi::{parse_strict, Encode, Decode, Inputs}; -use zokrates_core::compile::{compile as core_compile, CompilationArtifacts}; +use zokrates_core::compile::{compile as core_compile, CompilationArtifacts, CompileError}; use zokrates_core::imports::Error; use zokrates_core::ir; use zokrates_core::proof_system::{self, ProofSystem}; @@ -30,8 +31,8 @@ pub struct ComputationResult { } impl ResolverResult { - fn into_tuple(self) -> (String, String) { - (self.source, self.location) + fn into_tuple(self) -> (String, PathBuf) { + (self.source, PathBuf::from(self.location)) } } @@ -53,30 +54,38 @@ pub fn compile( location: JsValue, resolve: &js_sys::Function, ) -> Result { - let closure = |l: String, p: String| { + let closure = |l: PathBuf, p: PathBuf| { let value = resolve - .call2(&JsValue::UNDEFINED, &l.into(), &p.clone().into()) + .call2(&JsValue::UNDEFINED, &l.display().to_string().into(), &p.clone().display().to_string().into()) .map_err(|_| { Error::new(format!( "Error thrown in callback: Could not resolve `{}`", - p + p.display() )) })?; if value.is_null() || value.is_undefined() { - Err(Error::new(format!("Could not resolve `{}`", p))) + Err(Error::new(format!("Could not resolve `{}`", p.display()))) } else { let result: ResolverResult = value.into_serde().unwrap(); Ok(result.into_tuple()) } }; + let fmt_error = |e: &CompileError| { + format!( + "{}:{}", + e.file().display(), + e.value() + ) + }; + let artifacts: CompilationArtifacts = core_compile( source.as_string().unwrap(), - location.as_string().unwrap(), + PathBuf::from(location.as_string().unwrap()), Some(&closure), ) - .map_err(|ce| JsValue::from_str(&format!("{}", ce)))?; + .map_err(|ce| JsValue::from_str(&format!("{}", ce.0.iter().map(|e| fmt_error(e)).collect::>().join("\n"))))?; let result = CompilationResult { program: serialize_program(artifacts.prog())?,