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: only add .nr files to file manager #4380

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Changes from all 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
26 changes: 19 additions & 7 deletions tooling/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod workspace;

use std::collections::BTreeMap;

use fm::FileManager;
use fm::{FileManager, FILE_EXTENSION};
use noirc_driver::{add_dep, prepare_crate, prepare_dependency};
use noirc_frontend::{
graph::{CrateId, CrateName},
Expand Down Expand Up @@ -69,8 +69,8 @@ fn insert_all_files_for_package_into_file_manager(
.clone();

// Get all files in the package and add them to the file manager
let paths =
get_all_paths_in_dir(entry_path_parent).expect("could not get all paths in the package");
let paths = get_all_noir_source_in_dir(entry_path_parent)
.expect("could not get all paths in the package");
for path in paths {
let source = std::fs::read_to_string(path.as_path())
.unwrap_or_else(|_| panic!("could not read file {:?} into string", path));
Expand Down Expand Up @@ -125,14 +125,26 @@ pub fn prepare_package<'file_manager, 'parsed_files>(
(context, crate_id)
}

// Get all Noir source files in the directory and subdirectories.
//
// Panics: If the path is not a path to a directory.
fn get_all_noir_source_in_dir(dir: &std::path::Path) -> std::io::Result<Vec<std::path::PathBuf>> {
get_all_paths_in_dir(dir, |path| {
path.extension().map_or(false, |extension| extension == FILE_EXTENSION)
})
}

// Get all paths in the directory and subdirectories.
//
// Panics: If the path is not a path to a directory.
//
// TODO: Along with prepare_package, this function is an abstraction leak
// TODO: given that this crate should not know about the file manager.
// TODO: We can clean this up in a future refactor
fn get_all_paths_in_dir(dir: &std::path::Path) -> std::io::Result<Vec<std::path::PathBuf>> {
fn get_all_paths_in_dir(
dir: &std::path::Path,
predicate: fn(&std::path::Path) -> bool,
) -> std::io::Result<Vec<std::path::PathBuf>> {
assert!(dir.is_dir(), "directory {dir:?} is not a path to a directory");

let mut paths = Vec::new();
Expand All @@ -142,9 +154,9 @@ fn get_all_paths_in_dir(dir: &std::path::Path) -> std::io::Result<Vec<std::path:
let entry = entry?;
let path = entry.path();
if path.is_dir() {
let mut sub_paths = get_all_paths_in_dir(&path)?;
let mut sub_paths = get_all_paths_in_dir(&path, predicate)?;
paths.append(&mut sub_paths);
} else {
} else if predicate(&path) {
paths.push(path);
}
}
Expand Down Expand Up @@ -177,7 +189,7 @@ mod tests {
create_test_dir_structure(temp_dir.path())
.expect("could not create test directory structure");

let paths = get_all_paths_in_dir(temp_dir.path())
let paths = get_all_paths_in_dir(temp_dir.path(), |_| true)
.expect("could not get all paths in the test directory");

// This should be the paths to all of the files in the directory and the subdirectory
Expand Down
Loading