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

chore: try using two methods for adding files; one which assumes the path is canonical and the other which adds the root and canonicalizes #3763

Merged
merged 3 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
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
52 changes: 30 additions & 22 deletions compiler/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FileId> {
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<FileId> {
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) {
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion compiler/wasm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading