diff --git a/test/embed/BUILD b/test/embed/BUILD index af34d8d8..0509edd1 100644 --- a/test/embed/BUILD +++ b/test/embed/BUILD @@ -3,13 +3,13 @@ subinclude("//build_defs:go") go_library( name = "embed", srcs = ["embed.go"], - resources = ["hello.txt"], + resources = ["hello.txt", "subdir"], ) go_test( name = "embed_test", srcs = ["embed_test.go"], - resources = ["hello.txt"], + resources = ["hello.txt", "subdir"], deps = [ ":embed", "//third_party/go:testify", diff --git a/test/embed/embed_test.go b/test/embed/embed_test.go index 5d82a15a..7b57dc0a 100644 --- a/test/embed/embed_test.go +++ b/test/embed/embed_test.go @@ -17,7 +17,7 @@ func TestEmbedDir(t *testing.T) { } func TestEmbedDirAll(t *testing.T) { - b, err := subdir.ReadFile("subdir/_test.txt") + b, err := subdirAll.ReadFile("subdir/_test.txt") assert.NoError(t, err) assert.Equal(t, "hello", strings.TrimSpace(string(b))) } diff --git a/tools/please_go/embed/embed.go b/tools/please_go/embed/embed.go index 5c2a4f8c..679433af 100644 --- a/tools/please_go/embed/embed.go +++ b/tools/please_go/embed/embed.go @@ -7,6 +7,7 @@ import ( "go/build" "io" "io/fs" + "log" "path" "path/filepath" "strings" @@ -54,6 +55,7 @@ func Parse(gofiles []string) (*Cfg, error) { // AddPackage parses a go package and adds any embed patterns to the configuration func (cfg *Cfg) AddPackage(pkg *build.Package) error { for _, pattern := range append(append(pkg.EmbedPatterns, pkg.TestEmbedPatterns...), pkg.XTestEmbedPatterns...) { + log.Printf("here %s", pattern) paths, err := relglob(pkg.Dir, pattern) if err != nil { return err @@ -79,6 +81,13 @@ func dirs(files []string) []string { } func relglob(dir, pattern string) ([]string, error) { + // Go allows prefixing the pattern with all: which picks up files prefixed with . or _ (by default these should be ignored) + includeHidden := false + if strings.HasPrefix(pattern, "all:") { + pattern = strings.TrimPrefix(pattern, "all:") + includeHidden = true + } + paths, err := filepath.Glob(path.Join(dir, pattern)) if err == nil && len(paths) == 0 { return nil, fmt.Errorf("pattern %s: no matching paths found", pattern) @@ -89,7 +98,9 @@ func relglob(dir, pattern string) ([]string, error) { if err != nil { return err } else if !d.IsDir() { - ret = append(ret, strings.TrimLeft(strings.TrimPrefix(path, dir), string(filepath.Separator))) + if hidden := strings.HasPrefix(d.Name(), ".") || strings.HasPrefix(d.Name(), "_"); !hidden || includeHidden { + ret = append(ret, strings.TrimLeft(strings.TrimPrefix(path, dir), string(filepath.Separator))) + } } return nil }); err != nil {