Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

`-f` drops blank lines and trims whitespace in pattern files; an empty pattern file matches every line

Đã đóng
#181 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Một pull request liên quan đã được merge.

  • #187 của @shengyfu — đã merge

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust
Lĩnh vực
cli, search

Hướng nghiên cứu

Start with SearchOptions::all_patterns in tgrep-cli/src/search.rs and trace how its pattern list reaches combine_patterns in tgrep-cli/src/matching.rs and build_multi_pattern_plan in tgrep-core/src/query.rs. Add regression coverage for blank lines, whitespace, and an empty pattern file, beginning with the --no-index examples in the issue. Done when pattern lines retain their contents, empty files match nothing, and the behavior is verified for the described search paths.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Summary

-f/--file reads pattern files differently from ripgrep. Every line is trimmed and blank lines are dropped, so:

  • a blank line, which ripgrep treats as an empty pattern that matches every line, is ignored;
  • leading and trailing whitespace is removed from each pattern ( test is searched as test);
  • an empty pattern file matches every line and exits 0, while ripgrep matches nothing and exits 1.

ripgrep documents the first case explicitly: "Empty pattern lines will match all input lines, and the newline is not counted as part of the pattern."

All three happen with --no-index, with the on-disk index, and through tgrep serve.

Reproduce

mkdir repro && cd repro
printf 'alpha\nbeta\n' > a.txt
printf 'alpha\n\n'     > blank_line.txt   # "alpha" plus one empty pattern
: >                      empty.txt        # no patterns at all
printf ' test\n'       > b.txt
printf ' test\n'       > space.txt        # one pattern with a leading space

tgrep --no-index -n -f blank_line.txt a.txt
tgrep --no-index -n -f empty.txt a.txt
tgrep --no-index -o -f space.txt b.txt
Query Actual: tgrep Expected: rg 15.1.0
-n -f blank_line.txt a.txt 1:alpha 1:alpha, 2:beta
-n -f empty.txt a.txt 1:alpha, 2:beta (exit 0) no output (exit 1)
-o -f space.txt b.txt test test

Environment

  • tgrep 1.1.0, built from 1120aca (current main) with rustc 1.95.0 (59807616e 2026-04-14)
  • Windows 11 Pro (build 28000); the code involved is platform-independent
  • Same results with --no-index, with a local index (tgrep index .), and through tgrep serve

Cause

SearchOptions::all_patterns trims each line of the pattern file and skips it if the result is empty:

https://github.com/microsoft/tgrep/blob/1120aca41dd192ae61bdd996e9886cb354a78a27/tgrep-cli/src/search.rs#L441-L449

For an empty file the pattern list ends up empty. combine_patterns then joins zero patterns into (?:), which matches everywhere, and build_multi_pattern_plan returns MatchAll for zero patterns. The client builds the server request from the same list (search.rs#L994), so serve behaves the same way.

Possible fix

  • Keep each line as written. str::lines already strips both \n and \r\n, so dropping the trim() and the empty-line filter is enough, and pattern files with CRLF line endings keep working (a file containing alpha\r\n finds alpha in both tools today).
  • Treat "no patterns" as "matches nothing" rather than as one empty pattern: skip the search and exit 1, as ripgrep does. An empty line in the file is one empty pattern and should still match every line.

A regression test could run the three cases above with --no-index, against a local index, and through serve.

Ngôn ngữ chính
Rust
Star
3.5k
Fork
139
Merge trung bình
16 giờ 31 phút
Pull request đã merge (30 ngày)
27

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/tgrep

Tất cả issue của microsoft/tgrep

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.