Skip to content
Open
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
4 changes: 2 additions & 2 deletions crates/cg-sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ fn event_paths(root: &Path, view: &View, paths: Option<&[PathBuf]>) -> Option<Ve
return None;
}
let rel = p.to_str()?.replace('\\', "/");
if rel.is_empty() || crate::watch::is_config_name(rel.rsplit('/').next()?) || root.join(&p).is_dir() {
if rel.is_empty() || crate::watch::is_config_name(rel.rsplit('/').next()?) || crate::watch::is_git_inventory_path(&p) || root.join(&p).is_dir() {
return None;
}
// A removed directory is no longer identifiable with stat.
Expand Down Expand Up @@ -193,7 +193,7 @@ fn scan_events(root: &Path, config: &ProjectConfig, view: &View, paths: &[String
None => {
// A tracked-but-ignored file can be included by the full scanner's git
// supplement; use that scanner rather than silently dropping the file.
if entries.contains_key(rel) && root.join(rel).is_file() && rules.is_ignored(rel, false) {
if root.join(rel).is_file() && rules.is_ignored(rel, false) && !rules.builtin_ignored(rel, false) {
return None;
}
entries.remove(rel);
Expand Down
67 changes: 65 additions & 2 deletions crates/cg-sync/src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,28 @@ pub(crate) fn is_config_name(name: &str) -> bool {
|| (name.starts_with("requirements") && name.ends_with(".txt"))
}

pub(crate) fn is_git_inventory_path(rel: &Path) -> bool {
rel == Path::new(".git/index") || rel == Path::new(".git/info/exclude")
}

struct Filter {
root: PathBuf,
rules: IgnoreRules,
indexed: Option<cg_store::Snapshot>,
}

impl Filter {
fn new(root: &Path) -> Filter {
let config = ProjectConfig::load(root);
Filter { root: root.to_path_buf(), rules: IgnoreRules::new(root, &config) }
Filter { root: root.to_path_buf(), rules: IgnoreRules::new(root, &config), indexed: Store::at(root).open_snapshot().ok() }
}

/// Does an event at `p` possibly change what the index says?
fn relevant(&self, p: &Path) -> bool {
let Ok(rel) = p.strip_prefix(&self.root) else { return false };
if is_git_inventory_path(rel) {
return true;
}
let rel = rel.to_string_lossy().replace('\\', "/");
if rel.is_empty() || rel == brand::DATA_DIR || rel.starts_with(&format!("{}/", brand::DATA_DIR)) || rel == ".git" || rel.starts_with(".git/") {
return false;
Expand All @@ -112,7 +120,20 @@ impl Filter {
return true;
}
let is_dir = p.is_dir();
!self.rules.is_ignored(&rel, is_dir)
if !self.rules.is_ignored(&rel, is_dir) {
return true;
}
if self.rules.builtin_ignored(&rel, is_dir) {
return false;
}
// The full scan revives tracked-but-ignored sources. Use its published
// inventory here, including deleted files and their removed directories.
let Some(indexed) = &self.indexed else { return false };
if indexed.view.file_by_path(&rel).is_some() {
return true;
}
let prefix = format!("{rel}/");
!p.is_file() && indexed.view.files().any(|f| indexed.view.file(f).path().starts_with(&prefix))
}
}

Expand Down Expand Up @@ -203,6 +224,9 @@ fn run_watch(root: &Path, opts: &SyncOptions, stop: &AtomicBool) {
}
if !matches!(ev.kind, EventKind::Access(_)) {
for p in ev.paths {
if p.strip_prefix(root).is_ok_and(is_git_inventory_path) {
full_scan = true;
}
if p.file_name().and_then(|n| n.to_str()).is_some_and(is_config_name) {
filter = Filter::new(root);
}
Expand Down Expand Up @@ -236,20 +260,24 @@ fn run_watch(root: &Path, opts: &SyncOptions, stop: &AtomicBool) {
full_scan = false;
burst_started = None;
run_sync(root, &local);
filter = Filter::new(root);
last_scan = Instant::now();
}
} else if degraded && last_scan.elapsed() >= FALLBACK_SCAN.min(Duration::from_secs(5)) {
run_sync(root, opts);
filter = Filter::new(root);
last_scan = Instant::now();
} else if last_scan.elapsed() >= FALLBACK_SCAN * 5 {
// slow safety net: events can be dropped (inotify queue overflow, network drives)
run_sync(root, opts);
filter = Filter::new(root);
last_scan = Instant::now();
}
if dirty.is_empty() && !full_scan && last_event.elapsed() >= Duration::from_secs(30) && last_maintenance.elapsed() >= Duration::from_secs(30) {
if let Ok(report) = crate::compact_if_due(root) {
if report.compacted {
tracing::info!("compacted idle snapshot");
filter = Filter::new(root);
}
}
last_maintenance = Instant::now();
Expand All @@ -260,3 +288,38 @@ fn run_watch(root: &Path, opts: &SyncOptions, stop: &AtomicBool) {
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use cg_resolve::{index_project, IndexOptions};
use std::process::Command;

#[test]
fn filter_keeps_indexed_ignored_paths_and_their_removed_parents() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
std::fs::create_dir(root.join("ignored")).unwrap();
std::fs::create_dir_all(root.join("node_modules/dep")).unwrap();
std::fs::write(root.join(".gitignore"), "ignored/\n").unwrap();
std::fs::write(root.join("cgr.json"), r#"{"exclude":["ignored/excluded.ts"]}"#).unwrap();
for rel in ["ignored/tracked.ts", "ignored/excluded.ts", "ignored/untracked.ts", "node_modules/dep/index.ts"] {
std::fs::write(root.join(rel), "export function run() {}\n").unwrap();
}
for args in [vec!["init", "--quiet"], vec!["add", "-f", "ignored/tracked.ts", "ignored/excluded.ts", "node_modules/dep/index.ts"]] {
let result = Command::new("git").arg("-C").arg(root).args(args).output().unwrap();
assert!(result.status.success(), "{}", String::from_utf8_lossy(&result.stderr));
}
index_project(root, &IndexOptions { threads: Some(2), ..Default::default() }).unwrap();
let filter = Filter::new(root);
assert!(filter.relevant(&root.join("ignored/tracked.ts")));
for rel in ["ignored/untracked.ts", "ignored/excluded.ts", "node_modules/dep/index.ts", ".git/objects/ab/object", ".cgr/manifest"] {
assert!(!filter.relevant(&root.join(rel)), "irrelevant: {rel}");
}
assert!(filter.relevant(&root.join(".git/index")), "Git inventory changes need a full scan");
assert!(filter.relevant(&root.join(".git/info/exclude")), "Git ignore changes need a full scan");
std::fs::remove_dir_all(root.join("ignored")).unwrap();
assert!(filter.relevant(&root.join("ignored/tracked.ts")), "deleted tracked source");
assert!(filter.relevant(&root.join("ignored")), "deleted ignored parent");
}
}
159 changes: 159 additions & 0 deletions crates/cg-sync/tests/ignored_paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
//! Git-tracked ignored sources must follow the same inventory policy on path-driven sync.
use cg_resolve::{build_seg, index_project, IndexOptions};
use cg_store::{dump, Store, View};
use cg_sync::{spawn_watcher, sync, SyncOptions};
use std::path::Path;
use std::process::Command;
use std::sync::Arc;
use std::time::{Duration, Instant};

fn git(root: &Path, args: &[&str]) {
let output = Command::new("git").arg("-C").arg(root).args(args).output().unwrap();
assert!(output.status.success(), "git {args:?}: {}", String::from_utf8_lossy(&output.stderr));
}

fn write(root: &Path, rel: &str, text: &str) {
let path = root.join(rel);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, text).unwrap();
}

fn graph(root: &Path) -> Vec<String> {
dump::canonical(&Store::at(root).open_snapshot().unwrap().view)
}

fn assert_fresh(root: &Path, opts: &IndexOptions) {
// Build against the same Git inventory without replacing the incremental snapshot.
let fresh = View::new(Arc::new(build_seg(root, opts).unwrap().seg));
assert_eq!(graph(root), dump::canonical(&fresh));
}

#[test]
fn newly_tracked_ignored_sources_are_discovered_by_path_sync() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
git(root, &["init", "--quiet"]);
write(root, ".gitignore", "ignored/\n");
write(root, "main.ts", "export function main() {}\n");
write(root, "ignored/new.ts", "export function added() {}\n");
write(root, "ignored/untracked.ts", "export function untracked() {}\n");
write(root, "ignored/excluded.ts", "export function excluded() {}\n");
write(root, "node_modules/dep/index.ts", "export function dependency() {}\n");
write(root, "cgr.json", r#"{"exclude":["ignored/excluded.ts"]}"#);
for i in 0..48 {
write(root, &format!("filler{i}.ts"), &format!("export function filler{i}() {{}}\n"));
}
let opts = IndexOptions { threads: Some(2), ..Default::default() };
index_project(root, &opts).unwrap();
assert!(!graph(root).iter().any(|l| l.contains("ignored/new.ts")));

git(root, &["add", "-f", "ignored/new.ts", "ignored/excluded.ts", "node_modules/dep/index.ts"]);
let report = sync(
root,
&SyncOptions {
index: opts.clone(),
paths: Some(vec![root.join("ignored/new.ts"), root.join("ignored/excluded.ts"), root.join("node_modules/dep/index.ts")]),
..Default::default()
},
)
.unwrap();
assert!(report.full_rebuild.is_none(), "{report:?}");
assert_eq!(report.added, 1, "{report:?}");
let actual = graph(root);
assert!(actual.iter().any(|l| l.contains("ignored/new.ts")));
for path in ["ignored/untracked.ts", "ignored/excluded.ts", "node_modules/dep/index.ts"] {
assert!(!actual.iter().any(|l| l.contains(path)), "must exclude {path}");
}
assert_fresh(root, &opts);

write(root, "ignored/new.ts", "export function renamed() { return 123; }\n");
let report = sync(root, &SyncOptions { index: opts.clone(), paths: Some(vec![root.join("ignored/new.ts")]), ..Default::default() }).unwrap();
assert!(report.full_rebuild.is_none(), "{report:?}");
assert!(graph(root).iter().any(|l| l.ends_with(" name=renamed")));
assert_fresh(root, &opts);

std::fs::remove_file(root.join("ignored/new.ts")).unwrap();
let report = sync(root, &SyncOptions { index: opts.clone(), paths: Some(vec![root.join("ignored/new.ts")]), ..Default::default() }).unwrap();
assert!(report.full_rebuild.is_none(), "{report:?}");
assert!(!graph(root).iter().any(|l| l.contains("ignored/new.ts")));
assert_fresh(root, &opts);
}

#[test]
fn git_inventory_events_refresh_ignored_file_membership() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path();
git(root, &["init", "--quiet"]);
write(root, ".gitignore", "ignored/\n");
write(root, "ignored/late.ts", "export function late() {}\n");
write(root, "conditional.ts", "export function conditional() {}\n");
for i in 0..48 {
write(root, &format!("filler{i}.ts"), &format!("export function filler{i}() {{}}\n"));
}
let opts = IndexOptions { threads: Some(2), ..Default::default() };
index_project(root, &opts).unwrap();
let sync_event = |rel: &str| sync(root, &SyncOptions { index: opts.clone(), paths: Some(vec![root.join(rel)]), ..Default::default() }).unwrap();

git(root, &["add", "-f", "ignored/late.ts"]);
let report = sync_event(".git/index");
assert_eq!(report.added, 1, "{report:?}");
assert_fresh(root, &opts);

git(root, &["rm", "--cached", "--quiet", "ignored/late.ts"]);
let report = sync_event(".git/index");
assert!(root.join("ignored/late.ts").is_file());
assert!(report.full_rebuild.is_none(), "{report:?}");
assert!(!graph(root).iter().any(|l| l.contains("ignored/late.ts")));
assert_fresh(root, &opts);

write(root, ".git/info/exclude", "conditional.ts\n");
let report = sync_event(".git/info/exclude");
assert_eq!(report.removed, 1, "{report:?}");
assert_fresh(root, &opts);

write(root, ".git/info/exclude", "");
let report = sync_event(".git/info/exclude");
assert!(report.full_rebuild.is_none(), "{report:?}");
assert!(graph(root).iter().any(|l| l.contains("conditional.ts")));
assert_fresh(root, &opts);
}

#[test]
fn watcher_observes_ignored_edits_and_git_index_changes() {
let dir = tempfile::tempdir().unwrap();
let root = dir.path().canonicalize().unwrap();
git(&root, &["init", "--quiet"]);
write(&root, ".gitignore", "ignored/\n");
write(&root, "ignored/tracked.ts", "export function before() {}\n");
git(&root, &["add", "-f", "ignored/tracked.ts"]);
let opts = IndexOptions { threads: Some(2), ..Default::default() };
index_project(&root, &opts).unwrap();
let watcher = spawn_watcher(root.clone(), SyncOptions { index: opts.clone(), ..Default::default() });
let await_graph = |predicate: &dyn Fn(&[String]) -> bool| {
let deadline = Instant::now() + Duration::from_secs(15);
loop {
let actual = graph(&root);
if predicate(&actual) {
break;
}
assert!(Instant::now() < deadline, "watcher did not update the graph: {actual:#?}");
std::thread::sleep(Duration::from_millis(50));
}
assert_fresh(&root, &opts);
};
write(&root, "ignored/tracked.ts", "export function after_edit() {}\n");
await_graph(&|g| g.iter().any(|l| l.ends_with(" name=after_edit")));

write(&root, "ignored/late.ts", "export function late() {}\n");
git(&root, &["add", "-f", "ignored/late.ts"]);
await_graph(&|g| g.iter().any(|l| l.contains("ignored/late.ts")));
// The filter must refresh after publication to admit edits to the newly tracked source.
write(&root, "ignored/late.ts", "export function late_edit() {}\n");
await_graph(&|g| g.iter().any(|l| l.ends_with(" name=late_edit")));

git(&root, &["rm", "--cached", "--force", "--quiet", "ignored/late.ts"]);
await_graph(&|g| !g.iter().any(|l| l.contains("ignored/late.ts")));
std::fs::remove_file(root.join("ignored/tracked.ts")).unwrap();
await_graph(&|g| !g.iter().any(|l| l.contains("ignored/tracked.ts")));
watcher.stop();
}
3 changes: 3 additions & 0 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ so another session takes over). A bounded queue feeds debounced paths to sync, a
filesystem walk for normal events. Config/directory changes, rescan flags, errors or lost/burst
events fall back to a full scan. Periodic scanning recovers missing events. The inventory is
still assembled in memory and earlier delta files may be re-extracted against the same base.
The event filter retains ignored sources already present in the published inventory;
`.git/index` and `.git/info/exclude` events request a full scan so tracked-file membership and
local ignore changes are visible. Built-in skips and explicit `exclude` patterns still apply.

Kernel output buffers accumulated during extraction can spill to shared immutable temporary
chunks, then be read through the same borrowed ABI views. Source text is not retained there.
Expand Down