Skip to content

Commit

Permalink
Merge branch 'master' into jf/fix-2045
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Aug 7, 2023
2 parents 2ffeb2d + 1be1bcc commit 45f586e
Show file tree
Hide file tree
Showing 816 changed files with 5,756 additions and 894 deletions.
137 changes: 135 additions & 2 deletions .github/workflows/wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,90 @@ concurrency:
cancel-in-progress: true

jobs:
build:
name: Build Wasm
# TODO: Replace this step with downloading a wasm binary from a set release of Barretenberg
build-barretenberg:
runs-on: ubuntu-latest
steps:
- name: Checkout Noir repo
uses: actions/checkout@v3

- name: Collect locked barretenberg rev
run: |
echo "BB_REV=$(jq -r .nodes.barretenberg.locked.rev ./flake.lock)" >> $GITHUB_ENV
echo "BB_REV is ${{ env.BB_REV }}"
- uses: cachix/install-nix-action@v20
with:
nix_path: nixpkgs=channel:nixos-22.11
github_access_token: ${{ secrets.GITHUB_TOKEN }}

- uses: cachix/cachix-action@v12
with:
name: barretenberg
authToken: "${{ secrets.CACHIX_AUTH_TOKEN }}"

# Upload does not work with symlinks, using this workaround:
# https://github.com/actions/upload-artifact/issues/92#issuecomment-1080347032
- name: Build barretenberg as libbarretenberg-wasm32
run: |
echo "BB_REV is ${{ env.BB_REV }}"
nix build "github:AztecProtocol/barretenberg/${{ env.BB_REV }}#wasm32"
echo "ARTIFACT_UPLOAD_PATH=$(readlink -f result)" >> $GITHUB_ENV
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: libbarretenberg-wasm32
path: ${{ env.ARTIFACT_UPLOAD_PATH }}
retention-days: 3

build-nargo:
needs: [build-barretenberg]
runs-on: ubuntu-22.04

steps:
- name: Checkout Noir repo
uses: actions/checkout@v3

- uses: actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Download artifact
uses: actions/download-artifact@v3
with:
name: libbarretenberg-wasm32
path: ${{ github.workspace }}/libbarretenberg-wasm32

- name: Setup toolchain
uses: dtolnay/[email protected]

- name: Build Nargo
env:
BARRETENBERG_BIN_DIR: ${{ github.workspace }}/libbarretenberg-wasm32/bin
run: |
cargo build --package nargo_cli --release --no-default-features --features plonk_bn254_wasm
- name: Package artifacts
run: |
mkdir dist
cp ./target/release/nargo ./dist/nargo
7z a -ttar -so -an ./dist/* | 7z a -si ./nargo-x86_64-unknown-linux-gnu.tar.gz
- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: nargo
path: ./dist/*
retention-days: 3

build-wasm:
runs-on: ubuntu-latest
steps:
- name: Checkout sources
Expand All @@ -25,3 +107,54 @@ jobs:
- name: Build wasm package
run: |
nix build -L .#wasm
- name: Dereference symlink
run: echo "UPLOAD_PATH=$(readlink -f result)" >> $GITHUB_ENV

- name: Upload artifact
uses: actions/upload-artifact@v3
with:
name: noir_wasm
path: ${{ env.UPLOAD_PATH }}
retention-days: 3

test:
needs: [build-wasm, build-nargo]
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout noir-lang/noir
uses: actions/checkout@v3

- name: Download wasm package artifact
uses: actions/download-artifact@v3
with:
name: noir_wasm
path: ./crates/wasm/dist

- name: Download nargo binary
uses: actions/download-artifact@v3
with:
name: nargo
path: ./nargo

- name: Compile test program with Nargo CLI
working-directory: ./crates/wasm/noir-script
run: |
nargo_binary=${{ github.workspace }}/nargo/nargo
chmod +x $nargo_binary
$nargo_binary compile
- name: Install dependencies
working-directory: ./crates/wasm
run: yarn install

- name: Install playwright deps
working-directory: ./crates/wasm
run: |
npx playwright install
npx playwright install-deps
- name: Run tests
working-directory: ./crates/wasm
run: yarn test:browser
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ result
*.vk
**/Verifier.toml
**/target
!crates/nargo_cli/tests/test_data/*/target
!crates/nargo_cli/tests/test_data/*/target/witness.tr
!crates/nargo_cli/tests/execution_success/*/target
!crates/nargo_cli/tests/execution_success/*/target/witness.tr
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ noirc_evaluator = { path = "crates/noirc_evaluator" }
noirc_frontend = { path = "crates/noirc_frontend" }
noir_wasm = { path = "crates/wasm" }
cfg-if = "1.0.0"
clap = { version = "4.1.4", features = ["derive"] }
clap = { version = "4.3.19", features = ["derive"] }
codespan = { version = "0.11.1", features = ["serialization"] }
codespan-lsp = "0.11.1"
codespan-reporting = "0.11.1"
Expand Down
1 change: 1 addition & 0 deletions crates/fm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ wasm-bindgen.workspace = true

[dev-dependencies]
tempfile = "3.2.0"
iter-extended.workspace = true
82 changes: 64 additions & 18 deletions crates/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub struct FileManager {
impl FileManager {
pub fn new(root: &Path) -> Self {
Self {
root: normalize_path(root),
root: root.normalize(),
file_map: Default::default(),
id_to_path: Default::default(),
path_to_id: Default::default(),
Expand All @@ -44,7 +44,7 @@ impl FileManager {
// TODO: The stdlib path should probably be an absolute path rooted in something people would never create
file_name.to_path_buf()
} else {
normalize_path(&self.root.join(file_name))
self.root.join(file_name).normalize()
};

// Check that the resolved path already exists in the file map, if it is, we return it.
Expand Down Expand Up @@ -80,7 +80,7 @@ impl FileManager {
self.id_to_path.get(&file_id).unwrap().0.as_path()
}

pub fn resolve_path(&mut self, anchor: FileId, mod_name: &str) -> Result<FileId, String> {
pub fn find_module(&mut self, anchor: FileId, mod_name: &str) -> Result<FileId, String> {
let mut candidate_files = Vec::new();

let anchor_path = self.path(anchor).to_path_buf();
Expand All @@ -101,13 +101,33 @@ impl FileManager {
}
}

/// Replacement for `std::fs::canonicalize` that doesn't verify the path exists.
///
/// Plucked from https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61
/// Advice from https://www.reddit.com/r/rust/comments/hkkquy/comment/fwtw53s/
fn normalize_path(path: &Path) -> PathBuf {
let mut components = path.components().peekable();
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
pub trait NormalizePath {
/// Replacement for `std::fs::canonicalize` that doesn't verify the path exists.
///
/// Plucked from https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61
/// Advice from https://www.reddit.com/r/rust/comments/hkkquy/comment/fwtw53s/
fn normalize(&self) -> PathBuf;
}

impl NormalizePath for PathBuf {
fn normalize(&self) -> PathBuf {
let components = self.components();
resolve_components(components)
}
}

impl NormalizePath for &Path {
fn normalize(&self) -> PathBuf {
let components = self.components();
resolve_components(components)
}
}

fn resolve_components<'a>(components: impl Iterator<Item = Component<'a>>) -> PathBuf {
let mut components = components.peekable();

// Preserve path prefix if one exists.
let mut normalized_path = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
components.next();
PathBuf::from(c.as_os_str())
} else {
Expand All @@ -116,20 +136,46 @@ fn normalize_path(path: &Path) -> PathBuf {

for component in components {
match component {
Component::Prefix(..) => unreachable!(),
Component::Prefix(..) => unreachable!("Path cannot contain multiple prefixes"),
Component::RootDir => {
ret.push(component.as_os_str());
normalized_path.push(component.as_os_str());
}
Component::CurDir => {}
Component::ParentDir => {
ret.pop();
normalized_path.pop();
}
Component::Normal(c) => {
ret.push(c);
normalized_path.push(c);
}
}
}
ret

normalized_path
}

#[cfg(test)]
mod path_normalization {
use iter_extended::vecmap;
use std::path::PathBuf;

use crate::NormalizePath;

#[test]
fn normalizes_paths_correctly() {
// Note that tests are run on unix so prefix handling can't be tested (as these only exist on Windows)
let test_cases = vecmap(
[
("/", "/"), // Handles root
("/foo/bar/../baz/../bar", "/foo/bar"), // Handles backtracking
("/././././././././baz", "/baz"), // Removes no-ops
],
|(unnormalized, normalized)| (PathBuf::from(unnormalized), PathBuf::from(normalized)),
);

for (path, expected_result) in test_cases {
assert_eq!(path.normalize(), expected_result);
}
}
}

/// Takes a path to a noir file. This will panic on paths to directories
Expand Down Expand Up @@ -165,7 +211,7 @@ mod tests {

let dep_file_name = Path::new("foo.nr");
create_dummy_file(&dir, dep_file_name);
fm.resolve_path(file_id, "foo").unwrap();
fm.find_module(file_id, "foo").unwrap();
}
#[test]
fn path_resolve_file_module_other_ext() {
Expand Down Expand Up @@ -212,10 +258,10 @@ mod tests {
create_dummy_file(&dir, Path::new(&format!("{}.nr", sub_dir_name)));

// First check for the sub_dir.nr file and add it to the FileManager
let sub_dir_file_id = fm.resolve_path(file_id, sub_dir_name).unwrap();
let sub_dir_file_id = fm.find_module(file_id, sub_dir_name).unwrap();

// Now check for files in it's subdirectory
fm.resolve_path(sub_dir_file_id, "foo").unwrap();
fm.find_module(sub_dir_file_id, "foo").unwrap();
}

/// Tests that two identical files that have different paths are treated as the same file
Expand Down
9 changes: 3 additions & 6 deletions crates/lsp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ use lsp_types::{
};
use noirc_driver::{check_crate, prepare_crate};
use noirc_errors::{DiagnosticKind, FileDiagnostic};
use noirc_frontend::{
graph::{CrateGraph, CrateType},
hir::Context,
};
use noirc_frontend::{graph::CrateGraph, hir::Context};
use serde_json::Value as JsonValue;
use tower::Service;

Expand Down Expand Up @@ -190,7 +187,7 @@ fn on_code_lens_request(
}
};

let crate_id = prepare_crate(&mut context, file_path, CrateType::Binary);
let crate_id = prepare_crate(&mut context, file_path);

// We ignore the warnings and errors produced by compilation for producing codelenses

Check warning on line 192 in crates/lsp/src/lib.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (codelenses)
// because we can still get the test functions even if compilation fails
Expand Down Expand Up @@ -283,7 +280,7 @@ fn on_did_save_text_document(
}
};

let crate_id = prepare_crate(&mut context, file_path, CrateType::Binary);
let crate_id = prepare_crate(&mut context, file_path);

let mut diagnostics = Vec::new();

Expand Down
Loading

0 comments on commit 45f586e

Please sign in to comment.