From e4389dbfa2619b6a5149ce136733b35627238683 Mon Sep 17 00:00:00 2001 From: Koby Hall <102518238+kobyhallx@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:48:29 +0200 Subject: [PATCH 1/5] chore: align versions for js packages (#3019) --- compiler/integration-tests/package.json | 1 - package.json | 1 - release-please-config.json | 16 +++++++++++++--- release-tests/package.json | 1 - tooling/noir_js_types/package.json | 2 +- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/compiler/integration-tests/package.json b/compiler/integration-tests/package.json index c01d66cb43d..cf6df8b4703 100644 --- a/compiler/integration-tests/package.json +++ b/compiler/integration-tests/package.json @@ -1,6 +1,5 @@ { "name": "integration-tests", - "version": "1.0.0", "license": "(MIT OR Apache-2.0)", "main": "index.js", "private": true, diff --git a/package.json b/package.json index e8985ff4088..9b7cf4a04b5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,5 @@ { "name": "@noir-lang/root", - "version": "0.11.1", "private": true, "workspaces": [ "compiler/wasm", diff --git a/release-please-config.json b/release-please-config.json index e985ada4ed5..ddf6ff7b3a9 100644 --- a/release-please-config.json +++ b/release-please-config.json @@ -16,12 +16,12 @@ "flake.nix", { "type": "json", - "path": "compiler/wasm/package.json", + "path": "compiler/source-resolver/package.json", "jsonpath": "$.version" }, { "type": "json", - "path": "tooling/noirc_abi_wasm/package.json", + "path": "compiler/wasm/package.json", "jsonpath": "$.version" }, { @@ -31,7 +31,17 @@ }, { "type": "json", - "path": "compiler/source-resolver/package.json", + "path": "tooling/noir_js_backend_barretenberg/package.json", + "jsonpath": "$.version" + }, + { + "type": "json", + "path": "tooling/noir_js_types/package.json", + "jsonpath": "$.version" + }, + { + "type": "json", + "path": "tooling/noirc_abi_wasm/package.json", "jsonpath": "$.version" } ] diff --git a/release-tests/package.json b/release-tests/package.json index d1f581469e9..3b73ad18574 100644 --- a/release-tests/package.json +++ b/release-tests/package.json @@ -1,6 +1,5 @@ { "name": "release-tests", - "version": "0.0.0", "main": "index.js", "license": "(MIT OR Apache-2.0)", "private": true, diff --git a/tooling/noir_js_types/package.json b/tooling/noir_js_types/package.json index 73bab16d92e..d9f64ae9b31 100644 --- a/tooling/noir_js_types/package.json +++ b/tooling/noir_js_types/package.json @@ -4,7 +4,7 @@ "The Noir Team " ], "packageManager": "yarn@3.5.1", - "version": "0.14.1", + "version": "0.16.0", "license": "(MIT OR Apache-2.0)", "files": [ "lib", From 9893dc749c1db7ffe232dc91e1e4920daa41e97f Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 6 Oct 2023 16:27:20 +0100 Subject: [PATCH 2/5] chore: run tests for `noir_wasm` in node under default resolver (#3020) --- compiler/wasm/package.json | 4 +-- compiler/wasm/test/browser/index.test.ts | 34 ++++++++++++++++++++++-- compiler/wasm/test/index.d.ts | 1 - compiler/wasm/test/node/index.test.ts | 12 +++------ compiler/wasm/test/shared.ts | 32 ---------------------- 5 files changed, 37 insertions(+), 46 deletions(-) delete mode 100644 compiler/wasm/test/index.d.ts diff --git a/compiler/wasm/package.json b/compiler/wasm/package.json index 4bc4ddc8288..446a189302b 100644 --- a/compiler/wasm/package.json +++ b/compiler/wasm/package.json @@ -20,8 +20,8 @@ }, "scripts": { "build": "bash ./build.sh", - "test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha", - "test:node": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha", + "test": "yarn test:node && yarn test:browser", + "test:node": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\"}' mocha", "test:browser": "web-test-runner", "clean": "chmod u+w web nodejs || true && rm -rf ./nodejs ./web ./target ./result", "publish": "echo 📡 publishing `$npm_package_name` && yarn npm publish", diff --git a/compiler/wasm/test/browser/index.test.ts b/compiler/wasm/test/browser/index.test.ts index 662aae5a666..02263d9adfb 100644 --- a/compiler/wasm/test/browser/index.test.ts +++ b/compiler/wasm/test/browser/index.test.ts @@ -1,6 +1,7 @@ import { expect } from '@esm-bundle/chai'; -import initNoirWasm from '@noir-lang/noir_wasm'; -import { compileNoirSource, nargoArtifactPath, noirSourcePath } from '../shared'; +import initNoirWasm, { compile } from '@noir-lang/noir_wasm'; +import { initializeResolver } from '@noir-lang/source-resolver'; +import { nargoArtifactPath, noirSourcePath } from '../shared'; beforeEach(async () => { await initNoirWasm(); @@ -22,6 +23,35 @@ async function getPrecompiledSource(): Promise { return JSON.parse(compiledData); } +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export async function compileNoirSource(noir_source: string): Promise { + console.log('Compiling Noir source...'); + + initializeResolver((id: string) => { + console.log(`Resolving source ${id}`); + + const source = noir_source; + + if (typeof source === 'undefined') { + throw Error(`Could not resolve source for '${id}'`); + } else if (id !== '/main.nr') { + throw Error(`Unexpected id: '${id}'`); + } else { + return source; + } + }); + + try { + const compiled_noir = compile('main.nr'); + + console.log('Noir source compilation done.'); + + return compiled_noir; + } catch (e) { + console.log('Error while compiling:', e); + } +} + describe('noir wasm compilation', () => { it('matches nargos compilation', async () => { const source = await getSource(); diff --git a/compiler/wasm/test/index.d.ts b/compiler/wasm/test/index.d.ts deleted file mode 100644 index 2b4401d3bc8..00000000000 --- a/compiler/wasm/test/index.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module '@noir-lang/source-resolver'; diff --git a/compiler/wasm/test/node/index.test.ts b/compiler/wasm/test/node/index.test.ts index 3ecb1bfc4b4..78aed535df7 100644 --- a/compiler/wasm/test/node/index.test.ts +++ b/compiler/wasm/test/node/index.test.ts @@ -1,16 +1,13 @@ import { expect } from 'chai'; -import { compileNoirSource, nargoArtifactPath, noirSourcePath } from '../shared'; +import { nargoArtifactPath, noirSourcePath } from '../shared'; import { readFileSync } from 'node:fs'; import { join } from 'node:path'; +import { compile } from '@noir-lang/noir_wasm'; async function getFileContent(path: string): Promise { return readFileSync(join(__dirname, path)).toString(); } -async function getSource(): Promise { - return getFileContent(noirSourcePath); -} - // eslint-disable-next-line @typescript-eslint/no-explicit-any async function getPrecompiledSource(): Promise { const compiledData = await getFileContent(nargoArtifactPath); @@ -19,10 +16,7 @@ async function getPrecompiledSource(): Promise { describe('noir wasm compilation', () => { it('matches nargos compilation', async () => { - const source = await getSource(); - - const wasmCircuit = await compileNoirSource(source); - + const wasmCircuit = await compile(noirSourcePath); const cliCircuit = await getPrecompiledSource(); // We don't expect the hashes to match due to how `noir_wasm` handles dependencies diff --git a/compiler/wasm/test/shared.ts b/compiler/wasm/test/shared.ts index 91f420741cb..f726316cd74 100644 --- a/compiler/wasm/test/shared.ts +++ b/compiler/wasm/test/shared.ts @@ -1,34 +1,2 @@ -import { initializeResolver } from '@noir-lang/source-resolver'; -import { compile } from '@noir-lang/noir_wasm'; - export const noirSourcePath = '../../noir-script/src/main.nr'; export const nargoArtifactPath = '../../noir-script/target/noir_wasm_testing.json'; - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -export async function compileNoirSource(noir_source: string): Promise { - console.log('Compiling Noir source...'); - - initializeResolver((id: string) => { - console.log(`Resolving source ${id}`); - - const source = noir_source; - - if (typeof source === 'undefined') { - throw Error(`Could not resolve source for '${id}'`); - } else if (id !== '/main.nr') { - throw Error(`Unexpected id: '${id}'`); - } else { - return source; - } - }); - - try { - const compiled_noir = compile('main.nr'); - - console.log('Noir source compilation done.'); - - return compiled_noir; - } catch (e) { - console.log('Error while compiling:', e); - } -} From 03c7923ce7825b1f963c345edf236b62c8ff801c Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Fri, 6 Oct 2023 21:11:30 +0100 Subject: [PATCH 3/5] chore: codegen formatter test cases (#3006) --- tooling/nargo_fmt/build.rs | 68 +++++++++++++++++++ tooling/nargo_fmt/src/lib.rs | 48 ------------- tooling/nargo_fmt/tests/execute.rs | 5 ++ .../{nested-if-else.nr => nested_if_else.nr} | 0 .../{nested-if-else.nr => nested_if_else.nr} | 0 5 files changed, 73 insertions(+), 48 deletions(-) create mode 100644 tooling/nargo_fmt/build.rs create mode 100644 tooling/nargo_fmt/tests/execute.rs rename tooling/nargo_fmt/tests/expected/{nested-if-else.nr => nested_if_else.nr} (100%) rename tooling/nargo_fmt/tests/input/{nested-if-else.nr => nested_if_else.nr} (100%) diff --git a/tooling/nargo_fmt/build.rs b/tooling/nargo_fmt/build.rs new file mode 100644 index 00000000000..0ff39e99129 --- /dev/null +++ b/tooling/nargo_fmt/build.rs @@ -0,0 +1,68 @@ +use std::fs::File; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::{env, fs}; + +fn main() { + let out_dir = env::var("OUT_DIR").unwrap(); + let destination = Path::new(&out_dir).join("execute.rs"); + let mut test_file = File::create(destination).unwrap(); + + // Try to find the directory that Cargo sets when it is running; otherwise fallback to assuming the CWD + // is the root of the repository and append the crate path + let manifest_dir = match std::env::var("CARGO_MANIFEST_DIR") { + Ok(dir) => PathBuf::from(dir), + Err(_) => std::env::current_dir().unwrap().join("crates").join("nargo_cli"), + }; + let test_dir = manifest_dir.join("tests"); + + generate_formatter_tests(&mut test_file, &test_dir); +} + +fn generate_formatter_tests(test_file: &mut File, test_data_dir: &Path) { + let inputs_dir = test_data_dir.join("input"); + let outputs_dir = test_data_dir.join("expected"); + + let test_case_files = + fs::read_dir(inputs_dir).unwrap().flatten().filter(|c| c.path().is_file()); + + for file in test_case_files { + let file_path = file.path(); + let file_name = file_path.file_name().unwrap(); + let test_name = file_path.file_stem().unwrap().to_str().unwrap(); + + if test_name.contains('-') { + panic!( + "Invalid test directory: {test_name}. Cannot include `-`, please convert to `_`" + ); + }; + + let input_source_path = file.path(); + let input_source = std::fs::read_to_string(input_source_path).unwrap(); + + let output_source_path = outputs_dir.join(file_name); + let output_source = std::fs::read_to_string(output_source_path).unwrap(); + + write!( + test_file, + r##" +#[test] +fn format_{test_name}() {{ + let input = r#"{input_source}"#; + let expected_output = r#"{output_source}"#; + + + let (parsed_module, errors) = noirc_frontend::parse_program(&input); + assert!(errors.is_empty()); + + let config = nargo_fmt::Config::default(); + let fmt_text = nargo_fmt::format(&input, parsed_module, &config); + + + assert_eq!(fmt_text, expected_output); +}} + "## + ) + .expect("Could not write templated test file."); + } +} diff --git a/tooling/nargo_fmt/src/lib.rs b/tooling/nargo_fmt/src/lib.rs index 754449aa425..c938a70e7a5 100644 --- a/tooling/nargo_fmt/src/lib.rs +++ b/tooling/nargo_fmt/src/lib.rs @@ -33,51 +33,3 @@ pub fn format(source: &str, parsed_module: ParsedModule, config: &Config) -> Str fmt.visit_module(parsed_module); fmt.finish() } - -#[cfg(test)] -mod tests { - use std::{ffi::OsStr, path::PathBuf}; - - use crate::Config; - - #[test] - fn test() { - let files = std::fs::read_dir("tests/input").unwrap(); - for file in files { - let file = file.unwrap(); - - let config = Config::default(); - - let source_path = file.path(); - let source = std::fs::read_to_string(&source_path).unwrap(); - - let (parsed_module, errors) = noirc_frontend::parse_program(&source); - let fmt_text = crate::format(&source, parsed_module, &config); - - assert!(errors.is_empty()); - - let target_path: PathBuf = source_path - .components() - .map(|component| { - if component.as_os_str() == "input" { - OsStr::new("expected") - } else { - component.as_os_str() - } - }) - .collect(); - - let target = match std::fs::read_to_string(&target_path) { - Ok(t) => t, - Err(err) if err.kind() == std::io::ErrorKind::NotFound => { - std::fs::write(target_path, fmt_text.clone()).unwrap(); - fmt_text.clone() - } - Err(err) => unreachable!("{err}"), - }; - - // FIXME: better diff - assert_eq!(fmt_text, target); - } - } -} diff --git a/tooling/nargo_fmt/tests/execute.rs b/tooling/nargo_fmt/tests/execute.rs new file mode 100644 index 00000000000..d8802fd926b --- /dev/null +++ b/tooling/nargo_fmt/tests/execute.rs @@ -0,0 +1,5 @@ +#[cfg(test)] +mod tests { + // include tests generated by `build.rs` + include!(concat!(env!("OUT_DIR"), "/execute.rs")); +} diff --git a/tooling/nargo_fmt/tests/expected/nested-if-else.nr b/tooling/nargo_fmt/tests/expected/nested_if_else.nr similarity index 100% rename from tooling/nargo_fmt/tests/expected/nested-if-else.nr rename to tooling/nargo_fmt/tests/expected/nested_if_else.nr diff --git a/tooling/nargo_fmt/tests/input/nested-if-else.nr b/tooling/nargo_fmt/tests/input/nested_if_else.nr similarity index 100% rename from tooling/nargo_fmt/tests/input/nested-if-else.nr rename to tooling/nargo_fmt/tests/input/nested_if_else.nr From ae8d0e9013f26b52e8f0bdc9f84866ffec50872d Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 9 Oct 2023 12:26:05 +0100 Subject: [PATCH 4/5] fix: include .nr and .sol files in builds (#3039) --- flake.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 4334cb8a3f0..0a9bf65322d 100644 --- a/flake.nix +++ b/flake.nix @@ -81,7 +81,7 @@ # Custom filter with various file extensions that we rely upon to build packages # Currently: `.nr`, `.sol`, `.sh`, `.json`, `.md` filter = path: type: - (builtins.match ".*\.(sh|json|md)$" path != null) || (craneLib.filterCargoSources path type); + (builtins.match ".*\.(nr|sol|sh|json|md)$" path != null) || (craneLib.filterCargoSources path type); }; # TODO(#1198): It'd be nice to include these flags when running `cargo clippy` in a devShell. From 1f1039529f722b8dcef2e375fd2249083e0caa15 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 9 Oct 2023 12:53:00 +0100 Subject: [PATCH 5/5] chore: make paths in `wasm` tests absolute (#3040) --- compiler/wasm/test/node/index.test.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/compiler/wasm/test/node/index.test.ts b/compiler/wasm/test/node/index.test.ts index 78aed535df7..4ec6d83c3c3 100644 --- a/compiler/wasm/test/node/index.test.ts +++ b/compiler/wasm/test/node/index.test.ts @@ -4,19 +4,18 @@ import { readFileSync } from 'node:fs'; import { join } from 'node:path'; import { compile } from '@noir-lang/noir_wasm'; -async function getFileContent(path: string): Promise { - return readFileSync(join(__dirname, path)).toString(); -} +const absoluteNoirSourcePath = join(__dirname, noirSourcePath); +const absoluteNargoArtifactPath = join(__dirname, nargoArtifactPath); // eslint-disable-next-line @typescript-eslint/no-explicit-any async function getPrecompiledSource(): Promise { - const compiledData = await getFileContent(nargoArtifactPath); + const compiledData = readFileSync(absoluteNargoArtifactPath).toString(); return JSON.parse(compiledData); } describe('noir wasm compilation', () => { it('matches nargos compilation', async () => { - const wasmCircuit = await compile(noirSourcePath); + const wasmCircuit = await compile(absoluteNoirSourcePath); const cliCircuit = await getPrecompiledSource(); // We don't expect the hashes to match due to how `noir_wasm` handles dependencies