From 8536c7c8ea8fc6b740b2ae6d1aef3bc7e1907b8c Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Thu, 15 Feb 2024 11:47:11 +0000 Subject: [PATCH] fix: only add `.nr` files to file manager (#4380) # Description ## Problem\* Resolves https://github.com/noir-lang/noir/issues/4379 ## Summary\* This PR adds a filter so we only add files with a `.nr` extension to the file manager. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- tooling/nargo/src/lib.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/tooling/nargo/src/lib.rs b/tooling/nargo/src/lib.rs index 0fdff8b202f..e12bf4d4ad1 100644 --- a/tooling/nargo/src/lib.rs +++ b/tooling/nargo/src/lib.rs @@ -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}, @@ -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)); @@ -125,6 +125,15 @@ 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> { + 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. @@ -132,7 +141,10 @@ pub fn prepare_package<'file_manager, 'parsed_files>( // 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> { +fn get_all_paths_in_dir( + dir: &std::path::Path, + predicate: fn(&std::path::Path) -> bool, +) -> std::io::Result> { assert!(dir.is_dir(), "directory {dir:?} is not a path to a directory"); let mut paths = Vec::new(); @@ -142,9 +154,9 @@ fn get_all_paths_in_dir(dir: &std::path::Path) -> std::io::Result