Skip to content

Commit ebfa266

Browse files
bors[bot]pnkfelix
andcommitted
Merge #818
818: In `RootConfig::contains`, check against canonicalized version of root path r=matklad a=pnkfelix In `RootConfig::contains`, check against canonicalized version of root path since OS may hand us data that uses the canonical form rather than the root as specified by the user. This is a step towards a resolution of issue #734 but does not completely fix the problem there. Co-authored-by: Felix S. Klock II <pnkfelix@pnkfx.org>
2 parents cb4327b + da300fd commit ebfa266

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

crates/ra_vfs/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ impl_arena_id!(VfsRoot);
4444
/// returns `true` iff path belongs to the source root
4545
pub(crate) struct RootConfig {
4646
root: PathBuf,
47+
// result of `root.canonicalize()` if that differs from `root`; `None` otherwise.
48+
canonical_root: Option<PathBuf>,
4749
excluded_dirs: Vec<PathBuf>,
4850
}
4951

@@ -60,15 +62,26 @@ impl std::ops::Deref for Roots {
6062

6163
impl RootConfig {
6264
fn new(root: PathBuf, excluded_dirs: Vec<PathBuf>) -> RootConfig {
63-
RootConfig { root, excluded_dirs }
65+
let mut canonical_root = root.canonicalize().ok();
66+
if Some(&root) == canonical_root.as_ref() {
67+
canonical_root = None;
68+
}
69+
RootConfig { root, canonical_root, excluded_dirs }
6470
}
6571
/// Checks if root contains a path and returns a root-relative path.
6672
pub(crate) fn contains(&self, path: &Path) -> Option<RelativePathBuf> {
6773
// First, check excluded dirs
6874
if self.excluded_dirs.iter().any(|it| path.starts_with(it)) {
6975
return None;
7076
}
71-
let rel_path = path.strip_prefix(&self.root).ok()?;
77+
let rel_path = path
78+
.strip_prefix(&self.root)
79+
.or_else(|err_payload| {
80+
self.canonical_root
81+
.as_ref()
82+
.map_or(Err(err_payload), |canonical_root| path.strip_prefix(canonical_root))
83+
})
84+
.ok()?;
7285
let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
7386

7487
// Ignore some common directories.

0 commit comments

Comments
 (0)