fix(source): treat exclude paths with trailing slash as directories (#4892)

A trailing slash on --exclude (e.g. './lib/') is dropped during pattern
normalization but doublestar.Match still requires an exact string match,
so the resulting pattern silently matches nothing and the directory is
not excluded. Strip a trailing slash so './lib/' and './lib' behave the
same.

Fixes #4839

Signed-off-by: ChrisJr404 <chris@hacknow.com>
This commit is contained in:
ChrisJr404 2026-05-06 10:51:41 -04:00 committed by GitHub
parent 48e91312e8
commit 1caf243d29
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 23 additions and 0 deletions

View File

@ -145,6 +145,9 @@ func GetDirectoryExclusionFunctions(root string, exclusions []string) ([]fileres
// check exclusions for supported paths, these are all relative to the "scan root" // check exclusions for supported paths, these are all relative to the "scan root"
if strings.HasPrefix(exclusion, "./") || strings.HasPrefix(exclusion, "*/") || strings.HasPrefix(exclusion, "**/") { if strings.HasPrefix(exclusion, "./") || strings.HasPrefix(exclusion, "*/") || strings.HasPrefix(exclusion, "**/") {
exclusion = strings.TrimPrefix(exclusion, "./") exclusion = strings.TrimPrefix(exclusion, "./")
// a trailing slash signals a directory but is otherwise discarded by doublestar.Match,
// causing the pattern to silently match nothing (see issue #4839)
exclusion = strings.TrimSuffix(exclusion, "/")
exclusions[idx] = root + exclusion exclusions[idx] = root + exclusion
} else { } else {
errors = append(errors, exclusion) errors = append(errors, exclusion)

View File

@ -175,6 +175,18 @@ func Test_DirectorySource_Exclusions(t *testing.T) {
}, },
exclusions: []string{"./target"}, exclusions: []string{"./target"},
}, },
{
input: "testdata/image-simple",
desc: "exclude explicit directory with trailing slash (issue #4839)",
glob: "**",
expected: []string{
"Dockerfile",
"file-1.txt",
"file-2.txt",
//"target/really/nested/file-3.txt", // explicitly skipped
},
exclusions: []string{"./target/"},
},
{ {
input: "testdata/image-simple", input: "testdata/image-simple",
desc: "exclude explicit file relative to the root", desc: "exclude explicit file relative to the root",
@ -329,6 +341,14 @@ func Test_getDirectoryExclusionFunctions_crossPlatform(t *testing.T) {
finfo: file.ManualInfo{ModeValue: os.ModeDir}, finfo: file.ManualInfo{ModeValue: os.ModeDir},
walkHint: fs.SkipDir, walkHint: fs.SkipDir,
}, },
{
desc: "directory exclusion with trailing slash (issue #4839)",
root: "/usr/var",
path: "/usr/var/lib",
exclude: "./lib/",
finfo: file.ManualInfo{ModeValue: os.ModeDir},
walkHint: fs.SkipDir,
},
{ {
desc: "no file info", desc: "no file info",
root: "/", root: "/",