diff --git a/.github/workflows/snarkjs.sh b/.github/workflows/snarkjs.sh index a3bc6ddf1..7031f163b 100755 --- a/.github/workflows/snarkjs.sh +++ b/.github/workflows/snarkjs.sh @@ -9,10 +9,6 @@ fi DIR_PATH=$1 CURVE=$2 -# Init stdlib in .noname/release/src/stdlib instead of downloading -echo "Overriding stdlib in .noname/release/src/stdlib..." -mkdir -p ~/.noname/release/src/stdlib/ && cp -r /app/noname/src/stdlib/* ~/.noname/release/src/stdlib/ - # Ensure the circuit directory exists and is initialized echo "Initializing a new Noname package..." noname new --path circuit_noname diff --git a/Cargo.lock b/Cargo.lock index 16c9c7462..e4cca0c2e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -768,6 +768,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + [[package]] name = "futures" version = "0.3.30" @@ -1369,6 +1375,7 @@ dependencies = [ "dirs", "educe", "ena", + "fs_extra", "hex", "itertools", "kimchi", diff --git a/Cargo.toml b/Cargo.toml index 970ed5656..12e69df02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,10 +2,15 @@ name = "noname" version = "0.7.0" edition = "2021" +build = "build.rs" description = "a programming language for writing zkapps" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[build-dependencies] +fs_extra = "1.3.0" +dirs = "4.0" + [dependencies] ark-ec = "0.3.0" # elliptic curve library ark-ff = "0.3.0" diff --git a/build.rs b/build.rs new file mode 100644 index 000000000..cb1ca18d5 --- /dev/null +++ b/build.rs @@ -0,0 +1,27 @@ +use std::{env, path::PathBuf}; + +use fs_extra::dir::{self, CopyOptions}; + +fn main() { + let home_dir: PathBuf = + dirs::home_dir().expect("could not find home directory of current user"); + + let noname_dir = home_dir.join(".noname"); + let release_dir = noname_dir.join("release"); + + // If it exists, then remove it + if release_dir.exists() { + fs_extra::remove_items(&[&release_dir]).expect("could not remove release directory"); + } + + let current_dir = env::current_dir().expect("could not get current directory"); + + // Set up copy options + let mut options = CopyOptions::new(); + options.overwrite = true; // Overwrite existing files + options.copy_inside = true; // Copy contents inside the source directory + + // Copy the current folder to the release directory + dir::copy(current_dir, &release_dir, &options) + .expect("could not copy current directory to release directory"); +} diff --git a/src/backends/kimchi/mod.rs b/src/backends/kimchi/mod.rs index e53bf555d..f2388a721 100644 --- a/src/backends/kimchi/mod.rs +++ b/src/backends/kimchi/mod.rs @@ -771,6 +771,6 @@ impl Backend for KimchiVesta { msg: String, span: Span, ) { - todo!() + println!("todo: implement log_var for kimchi backend"); } } diff --git a/src/cli/cmd_build_and_check.rs b/src/cli/cmd_build_and_check.rs index bae53a02c..a3e49f3ce 100644 --- a/src/cli/cmd_build_and_check.rs +++ b/src/cli/cmd_build_and_check.rs @@ -19,8 +19,7 @@ use crate::{ }; use super::packages::{ - download_stdlib, get_deps_of_package, is_lib, validate_package_and_get_manifest, - DependencyGraph, UserRepo, + get_deps_of_package, is_lib, validate_package_and_get_manifest, DependencyGraph, UserRepo, }; const COMPILED_DIR: &str = "compiled"; @@ -165,13 +164,7 @@ fn add_stdlib( let mut node_id = node_id; // check if the release folder exists, otherwise download the latest release - // todo: check the latest version and compare it with the current version, to decide if download is needed let stdlib_dir = path_to_stdlib(); - - if !stdlib_dir.exists() { - download_stdlib()?; - } - node_id = init_stdlib_dep(sources, tast, node_id, stdlib_dir.as_ref(), server_mode); Ok(node_id) @@ -456,7 +449,27 @@ fn test_r1cs_backend( where F: BackendField, { - let (tast, sources) = typecheck_file(path)?; + let mut sources = Sources::new(); + let mut node_id = 0; + + let mut tast = TypeChecker::new(); + + // adding stdlib + node_id = add_stdlib(&mut sources, &mut tast, node_id, &mut None)?; + + let code = std::fs::read_to_string(path) + .into_diagnostic() + .wrap_err_with(|| format!("could not read file `{path}`"))?; + + typecheck_next_file( + &mut tast, + None, + &mut sources, + path.to_string(), + code, + node_id, + &mut None, + )?; let compiled_circuit = compile(&sources, tast, r1cs, &mut None)?; diff --git a/src/cli/packages.rs b/src/cli/packages.rs index 263c12401..7b2ed5f87 100644 --- a/src/cli/packages.rs +++ b/src/cli/packages.rs @@ -244,13 +244,17 @@ pub(crate) fn path_to_package(dep: &UserRepo) -> PathBuf { } pub(crate) fn path_to_stdlib() -> PathBuf { + path_to_release_dir().join(STDLIB_DIRECTORY) +} + +pub(crate) fn path_to_release_dir() -> PathBuf { let home_dir: PathBuf = dirs::home_dir() .expect("could not find home directory of current user") .try_into() .expect("invalid UTF8 path"); let noname_dir = home_dir.join(NONAME_DIRECTORY); - noname_dir.join(RELEASE_DIRECTORY).join(STDLIB_DIRECTORY) + noname_dir.join(RELEASE_DIRECTORY) } /// download package from github @@ -276,44 +280,6 @@ pub fn download_from_github(dep: &UserRepo) -> Result<()> { Ok(()) } -pub fn download_stdlib() -> Result<()> { - // Hardcoded repository details and target branch - let repo_owner = "zksecurity"; - let repo_name = "noname"; - let target_branch = "main"; - let repo_url = format!( - "https://github.com/{owner}/{repo}.git", - owner = repo_owner, - repo = repo_name - ); - - let home_dir: PathBuf = dirs::home_dir() - .expect("could not find home directory of current user") - .try_into() - .expect("invalid UTF8 path"); - let noname_dir = home_dir.join(NONAME_DIRECTORY); - let release_dir = noname_dir.join("release"); - - // Clone the repository and checkout the specified branch to the temporary directory - let output = process::Command::new("git") - .arg("clone") - .arg("--branch") - .arg(target_branch) - .arg("--single-branch") - .arg(repo_url) - .arg(release_dir) - .output() - .expect("failed to execute git clone command"); - - if !output.status.success() { - miette::bail!(format!( - "Could not clone branch `{target_branch}` of repository `{repo_owner}/{repo_name}`." - )); - } - - Ok(()) -} - pub fn is_lib(path: &PathBuf) -> bool { path.join("src").join("lib.no").exists() } diff --git a/src/server/mod.rs b/src/server/mod.rs index 67008cdc4..083552ce6 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -11,6 +11,8 @@ use std::thread; use tokio::sync::{mpsc, Mutex}; use tower_http::services::ServeDir; +use crate::cli::packages::path_to_release_dir; + // // The interface for the rest of the compiler which doens't use async/await // @@ -104,7 +106,10 @@ async fn run_server(tx: mpsc::Sender, rx: mpsc::Receiver( for lib in libs { let module = UserRepo::new(&format!("std/{}", lib)); let prefix_stdlib = Path::new(path_prefix); - let code = std::fs::read_to_string(prefix_stdlib.join(format!("{lib}.no"))).unwrap(); + let code = std::fs::read_to_string(prefix_stdlib.join(format!("{lib}/lib.no"))).unwrap(); node_id = typecheck_next_file( tast, Some(module), sources, lib.to_string(), code, - 0, + node_id, server_mode, ) .unwrap(); - let code = std::fs::read_to_string(prefix_stdlib.join(format!("{lib}/lib.no"))).unwrap(); - node_id = - typecheck_next_file(tast, Some(module), sources, lib.to_string(), code, 0).unwrap(); } node_id diff --git a/src/tests/examples.rs b/src/tests/examples.rs index 9147e28f8..79fe56ed8 100644 --- a/src/tests/examples.rs +++ b/src/tests/examples.rs @@ -110,7 +110,13 @@ fn test_file( let mut sources = Sources::new(); let mut tast = TypeChecker::new(); let mut node_id = 0; - node_id = init_stdlib_dep(&mut sources, &mut tast, node_id, STDLIB_DIRECTORY); + node_id = init_stdlib_dep( + &mut sources, + &mut tast, + node_id, + STDLIB_DIRECTORY, + &mut None, + ); let this_module = None; let _node_id = typecheck_next_file( &mut tast, @@ -118,7 +124,7 @@ fn test_file( &mut sources, file_name.to_string(), code.clone(), - 0, + node_id, &mut None, ) .unwrap();