Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(crates): do not process relative dependencies twice #1856

Merged
merged 9 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 37 additions & 4 deletions crates/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,21 @@ pub struct FileManager {
impl FileManager {
// XXX: Maybe use a AsRef<Path> here, for API ergonomics
pub fn add_file(&mut self, path_to_file: &Path, file_type: FileType) -> Option<FileId> {
let source = file_reader::read_file_to_string(path_to_file).ok()?;
// Handle both relative file paths and std/lib virtual paths.
let base = Path::new(".").canonicalize().expect("Base path canonicalize failed");
let res = path_to_file.canonicalize().unwrap_or_else(|_| path_to_file.to_path_buf());
let resolved_path = res.strip_prefix(base).unwrap_or(&res);
jfecher marked this conversation as resolved.
Show resolved Hide resolved

// Check that the resolved path already exists in the file map, if it is, we return it.
let path_to_file = virtualize_path(resolved_path, file_type);
if let Some(file_id) = self.path_to_id.get(&path_to_file) {
return Some(*file_id);
}

let file_id = self.file_map.add_file(path_to_file.to_path_buf().into(), source);
let path_to_file = virtualize_path(path_to_file, file_type);
// Otherwise we add the file
let source = file_reader::read_file_to_string(resolved_path).ok()?;
let file_id = self.file_map.add_file(resolved_path.to_path_buf().into(), source);
self.register_path(file_id, path_to_file);

Some(file_id)
}

Expand Down Expand Up @@ -177,4 +186,28 @@ mod tests {
// Now check for files in it's subdirectory
fm.resolve_path(sub_dir_file_id, "foo").unwrap();
}

/// Tests that two identical files that have different paths are treated as the same file
/// e.g. if we start in the dir ./src and have a file ../../foo.nr
/// that should be treated as the same file as ../ starting in ./
/// they should both resolve to ../foo.nr
#[test]
fn path_resolve_modules_with_different_paths_as_same_file() {
let mut fm = FileManager::default();

// Create a lib.nr file at the root.
let dir = tempdir().unwrap();
let sub_dir = TempDir::new_in(&dir).unwrap();
let sub_sub_dir = TempDir::new_in(&sub_dir).unwrap();
let file_path = dummy_file_path(&dir, "lib.nr");

// Create another file in a subdirectory with a convoluted path
let second_file_path = dummy_file_path(&sub_sub_dir, "./../../lib.nr");

// Add both files to the file manager
let file_id = fm.add_file(&file_path, FileType::Root).unwrap();
let second_file_id = fm.add_file(&second_file_path, FileType::Root).unwrap();

assert_eq!(file_id, second_file_id);
}
}
7 changes: 7 additions & 0 deletions crates/nargo_cli/tests/test_data/diamond_deps_0/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
authors = [""]
compiler_version = "0.7.1"

[dependencies]
dep1 = { path = "../../test_libraries/diamond_deps_1" }
dep2 = { path = "../../test_libraries/diamond_deps_2" }
3 changes: 3 additions & 0 deletions crates/nargo_cli/tests/test_data/diamond_deps_0/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
x = 1
y = 1
return = 5
7 changes: 7 additions & 0 deletions crates/nargo_cli/tests/test_data/diamond_deps_0/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use dep::dep1::call_dep1_then_dep2;
use dep::dep2::call_dep2;
use dep::dep2::RESOLVE_THIS;

fn main(x : Field, y : pub Field) -> pub Field {
call_dep1_then_dep2(x, y) + call_dep2(x, y) + RESOLVE_THIS
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
authors = [""]
compiler_version = "0.7.1"

[dependencies]
dep2 = { path = "../diamond_deps_2" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use dep::dep2::call_dep2;

fn call_dep1_then_dep2(x : Field, y : Field) -> Field {
call_dep2(x, y)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.7.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global RESOLVE_THIS = 3;


fn call_dep2(x : Field, y : Field) -> Field {
x + y
}
6 changes: 5 additions & 1 deletion crates/noirc_frontend/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ impl CrateGraph {
pub fn add_crate_root(&mut self, crate_type: CrateType, file_id: FileId) -> CrateId {
let mut roots_with_file_id =
self.arena.iter().filter(|(_, crate_data)| crate_data.root_file_id == file_id);
assert!(roots_with_file_id.next().is_none(), "you cannot add the same file id twice");
jfecher marked this conversation as resolved.
Show resolved Hide resolved

let next_file_id = roots_with_file_id.next();
if let Some(file_id) = next_file_id {
return *file_id.0;
}

let data = CrateData { root_file_id: file_id, crate_type, dependencies: Vec::new() };
let crate_id = CrateId(self.arena.len());
Expand Down