Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/compose/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (s *composeService) watch(ctx context.Context, syncChannel chan bool, proje
service.PullPolicy = types.PullPolicyBuild
project.Services[i] = service

dockerIgnores, err := watch.LoadDockerIgnore(service.Build.Context)
dockerIgnores, err := watch.LoadDockerIgnore(service.Build)
if err != nil {
return err
}
Expand Down
36 changes: 21 additions & 15 deletions pkg/watch/dockerignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ package watch

import (
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/compose-spec/compose-go/v2/types"
"github.com/docker/compose/v2/internal/paths"
"github.com/moby/patternmatcher"
"github.com/moby/patternmatcher/ignorefile"
Expand Down Expand Up @@ -61,13 +63,28 @@ func (i dockerPathMatcher) MatchesEntireDir(f string) (bool, error) {
return true, nil
}

func LoadDockerIgnore(repoRoot string) (*dockerPathMatcher, error) {
func LoadDockerIgnore(build *types.BuildConfig) (*dockerPathMatcher, error) {
repoRoot := build.Context
absRoot, err := filepath.Abs(repoRoot)
if err != nil {
return nil, err
}

patterns, err := readDockerignorePatterns(absRoot)
// first try Dockerfile-specific ignore-file
f, err := os.Open(filepath.Join(repoRoot, build.Dockerfile+".dockerignore"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is build.Dockerfile always just the file name, or can it be an absolute path? (in that case, no need to join if with the repo-root)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually running some tests as I wondered watch do abspath resolution, while compose-go should already have done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dockerfile is always just the file name within the context

if os.IsNotExist(err) {
// defaults to a global .dockerignore
f, err = os.Open(filepath.Join(repoRoot, ".dockerignore"))
if os.IsNotExist(err) {
return NewDockerPatternMatcher(repoRoot, nil)
}
}
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()

patterns, err := readDockerignorePatterns(f)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -122,19 +139,8 @@ func NewDockerPatternMatcher(repoRoot string, patterns []string) (*dockerPathMat
}, nil
}

func readDockerignorePatterns(repoRoot string) ([]string, error) {
var excludes []string

f, err := os.Open(filepath.Join(repoRoot, ".dockerignore"))
switch {
case os.IsNotExist(err):
return excludes, nil
case err != nil:
return nil, err
}
defer func() { _ = f.Close() }()

patterns, err := ignorefile.ReadAll(f)
func readDockerignorePatterns(r io.Reader) ([]string, error) {
patterns, err := ignorefile.ReadAll(r)
if err != nil {
return nil, fmt.Errorf("error reading .dockerignore: %w", err)
}
Expand Down