Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7e0a3d1
make `find_module` be an immutable reference
kevaundray Dec 10, 2023
b2ff05b
add method in nargo that pre-populates the FileManager
kevaundray Dec 10, 2023
5592348
change add_file to `name_to_id` -- we assume that the file manager ha…
kevaundray Dec 10, 2023
d5fd4e0
cargo
kevaundray Dec 10, 2023
2f137b7
Update tooling/nargo/src/lib.rs
kevaundray Dec 10, 2023
41ae81f
add a method in file manager that allows us to add a file with its so…
kevaundray Dec 10, 2023
c7f576c
add deprecation TODO to file_reader
kevaundray Dec 10, 2023
a2460e6
add stdlib file in noirc_driver that returns the stdlib paths alongsi…
kevaundray Dec 10, 2023
270a0f4
add the contents of the stdlib whenever we call prepare_crate
kevaundray Dec 10, 2023
52d33a5
cargo
kevaundray Dec 10, 2023
a38c27c
cargo fmt
kevaundray Dec 10, 2023
6a5bf33
move tempfile to dev dependencies
kevaundray Dec 10, 2023
f72ce99
add files into file manager in test since find_module does not add fi…
kevaundray Dec 10, 2023
b30ad54
insert all files for this packages dependencies into the file manager…
kevaundray Dec 10, 2023
15a94ee
cargo fmt
kevaundray Dec 10, 2023
4599361
remove un-needed fully qualified path
kevaundray Dec 10, 2023
f20109e
Add note on stdlib
kevaundray Dec 10, 2023
4413350
cargo fmt
kevaundray Dec 10, 2023
d6a24e8
add comment to process_dep_graph and fix setup_test_context
kevaundray Dec 11, 2023
552a4dd
replace expect with unwrap_or_else: expect doesn't allow you to add p…
kevaundray Dec 11, 2023
3f8551b
try: add a `PathToFileSourceMap` object
kevaundray Dec 11, 2023
dd94b46
remove extraneous forward slash
kevaundray Dec 11, 2023
3495de5
add file_manager_with_source_map method so we ensure that FileManager…
kevaundray Dec 11, 2023
851fab1
add expect
kevaundray Dec 11, 2023
3cd35fd
fix node tests
kevaundray Dec 11, 2023
d2590d5
fix browser tests and naming nit
kevaundray Dec 11, 2023
5b867d5
chore!: Remove `add_file` and `file_reader` from FileManager (#3762)
kevaundray Dec 11, 2023
5ebf83a
Update compiler/fm/src/lib.rs
kevaundray Dec 11, 2023
7a787d7
Merge branch 'master' into kw/make-fm-read-only
kevaundray Dec 11, 2023
419273e
file_reader is no longer being used
kevaundray Dec 11, 2023
d120852
Merge branch 'master' into kw/make-fm-read-only
kevaundray Dec 11, 2023
1e9ac97
chore: add simple doc comments for `add_file_with_source` methods
TomAFrench Dec 13, 2023
cc92c07
Merge remote-tracking branch 'origin/master' into kw/make-fm-read-only
kevaundray Dec 13, 2023
9aa0bb7
Update tooling/nargo/src/lib.rs
kevaundray Dec 13, 2023
3e26e3c
read entry_path and get all files in its parent
kevaundray Dec 13, 2023
0b53332
clippy
kevaundray Dec 13, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions compiler/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ impl FileManager {
self.id_to_path.get(&file_id).unwrap().as_path()
}

pub fn find_module(&mut self, anchor: FileId, mod_name: &str) -> Result<FileId, String> {
pub fn find_module(&self, anchor: FileId, mod_name: &str) -> Result<FileId, String> {
let anchor_path = self.path(anchor).with_extension("");
let anchor_dir = anchor_path.parent().unwrap();

Expand All @@ -107,9 +107,10 @@ impl FileManager {
anchor_path.join(format!("{mod_name}.{FILE_EXTENSION}"))
};

self.add_file(&candidate).ok_or_else(|| candidate.as_os_str().to_string_lossy().to_string())
self.name_to_id(candidate.clone()).ok_or_else(|| candidate.as_os_str().to_string_lossy().to_string())
}

// TODO: This should accept a &Path instead of a PathBuf
Comment thread
TomAFrench marked this conversation as resolved.
pub fn name_to_id(&self, file_name: PathBuf) -> Option<FileId> {
self.file_map.get_file_id(&PathString::from_path(file_name))
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub fn prepare_crate(context: &mut Context, file_name: &Path) -> CrateId {
let std_file_id = context.file_manager.add_file(&path_to_std_lib_file).unwrap();
let std_crate_id = context.crate_graph.add_stdlib(std_file_id);

let root_file_id = context.file_manager.add_file(file_name).unwrap();
let root_file_id = context.file_manager.name_to_id(file_name.to_path_buf()).unwrap();

let root_crate_id = context.crate_graph.add_crate_root(root_file_id);

Expand All @@ -89,7 +89,7 @@ pub fn prepare_crate(context: &mut Context, file_name: &Path) -> CrateId {

// Adds the file from the file system at `Path` to the crate graph
pub fn prepare_dependency(context: &mut Context, file_name: &Path) -> CrateId {
let root_file_id = context.file_manager.add_file(file_name).unwrap();
let root_file_id = context.file_manager.name_to_id(file_name.to_path_buf()).expect("files are expected to be added to the FileManager before reaching the compiler");

let crate_id = context.crate_graph.add_crate(root_file_id);

Expand Down
5 changes: 4 additions & 1 deletion tooling/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ iter-extended.workspace = true
serde.workspace = true
thiserror.workspace = true
codespan-reporting.workspace = true
rayon = "1.8.0"
rayon = "1.8.0"
# TODO: This dependency is used to generate unit tests for `get_all_paths_in_dir`
# TODO: once that method is moved to nargo_cli, we can move this dependency to nargo_cli
tempfile = "3.2.0"
87 changes: 86 additions & 1 deletion tooling/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,29 @@ pub fn prepare_dependencies(
}
}

// We will pre-populate the file manager with all the files in the package
// This is so that we can avoid having to read from disk when we are compiling
//
// This does not require parsing because we are interested in the files under the src directory
// it may turn out that we do not need to include some Noir files that we add to the file
// manager
pub fn insert_all_files_for_package_into_file_manager(package : &Package, file_manager : &mut FileManager) {
// Start off at the root directory of the package and add all of the files located
// in that directory.
let root_path = package.root_dir.clone();

// Get all files in the package and add them to the file manager
let paths = get_all_paths_in_dir(&root_path).expect("could not get all paths in the package");
Comment thread
kevaundray marked this conversation as resolved.
Outdated
for path in paths {
file_manager.add_file(path.as_path());
}
}

pub fn prepare_package(package: &Package, file_reader: Box<FileReader>) -> (Context, CrateId) {
// TODO: FileManager continues to leak into various crates
Comment thread
kevaundray marked this conversation as resolved.
Outdated
let fm = FileManager::new(&package.root_dir, file_reader);
let mut fm = FileManager::new(&package.root_dir, file_reader);
insert_all_files_for_package_into_file_manager(package, &mut fm);

let graph = CrateGraph::default();
let mut context = Context::new(fm, graph);

Expand All @@ -54,3 +74,68 @@ pub fn prepare_package(package: &Package, file_reader: Box<FileReader>) -> (Cont

(context, crate_id)
}

// 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.
Comment thread
kevaundray marked this conversation as resolved.
Outdated
// 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>> {
assert!(dir.is_dir(), "directory {dir:?} is not a path to a directory");

let mut paths = Vec::new();

if dir.is_dir() {
for entry in std::fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
let mut sub_paths = get_all_paths_in_dir(&path)?;
paths.append(&mut sub_paths);
} else {
paths.push(path);
}
}
}

Ok(paths)
}

#[cfg(test)]
mod tests {
use crate::get_all_paths_in_dir;
use std::{fs::{self, File}, path::Path};
use tempfile::tempdir;

fn create_test_dir_structure(temp_dir: &Path) -> std::io::Result<()> {
fs::create_dir(temp_dir.join("subdir1"))?;
File::create(temp_dir.join("subdir1/file1.txt"))?;
fs::create_dir(temp_dir.join("subdir2"))?;
File::create(temp_dir.join("subdir2/file2.txt"))?;
File::create(temp_dir.join("file3.txt"))?;
Ok(())
}

#[test]
fn test_get_all_paths_in_dir() {
let temp_dir = tempdir().expect("could not create a temporary directory");
create_test_dir_structure(temp_dir.path()).expect("could not create test directory structure");

let paths = get_all_paths_in_dir(temp_dir.path()).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
let expected_paths = vec![
temp_dir.path().join("file3.txt"),
temp_dir.path().join("subdir1/file1.txt"),
temp_dir.path().join("subdir2/file2.txt"),
];

assert_eq!(paths.len(), expected_paths.len());
for path in expected_paths {
assert!(paths.contains(&path));
}
}

}
6 changes: 4 additions & 2 deletions tooling/nargo_cli/src/cli/fmt_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::{fs::DirEntry, path::Path};

use clap::Args;
use fm::FileManager;
use nargo::insert_all_files_for_package_into_file_manager;
use nargo_toml::{get_package_manifest, resolve_workspace_from_toml, PackageSelection};
use noirc_driver::NOIR_ARTIFACT_VERSION_STRING;
use noirc_errors::CustomDiagnostic;
Expand Down Expand Up @@ -37,9 +38,10 @@ pub(crate) fn run(args: FormatCommand, config: NargoConfig) -> Result<(), CliErr
for package in &workspace {
let mut file_manager =
FileManager::new(&package.root_dir, Box::new(|path| std::fs::read_to_string(path)));

insert_all_files_for_package_into_file_manager(package, &mut file_manager);

visit_noir_files(&package.root_dir.join("src"), &mut |entry| {
let file_id = file_manager.add_file(&entry.path()).expect("file exists");
let file_id = file_manager.name_to_id(entry.path().to_path_buf()).expect("The file should exist since we added all files in the package into the file manager");
let (parsed_module, errors) = parse_file(&file_manager, file_id);

let is_all_warnings = errors.iter().all(ParserError::is_warning);
Expand Down