fix: ignored-parent semantics#658
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
This comment has been minimized.
This comment has been minimized.
| parsedRules := parseIgnoreFile(content, filepath.Dir(ignoreFile)) | ||
| globs = append(globs, parsedRules...) | ||
| } | ||
| globPatternMatcher = gitignore.CompileIgnoreLines(globs...) |
There was a problem hiding this comment.
Issue: This is a proper performance issue.
There was a problem hiding this comment.
Compiling is a very CPU cost intensive operation
There was a problem hiding this comment.
@PeterSchafer it seems it is a real issue as nested .gitignore are treated as a flat list. It diverges from the .gitignore expected behavior
There was a problem hiding this comment.
Probably this current approach is not good as you said, but we might wanna check what is being ignored and ignore nested ones
There was a problem hiding this comment.
I believe a strategy building a "tree" where we can prune the branches is the best approach
| { | ||
| name: "ignore file below ignored parent cannot reinclude files", | ||
| files: map[string]string{ | ||
| ".gitignore": "node_modules\n", | ||
| "node_modules/pkg/.gitignore": "!index.js\n", | ||
| "node_modules/pkg/index.js": "x", | ||
| "node_modules/pkg/other.js": "x", | ||
| "src/index.js": "x", | ||
| }, | ||
| ruleFiles: []string{".gitignore"}, | ||
| excluded: []string{"node_modules/pkg/index.js", "node_modules/pkg/other.js"}, | ||
| kept: []string{"src/index.js"}, | ||
| }, |
There was a problem hiding this comment.
it seems for .gitignore inside node_modules is not something common / expected to have. But this reveals a potential issue regarding nested directories / experimental repos inside real repos, etc.
Nice finding!
|
Suggestion: Let's understand the problem better and the possible solutions. This is a crucial logic that needs to be carefully handled. |
|
closed by mistake, reopening |
PR Reviewer Guide 🔍
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit fe47ad7. Configure here.
| globPatternMatcher := gitignore.CompileIgnoreLines() | ||
| for _, ignoreFile := range ignoreFiles { | ||
| if globPatternMatcher.MatchesPath(ignoreFile) { | ||
| continue |
There was a problem hiding this comment.
Skipped root policy rule files
Medium Severity
buildGlobs skips reading a rule file when MatchesPath is true for that file’s path. That also drops later .snyk or .dcignore files at the repo root when an earlier .gitignore ignores them, even though their parent directory is not excluded. Those policy excludes no longer enter globs, so scanning can miss paths that used to be filtered.
Reviewed by Cursor Bugbot for commit fe47ad7. Configure here.
| parsedRules := parseIgnoreFile(content, filepath.Dir(ignoreFile)) | ||
| globs = append(globs, parsedRules...) | ||
| } | ||
| globPatternMatcher = gitignore.CompileIgnoreLines(globs...) |
There was a problem hiding this comment.
Recompiles matcher every ignore file
Medium Severity
Each loop iteration in buildGlobs calls gitignore.CompileIgnoreLines on the entire growing globs slice. CompileIgnoreLines is expensive, so repos with many nested ignore files pay roughly quadratic CPU during rule collection compared to the previous single-pass append.
Reviewed by Cursor Bugbot for commit fe47ad7. Configure here.


Description
Prevents ignore files inside an already ignored directory from re-including descendant files.
GAF collected rules from every nested
.gitignore,.dcignore, and.snykfile before filtering paths.Example
The nested negation incorrectly re-included
index.js. Git does not process ignore files beneath an ignored parent directory.Solution
While building ignore rules, ignore files whose path has been already ignored.
Testing
code-client-goandsnyk-lsconsumers locally.make format,make generate,make lint,make testRelated task
CLI-1526
Checklist
make test)make generate)make lint)go get github.com/snyk/go-application-framework@YOUR_LATEST_GAF_COMMITin thecliv2directory.go.modto point to your local GAF code.go mod tidyin thecliv2directory.go.modandgo.sumchanges.Note
Medium Risk
Changes which files are scanned for Snyk/CLI uploads by altering ignore semantics; behavior shifts for repos that relied on nested negations under excluded trees, but matches Git and reduces incorrect inclusions.
Overview
Aligns ignore-rule collection with Git by not reading
.gitignore,.dcignore, or.snykfiles whose path is already covered by rules gathered from earlier ignore files.In
buildGlobs, a matcher is built incrementally from accumulated globs. Each candidate ignore file is skipped when that matcher already matches its path; otherwise its rules are merged and the matcher is refreshed. That stops nested negations (e.g.node_modulesat the root plus!index.jsundernode_modules/pkg/) from re-including files Git would still exclude.Tests add
node_modules/.snykparent cases and tighten expectations for ignored folders with negation rules so descendants under an ignored parent stay excluded.Reviewed by Cursor Bugbot for commit fe47ad7. Bugbot is set up for automated code reviews on this repo. Configure here.