Skip to content

utils: match .dockerignore patterns against every parent directory - #3454

Open
SparkDrago05 wants to merge 1 commit into
docker:mainfrom
SparkDrago05:fix-dockerignore-parent-and-wildcard-exceptions
Open

SparkDrago05 wants to merge 1 commit into
docker:mainfrom
SparkDrago05:fix-dockerignore-parent-and-wildcard-exceptions

Conversation

@SparkDrago05

Copy link
Copy Markdown

Fixes #3449

Problem

PatternMatcher.matches() only compared a pattern against the parent prefix with the same number of components as the pattern. A pattern like **/cache has two components, so for cache/drop.txt (one parent component) the parent check never ran. When an exception such as !cache/keep.txt made the walker descend into cache/, every file in it was kept.

Fix

Check the pattern against every parent prefix, the same as MatchesOrParentMatches in moby/patternmatcher. The Docker CLI and BuildKit use that matcher.

Verification

Same context and .dockerignore (**/cache, !cache/keep.txt) on Docker 29.8.1:

Files in context
BuildKit cache/keep.txt
Classic builder (DOCKER_BUILDKIT=0) cache/keep.txt
docker-py before cache/drop.txt, cache/keep.txt
docker-py after cache/keep.txt
  • New test test_double_wildcard_dir_with_file_exception fails before the change and passes after it. It also covers a nested file (cache/sub/drop.txt).
  • tests/unit passes. The one exception is test_set_auth_headers_with_dict_and_no_auth_configs, which also fails on main because of local Docker credentials.
  • ruff==0.1.8 is clean.

#3448 is a separate case. There, docker-py matches the classic builder but not BuildKit. I left it out of this PR and posted details on that issue.

PatternMatcher.matches() only compared a pattern against the parent
prefix with the same number of path components as the pattern. A
pattern such as **/cache never matched the parent directory of
cache/drop.txt, so files inside an excluded directory were sent in the
build context whenever an exception pattern made the walker descend
into that directory.

Check every parent prefix instead, as moby/patternmatcher's
MatchesOrParentMatches does. Both BuildKit and the classic builder
exclude these files.

Fixes docker#3449

Signed-off-by: SparkDrago05 <huzaifa.farooq05@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.dockerignore reinclusion retains unrelated files in a glob-excluded directory

1 participant