From 1faaedc19821bf7a17824c22c9f3315250386f2d Mon Sep 17 00:00:00 2001 From: "Napat (Tan) Phasundhiae" Date: Fri, 6 Nov 2026 11:56:13 +0700 Subject: [PATCH] fix(core): prune directories with all files excluded from directory tree (#111) - Change `include_in_tree` condition in `crates/code2prompt-core/src/path.rs` from `path.is_dir() || entry_match` to `config.full_directory_tree || (path.is_file() && entry_match)` so directories whose files are all excluded via `--exclude` are not rendered as empty branches in `` unless `--full-directory-tree` is enabled (Fixes #111) - Add regression test `test_excluded_files_prune_empty_directories_from_tree` in `crates/code2prompt-core/tests/path_test.rs` Signed-off-by: Napat (Tan) Phasundhiae --- crates/code2prompt-core/src/path.rs | 2 +- crates/code2prompt-core/tests/path_test.rs | 33 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/crates/code2prompt-core/src/path.rs b/crates/code2prompt-core/src/path.rs index e6a574d..6f5fd94 100644 --- a/crates/code2prompt-core/src/path.rs +++ b/crates/code2prompt-core/src/path.rs @@ -136,7 +136,7 @@ fn discover_files( } // Directory Tree - let include_in_tree = config.full_directory_tree || entry_match; + let include_in_tree = config.full_directory_tree || (path.is_file() && entry_match); if include_in_tree { let mut current_tree = &mut tree; diff --git a/crates/code2prompt-core/tests/path_test.rs b/crates/code2prompt-core/tests/path_test.rs index 8035a89..4c5e93d 100644 --- a/crates/code2prompt-core/tests/path_test.rs +++ b/crates/code2prompt-core/tests/path_test.rs @@ -336,4 +336,37 @@ mod tests { #[cfg(unix)] assert!(tree_str.contains("link_to_file")); } + + #[rstest] + fn test_excluded_files_prune_empty_directories_from_tree(simple_dir_structure: TempDir) { + let config = Code2PromptConfig::builder() + .path(simple_dir_structure.path().to_path_buf()) + .exclude_patterns(vec!["subdir/**".to_string()]) + .full_directory_tree(false) + .build() + .unwrap(); + + let (tree_str, files) = traverse_directory(&config, None).unwrap(); + + assert_eq!(files.len(), 1); + assert!(file_exists(&files, "file1.txt")); + assert!( + !tree_str.contains("subdir"), + "Excluded directory with no remaining files should not appear in tree when full_directory_tree=false, got:\n{tree_str}" + ); + assert!(!tree_str.contains("nested")); + + let full_tree_config = Code2PromptConfig::builder() + .path(simple_dir_structure.path().to_path_buf()) + .exclude_patterns(vec!["subdir/**".to_string()]) + .full_directory_tree(true) + .build() + .unwrap(); + + let (full_tree_str, _) = traverse_directory(&full_tree_config, None).unwrap(); + assert!( + full_tree_str.contains("subdir"), + "Excluded directory should appear in tree when full_directory_tree=true, got:\n{full_tree_str}" + ); + } }