aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_vfs/Cargo.toml18
-rw-r--r--crates/ra_vfs/src/io.rs286
-rw-r--r--crates/ra_vfs/src/lib.rs296
-rw-r--r--crates/ra_vfs/src/roots.rs108
-rw-r--r--crates/ra_vfs/tests/vfs.rs222
5 files changed, 0 insertions, 930 deletions
diff --git a/crates/ra_vfs/Cargo.toml b/crates/ra_vfs/Cargo.toml
deleted file mode 100644
index fdaf31b9c..000000000
--- a/crates/ra_vfs/Cargo.toml
+++ /dev/null
@@ -1,18 +0,0 @@
1[package]
2edition = "2018"
3name = "ra_vfs"
4version = "0.1.0"
5authors = ["rust-analyzer developers"]
6
7[dependencies]
8walkdir = "2.2.7"
9relative-path = "0.4.0"
10rustc-hash = "1.0"
11crossbeam-channel = "0.3.5"
12log = "0.4.6"
13notify = "4.0.9"
14parking_lot = "0.7.0"
15
16[dev-dependencies]
17tempfile = "3"
18flexi_logger = "0.10.0"
diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs
deleted file mode 100644
index 5969ee0d0..000000000
--- a/crates/ra_vfs/src/io.rs
+++ /dev/null
@@ -1,286 +0,0 @@
1use std::{
2 fs,
3 path::{Path, PathBuf},
4 sync::{mpsc, Arc},
5 time::Duration,
6 thread,
7};
8use crossbeam_channel::{Sender, Receiver, unbounded, RecvError, select};
9use relative_path::RelativePathBuf;
10use walkdir::WalkDir;
11use notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher as _Watcher};
12
13use crate::{Roots, VfsRoot, VfsTask};
14
15pub(crate) enum Task {
16 AddRoot { root: VfsRoot },
17}
18
19/// `TaskResult` transfers files read on the IO thread to the VFS on the main
20/// thread.
21#[derive(Debug)]
22pub(crate) enum TaskResult {
23 /// Emitted when we've recursively scanned a source root during the initial
24 /// load.
25 BulkLoadRoot { root: VfsRoot, files: Vec<(RelativePathBuf, String)> },
26 /// Emitted when we've noticed that a single file has changed.
27 ///
28 /// Note that this by design does not distinguish between
29 /// create/delete/write events, and instead specifies the *current* state of
30 /// the file. The idea is to guarantee that in the quiescent state the sum
31 /// of all results equals to the current state of the file system, while
32 /// allowing to skip intermediate events in non-quiescent states.
33 SingleFile { root: VfsRoot, path: RelativePathBuf, text: Option<String> },
34}
35
36/// The kind of raw notification we've received from the notify library.
37///
38/// Note that these are not necessary 100% precise (for example we might receive
39/// `Create` instead of `Write`, see #734), but we try do distinguish `Create`s
40/// to implement recursive watching of directories.
41#[derive(Debug)]
42enum ChangeKind {
43 Create,
44 Write,
45 Remove,
46}
47
48const WATCHER_DELAY: Duration = Duration::from_millis(250);
49
50// Like thread::JoinHandle, but joins the thread on drop.
51//
52// This is useful because it guarantees the absence of run-away threads, even if
53// code panics. This is important, because we might see panics in the test and
54// we might be used in an IDE context, where a failed component is just
55// restarted.
56//
57// Because all threads are joined, care must be taken to avoid deadlocks. That
58// typically means ensuring that channels are dropped before the threads.
59struct ScopedThread(Option<thread::JoinHandle<()>>);
60
61impl ScopedThread {
62 fn spawn(name: String, f: impl FnOnce() + Send + 'static) -> ScopedThread {
63 let handle = thread::Builder::new().name(name).spawn(f).unwrap();
64 ScopedThread(Some(handle))
65 }
66}
67
68impl Drop for ScopedThread {
69 fn drop(&mut self) {
70 let res = self.0.take().unwrap().join();
71 if !thread::panicking() {
72 res.unwrap();
73 }
74 }
75}
76
77pub(crate) struct Worker {
78 // XXX: field order is significant here.
79 //
80 // In Rust, fields are dropped in the declaration order, and we rely on this
81 // here. We must close sender first, so that the `thread` (who holds the
82 // opposite side of the channel) noticed shutdown. Then, we must join the
83 // thread, but we must keep receiver alive so that the thread does not
84 // panic.
85 pub(crate) sender: Sender<Task>,
86 _thread: ScopedThread,
87 pub(crate) receiver: Receiver<VfsTask>,
88}
89
90pub(crate) fn start(roots: Arc<Roots>) -> Worker {
91 // This is a pretty elaborate setup of threads & channels! It is
92 // explained by the following concerns:
93 // * we need to burn a thread translating from notify's mpsc to
94 // crossbeam_channel.
95 // * we want to read all files from a single thread, to guarantee that
96 // we always get fresher versions and never go back in time.
97 // * we want to tear down everything neatly during shutdown.
98 let _thread;
99 // This are the channels we use to communicate with outside world.
100 // If `input_receiver` is closed we need to tear ourselves down.
101 // `output_sender` should not be closed unless the parent died.
102 let (input_sender, input_receiver) = unbounded();
103 let (output_sender, output_receiver) = unbounded();
104
105 _thread = ScopedThread::spawn("vfs".to_string(), move || {
106 // Make sure that the destruction order is
107 //
108 // * notify_sender
109 // * _thread
110 // * watcher_sender
111 //
112 // this is required to avoid deadlocks.
113
114 // These are the corresponding crossbeam channels
115 let (watcher_sender, watcher_receiver) = unbounded();
116 let _notify_thread;
117 {
118 // These are `std` channels notify will send events to
119 let (notify_sender, notify_receiver) = mpsc::channel();
120
121 let mut watcher = notify::watcher(notify_sender, WATCHER_DELAY)
122 .map_err(|e| log::error!("failed to spawn notify {}", e))
123 .ok();
124 // Start a silly thread to transform between two channels
125 _notify_thread = ScopedThread::spawn("notify-convertor".to_string(), move || {
126 notify_receiver
127 .into_iter()
128 .for_each(|event| convert_notify_event(event, &watcher_sender))
129 });
130
131 // Process requests from the called or notifications from
132 // watcher until the caller says stop.
133 loop {
134 select! {
135 // Received request from the caller. If this channel is
136 // closed, we should shutdown everything.
137 recv(input_receiver) -> t => match t {
138 Err(RecvError) => {
139 drop(input_receiver);
140 break
141 },
142 Ok(Task::AddRoot { root }) => {
143 watch_root(watcher.as_mut(), &output_sender, &*roots, root);
144 }
145 },
146 // Watcher send us changes. If **this** channel is
147 // closed, the watcher has died, which indicates a bug
148 // -- escalate!
149 recv(watcher_receiver) -> event => match event {
150 Err(RecvError) => panic!("watcher is dead"),
151 Ok((path, change)) => {
152 handle_change(watcher.as_mut(), &output_sender, &*roots, path, change);
153 }
154 },
155 }
156 }
157 }
158 // Drain pending events: we are not interested in them anyways!
159 watcher_receiver.into_iter().for_each(|_| ());
160 });
161 Worker { sender: input_sender, _thread, receiver: output_receiver }
162}
163
164fn watch_root(
165 watcher: Option<&mut RecommendedWatcher>,
166 sender: &Sender<VfsTask>,
167 roots: &Roots,
168 root: VfsRoot,
169) {
170 let root_path = roots.path(root);
171 log::debug!("loading {} ...", root_path.display());
172 let files = watch_recursive(watcher, root_path, roots, root)
173 .into_iter()
174 .filter_map(|path| {
175 let abs_path = path.to_path(&root_path);
176 let text = read_to_string(&abs_path)?;
177 Some((path, text))
178 })
179 .collect();
180 let res = TaskResult::BulkLoadRoot { root, files };
181 sender.send(VfsTask(res)).unwrap();
182 log::debug!("... loaded {}", root_path.display());
183}
184
185fn convert_notify_event(event: DebouncedEvent, sender: &Sender<(PathBuf, ChangeKind)>) {
186 // forward relevant events only
187 match event {
188 DebouncedEvent::NoticeWrite(_)
189 | DebouncedEvent::NoticeRemove(_)
190 | DebouncedEvent::Chmod(_) => {
191 // ignore
192 }
193 DebouncedEvent::Rescan => {
194 // TODO: rescan all roots
195 }
196 DebouncedEvent::Create(path) => {
197 sender.send((path, ChangeKind::Create)).unwrap();
198 }
199 DebouncedEvent::Write(path) => {
200 sender.send((path, ChangeKind::Write)).unwrap();
201 }
202 DebouncedEvent::Remove(path) => {
203 sender.send((path, ChangeKind::Remove)).unwrap();
204 }
205 DebouncedEvent::Rename(src, dst) => {
206 sender.send((src, ChangeKind::Remove)).unwrap();
207 sender.send((dst, ChangeKind::Create)).unwrap();
208 }
209 DebouncedEvent::Error(err, path) => {
210 // TODO: should we reload the file contents?
211 log::warn!("watcher error \"{}\", {:?}", err, path);
212 }
213 }
214}
215
216fn handle_change(
217 watcher: Option<&mut RecommendedWatcher>,
218 sender: &Sender<VfsTask>,
219 roots: &Roots,
220 path: PathBuf,
221 kind: ChangeKind,
222) {
223 let (root, rel_path) = match roots.find(&path) {
224 None => return,
225 Some(it) => it,
226 };
227 match kind {
228 ChangeKind::Create => {
229 let mut paths = Vec::new();
230 if path.is_dir() {
231 paths.extend(watch_recursive(watcher, &path, roots, root));
232 } else {
233 paths.push(rel_path);
234 }
235 paths
236 .into_iter()
237 .try_for_each(|rel_path| {
238 let abs_path = rel_path.to_path(&roots.path(root));
239 let text = read_to_string(&abs_path);
240 let res = TaskResult::SingleFile { root, path: rel_path, text };
241 sender.send(VfsTask(res))
242 })
243 .unwrap()
244 }
245 ChangeKind::Write | ChangeKind::Remove => {
246 let text = read_to_string(&path);
247 let res = TaskResult::SingleFile { root, path: rel_path, text };
248 sender.send(VfsTask(res)).unwrap();
249 }
250 }
251}
252
253fn watch_recursive(
254 mut watcher: Option<&mut RecommendedWatcher>,
255 dir: &Path,
256 roots: &Roots,
257 root: VfsRoot,
258) -> Vec<RelativePathBuf> {
259 let mut files = Vec::new();
260 for entry in WalkDir::new(dir)
261 .into_iter()
262 .filter_entry(|it| roots.contains(root, it.path()).is_some())
263 .filter_map(|it| it.map_err(|e| log::warn!("watcher error: {}", e)).ok())
264 {
265 if entry.file_type().is_dir() {
266 if let Some(watcher) = &mut watcher {
267 watch_one(watcher, entry.path());
268 }
269 } else {
270 let path = roots.contains(root, entry.path()).unwrap();
271 files.push(path.to_owned());
272 }
273 }
274 files
275}
276
277fn watch_one(watcher: &mut RecommendedWatcher, dir: &Path) {
278 match watcher.watch(dir, RecursiveMode::NonRecursive) {
279 Ok(()) => log::debug!("watching \"{}\"", dir.display()),
280 Err(e) => log::warn!("could not watch \"{}\": {}", dir.display(), e),
281 }
282}
283
284fn read_to_string(path: &Path) -> Option<String> {
285 fs::read_to_string(&path).map_err(|e| log::warn!("failed to read file {}", e)).ok()
286}
diff --git a/crates/ra_vfs/src/lib.rs b/crates/ra_vfs/src/lib.rs
deleted file mode 100644
index 808c138df..000000000
--- a/crates/ra_vfs/src/lib.rs
+++ /dev/null
@@ -1,296 +0,0 @@
1//! VFS stands for Virtual File System.
2//!
3//! When doing analysis, we don't want to do any IO, we want to keep all source
4//! code in memory. However, the actual source code is stored on disk, so you
5//! need to get it into the memory in the first place somehow. VFS is the
6//! component which does this.
7//!
8//! It is also responsible for watching the disk for changes, and for merging
9//! editor state (modified, unsaved files) with disk state.
10//!
11//! TODO: Some LSP clients support watching the disk, so this crate should to
12//! support custom watcher events (related to
13//! <https://github.com/rust-analyzer/rust-analyzer/issues/131>)
14//!
15//! VFS is based on a concept of roots: a set of directories on the file system
16//! which are watched for changes. Typically, there will be a root for each
17//! Cargo package.
18mod roots;
19mod io;
20
21use std::{
22 fmt, fs, mem,
23 path::{Path, PathBuf},
24 sync::Arc,
25};
26
27use crossbeam_channel::Receiver;
28use relative_path::{RelativePath, RelativePathBuf};
29use rustc_hash::{FxHashMap, FxHashSet};
30
31use crate::{
32 io::{TaskResult, Worker},
33 roots::Roots,
34};
35
36pub use crate::roots::VfsRoot;
37
38/// Opaque wrapper around file-system event.
39///
40/// Calling code is expected to just pass `VfsTask` to `handle_task` method. It
41/// is exposed as a public API so that the caller can plug vfs events into the
42/// main event loop and be notified when changes happen.
43pub struct VfsTask(TaskResult);
44
45impl fmt::Debug for VfsTask {
46 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
47 f.write_str("VfsTask { ... }")
48 }
49}
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
52pub struct VfsFile(pub u32);
53
54struct VfsFileData {
55 root: VfsRoot,
56 path: RelativePathBuf,
57 is_overlayed: bool,
58 text: Arc<String>,
59}
60
61pub struct Vfs {
62 roots: Arc<Roots>,
63 files: Vec<VfsFileData>,
64 root2files: FxHashMap<VfsRoot, FxHashSet<VfsFile>>,
65 pending_changes: Vec<VfsChange>,
66 worker: Worker,
67}
68
69impl fmt::Debug for Vfs {
70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 f.debug_struct("Vfs")
72 .field("n_roots", &self.roots.len())
73 .field("n_files", &self.files.len())
74 .field("n_pending_changes", &self.pending_changes.len())
75 .finish()
76 }
77}
78
79#[derive(Debug, Clone)]
80pub enum VfsChange {
81 AddRoot { root: VfsRoot, files: Vec<(VfsFile, RelativePathBuf, Arc<String>)> },
82 AddFile { root: VfsRoot, file: VfsFile, path: RelativePathBuf, text: Arc<String> },
83 RemoveFile { root: VfsRoot, file: VfsFile, path: RelativePathBuf },
84 ChangeFile { file: VfsFile, text: Arc<String> },
85}
86
87impl Vfs {
88 pub fn new(roots: Vec<PathBuf>) -> (Vfs, Vec<VfsRoot>) {
89 let roots = Arc::new(Roots::new(roots));
90 let worker = io::start(Arc::clone(&roots));
91 let mut root2files = FxHashMap::default();
92
93 for root in roots.iter() {
94 root2files.insert(root, Default::default());
95 worker.sender.send(io::Task::AddRoot { root }).unwrap();
96 }
97 let res = Vfs { roots, files: Vec::new(), root2files, worker, pending_changes: Vec::new() };
98 let vfs_roots = res.roots.iter().collect();
99 (res, vfs_roots)
100 }
101
102 pub fn root2path(&self, root: VfsRoot) -> PathBuf {
103 self.roots.path(root).to_path_buf()
104 }
105
106 pub fn path2file(&self, path: &Path) -> Option<VfsFile> {
107 if let Some((_root, _path, Some(file))) = self.find_root(path) {
108 return Some(file);
109 }
110 None
111 }
112
113 pub fn file2path(&self, file: VfsFile) -> PathBuf {
114 let rel_path = &self.file(file).path;
115 let root_path = &self.roots.path(self.file(file).root);
116 rel_path.to_path(root_path)
117 }
118
119 pub fn n_roots(&self) -> usize {
120 self.roots.len()
121 }
122
123 pub fn load(&mut self, path: &Path) -> Option<VfsFile> {
124 if let Some((root, rel_path, file)) = self.find_root(path) {
125 return if let Some(file) = file {
126 Some(file)
127 } else {
128 let text = fs::read_to_string(path).unwrap_or_default();
129 let text = Arc::new(text);
130 let file = self.raw_add_file(root, rel_path.clone(), Arc::clone(&text), false);
131 let change = VfsChange::AddFile { file, text, root, path: rel_path };
132 self.pending_changes.push(change);
133 Some(file)
134 };
135 }
136 None
137 }
138
139 pub fn add_file_overlay(&mut self, path: &Path, text: String) -> Option<VfsFile> {
140 let (root, rel_path, file) = self.find_root(path)?;
141 if let Some(file) = file {
142 self.change_file_event(file, text, true);
143 Some(file)
144 } else {
145 self.add_file_event(root, rel_path, text, true)
146 }
147 }
148
149 pub fn change_file_overlay(&mut self, path: &Path, new_text: String) {
150 if let Some((_root, _path, file)) = self.find_root(path) {
151 let file = file.expect("can't change a file which wasn't added");
152 self.change_file_event(file, new_text, true);
153 }
154 }
155
156 pub fn remove_file_overlay(&mut self, path: &Path) -> Option<VfsFile> {
157 let (root, rel_path, file) = self.find_root(path)?;
158 let file = file.expect("can't remove a file which wasn't added");
159 let full_path = rel_path.to_path(&self.roots.path(root));
160 if let Ok(text) = fs::read_to_string(&full_path) {
161 self.change_file_event(file, text, false);
162 } else {
163 self.remove_file_event(root, rel_path, file);
164 }
165 Some(file)
166 }
167
168 pub fn commit_changes(&mut self) -> Vec<VfsChange> {
169 mem::replace(&mut self.pending_changes, Vec::new())
170 }
171
172 pub fn task_receiver(&self) -> &Receiver<VfsTask> {
173 &self.worker.receiver
174 }
175
176 pub fn handle_task(&mut self, task: VfsTask) {
177 match task.0 {
178 TaskResult::BulkLoadRoot { root, files } => {
179 let mut cur_files = Vec::new();
180 // While we were scanning the root in the background, a file might have
181 // been open in the editor, so we need to account for that.
182 let existing = self.root2files[&root]
183 .iter()
184 .map(|&file| (self.file(file).path.clone(), file))
185 .collect::<FxHashMap<_, _>>();
186 for (path, text) in files {
187 if let Some(&file) = existing.get(&path) {
188 let text = Arc::clone(&self.file(file).text);
189 cur_files.push((file, path, text));
190 continue;
191 }
192 let text = Arc::new(text);
193 let file = self.raw_add_file(root, path.clone(), Arc::clone(&text), false);
194 cur_files.push((file, path, text));
195 }
196
197 let change = VfsChange::AddRoot { root, files: cur_files };
198 self.pending_changes.push(change);
199 }
200 TaskResult::SingleFile { root, path, text } => {
201 let existing_file = self.find_file(root, &path);
202 if existing_file.map(|file| self.file(file).is_overlayed) == Some(true) {
203 return;
204 }
205 match (existing_file, text) {
206 (Some(file), None) => {
207 self.remove_file_event(root, path, file);
208 }
209 (None, Some(text)) => {
210 self.add_file_event(root, path, text, false);
211 }
212 (Some(file), Some(text)) => {
213 self.change_file_event(file, text, false);
214 }
215 (None, None) => (),
216 }
217 }
218 }
219 }
220
221 // *_event calls change the state of VFS and push a change onto pending
222 // changes array.
223
224 fn add_file_event(
225 &mut self,
226 root: VfsRoot,
227 path: RelativePathBuf,
228 text: String,
229 is_overlay: bool,
230 ) -> Option<VfsFile> {
231 let text = Arc::new(text);
232 let file = self.raw_add_file(root, path.clone(), text.clone(), is_overlay);
233 self.pending_changes.push(VfsChange::AddFile { file, root, path, text });
234 Some(file)
235 }
236
237 fn change_file_event(&mut self, file: VfsFile, text: String, is_overlay: bool) {
238 let text = Arc::new(text);
239 self.raw_change_file(file, text.clone(), is_overlay);
240 self.pending_changes.push(VfsChange::ChangeFile { file, text });
241 }
242
243 fn remove_file_event(&mut self, root: VfsRoot, path: RelativePathBuf, file: VfsFile) {
244 self.raw_remove_file(file);
245 self.pending_changes.push(VfsChange::RemoveFile { root, path, file });
246 }
247
248 // raw_* calls change the state of VFS, but **do not** emit events.
249
250 fn raw_add_file(
251 &mut self,
252 root: VfsRoot,
253 path: RelativePathBuf,
254 text: Arc<String>,
255 is_overlayed: bool,
256 ) -> VfsFile {
257 let data = VfsFileData { root, path, text, is_overlayed };
258 let file = VfsFile(self.files.len() as u32);
259 self.files.push(data);
260 self.root2files.get_mut(&root).unwrap().insert(file);
261 file
262 }
263
264 fn raw_change_file(&mut self, file: VfsFile, new_text: Arc<String>, is_overlayed: bool) {
265 let mut file_data = &mut self.file_mut(file);
266 file_data.text = new_text;
267 file_data.is_overlayed = is_overlayed;
268 }
269
270 fn raw_remove_file(&mut self, file: VfsFile) {
271 // FIXME: use arena with removal
272 self.file_mut(file).text = Default::default();
273 self.file_mut(file).path = Default::default();
274 let root = self.file(file).root;
275 let removed = self.root2files.get_mut(&root).unwrap().remove(&file);
276 assert!(removed);
277 }
278
279 fn find_root(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf, Option<VfsFile>)> {
280 let (root, path) = self.roots.find(&path)?;
281 let file = self.find_file(root, &path);
282 Some((root, path, file))
283 }
284
285 fn find_file(&self, root: VfsRoot, path: &RelativePath) -> Option<VfsFile> {
286 self.root2files[&root].iter().map(|&it| it).find(|&file| self.file(file).path == path)
287 }
288
289 fn file(&self, file: VfsFile) -> &VfsFileData {
290 &self.files[file.0 as usize]
291 }
292
293 fn file_mut(&mut self, file: VfsFile) -> &mut VfsFileData {
294 &mut self.files[file.0 as usize]
295 }
296}
diff --git a/crates/ra_vfs/src/roots.rs b/crates/ra_vfs/src/roots.rs
deleted file mode 100644
index 4503458ee..000000000
--- a/crates/ra_vfs/src/roots.rs
+++ /dev/null
@@ -1,108 +0,0 @@
1use std::{
2 iter,
3 path::{Path, PathBuf},
4};
5
6use relative_path::{ RelativePath, RelativePathBuf};
7
8/// VfsRoot identifies a watched directory on the file system.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct VfsRoot(pub u32);
11
12/// Describes the contents of a single source root.
13///
14/// `RootConfig` can be thought of as a glob pattern like `src/**.rs` which
15/// specifies the source root or as a function which takes a `PathBuf` and
16/// returns `true` iff path belongs to the source root
17struct RootData {
18 path: PathBuf,
19 // result of `root.canonicalize()` if that differs from `root`; `None` otherwise.
20 canonical_path: Option<PathBuf>,
21 excluded_dirs: Vec<RelativePathBuf>,
22}
23
24pub(crate) struct Roots {
25 roots: Vec<RootData>,
26}
27
28impl Roots {
29 pub(crate) fn new(mut paths: Vec<PathBuf>) -> Roots {
30 let mut roots = Vec::new();
31 // A hack to make nesting work.
32 paths.sort_by_key(|it| std::cmp::Reverse(it.as_os_str().len()));
33 paths.dedup();
34 for (i, path) in paths.iter().enumerate() {
35 let nested_roots =
36 paths[..i].iter().filter_map(|it| rel_path(path, it)).collect::<Vec<_>>();
37
38 roots.push(RootData::new(path.clone(), nested_roots));
39 }
40 Roots { roots }
41 }
42 pub(crate) fn find(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf)> {
43 self.iter().find_map(|root| {
44 let rel_path = self.contains(root, path)?;
45 Some((root, rel_path))
46 })
47 }
48 pub(crate) fn len(&self) -> usize {
49 self.roots.len()
50 }
51 pub(crate) fn iter<'a>(&'a self) -> impl Iterator<Item = VfsRoot> + 'a {
52 (0..self.roots.len()).into_iter().map(|idx| VfsRoot(idx as u32))
53 }
54 pub(crate) fn path(&self, root: VfsRoot) -> &Path {
55 self.root(root).path.as_path()
56 }
57 /// Checks if root contains a path and returns a root-relative path.
58 pub(crate) fn contains(&self, root: VfsRoot, path: &Path) -> Option<RelativePathBuf> {
59 let data = self.root(root);
60 iter::once(&data.path)
61 .chain(data.canonical_path.as_ref().into_iter())
62 .find_map(|base| rel_path(base, path))
63 .filter(|path| !data.excluded_dirs.contains(path))
64 .filter(|path| !data.is_excluded(path))
65 }
66
67 fn root(&self, root: VfsRoot) -> &RootData {
68 &self.roots[root.0 as usize]
69 }
70}
71
72impl RootData {
73 fn new(path: PathBuf, excluded_dirs: Vec<RelativePathBuf>) -> RootData {
74 let mut canonical_path = path.canonicalize().ok();
75 if Some(&path) == canonical_path.as_ref() {
76 canonical_path = None;
77 }
78 RootData { path, canonical_path, excluded_dirs }
79 }
80
81 fn is_excluded(&self, path: &RelativePath) -> bool {
82 if self.excluded_dirs.iter().any(|it| it == path) {
83 return true;
84 }
85 // Ignore some common directories.
86 //
87 // FIXME: don't hard-code, specify at source-root creation time using
88 // gitignore
89 for (i, c) in path.components().enumerate() {
90 if let relative_path::Component::Normal(c) = c {
91 if (i == 0 && c == "target") || c == ".git" || c == "node_modules" {
92 return true;
93 }
94 }
95 }
96
97 match path.extension() {
98 None | Some("rs") => false,
99 _ => true,
100 }
101 }
102}
103
104fn rel_path(base: &Path, path: &Path) -> Option<RelativePathBuf> {
105 let path = path.strip_prefix(base).ok()?;
106 let path = RelativePathBuf::from_path(path).unwrap();
107 Some(path)
108}
diff --git a/crates/ra_vfs/tests/vfs.rs b/crates/ra_vfs/tests/vfs.rs
deleted file mode 100644
index 200a03e54..000000000
--- a/crates/ra_vfs/tests/vfs.rs
+++ /dev/null
@@ -1,222 +0,0 @@
1use std::{collections::HashSet, fs, time::Duration};
2
3// use flexi_logger::Logger;
4use crossbeam_channel::RecvTimeoutError;
5use ra_vfs::{Vfs, VfsChange};
6use tempfile::tempdir;
7
8/// Processes exactly `num_tasks` events waiting in the `vfs` message queue.
9///
10/// Panics if there are not exactly that many tasks enqueued for processing.
11fn process_tasks(vfs: &mut Vfs, num_tasks: u32) {
12 process_tasks_in_range(vfs, num_tasks, num_tasks);
13}
14
15/// Processes up to `max_count` events waiting in the `vfs` message queue.
16///
17/// Panics if it cannot process at least `min_count` events.
18/// Panics if more than `max_count` events are enqueued for processing.
19fn process_tasks_in_range(vfs: &mut Vfs, min_count: u32, max_count: u32) {
20 for i in 0..max_count {
21 let task = match vfs.task_receiver().recv_timeout(Duration::from_secs(3)) {
22 Err(RecvTimeoutError::Timeout) if i >= min_count => return,
23 otherwise => otherwise.unwrap(),
24 };
25 log::debug!("{:?}", task);
26 vfs.handle_task(task);
27 }
28 assert!(vfs.task_receiver().is_empty());
29}
30
31macro_rules! assert_match {
32 ($x:expr, $pat:pat) => {
33 assert_match!($x, $pat, ())
34 };
35 ($x:expr, $pat:pat, $assert:expr) => {
36 match $x {
37 $pat => $assert,
38 x => assert!(false, "Expected {}, got {:?}", stringify!($pat), x),
39 };
40 };
41}
42
43#[test]
44fn test_vfs_works() -> std::io::Result<()> {
45 // Logger::with_str("vfs=debug,ra_vfs=debug").start().unwrap();
46
47 let files = [("a/foo.rs", "hello"), ("a/bar.rs", "world"), ("a/b/baz.rs", "nested hello")];
48
49 let dir = tempdir().unwrap();
50 for (path, text) in files.iter() {
51 let file_path = dir.path().join(path);
52 fs::create_dir_all(file_path.parent().unwrap()).unwrap();
53 fs::write(file_path, text)?
54 }
55
56 let a_root = dir.path().join("a");
57 let b_root = dir.path().join("a/b");
58
59 let (mut vfs, _) = Vfs::new(vec![a_root, b_root]);
60 process_tasks(&mut vfs, 2);
61 {
62 let files = vfs
63 .commit_changes()
64 .into_iter()
65 .flat_map(|change| {
66 let files = match change {
67 VfsChange::AddRoot { files, .. } => files,
68 _ => panic!("unexpected change"),
69 };
70 files.into_iter().map(|(_id, path, text)| {
71 let text: String = (&*text).clone();
72 (format!("{}", path.display()), text)
73 })
74 })
75 .collect::<HashSet<_>>();
76
77 let expected_files = [("foo.rs", "hello"), ("bar.rs", "world"), ("baz.rs", "nested hello")]
78 .iter()
79 .map(|(path, text)| (path.to_string(), text.to_string()))
80 .collect::<HashSet<_>>();
81
82 assert_eq!(files, expected_files);
83 }
84
85 // rust-analyzer#734: fsevents has a bunch of events still sitting around.
86 process_tasks_in_range(&mut vfs, 0, if cfg!(target_os = "macos") { 7 } else { 0 });
87 match vfs.commit_changes().as_slice() {
88 [] => {}
89
90 // This arises on fsevents (unless we wait 30 seconds before
91 // calling `Vfs::new` above). We need to churn through these
92 // events so that we can focus on the event that arises from
93 // the `fs::write` below.
94 [VfsChange::ChangeFile { .. }, // hello
95 VfsChange::ChangeFile { .. }, // world
96 VfsChange::AddFile { .. }, // b/baz.rs, nested hello
97 VfsChange::ChangeFile { .. }, // hello
98 VfsChange::ChangeFile { .. }, // world
99 VfsChange::ChangeFile { .. }, // nested hello
100 VfsChange::ChangeFile { .. }, // nested hello
101 ] => {}
102
103 changes => panic!("Expected events for setting up initial files, got: {GOT:?}",
104 GOT=changes),
105 }
106
107 fs::write(&dir.path().join("a/b/baz.rs"), "quux").unwrap();
108 process_tasks(&mut vfs, 1);
109 assert_match!(
110 vfs.commit_changes().as_slice(),
111 [VfsChange::ChangeFile { text, .. }],
112 assert_eq!(text.as_str(), "quux")
113 );
114
115 vfs.add_file_overlay(&dir.path().join("a/b/baz.rs"), "m".to_string());
116 assert_match!(
117 vfs.commit_changes().as_slice(),
118 [VfsChange::ChangeFile { text, .. }],
119 assert_eq!(text.as_str(), "m")
120 );
121
122 // changing file on disk while overlayed doesn't generate a VfsChange
123 fs::write(&dir.path().join("a/b/baz.rs"), "corge").unwrap();
124 process_tasks(&mut vfs, 1);
125 assert_match!(vfs.commit_changes().as_slice(), []);
126
127 // removing overlay restores data on disk
128 vfs.remove_file_overlay(&dir.path().join("a/b/baz.rs"));
129 assert_match!(
130 vfs.commit_changes().as_slice(),
131 [VfsChange::ChangeFile { text, .. }],
132 assert_eq!(text.as_str(), "corge")
133 );
134
135 vfs.add_file_overlay(&dir.path().join("a/b/spam.rs"), "spam".to_string());
136 assert_match!(vfs.commit_changes().as_slice(), [VfsChange::AddFile { text, path, .. }], {
137 assert_eq!(text.as_str(), "spam");
138 assert_eq!(path, "spam.rs");
139 });
140
141 vfs.remove_file_overlay(&dir.path().join("a/b/spam.rs"));
142 assert_match!(
143 vfs.commit_changes().as_slice(),
144 [VfsChange::RemoveFile { path, .. }],
145 assert_eq!(path, "spam.rs")
146 );
147
148 fs::create_dir_all(dir.path().join("a/sub1/sub2")).unwrap();
149 fs::write(dir.path().join("a/sub1/sub2/new.rs"), "new hello").unwrap();
150 process_tasks(&mut vfs, 1);
151 assert_match!(vfs.commit_changes().as_slice(), [VfsChange::AddFile { text, path, .. }], {
152 assert_eq!(text.as_str(), "new hello");
153 assert_eq!(path, "sub1/sub2/new.rs");
154 });
155
156 fs::rename(&dir.path().join("a/sub1/sub2/new.rs"), &dir.path().join("a/sub1/sub2/new1.rs"))
157 .unwrap();
158
159 // rust-analyzer#734: For testing purposes, work-around
160 // passcod/notify#181 by processing either 1 or 2 events. (In
161 // particular, Mac can hand back either 1 or 2 events in a
162 // timing-dependent fashion.)
163 //
164 // rust-analyzer#827: Windows generates extra `Write` events when
165 // renaming? meaning we have extra tasks to process.
166 process_tasks_in_range(&mut vfs, 1, if cfg!(windows) { 4 } else { 2 });
167 match vfs.commit_changes().as_slice() {
168 [VfsChange::RemoveFile { path: removed_path, .. }, VfsChange::AddFile { text, path: added_path, .. }] =>
169 {
170 assert_eq!(removed_path, "sub1/sub2/new.rs");
171 assert_eq!(added_path, "sub1/sub2/new1.rs");
172 assert_eq!(text.as_str(), "new hello");
173 }
174
175 // Hopefully passcod/notify#181 will be addressed in some
176 // manner that will reliably emit an event mentioning
177 // `sub1/sub2/new.rs`. But until then, must accept that
178 // debouncing loses information unrecoverably.
179 [VfsChange::AddFile { text, path: added_path, .. }] => {
180 assert_eq!(added_path, "sub1/sub2/new1.rs");
181 assert_eq!(text.as_str(), "new hello");
182 }
183
184 changes => panic!(
185 "Expected events for rename of {OLD} to {NEW}, got: {GOT:?}",
186 OLD = "sub1/sub2/new.rs",
187 NEW = "sub1/sub2/new1.rs",
188 GOT = changes
189 ),
190 }
191
192 fs::remove_file(&dir.path().join("a/sub1/sub2/new1.rs")).unwrap();
193 process_tasks(&mut vfs, 1);
194 assert_match!(
195 vfs.commit_changes().as_slice(),
196 [VfsChange::RemoveFile { path, .. }],
197 assert_eq!(path, "sub1/sub2/new1.rs")
198 );
199
200 {
201 vfs.add_file_overlay(&dir.path().join("a/memfile.rs"), "memfile".to_string());
202 assert_match!(
203 vfs.commit_changes().as_slice(),
204 [VfsChange::AddFile { text, .. }],
205 assert_eq!(text.as_str(), "memfile")
206 );
207 fs::write(&dir.path().join("a/memfile.rs"), "ignore me").unwrap();
208 process_tasks(&mut vfs, 1);
209 assert_match!(vfs.commit_changes().as_slice(), []);
210 }
211
212 // should be ignored
213 fs::create_dir_all(dir.path().join("a/target")).unwrap();
214 fs::write(&dir.path().join("a/target/new.rs"), "ignore me").unwrap();
215
216 assert_match!(
217 vfs.task_receiver().recv_timeout(Duration::from_millis(300)), // slightly more than watcher debounce delay
218 Err(RecvTimeoutError::Timeout)
219 );
220
221 Ok(())
222}