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
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"); // Should never fail
jfecher marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
8 changes: 7 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,13 @@ 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();
assert!(next_file_id.is_none(), "you cannot add the same file id twice");

if let Some(file_id) = next_file_id {
return *file_id.0;
}
jfecher marked this conversation as resolved.
Show resolved Hide resolved

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