Skip to content

Commit d4e025a

Browse files
committed
linter: do not attempt to check for copying ignored file when negated patterns exist
It becomes too difficult to statically check whether a source path is an error or expected when negated patterns are involved. This is because a pattern may point to a directory and may exclude the directory, but a further pattern may exclude a specific pattern in that directory. In order to determine whether this happened when copying a source directory, we'd need to either have some knowledge of the actual files copied (wouldn't be a static pattern) or we would need to go through each exclusion and try to determine if there exists a certain text that would match both the original pattern and the excluded pattern. This is likely too difficult or too computationally intense for what's meant to be a simple linter check so just disable this linter check when exclusions exist. Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
1 parent c5cee9e commit d4e025a

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

frontend/dockerfile/dockerfile2llb/convert.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2041,7 +2041,13 @@ func addReachableStages(s *dispatchState, stages map[*dispatchState]struct{}) {
20412041
}
20422042

20432043
func validateCopySourcePath(src string, cfg *copyConfig) error {
2044-
if cfg.ignoreMatcher == nil {
2044+
// Do not validate copy source paths if there is no dockerignore file
2045+
// or if the dockerignore file contains exclusions.
2046+
//
2047+
// Exclusions are too difficult to statically determine if they're proper
2048+
// because it's ok for a directory to be excluded and a file inside the directory
2049+
// to be negated.
2050+
if cfg.ignoreMatcher == nil || cfg.ignoreMatcher.Exclusions() {
20452051
return nil
20462052
}
20472053
cmd := "Copy"

frontend/dockerfile/dockerfile_lint_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ COPY ./Dockerfile .
230230
Dockerfile: dockerfile,
231231
DockerIgnore: dockerignore,
232232
})
233+
234+
// No warnings should occur if we copy the directory.
235+
dockerfile = []byte(`
236+
FROM scratch
237+
COPY . .
238+
`)
239+
checkLinterWarnings(t, sb, &lintTestParams{
240+
Dockerfile: dockerfile,
241+
DockerIgnore: dockerignore,
242+
})
233243
}
234244

235245
func testSecretsUsedInArgOrEnv(t *testing.T, sb integration.Sandbox) {

0 commit comments

Comments
 (0)