Skip to content

Respect .gitignore during project detection - #253

Open
swpswpswp wants to merge 1 commit into
git-pkgs:mainfrom
swpswpswp:fix/detect-gitignored-evidence
Open

swpswpswp wants to merge 1 commit into
git-pkgs:mainfrom
swpswpswp:fix/detect-gitignored-evidence

Conversation

@swpswpswp

@swpswpswp swpswpswp commented Nov 19, 2026 •

Copy link
Copy Markdown

Closes #204.

Load project and nested .gitignore rules as the bounded project walk visits each directory. Prune ignored untracked subtrees and apply the same visibility to direct tool, script, and manifest probes. Tracked paths keep contributing even when they match a rule, and ordinary untracked source remains visible. Included submodules use their own rule scopes. Project resources drawn from the file index follow the same visibility rules.

Two regressions failed on the original implementation and pass here. A temporary Git repository exercises tracked files inside an ignored directory, ordinary new source, and ignored tool and manifest files. An in-memory tree verifies nested negation and that a pruned subtree does not consume its children's scan budget. The full detect package suite and go vet passed on Windows with Go 1.27.3.

Summary by CodeRabbit

  • New Features
    • Bounded scans now respect .gitignore rules in the project root and visited directories, excluding ignored untracked paths while keeping tracked files and non-ignored new files available.
    • Included submodules use their own ignore rules.
  • Bug Fixes
    • Ignored files no longer contribute detection results or configuration evidence when tracked-only scanning is off.

@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Project scans now apply .gitignore rules from the analysis root and visited directories. Ignored untracked paths are excluded from scan results and detection evidence. Tracked-only scans bypass ignore filtering.

Changes: Gitignore-aware detection

Layer / File(s) Summary
Load rules and filter scanned paths
detect/ignores.go, detect/detect.go, detect/ignores_test.go, detect/detect_test.go, go.mod, README.md
The engine loads .gitignore rules and filters matching untracked paths during scans. Tests cover tracked-only behavior, nested negation, and scan limits. The README documents the behavior, and go.mod lists the gitignore package as a direct dependency.
Filter detection evidence reads
detect/ignores.go, detect/detect.go, detect/ignores_test.go
An ignore-aware reader now handles source, manifest, script, and runtime-version reads. A Git-backed test checks language evidence and ignored report and configuration paths.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant Engine as detect.Engine
  participant Filesystem
  participant Matcher as gitignore matcher
  participant Index as Tracked-path index
  Engine->>Filesystem: Read .gitignore files
  Filesystem-->>Engine: Return ignore rules
  Engine->>Matcher: Register rules and match project paths
  Matcher-->>Engine: Return path match results
  Engine->>Index: Load tracked paths for matching paths
  Index-->>Engine: Return tracked paths
  Engine->>Engine: Exclude ignored untracked paths from scans and evidence reads
Loading

Suggested labels: go

Suggested reviewers: andrew, abhinavgautam01

Merge Risk: Minimal · up to 41f58

Project detection now skips gitignored untracked files while still counting tracked files and ordinary new source. No concrete merge-blocking risk was identified in the supplied change.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Respect .gitignore during project detection" matches the main change. The raw summary shows detect/ignores.go adding bounded .gitignore loading and path filtering, detect/detect.go routing …
Linked Issues check Passed Issue #204 requires project detection to skip gitignored files by default. The reviewed head implements this. loadIgnoreRules in detect/ignores.go loads .gitignore files as the bounded walk visits…
Out of Scope Changes check Passed All changed areas support the objective in #204. detect/ignores.go and detect/ignores_test.go implement and verify ignore handling. detect/detect.go routes file, directory, scan-index, and evidence re…
Full details: Title check

Explanation

The title "Respect .gitignore during project detection" matches the main change. The raw summary shows detect/ignores.go adding bounded .gitignore loading and path filtering, detect/detect.go routing file, directory, and evidence reads through those ignore rules, and tests covering ignored directories and nested negation. The title is specific, concise, and free of file lists or vague terms.

Full details: Linked Issues check

Explanation

Issue #204 requires project detection to skip gitignored files by default. The reviewed head implements this. loadIgnoreRules in detect/ignores.go loads .gitignore files as the bounded walk visits each directory, unless TrackedOnly is set. ignoredProjectPath excludes untracked paths that match a rule. In filesystem-only mode it excludes every matching path. In Git mode it loads the tracked index only after the first matching rule, and keeps tracked files and tracked directories visible. readEvidenceFile applies the same check to tool, script, and manifest reads. Git-independent behavior is covered: a non-Git tree still excludes matching paths, which addresses the issue's complaint about -tracked erroring outside Git. The go.mod change promotes github.com/git-pkgs/gitignore to a direct dependency, as the issue suggests. Tests cover a Git-backed case and an in-memory case with nested negation and pruning. Not established: the scc invocation (sccArgs) is not in the summary, so I did not confirm whether the line-count inconsistency changed. The issue treats that as context rather than a separate requirement. The issue's open decision about licence and security scanning is not addressed by the PR. The PR description scopes visibility to detection probes and indexed project resources only, so this does not fail the assessment.

Full details: Out of Scope Changes check

Explanation

All changed areas support the objective in #204. detect/ignores.go and detect/ignores_test.go implement and verify ignore handling. detect/detect.go routes file, directory, scan-index, and evidence reads through the new checks. detect/detect_test.go adds TrackedOnly to an existing depth test, which matches the new TrackedOnly semantics. go.mod promotes the dependency the issue names. README.md documents the new behavior. No unrelated functional change appears in the summary. The tracked-file exception (tracked paths remain visible when ignored) follows the PR objective and is a deliberate design choice, not unrelated work.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

OSV Scanner (2.7.0)

go.mod

OSV Scanner exited with code 127 without a usable report

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 95b3ab93-f1a6-4789-b6e6-20e593ee75fd
Commits

Reviewing files that changed from the base of the PR and between f037b36 and 41f58e6.

Files selected for processing: 6

  • README.md
  • detect/detect.go
  • detect/detect_test.go
  • detect/ignores.go
  • detect/ignores_test.go
  • go.mod

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #204. Root .gitignore rules, pruning, and tracked files inside an ignored directory all work when I run this against a scratch repo. A few things need to change before it goes in.

Rules above the scan root are never loaded. loadIgnoreRules only reads .gitignore files in directories the walk visits, so with /sub/data/ in the repository root's .gitignore, brief ./sub still counts everything under sub/data/. In my test that put Ruby first from five ignored .rb files against one tracked .go file. When the root is inside a work tree, find the top of the work tree and load each ancestor .gitignore, scoped to its own directory, before the walk starts. Please add a test that scans a subdirectory.

.git/info/exclude is not read, because the matcher is built with gitignore.New(""). A directory listed only there shows as ignored in git status but still supplies language evidence. Load .git/info/exclude for each analysis root on real directories. Leave the global core.excludesFile out so the report does not vary by machine, and say so in the README sentence.

The README says each included submodule uses its own rules, but no test covers a submodule. The non-git on-disk path is also untested: the git test always runs inside a repository and the other one uses fstest.MapFS. One test for each, please.

In detect/ignores_test.go, the language check packs four conditions into one expression, including hasLanguage("Python") == trackedOnly. Split it into one assertion per language so a failure names the language that was wrong.

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.

Skip gitignored files during detection

2 participants