Skip to content

Fix ADD --exclude having no effect on archive contents with --unpack - #7422

Open
ToniAdreal wants to merge 1 commit into
moby:masterfrom
ToniAdreal:fix-7376-add-unpack-exclude
Open

ToniAdreal wants to merge 1 commit into
moby:masterfrom
ToniAdreal:fix-7376-add-unpack-exclude

Conversation

@ToniAdreal

Copy link
Copy Markdown

Fixes #7376

Problem

ADD --exclude patterns are silently ignored when the added archive is unpacked (--unpack). The same patterns work on the plain-copy path, so a Dockerfile like ADD --exclude='go/doc/**' --unpack archive.tar.gz /dest still extracts the excluded entries.

Root cause

When AttemptUnpackDockerCompatibility is set, docopy hands the archive to unpack(), which extracts every entry via chrootarchive.Untar and never sees the FileActionCopy ExcludePatterns: the copy path that honors them is skipped entirely. The vendored go-archive unpack filter is a plain prefix test and cannot express globs like go/doc/**, so passing the patterns through to it would not work either.

Fix

Thread the patterns into unpack() and filter the tar stream in buildkit with the same patternmatcher the regular copy path uses, matching entries against their paths inside the archive (so go/doc/** filters go/doc/...). A bare go/doc pattern does not over-match siblings like go/docker/... because matching goes through the glob matcher, not a prefix test.

Verification

  • gofmt clean, go vet ./solver/llbsolver/file/ clean
  • go test ./solver/llbsolver/file/ passes (5 new tests in unpack_test.go: glob exclude, bare directory, no-exclude passthrough, non-archive passthrough, and a direct filterTar test)
  • go test ./frontend/dockerfile/dockerfile2llb/ passes
  • Verified on master e4d319d after rebase; DCO signed

Note

@sainimal1ba-hue picked this up on Nov 7 and planned a failing test first. No PR has landed in the week since, so I put this up to keep the issue moving. Happy to hand it over or rework it if you are still on it.

When AttemptUnpackDockerCompatibility is set, docopy hands the archive
to unpack(), which extracts every entry via chrootarchive.Untar and
never sees the FileActionCopy ExcludePatterns: the copy path that
honors them is skipped entirely. Thread the patterns into unpack()
and filter the tar stream with the same patternmatcher the regular
copy path uses, matching entries against their paths inside the
archive (so go/doc/** filters go/doc/...). go-archive's own unpack
filter is a plain prefix test and cannot express globs like
go/doc/**, so the filtering happens in buildkit before the stream
reaches UntarUncompressed.

Adds regression tests covering glob excludes, a bare directory
pattern, the no-exclude path, and non-archive sources.

Fixes moby#7376

Signed-off-by: ToniAdreal <184365500+ToniAdreal@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dockerfile: ADD --exclude has no effect on archive contents with --unpack

1 participant