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
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/codegraph-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ tracing-subscriber.workspace = true
lru.workspace = true
regex.workspace = true
globset.workspace = true
ignore = "0.4"
fs2.workspace = true

# CodeGraph ecosystem
codegraph.workspace = true
Expand Down
110 changes: 44 additions & 66 deletions crates/codegraph-server/src/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use crate::index_state::IndexState;
use crate::parser_registry::ParserRegistry;
use crate::path_filter::WorkspaceFilter;
use crate::watcher::GraphUpdater;
use codegraph::CodeGraph;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -323,6 +324,21 @@ pub struct IndexResult {
pub parser_errors_by_language: std::collections::HashMap<String, usize>,
}

type DirectoryIndexFuture<'a> = std::pin::Pin<
Box<
dyn std::future::Future<
Output = (
usize,
usize,
usize,
std::collections::HashMap<String, usize>,
std::collections::HashMap<String, usize>,
),
> + Send
+ 'a,
>,
>;

/// Shared indexer for walking directories, hashing files, and parsing them
/// into a [`CodeGraph`].
pub struct Indexer {
Expand Down Expand Up @@ -368,14 +384,8 @@ impl Indexer {
let mut result = IndexResult::default();

for folder in folders {
// Bounty 2026-05-03 — extend exclude_patterns from this
// folder's `.codegraphignore` if present. Per-folder so each
// workspace can have its own rules; doesn't pollute other
// folders' configs.
let mut folder_config = config.clone();
folder_config.extend_from_codegraphignore(folder);
let (total, parsed, skipped, by_lang, parser_errors) = self
.index_directory(graph, folder, &folder_config, 0, counter.clone())
.index_directory(graph, folder, config, 0, counter.clone())
.await;
result.total_files += total;
result.files_parsed += parsed;
Expand Down Expand Up @@ -429,20 +439,23 @@ impl Indexer {
config: &'a IndexConfig,
depth: u32,
counter: Arc<std::sync::atomic::AtomicUsize>,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = (
usize,
usize,
usize,
std::collections::HashMap<String, usize>,
std::collections::HashMap<String, usize>,
),
> + Send
+ 'a,
>,
> {
) -> DirectoryIndexFuture<'a> {
Box::pin(async move {
let mut filter = WorkspaceFilter::new(dir, config);
self.index_directory_filtered(graph, dir, config, depth, counter, &mut filter)
.await
})
}

fn index_directory_filtered<'a>(
&'a self,
graph: &'a Arc<RwLock<CodeGraph>>,
dir: &'a Path,
config: &'a IndexConfig,
depth: u32,
counter: Arc<std::sync::atomic::AtomicUsize>,
filter: &'a mut WorkspaceFilter,
) -> DirectoryIndexFuture<'a> {
Box::pin(async move {
use std::sync::atomic::Ordering;

Expand All @@ -462,7 +475,6 @@ impl Indexer {
return (0, 0, 0, empty_by_lang, empty_errors);
}

let exclude_set = config.build_exclude_set();
let supported_extensions = self.parsers.supported_extensions();

tracing::debug!("Scanning directory: {:?}", dir);
Expand Down Expand Up @@ -492,35 +504,20 @@ impl Indexer {

let path = entry.path();

// Skip hidden files and directories
if let Some(name) = path.file_name() {
if name.to_string_lossy().starts_with('.') {
continue;
}
if !filter.allows(&path, path.is_dir()) {
continue;
}

if path.is_dir() {
let dir_name = path
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_default();

// Skip hardcoded exclude directories
if config.exclude_dirs.iter().any(|e| e == &dir_name) {
continue;
}

// Skip directories matching user-configured exclude globs
let path_str = path.to_string_lossy();
if exclude_set.is_match(path_str.as_ref())
|| exclude_set.is_match(dir_name.as_str())
{
tracing::info!("Skipping {:?}: matched exclude pattern", path);
continue;
}

let (t, p, s, child_by_lang, child_errors) = self
.index_directory(graph, &path, config, depth + 1, counter.clone())
.index_directory_filtered(
graph,
&path,
config,
depth + 1,
counter.clone(),
filter,
)
.await;
total += t;
parsed += p;
Expand All @@ -532,25 +529,6 @@ impl Indexer {
*parser_errors.entry(lang).or_insert(0) += count;
}
} else if path.is_file() {
// Skip files matching exclude globs
let path_str = path.to_string_lossy();
if exclude_set.is_match(path_str.as_ref()) {
continue;
}

// Skip files that exceed the configurable size limit
if let Ok(metadata) = std::fs::metadata(&path) {
if metadata.len() > config.max_file_size_bytes {
tracing::info!(
"Skipping {:?}: file size {} exceeds limit of {}",
path,
metadata.len(),
config.max_file_size_bytes
);
continue;
}
}

// Check if file has a supported extension
if let Some(ext) = path.extension() {
let ext_str = ext.to_string_lossy();
Expand Down
1 change: 1 addition & 0 deletions crates/codegraph-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub mod mcp;
pub mod memory;
pub mod metadata;
pub mod parser_registry;
mod path_filter;
pub mod runtime_deps;
pub mod telemetry;
pub mod watcher;
Expand Down
72 changes: 71 additions & 1 deletion crates/codegraph-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@ struct Args {
socket: Option<PathBuf>,
}

impl Args {
fn engine_args(&self) -> Vec<std::ffi::OsString> {
let mut args = vec![
"--embedding-model".into(),
self.embedding_model.clone().into(),
"--max-files".into(),
self.max_files.to_string().into(),
];
for directory in &self.exclude {
args.extend(["--exclude".into(), directory.into()]);
}
if self.graph_only {
args.push("--graph-only".into());
}
if let Some(profile) = &self.profile {
args.extend(["--profile".into(), profile.into()]);
}
if self.full_body_embedding {
args.push("--full-body-embedding".into());
}
args
}
}

/// Default engine socket path (`~/.codegraph/cg-engine.sock`).
fn default_socket_path() -> PathBuf {
let home = std::env::var_os("HOME")
Expand Down Expand Up @@ -336,7 +360,7 @@ async fn run() {
std::env::current_dir().expect("Failed to get current directory")
});
if let Err(e) =
codegraph_server::mcp::engine::connect(&sock, workspace, &args.embedding_model).await
codegraph_server::mcp::engine::connect(&sock, workspace, &args.engine_args()).await
{
eprintln!("connect failed: {e}");
std::process::exit(1);
Expand Down Expand Up @@ -422,6 +446,14 @@ async fn run() {
exclude_dirs: args.exclude.clone(),
max_files: args.max_files,
full_body_embedding: args.full_body_embedding,
graph_only: args.graph_only,
tool_profile: codegraph_server::mcp::tools::ToolProfile::from_str_or_all(
&args
.profile
.clone()
.or_else(|| std::env::var("CODEGRAPH_TOOL_PROFILE").ok())
.unwrap_or_default(),
),
seeds: args.workspace.clone(),
};
if let Err(e) = codegraph_server::mcp::engine::serve(cfg).await {
Expand Down Expand Up @@ -520,6 +552,44 @@ async fn run() {
}
}

#[cfg(test)]
mod engine_args_tests {
use super::*;

#[test]
fn auto_spawn_preserves_resource_settings() {
let client = Args::try_parse_from([
"codegraph-server",
"--connect",
"--graph-only",
"--profile",
"graph",
"--exclude",
"cache",
"--exclude",
"generated",
"--max-files",
"12",
"--embedding-model",
"static",
])
.unwrap();
let mut command = vec![
std::ffi::OsString::from("codegraph-server"),
"--serve".into(),
];
command.extend(client.engine_args());
let engine = Args::try_parse_from(command).unwrap();
assert!(engine.serve);
assert!(engine.graph_only);
assert_eq!(engine.profile.as_deref(), Some("graph"));
assert_eq!(engine.exclude, ["cache", "generated"]);
assert_eq!(engine.max_files, 12);
assert_eq!(engine.embedding_model, "static");
assert_eq!(engine.full_body_embedding, client.full_body_embedding);
}
}

#[cfg(test)]
mod crash_breadcrumb_tests {
use super::{classify_panic, write_crash_breadcrumb};
Expand Down
Loading