From b388c5086d98e709254b33bafeb0f42e1b5edc8f Mon Sep 17 00:00:00 2001 From: Wilfred Hughes Date: Fri, 5 Apr 2024 15:40:43 -0700 Subject: [PATCH 1/2] [vfs] Don't confuse paths with source roots that have the same prefix --- crates/vfs/src/file_set.rs | 6 ++++++ crates/vfs/src/file_set/tests.rs | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index 7eeb10d544af..3ea8806de38a 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -170,6 +170,12 @@ impl FileSetConfigBuilder { for p in paths { let mut buf = Vec::new(); p.encode(&mut buf); + + // A root is a directory, so append a trailing separator to the string + // used in prefix checks. This ensures that r-a doesn't think that + // `/foo/bar.rs` lives in the `/foo/b/` root. + buf.extend(std::path::MAIN_SEPARATOR.to_string().as_bytes()); + entries.push((buf, i as u64)); } } diff --git a/crates/vfs/src/file_set/tests.rs b/crates/vfs/src/file_set/tests.rs index 2146df185d3a..3cdb60dcb260 100644 --- a/crates/vfs/src/file_set/tests.rs +++ b/crates/vfs/src/file_set/tests.rs @@ -40,3 +40,26 @@ fn name_prefix() { let partition = file_set.partition(&vfs).into_iter().map(|it| it.len()).collect::>(); assert_eq!(partition, vec![1, 1, 0]); } + +/// Ensure that we don't consider `/foo/bar_baz.rs` to be in the +/// `/foo/bar/` root. +#[test] +fn name_prefix_partially_matches() { + let mut file_set = FileSetConfig::builder(); + file_set.add_file_set(vec![VfsPath::new_virtual_path("/foo".into())]); + file_set.add_file_set(vec![VfsPath::new_virtual_path("/foo/bar".into())]); + let file_set = file_set.build(); + + let mut vfs = Vfs::default(); + + // These two are both in /foo. + vfs.set_file_contents(VfsPath::new_virtual_path("/foo/lib.rs".into()), Some(Vec::new())); + vfs.set_file_contents(VfsPath::new_virtual_path("/foo/bar_baz.rs".into()), Some(Vec::new())); + + // Only this file is in /foo/bar. + vfs.set_file_contents(VfsPath::new_virtual_path("/foo/bar/biz.rs".into()), Some(Vec::new())); + + let partition = file_set.partition(&vfs).into_iter().map(|it| it.len()).collect::>(); + + assert_eq!(partition, vec![2, 1, 0]); +} From 8885d3084bd0d6840ee385b0b2554bf1acfcb077 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 6 Apr 2024 09:13:30 +0200 Subject: [PATCH 2/2] Drop unnecessary allocaton in crates/vfs/src/file_set.rs --- crates/vfs/src/file_set.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/vfs/src/file_set.rs b/crates/vfs/src/file_set.rs index 3ea8806de38a..b48d9281f397 100644 --- a/crates/vfs/src/file_set.rs +++ b/crates/vfs/src/file_set.rs @@ -174,7 +174,7 @@ impl FileSetConfigBuilder { // A root is a directory, so append a trailing separator to the string // used in prefix checks. This ensures that r-a doesn't think that // `/foo/bar.rs` lives in the `/foo/b/` root. - buf.extend(std::path::MAIN_SEPARATOR.to_string().as_bytes()); + buf.extend(std::path::MAIN_SEPARATOR_STR.as_bytes()); entries.push((buf, i as u64)); }