diff --git a/cache/contenthash/checksum.go b/cache/contenthash/checksum.go index ce9f0752..17e8d929 100644 --- a/cache/contenthash/checksum.go +++ b/cache/contenthash/checksum.go @@ -477,6 +477,7 @@ func (cc *cacheContext) includedPaths(ctx context.Context, m *mount, p string, o } }() + endsInSep := len(p) != 0 && p[len(p)-1] == filepath.Separator p = keyPath(p) rootedIncludePatterns := make([]string, len(opts.IncludePatterns)) @@ -526,6 +527,11 @@ treeWalk: if len(k) > 0 && k[len(k)-1] == byte(0) { dirHeader = true fn = fn[:len(fn)-1] + if fn == p && endsInSep { + // We don't include the metadata header for a source dir which ends with a separator + k, _, kOk = iter.Next() + continue + } } b, partialMatch, err := shouldIncludePath(p, fn, opts.Wildcard, rootedIncludePatterns, excludePatternMatcher, lastIncludedDir) if err != nil { diff --git a/cache/contenthash/checksum_test.go b/cache/contenthash/checksum_test.go index 843bde81..221768dc 100644 --- a/cache/contenthash/checksum_test.go +++ b/cache/contenthash/checksum_test.go @@ -466,6 +466,8 @@ func TestChecksumIncludeExclude(t *testing.T) { dgstFoo, err := cc.Checksum(context.TODO(), ref, "", ChecksumOpts{IncludePatterns: []string{"foo"}}, nil) require.NoError(t, err) + require.Equal(t, dgstFileData0, dgst) + dgstFooBar, err := cc.Checksum(context.TODO(), ref, "", ChecksumOpts{IncludePatterns: []string{"foo", "bar"}}, nil) require.NoError(t, err)