mirror of
https://github.com/moby/buildkit.git
synced 2026-08-09 17:18:11 +00:00
Merge pull request #6930 from tonistiigi/dockerignore-root-fix
dockerfile: fix CopyIgnoredFile for context root
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/moby/buildkit/frontend/dockerfile/parser"
|
||||
"github.com/moby/buildkit/frontend/dockerfile/shell"
|
||||
"github.com/moby/buildkit/util/suggest"
|
||||
"github.com/moby/patternmatcher"
|
||||
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@@ -46,6 +47,13 @@ func validateCopySourcePath(src string, cfg *copyConfig) error {
|
||||
}
|
||||
|
||||
src = filepath.ToSlash(filepath.Clean(src))
|
||||
if src == "." || src == "/" {
|
||||
// "." and "/" are context roots, not real paths that can be excluded.
|
||||
// Only keep the warning for patterns that exclude all root entries.
|
||||
if !copySourceRootIgnored(cfg.ignoreMatcher) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
ok, err := cfg.ignoreMatcher.MatchesOrParentMatches(src)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -58,6 +66,19 @@ func validateCopySourcePath(src string, cfg *copyConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func copySourceRootIgnored(matcher *patternmatcher.PatternMatcher) bool {
|
||||
for _, pattern := range matcher.Patterns() {
|
||||
if pattern.Exclusion() {
|
||||
continue
|
||||
}
|
||||
switch pattern.String() {
|
||||
case "*", "**", "**/*":
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func validateCircularDependency(states []*dispatchState) error {
|
||||
var visit func(*dispatchState, []instructions.Command) []instructions.Command
|
||||
if states == nil {
|
||||
|
||||
@@ -48,6 +48,7 @@ var lintTests = integration.TestFuncs(
|
||||
testInvalidDefaultArgInFrom,
|
||||
testFromPlatformFlagConstDisallowed,
|
||||
testCopyIgnoredFiles,
|
||||
testCopyIgnoredFileContextRoot,
|
||||
testDefinitionDescription,
|
||||
testExposeProtoCasing,
|
||||
testExposeInvalidFormat,
|
||||
@@ -242,6 +243,41 @@ COPY . .
|
||||
})
|
||||
}
|
||||
|
||||
func testCopyIgnoredFileContextRoot(t *testing.T, sb integration.Sandbox) {
|
||||
dockerfile := []byte(`
|
||||
FROM scratch
|
||||
COPY . .
|
||||
`)
|
||||
|
||||
t.Run("dotfiles", func(t *testing.T) {
|
||||
checkLinterWarnings(t, sb, &lintTestParams{
|
||||
Dockerfile: dockerfile,
|
||||
DockerIgnore: []byte(`
|
||||
.*
|
||||
`),
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("wildcard", func(t *testing.T) {
|
||||
checkLinterWarnings(t, sb, &lintTestParams{
|
||||
Dockerfile: dockerfile,
|
||||
DockerIgnore: []byte(`
|
||||
*
|
||||
`),
|
||||
Warnings: []expectedLintWarning{
|
||||
{
|
||||
RuleName: "CopyIgnoredFile",
|
||||
Description: "Attempting to Copy file that is excluded by .dockerignore",
|
||||
Detail: `Attempting to Copy file "." that is excluded by .dockerignore`,
|
||||
URL: "https://docs.docker.com/go/dockerfile/rule/copy-ignored-file/",
|
||||
Level: 1,
|
||||
Line: 3,
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func testSecretsUsedInArgOrEnv(t *testing.T, sb integration.Sandbox) {
|
||||
dockerfile := []byte(`# check=skip=InvalidDefinitionDescription
|
||||
|
||||
|
||||
Reference in New Issue
Block a user