Skip to content

fix(core): prune directories with all files excluded from directory tree (#111) - #348

Closed
napattan wants to merge 1 commit into
mufeedvh:mainfrom
napattan:fix/issue-111-prune-excluded-empty-dirs-in-tree
Closed

napattan wants to merge 1 commit into
mufeedvh:mainfrom
napattan:fix/issue-111-prune-excluded-empty-dirs-in-tree

Conversation

@napattan

@napattan napattan commented Nov 6, 2026

Copy link
Copy Markdown

Summary

Resolves #111.

Root Cause

In crates/code2prompt-core/src/path.rs, traverse_directory added entries to the visual tree whenever if path.is_dir() || entry_match held (L139). Because ignore::WalkBuilder yields directory entries before visiting their children—and user --exclude glob patterns (such as --exclude "*.txt" or --exclude "**/subfolder/*") match file paths rather than the parent directory path itself—every traversed directory was unconditionally inserted into tree even when all files inside it were excluded.

Fix

  • Replace if path.is_dir() || entry_match with let include_in_tree = config.full_directory_tree || (path.is_file() && entry_match);.
  • Because termtree::Tree builds parent directory chains automatically via cur.split('/') whenever a matched file is added, non-empty directories continue to render identically, while directories with zero matched files are pruned unless --full-directory-tree is explicitly enabled.
  • Add regression test test_excluded_files_prune_empty_directories_from_tree in crates/code2prompt-core/tests/path_test.rs (cargo test -p code2prompt_core --test path_test -> 9/9 PASS).

Fixes #111

…ree (mufeedvh#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 `<directory_tree>` unless `--full-directory-tree` is enabled (Fixes mufeedvh#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 <tanfasantia@gmail.com>
@napattan napattan closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--no-ignore switch not functional

1 participant