-
Notifications
You must be signed in to change notification settings - Fork 18
fix: ignored-parent semantics #658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -148,7 +148,12 @@ func (fw *FileFilter) GetFilteredFiles(filesCh chan string, globs []string) chan | |
| // buildGlobs iterates a list of ignore filesToFilter and returns a list of glob patterns that can be used to test for ignored filesToFilter | ||
| func (fw *FileFilter) buildGlobs(ignoreFiles []string) ([]string, error) { | ||
| var globs = make([]string, 0) | ||
| globPatternMatcher := gitignore.CompileIgnoreLines() | ||
| for _, ignoreFile := range ignoreFiles { | ||
| if globPatternMatcher.MatchesPath(ignoreFile) { | ||
| continue | ||
| } | ||
|
|
||
| var content []byte | ||
| content, err := os.ReadFile(ignoreFile) | ||
| if err != nil { | ||
|
|
@@ -162,6 +167,7 @@ func (fw *FileFilter) buildGlobs(ignoreFiles []string) ([]string, error) { | |
| parsedRules := parseIgnoreFile(content, filepath.Dir(ignoreFile)) | ||
| globs = append(globs, parsedRules...) | ||
| } | ||
| globPatternMatcher = gitignore.CompileIgnoreLines(globs...) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Issue: This is a proper performance issue.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Compiling is a very CPU cost intensive operation
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @PeterSchafer it seems it is a real issue as nested .gitignore are treated as a flat list. It diverges from the .gitignore expected behavior
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably this current approach is not good as you said, but we might wanna check what is being ignored and ignore nested ones
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe a strategy building a "tree" where we can prune the branches is the best approach There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recompiles matcher every ignore fileMedium Severity Each loop iteration in Reviewed by Cursor Bugbot for commit fe47ad7. Configure here. |
||
| } | ||
|
|
||
| return globs, nil | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -606,6 +606,31 @@ func TestFileFilter_GetFilteredFiles_ignoreRuleScenarios(t *testing.T) { | |
| excluded: []string{"root.txt", "pkg/pkg.txt", "pkg/root.txt"}, | ||
| kept: []string{"pkg/keep.txt"}, | ||
| }, | ||
| { | ||
| 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"}, | ||
| }, | ||
|
Comment on lines
+609
to
+621
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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! |
||
| { | ||
| name: "gitignore below snyk-excluded parent cannot reinclude files", | ||
| files: map[string]string{ | ||
| ".snyk": "version: v1.25.1\nexclude:\n global:\n - node_modules\n", | ||
| "node_modules/pkg/.gitignore": "!index.js\n", | ||
| "node_modules/pkg/index.js": "x", | ||
| "src/index.js": "x", | ||
| }, | ||
| ruleFiles: []string{".gitignore", ".snyk"}, | ||
| excluded: []string{"node_modules/pkg/index.js"}, | ||
| kept: []string{"src/index.js"}, | ||
| }, | ||
|
|
||
| // --- C2. Special characters in the ignore rule pattern itself --- | ||
| // git treats parentheses/spaces in a gitignore pattern as literal (fnmatch), so a folder | ||
|
|
@@ -992,7 +1017,7 @@ func testCases(t *testing.T) []fileFilterTestCase { | |
| "a/.gitignore": "!*.txt", | ||
| }, | ||
| filesToFilter: []string{"a/file2.js"}, | ||
| expectedFiles: []string{"file1.js", "a/file1.txt", ".gitignore", "a/.gitignore"}, | ||
| expectedFiles: []string{"file1.js", ".gitignore", "a/.gitignore"}, | ||
| }, | ||
| { | ||
| name: "Supports .dcignore rule file", | ||
|
|
@@ -1003,7 +1028,7 @@ func testCases(t *testing.T) []fileFilterTestCase { | |
| "a/.dcignore": "!*.txt", | ||
| }, | ||
| filesToFilter: []string{"a/file2.js"}, | ||
| expectedFiles: []string{"file1.js", "a/file1.txt", ".dcignore", "a/.dcignore"}, | ||
| expectedFiles: []string{"file1.js", ".dcignore", "a/.dcignore"}, | ||
| }, | ||
| { | ||
| name: "Supports .snyk style exclude rules", | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skipped root policy rule files
Medium Severity
buildGlobsskips reading a rule file whenMatchesPathis true for that file’s path. That also drops later.snykor.dcignorefiles at the repo root when an earlier.gitignoreignores them, even though their parent directory is not excluded. Those policy excludes no longer enterglobs, so scanning can miss paths that used to be filtered.Reviewed by Cursor Bugbot for commit fe47ad7. Configure here.