From 4bdaa3bbdd2061fea454f74f561fd2c654da7715 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 11 Dec 2023 14:25:23 +0000 Subject: [PATCH 1/3] add file with source with canonical path --- compiler/noirc_driver/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_driver/src/lib.rs b/compiler/noirc_driver/src/lib.rs index c7441f96259..3fb164472e4 100644 --- a/compiler/noirc_driver/src/lib.rs +++ b/compiler/noirc_driver/src/lib.rs @@ -81,7 +81,7 @@ pub fn prepare_crate(context: &mut Context, file_name: &Path) -> CrateId { // to manually add it here. let stdlib_paths_with_source = stdlib::stdlib_paths_with_source(); for (path, source) in stdlib_paths_with_source { - context.file_manager.add_file_with_source(Path::new(&path), source); + context.file_manager.add_file_with_source_canonical_path(Path::new(&path), source); } let path_to_std_lib_file = Path::new(STD_CRATE_NAME).join("lib.nr"); From d398cc65cbc495dbbfe38c734c16252a908fec37 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 11 Dec 2023 14:26:11 +0000 Subject: [PATCH 2/3] add two methods so that we can keep the existing behavior --- compiler/fm/src/lib.rs | 52 ++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/compiler/fm/src/lib.rs b/compiler/fm/src/lib.rs index bc60fa3087e..3f1f5e09acc 100644 --- a/compiler/fm/src/lib.rs +++ b/compiler/fm/src/lib.rs @@ -46,8 +46,16 @@ impl FileManager { &self.file_map } - // Adds a file to the file manager with its source code pub fn add_file_with_source(&mut self, file_name: &Path, source: String) -> Option { + let file_name = self.root.join(file_name).normalize(); + self.add_file_with_source_canonical_path(&file_name, source) + } + + pub fn add_file_with_source_canonical_path( + &mut self, + file_name: &Path, + source: String, + ) -> Option { let file_name = file_name.normalize(); // Check that the file name already exists in the file map, if it is, we return it. if let Some(file_id) = self.path_to_id.get(&file_name) { @@ -289,30 +297,30 @@ mod tests { fm.find_module(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 dir = tempdir().unwrap(); - // let sub_dir = TempDir::new_in(&dir).unwrap(); - // let sub_sub_dir = TempDir::new_in(&sub_dir).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 dir = tempdir().unwrap(); + let sub_dir = TempDir::new_in(&dir).unwrap(); + let sub_sub_dir = TempDir::new_in(&sub_dir).unwrap(); - // let mut fm = FileManager::new(dir.path()); + let mut fm = FileManager::new(dir.path()); - // // Create a lib.nr file at the root. - // let file_name = Path::new("lib.nr"); - // create_dummy_file(&dir, file_name); + // Create a lib.nr file at the root. + let file_name = Path::new("lib.nr"); + create_dummy_file(&dir, file_name); - // // Create another path with `./` and `../` inside it - // let second_file_name = PathBuf::from(sub_sub_dir.path()).join("./../../lib.nr"); + // Create another path with `./` and `../` inside it + let second_file_name = PathBuf::from(sub_sub_dir.path()).join("./../../lib.nr"); - // // Add both files to the file manager - // let file_id = fm.add_file_with_source(file_name, "fn foo() {}".to_string()).unwrap(); - // let second_file_id = - // fm.add_file_with_source(&second_file_name, "fn foo() {}".to_string()).unwrap(); + // Add both files to the file manager + let file_id = fm.add_file_with_source(file_name, "fn foo() {}".to_string()).unwrap(); + let second_file_id = + fm.add_file_with_source(&second_file_name, "fn foo() {}".to_string()).unwrap(); - // assert_eq!(file_id, second_file_id); - // } + assert_eq!(file_id, second_file_id); + } } From d8717a6e57a48212df05873b9d7bfa916301fb34 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Mon, 11 Dec 2023 14:56:52 +0000 Subject: [PATCH 3/3] Fix root path -- we could likely get rid of paths and just have a components struct to match the usePaths --- compiler/wasm/src/compile.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/wasm/src/compile.rs b/compiler/wasm/src/compile.rs index 1dae8c11e08..13b366819b0 100644 --- a/compiler/wasm/src/compile.rs +++ b/compiler/wasm/src/compile.rs @@ -235,7 +235,7 @@ pub fn compile( // For all intents and purposes, the file manager being returned // should be considered as immutable. fn file_manager_with_source_map(source_map: PathToFileSourceMap) -> FileManager { - let root = Path::new("/"); + let root = Path::new(""); let mut fm = FileManager::new(root); for (path, source) in source_map.0 {