1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
pub mod handler;
use std::collections::{HashMap, HashSet};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::sync::mpsc::Sender;
use anyhow::{Context, Result};
use fs2::FileExt;
use ignore::gitignore::{Gitignore, GitignoreBuilder};
use notify::event::{CreateKind, EventKind, RemoveKind};
use notify::{Config, Event, RecommendedWatcher, RecursiveMode, Watcher};
use walkdir::WalkDir;
/// Built-in gitignore patterns (always applied).
/// These are either internal directories, IDE config, or OS cruft.
const BUILTIN_GITIGNORE: &[&str] = &[
// Git internals (but .git itself is used for project detection)
".git/",
// Our own output
".codeindex/",
// Editor/IDE directories
".vscode/",
".idea/",
".vs/",
// OS cruft
".DS_Store",
".Spotlight-V100/",
".Trashes/",
];
/// Mount mode determines whether the index can be written to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MountMode {
/// Read-write: acquires exclusive flock, allows modifications.
ReadWrite,
/// Read-only: no lock, index data is immutable.
ReadOnly,
}
/// Check if an EventKind represents a removal operation.
/// Used to determine if we can canonicalize the path (removed files can't be canonicalized).
pub fn is_removal_event(kind: &EventKind) -> bool {
matches!(
kind,
EventKind::Remove(RemoveKind::File) | EventKind::Remove(RemoveKind::Folder)
)
}
/// Filesystem events emitted to external consumers (handler/DB).
/// These are the result of processing raw notify events through mount rules.
#[derive(Debug, Clone)]
pub enum FsEvent {
/// A file was discovered or modified.
FileAdded {
/// Absolute path to the mount root.
mount: PathBuf,
/// Path relative to mount root.
path: String,
},
/// A file was deleted.
FileRemoved {
/// Absolute path to the mount root.
mount: PathBuf,
/// Path relative to mount root.
path: String,
},
/// A subproject (.git/ directory) was discovered.
ProjectAdded {
/// Absolute path to the subproject root (parent of .git/).
root: PathBuf,
},
/// A subproject was removed (its .git/ directory was deleted).
ProjectRemoved {
/// Absolute path to the subproject root (parent of .git/).
root: PathBuf,
},
/// Directory should be skipped (gitignore match).
/// Walker should call skip_current_dir() to avoid descending.
DirIgnored,
}
/// Event with mount identity attached (avoids lookup in handler).
pub type MountedEvent = (PathBuf, Result<Event, notify::Error>);
/// A single mounted directory with its index.
pub struct Mount {
/// Root directory of the mount.
pub root: PathBuf,
/// Mount mode (read-write or read-only).
pub mode: MountMode,
/// File lock handle (only present for ReadWrite mounts).
lock: Option<File>,
/// Whether the index has been modified and needs flushing.
pub dirty: bool,
/// Gitignore rules for this mount (built during walk, used by watcher).
/// None until walk() is called.
gitignore: Option<Gitignore>,
/// All gitignore files discovered (for rebuilding when new ones are added).
gitignore_files: Vec<PathBuf>,
/// File system watcher (only present for ReadWrite mounts).
watcher: Option<RecommendedWatcher>,
/// Directories currently being watched.
watched_dirs: HashSet<PathBuf>,
}
impl std::fmt::Debug for Mount {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Mount")
.field("root", &self.root)
.field("mode", &self.mode)
.field("dirty", &self.dirty)
.field("watched_dirs", &self.watched_dirs.len())
.finish()
}
}
impl Mount {
/// Create a new read-only mount (no lock, no watcher).
fn new_ro(root: PathBuf) -> Result<Self> {
let mut mount = Self {
root,
mode: MountMode::ReadOnly,
lock: None,
dirty: false,
gitignore: None,
gitignore_files: Vec::new(),
watcher: None,
watched_dirs: HashSet::new(),
};
mount.init_gitignore()?;
Ok(mount)
}
/// Create a new read-write mount with exclusive flock.
/// Does NOT start notify - call `init_notify()` separately.
fn new_rw(root: PathBuf) -> Result<Self> {
// Create .codeindex directory if it doesn't exist
let codeindex_dir = root.join(".codeindex");
std::fs::create_dir_all(&codeindex_dir).with_context(|| {
format!(
"failed to create .codeindex directory at {:?}",
codeindex_dir
)
})?;
// Acquire exclusive lock on index.json
let lock_path = codeindex_dir.join("index.json");
let lock_file = File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(&lock_path)
.with_context(|| format!("failed to open lock file at {:?}", lock_path))?;
lock_file
.try_lock_exclusive()
.with_context(|| format!("failed to acquire exclusive lock on {:?}", lock_path))?;
let mut mount = Self {
root,
mode: MountMode::ReadWrite,
lock: Some(lock_file),
dirty: false,
gitignore: None,
gitignore_files: Vec::new(),
watcher: None,
watched_dirs: HashSet::new(),
};
mount.init_gitignore()?;
Ok(mount)
}
/// Initialize gitignore with .git/info/exclude and root .gitignore.
fn init_gitignore(&mut self) -> Result<()> {
self.gitignore_files.clear();
// Add .git/info/exclude if it exists
let exclude_path = self.root.join(".git/info/exclude");
if exclude_path.exists() {
self.gitignore_files.push(exclude_path);
}
// Add root .gitignore - must be loaded first before walking siblings
let root_gitignore = self.root.join(".gitignore");
if root_gitignore.exists() {
self.gitignore_files.push(root_gitignore);
}
self.build_gitignore()
}
/// Build gitignore from all tracked files plus built-in patterns.
fn build_gitignore(&mut self) -> Result<()> {
let mut builder = GitignoreBuilder::new(&self.root);
// Add built-in patterns first
for pattern in BUILTIN_GITIGNORE {
let _ = builder.add_line(None, pattern);
}
// Add user gitignore files
for file in &self.gitignore_files {
builder.add(file);
}
self.gitignore = Some(
builder
.build()
.with_context(|| format!("failed to build gitignore for {:?}", self.root))?,
);
Ok(())
}
/// Handle a filesystem event (from walker or notify).
///
/// This is the central handler that applies ALL rules:
/// - Gitignore filtering
/// - Project detection (.git/)
/// - .gitignore file updates
/// - Watch management
///
/// Takes notify's `EventKind` directly - both walker and notify use the same type.
/// Returns an external event if this should be forwarded to consumers.
/// Returns None for event types we don't care about.
pub fn on_fs_event(&mut self, abs_path: &Path, kind: &EventKind) -> Option<FsEvent> {
// Check if path is under mount root
if !abs_path.starts_with(&self.root) {
return None;
}
match kind {
EventKind::Create(CreateKind::Folder) => self.on_dir_added(abs_path),
EventKind::Remove(RemoveKind::Folder) => self.on_dir_removed(abs_path),
EventKind::Create(CreateKind::File) | EventKind::Modify(_) => {
self.on_file_added(abs_path)
}
EventKind::Remove(RemoveKind::File) => self.on_file_removed(abs_path),
_ => None, // Ignore other event types
}
}
fn on_dir_added(&mut self, abs_path: &Path) -> Option<FsEvent> {
let name = abs_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
// Check if this is a .git directory -> project discovery
// (must check before gitignore since .git/ is in BUILTIN_GITIGNORE)
if name == ".git" {
return abs_path.parent().map(|root| FsEvent::ProjectAdded {
root: root.to_path_buf(),
});
}
// Check gitignore - return DirIgnored so walker can skip subtree
if self.is_ignored(abs_path) {
return Some(FsEvent::DirIgnored);
}
// Check if directory contains .git (file or dir) -> it's a subproject root
// Git submodules use a .git file pointing to parent's .git/modules/
if abs_path.join(".git").exists() {
return Some(FsEvent::ProjectAdded {
root: abs_path.to_path_buf(),
});
}
// Check for .gitignore in this directory : must be loaded first before walking siblings
let dir_gitignore = abs_path.join(".gitignore");
if dir_gitignore.exists() {
self.add_gitignore(&dir_gitignore);
}
// Add watch for new directory
let _ = self.watch_dir(abs_path);
None // DirAdded is internal only
}
fn on_dir_removed(&mut self, abs_path: &Path) -> Option<FsEvent> {
self.remove_watch(abs_path);
// Check if this is a .git directory -> project removal
let name = abs_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
if name == ".git" {
return abs_path.parent().map(|root| FsEvent::ProjectRemoved {
root: root.to_path_buf(),
});
}
None // DirRemoved is internal only
}
fn on_file_added(&mut self, abs_path: &Path) -> Option<FsEvent> {
let name = abs_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
// Check gitignore
if self.is_ignored(abs_path) {
return None;
}
// Check if it's a .gitignore file -> update rules
if name == ".gitignore" {
self.add_gitignore(abs_path);
return None; // Don't index .gitignore files themselves
}
// Skip hidden files (dotfiles)
if name.starts_with('.') {
return None;
}
let rel_path = abs_path
.strip_prefix(&self.root)
.map(|p| p.to_string_lossy().replace('\\', "/"))
.unwrap_or_default();
Some(FsEvent::FileAdded {
mount: self.root.clone(),
path: rel_path,
})
}
fn on_file_removed(&mut self, abs_path: &Path) -> Option<FsEvent> {
let name = abs_path.file_name().and_then(|n| n.to_str()).unwrap_or("");
// Skip hidden files
if name.starts_with('.') {
return None;
}
let rel_path = abs_path
.strip_prefix(&self.root)
.map(|p| p.to_string_lossy().replace('\\', "/"))
.unwrap_or_default();
Some(FsEvent::FileRemoved {
mount: self.root.clone(),
path: rel_path,
})
}
/// Add a .gitignore file to the rules.
fn add_gitignore(&mut self, gitignore_path: &Path) {
// Track the file
if !self.gitignore_files.contains(&gitignore_path.to_path_buf()) {
self.gitignore_files.push(gitignore_path.to_path_buf());
// Rebuild gitignore with all known files
let _ = self.build_gitignore();
}
}
/// Remove watch for a directory.
fn remove_watch(&mut self, path: &Path) {
if self.watched_dirs.remove(path)
&& let Some(ref mut watcher) = self.watcher
{
let _ = watcher.unwatch(path);
}
}
/// Walk all files in this mount, emitting events via callback.
///
/// This is a dumb walker - it just iterates and calls on_fs_event for each entry.
/// All the smart logic (gitignore, skip entries, project detection) is in on_fs_event.
///
/// After walk completes, the built gitignore is stored for use by the watcher.
pub fn walk<F>(&mut self, mut on_event: F) -> Result<()>
where
F: FnMut(FsEvent) -> Result<()>,
{
let root = self.root.clone();
tracing::debug!("walking mount at {}", root.display());
// Add watch for mount root
let _ = self.watch_dir(&root);
let mut iter = WalkDir::new(&root).follow_links(false).into_iter();
while let Some(result) = iter.next() {
let entry = result?;
let abs_path = entry.path();
// Skip mount root itself
if abs_path == root {
continue;
}
// Skip entries inside subprojects: if parent has .git (file or dir), skip this entry
// (but the subproject dir itself is allowed since its parent won't have .git)
// Git submodules use a .git file pointing to parent's .git/modules/
if let Some(parent) = abs_path.parent()
&& parent != root
&& parent.join(".git").exists()
{
if entry.file_type().is_dir() {
iter.skip_current_dir();
}
continue;
}
// Determine event kind (same as notify would emit)
let kind = if entry.file_type().is_dir() {
EventKind::Create(CreateKind::Folder)
} else if entry.file_type().is_file() {
EventKind::Create(CreateKind::File)
} else {
continue; // Skip symlinks, etc.
};
// Let on_fs_event handle all the logic
if let Some(event) = self.on_fs_event(abs_path, &kind) {
match event {
FsEvent::DirIgnored => {
// Skip the entire subtree
iter.skip_current_dir();
}
_ => {
on_event(event)?;
}
}
}
}
Ok(())
}
/// Initialize the notify file system watcher for this mount.
///
/// Must be called BEFORE walk() so that DirCreated events can add directories.
/// Does nothing for read-only mounts.
///
/// Events are sent to the provided channel with mount root attached,
/// avoiding the need for mount lookup in the handler.
pub fn init_notify(&mut self, tx: Sender<MountedEvent>) -> Result<()> {
if self.mode == MountMode::ReadOnly {
return Ok(()); // No watcher for read-only mounts
}
// Capture mount root for the callback
let mount_root = self.root.clone();
let config = Config::default();
let watcher = RecommendedWatcher::new(
move |res| {
let _ = tx.send((mount_root.clone(), res));
},
config,
)
.context("failed to create watcher")?;
self.watcher = Some(watcher);
self.watched_dirs.clear();
Ok(())
}
/// Watch a directory (non-recursive).
pub fn watch_dir(&mut self, path: &Path) -> Result<()> {
if !self.watched_dirs.contains(path)
&& let Some(ref mut watcher) = self.watcher
{
watcher
.watch(path, RecursiveMode::NonRecursive)
.with_context(|| format!("failed to watch {}", path.display()))?;
self.watched_dirs.insert(path.to_path_buf());
}
Ok(())
}
/// Get the number of watched directories.
pub fn watched_count(&self) -> usize {
self.watched_dirs.len()
}
/// Check if a path should be ignored according to gitignore rules.
///
/// Returns false if gitignore hasn't been built yet (walk() not called).
pub fn is_ignored(&self, path: &Path) -> bool {
if let Some(ref gi) = self.gitignore {
let is_dir = path.is_dir();
gi.matched_path_or_any_parents(path, is_dir).is_ignore()
} else {
false
}
}
/// Get a reference to the gitignore matcher (if built).
pub fn gitignore(&self) -> Option<&Gitignore> {
self.gitignore.as_ref()
}
/// Mark this mount as dirty (needs flushing).
pub fn mark_dirty(&mut self) {
self.dirty = true;
}
/// Clear the dirty flag (after flushing).
pub fn clear_dirty(&mut self) {
self.dirty = false;
}
}
impl Drop for Mount {
fn drop(&mut self) {
// Release the lock when the mount is dropped
if let Some(ref lock) = self.lock {
let _ = lock.unlock();
}
}
}
/// Table of mounted directories.
#[derive(Debug)]
pub struct MountTable {
/// Root of the workspace (where codeix was launched).
workspace_root: PathBuf,
mounts: HashMap<PathBuf, Mount>,
}
impl MountTable {
/// Create a new mount table with the given workspace root.
pub fn new(workspace_root: PathBuf) -> Self {
Self {
workspace_root,
mounts: HashMap::new(),
}
}
/// Get the workspace root.
pub fn workspace_root(&self) -> &Path {
&self.workspace_root
}
/// Compute the relative project path from workspace root.
/// Returns empty string for the root project.
pub fn relative_project(&self, project_root: &Path) -> String {
project_root
.strip_prefix(&self.workspace_root)
.map(|p| p.to_string_lossy().replace('\\', "/"))
.unwrap_or_default()
}
/// Get the absolute path for a relative project path.
/// Returns None if the project is not mounted.
pub fn project_root(&self, relative_path: &str) -> Option<PathBuf> {
let abs_path = if relative_path.is_empty() {
self.workspace_root.clone()
} else {
self.workspace_root.join(relative_path)
};
// Verify the project is actually mounted
let canonical = abs_path.canonicalize().ok()?;
if self.mounts.contains_key(&canonical) {
Some(canonical)
} else {
None
}
}
/// Check if a path is already mounted (exact match, not prefix).
pub fn is_mounted(&self, path: &Path) -> bool {
self.mounts.contains_key(path)
}
/// Mount a directory, trying RW first, falling back to RO if lock is held.
///
/// Returns the mount and its mode. Use this when you want best-effort RW.
pub fn mount(&mut self, root: impl AsRef<Path>) -> Result<&Mount> {
let root = root
.as_ref()
.canonicalize()
.with_context(|| format!("failed to canonicalize path {:?}", root.as_ref()))?;
if self.mounts.contains_key(&root) {
anyhow::bail!("directory already mounted: {:?}", root);
}
// Try RW first, fall back to RO if lock fails
let mount = match Mount::new_rw(root.clone()) {
Ok(m) => m,
Err(e) => {
// Check if it's a lock error (contains "lock" in message)
if e.to_string().to_lowercase().contains("lock") {
tracing::info!(
"could not acquire lock on {}, mounting read-only: {}",
root.display(),
e
);
Mount::new_ro(root.clone())?
} else {
return Err(e);
}
}
};
self.mounts.insert(root.clone(), mount);
Ok(self.mounts.get(&root).unwrap())
}
/// Mount a directory in read-write mode (acquires exclusive lock).
pub fn mount_rw(&mut self, root: impl AsRef<Path>) -> Result<&Mount> {
let root = root
.as_ref()
.canonicalize()
.with_context(|| format!("failed to canonicalize path {:?}", root.as_ref()))?;
if self.mounts.contains_key(&root) {
anyhow::bail!("directory already mounted: {:?}", root);
}
let mount = Mount::new_rw(root.clone())?;
self.mounts.insert(root.clone(), mount);
Ok(self.mounts.get(&root).unwrap())
}
/// Mount a directory in read-only mode (no lock).
pub fn mount_ro(&mut self, root: impl AsRef<Path>) -> Result<&Mount> {
let root = root
.as_ref()
.canonicalize()
.with_context(|| format!("failed to canonicalize path {:?}", root.as_ref()))?;
if self.mounts.contains_key(&root) {
anyhow::bail!("directory already mounted: {:?}", root);
}
let mount = Mount::new_ro(root.clone())?;
self.mounts.insert(root.clone(), mount);
Ok(self.mounts.get(&root).unwrap())
}
/// Find the mount that contains a given path (longest prefix match).
/// Canonicalizes the path first to handle symlinks.
pub fn find_mount(&self, path: &Path) -> Option<&Mount> {
let path = path.canonicalize().ok()?;
self.find_mount_canonical(&path)
}
/// Find the mount that contains a given path (longest prefix match).
/// Assumes the path is already canonical - use for hot paths to avoid
/// repeated canonicalization overhead.
pub fn find_mount_canonical(&self, path: &Path) -> Option<&Mount> {
self.mounts
.iter()
.filter(|(root, _)| path.starts_with(root))
.max_by_key(|(root, _)| root.components().count())
.map(|(_, mount)| mount)
}
/// Find the mount that contains a given path (mutable, longest prefix match).
/// Canonicalizes the path first to handle symlinks.
pub fn find_mount_mut(&mut self, path: &Path) -> Option<&mut Mount> {
let path = match path.canonicalize() {
Ok(p) => p,
Err(_) => return None,
};
self.find_mount_mut_canonical(&path)
}
/// Find the mount that contains a given path (mutable, longest prefix match).
/// Assumes the path is already canonical - use for hot paths to avoid
/// repeated canonicalization overhead.
pub fn find_mount_mut_canonical(&mut self, path: &Path) -> Option<&mut Mount> {
// Find the key with longest prefix match
let key = self
.mounts
.keys()
.filter(|root| path.starts_with(root))
.max_by_key(|root| root.components().count())
.cloned();
key.and_then(move |k| self.mounts.get_mut(&k))
}
/// Unmount a directory (releases lock if held).
pub fn unmount(&mut self, root: impl AsRef<Path>) -> Result<()> {
let root = root
.as_ref()
.canonicalize()
.with_context(|| format!("failed to canonicalize path {:?}", root.as_ref()))?;
if self.mounts.remove(&root).is_none() {
anyhow::bail!("directory not mounted: {:?}", root);
}
Ok(())
}
/// Unmount a directory by its exact path (no canonicalization).
/// Use this when the directory may have been deleted.
pub fn unmount_path(&mut self, root: &Path) -> bool {
self.mounts.remove(root).is_some()
}
/// Iterate over all mounts.
pub fn iter(&self) -> impl Iterator<Item = (&PathBuf, &Mount)> {
self.mounts.iter()
}
/// Iterate over all mounts (mutable).
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&PathBuf, &mut Mount)> {
self.mounts.iter_mut()
}
/// Mark a path's mount as dirty.
pub fn mark_dirty(&mut self, path: &Path) -> bool {
if let Some(mount) = self.find_mount_mut(path) {
mount.mark_dirty();
true
} else {
false
}
}
/// Mark a path's mount as dirty (assumes path is already canonical).
pub fn mark_dirty_canonical(&mut self, path: &Path) -> bool {
if let Some(mount) = self.find_mount_mut_canonical(path) {
mount.mark_dirty();
true
} else {
false
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_mount_rw() {
let tmp = TempDir::new().unwrap();
let mut table = MountTable::new(tmp.path().to_path_buf());
let mount = table.mount_rw(tmp.path()).unwrap();
assert_eq!(mount.mode, MountMode::ReadWrite);
assert!(!mount.dirty);
// .codeindex/index.json should exist
assert!(tmp.path().join(".codeindex/index.json").exists());
}
#[test]
fn test_mount_ro() {
let tmp = TempDir::new().unwrap();
let mut table = MountTable::new(tmp.path().to_path_buf());
let mount = table.mount_ro(tmp.path()).unwrap();
assert_eq!(mount.mode, MountMode::ReadOnly);
assert!(!mount.dirty);
}
#[test]
fn test_find_mount_longest_prefix() {
let tmp = TempDir::new().unwrap();
let subdir = tmp.path().join("sub");
fs::create_dir(&subdir).unwrap();
let mut table = MountTable::new(tmp.path().to_path_buf());
table.mount_ro(tmp.path()).unwrap();
table.mount_ro(&subdir).unwrap();
// File in subdir should match subdir mount
let file_in_sub = subdir.join("file.rs");
fs::write(&file_in_sub, "").unwrap();
let mount = table.find_mount(&file_in_sub).unwrap();
assert_eq!(
mount.root.canonicalize().unwrap(),
subdir.canonicalize().unwrap()
);
// File in root should match root mount
let file_in_root = tmp.path().join("file.rs");
fs::write(&file_in_root, "").unwrap();
let mount = table.find_mount(&file_in_root).unwrap();
assert_eq!(
mount.root.canonicalize().unwrap(),
tmp.path().canonicalize().unwrap()
);
}
#[test]
fn test_unmount() {
let tmp = TempDir::new().unwrap();
let mut table = MountTable::new(tmp.path().to_path_buf());
table.mount_rw(tmp.path()).unwrap();
assert!(table.find_mount(tmp.path()).is_some());
table.unmount(tmp.path()).unwrap();
assert!(table.find_mount(tmp.path()).is_none());
}
#[test]
fn test_double_mount_fails() {
let tmp = TempDir::new().unwrap();
let mut table = MountTable::new(tmp.path().to_path_buf());
table.mount_rw(tmp.path()).unwrap();
assert!(table.mount_rw(tmp.path()).is_err());
}
#[test]
fn test_mark_dirty() {
let tmp = TempDir::new().unwrap();
let file = tmp.path().join("file.rs");
fs::write(&file, "").unwrap();
let mut table = MountTable::new(tmp.path().to_path_buf());
table.mount_rw(tmp.path()).unwrap();
assert!(table.mark_dirty(&file));
let mount = table.find_mount(&file).unwrap();
assert!(mount.dirty);
}
#[test]
fn test_gitignore_respected() {
let tmp = TempDir::new().unwrap();
// Create .gitignore
fs::write(tmp.path().join(".gitignore"), "ignored/\n*.log\n").unwrap();
// Create files
fs::create_dir(tmp.path().join("src")).unwrap();
fs::write(tmp.path().join("src/main.rs"), "fn main() {}").unwrap();
fs::create_dir(tmp.path().join("ignored")).unwrap();
fs::write(tmp.path().join("ignored/secret.txt"), "secret").unwrap();
fs::write(tmp.path().join("debug.log"), "log").unwrap();
let mut table = MountTable::new(tmp.path().to_path_buf());
table.mount_ro(tmp.path()).unwrap();
let mount = table.find_mount_mut(tmp.path()).unwrap();
// Collect walked files
let mut files = Vec::new();
mount
.walk(|event| {
if let FsEvent::FileAdded { path, .. } = event {
files.push(path);
}
Ok(())
})
.unwrap();
// Should include src/main.rs
assert!(files.contains(&"src/main.rs".to_string()));
// Should NOT include ignored files
assert!(!files.iter().any(|f| f.contains("ignored")));
assert!(!files.iter().any(|f| f.ends_with(".log")));
}
#[test]
fn test_symlinks_not_followed() {
// Issue #36: symlinks in node_modules caused CPU spin
// Verify that follow_links(false) prevents infinite loops
let tmp = TempDir::new().unwrap();
// Create a directory structure with symlinks
fs::create_dir(tmp.path().join("real_dir")).unwrap();
fs::write(tmp.path().join("real_dir/file.txt"), "content").unwrap();
// Create a symlink pointing to parent (would cause infinite loop if followed)
#[cfg(unix)]
{
use std::os::unix::fs::symlink;
// Symlink to parent directory
symlink(tmp.path(), tmp.path().join("real_dir/parent_link")).unwrap();
// Symlink to self
symlink(
tmp.path().join("real_dir"),
tmp.path().join("real_dir/self_link"),
)
.unwrap();
}
let mut table = MountTable::new(tmp.path().to_path_buf());
table.mount_ro(tmp.path()).unwrap();
let mount = table.find_mount_mut(tmp.path()).unwrap();
// Walk should complete without hanging (symlinks not followed)
let mut files = Vec::new();
mount
.walk(|event| {
if let FsEvent::FileAdded { path, .. } = event {
files.push(path);
}
Ok(())
})
.unwrap();
// Should find the real file
assert!(files.contains(&"real_dir/file.txt".to_string()));
// Should NOT have followed symlinks (no duplicates or infinite recursion)
assert_eq!(
files.iter().filter(|f| *f == "real_dir/file.txt").count(),
1
);
}
#[test]
fn test_walk_adds_watches_internally() {
// Verify walk adds watches internally (no DirAdded events emitted)
let tmp = TempDir::new().unwrap();
let tmp_path = tmp.path().canonicalize().unwrap();
fs::create_dir_all(tmp_path.join("src/components")).unwrap();
fs::create_dir_all(tmp_path.join("tests")).unwrap();
fs::write(tmp_path.join("src/main.rs"), "fn main() {}").unwrap();
fs::write(tmp_path.join("src/components/button.rs"), "// button").unwrap();
let mut table = MountTable::new(tmp_path.clone());
table.mount_rw(&tmp_path).unwrap(); // RW to get watcher
let mount = table.find_mount_mut(&tmp_path).unwrap();
// Init watcher
let (tx, _rx) = std::sync::mpsc::channel::<MountedEvent>();
mount.init_notify(tx).unwrap();
let mut file_count = 0;
mount
.walk(|event| {
match event {
FsEvent::FileAdded { .. } => file_count += 1,
FsEvent::ProjectAdded { .. } => {}
FsEvent::FileRemoved { .. }
| FsEvent::ProjectRemoved { .. }
| FsEvent::DirIgnored => {}
}
Ok(())
})
.unwrap();
// Should have files
assert!(file_count >= 2);
// Should have watches registered internally
assert!(mount.watched_count() >= 4); // root, src, components, tests
}
#[test]
fn test_gitignore_built_on_mount() {
// Verify gitignore is available immediately after mounting
let tmp = TempDir::new().unwrap();
// Canonicalize for macOS where /var -> /private/var
let tmp_path = tmp.path().canonicalize().unwrap();
fs::write(tmp_path.join(".gitignore"), "target/\n*.log\n").unwrap();
fs::create_dir(tmp_path.join("src")).unwrap();
fs::write(tmp_path.join("src/main.rs"), "fn main() {}").unwrap();
let mut table = MountTable::new(tmp_path.clone());
table.mount_ro(&tmp_path).unwrap();
let mount = table.find_mount_mut(&tmp_path).unwrap();
// Gitignore is built during mount creation
assert!(mount.gitignore().is_some());
// Can use is_ignored() for watcher filtering
assert!(mount.is_ignored(&tmp_path.join("target/debug/foo")));
assert!(mount.is_ignored(&tmp_path.join("app.log")));
assert!(!mount.is_ignored(&tmp_path.join("src/main.rs")));
}
#[test]
fn test_nested_gitignore_files() {
// Verify nested .gitignore files are respected
let tmp = TempDir::new().unwrap();
// Root .gitignore
fs::write(tmp.path().join(".gitignore"), "*.log\n").unwrap();
// Nested directory with its own .gitignore
fs::create_dir_all(tmp.path().join("subdir")).unwrap();
fs::write(tmp.path().join("subdir/.gitignore"), "local_ignore/\n").unwrap();
// Create files
fs::write(tmp.path().join("root.log"), "log").unwrap();
fs::write(tmp.path().join("subdir/code.rs"), "// code").unwrap();
fs::write(tmp.path().join("subdir/sub.log"), "log").unwrap();
fs::create_dir(tmp.path().join("subdir/local_ignore")).unwrap();
fs::write(tmp.path().join("subdir/local_ignore/secret.txt"), "secret").unwrap();
let mut table = MountTable::new(tmp.path().to_path_buf());
table.mount_ro(tmp.path()).unwrap();
let mount = table.find_mount_mut(tmp.path()).unwrap();
let mut files = Vec::new();
mount
.walk(|event| {
if let FsEvent::FileAdded { path, .. } = event {
files.push(path);
}
Ok(())
})
.unwrap();
// Should include code.rs
assert!(files.contains(&"subdir/code.rs".to_string()));
// Should NOT include .log files (from root .gitignore)
assert!(!files.iter().any(|f| f.ends_with(".log")));
// Should NOT include local_ignore/ (from nested .gitignore)
assert!(!files.iter().any(|f| f.contains("local_ignore")));
}
#[test]
fn test_on_dir_removed_emits_project_removed() {
use notify::event::{EventKind, RemoveKind};
let tmp = TempDir::new().unwrap();
// Canonicalize to handle macOS /var -> /private/var symlink
let tmp_path = tmp.path().canonicalize().unwrap();
// Create a .git directory to simulate a subproject
let git_dir = tmp_path.join("subproject/.git");
fs::create_dir_all(&git_dir).unwrap();
let mut table = MountTable::new(tmp_path.clone());
table.mount_ro(&tmp_path).unwrap();
let mount = table.find_mount_mut(&tmp_path).unwrap();
// Simulate .git directory removal event
let event = mount.on_fs_event(&git_dir, &EventKind::Remove(RemoveKind::Folder));
// Should emit ProjectRemoved for the subproject root
match event {
Some(FsEvent::ProjectRemoved { root }) => {
assert_eq!(root, tmp_path.join("subproject"));
}
other => panic!("expected ProjectRemoved, got {:?}", other),
}
}
#[test]
fn test_on_dir_removed_regular_dir_no_event() {
use notify::event::{EventKind, RemoveKind};
let tmp = TempDir::new().unwrap();
// Canonicalize to handle macOS /var -> /private/var symlink
let tmp_path = tmp.path().canonicalize().unwrap();
let regular_dir = tmp_path.join("some_dir");
fs::create_dir_all(®ular_dir).unwrap();
let mut table = MountTable::new(tmp_path.clone());
table.mount_ro(&tmp_path).unwrap();
let mount = table.find_mount_mut(&tmp_path).unwrap();
// Simulate regular directory removal event
let event = mount.on_fs_event(®ular_dir, &EventKind::Remove(RemoveKind::Folder));
// Should not emit any event for regular directories
assert!(event.is_none());
}
}