Skip to content

Commit

Permalink
cmd/go: add //go:embed all:pattern
Browse files Browse the repository at this point in the history
When //go:embed d matches directory d, it embeds the directory
tree rooted at d, but it excludes files beginning with . and _,
as well as files having problematic names that will not be packaged
into modules (names such as .git and com1).

After long discussions on golang#42328 and golang#43854, we decided to keep
the behavior of excluding . and _ files by default, but to allow the pattern
prefix 'all:' to override this default. This CL implements that change.

Note that paths like .git and com1 are still excluded, as they must be,
since they will never be packed into a module.

Fixes golang#43854.

Change-Id: I4f3731e14ecffd4b691fda3a0890b460027fe209
Reviewed-on: https://go-review.googlesource.com/c/go/+/359413
Trust: Russ Cox <[email protected]>
Run-TryBot: Russ Cox <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
Reviewed-by: Bryan C. Mills <[email protected]>
  • Loading branch information
rsc committed Nov 9, 2021
1 parent 15a54d6 commit 36dbf7f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2017,13 +2017,18 @@ func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[st
for _, pattern = range patterns {
pid++

glob := pattern
all := strings.HasPrefix(pattern, "all:")
if all {
glob = pattern[len("all:"):]
}
// Check pattern is valid for //go:embed.
if _, err := path.Match(pattern, ""); err != nil || !validEmbedPattern(pattern) {
if _, err := path.Match(glob, ""); err != nil || !validEmbedPattern(glob) {
return nil, nil, fmt.Errorf("invalid pattern syntax")
}

// Glob to find matches.
match, err := fsys.Glob(pkgdir + string(filepath.Separator) + filepath.FromSlash(pattern))
match, err := fsys.Glob(pkgdir + string(filepath.Separator) + filepath.FromSlash(glob))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -2086,7 +2091,7 @@ func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[st
}
rel := filepath.ToSlash(path[len(pkgdir)+1:])
name := info.Name()
if path != file && (isBadEmbedName(name) || name[0] == '.' || name[0] == '_') {
if path != file && (isBadEmbedName(name) || ((name[0] == '.' || name[0] == '_') && !all)) {
// Ignore bad names, assuming they won't go into modules.
// Also avoid hidden files that user may not know about.
// See golang.org/issue/42328.
Expand Down
20 changes: 20 additions & 0 deletions src/cmd/go/testdata/script/embed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ rm t/x.txt
! go build m/use
stderr '^x.go:5:12: pattern [*]t: cannot embed directory t: contains no embeddable files$'

# all still ignores .git and symlinks
cp x.go3 x.go
! go build -x
stderr '^x.go:5:12: pattern all:t: cannot embed directory t: contains no embeddable files$'

# all finds dot files and underscore files
cp x.txt t/.x.txt
go build -x
rm t/.x.txt
cp x.txt t/_x.txt
go build -x

-- x.go --
package p

Expand Down Expand Up @@ -92,6 +104,14 @@ import "embed"
//go:embed *t
var X embed.FS

-- x.go3 --
package p

import "embed"

//go:embed all:t
var X embed.FS

-- x.txt --
hello

Expand Down
5 changes: 5 additions & 0 deletions src/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@
// var content embed.FS
//
// The difference is that ‘image/*’ embeds ‘image/.tempfile’ while ‘image’ does not.
// Neither embeds ‘image/dir/.tempfile’.
//
// If a pattern begins with the prefix ‘all:’, then the rule for walking directories is changed
// to include those files beginning with ‘.’ or ‘_’. For example, ‘all:image’ embeds
// both ‘image/.tempfile’ and ‘image/dir/.tempfile’.
//
// The //go:embed directive can be used with both exported and unexported variables,
// depending on whether the package wants to make the data available to other packages.
Expand Down

0 comments on commit 36dbf7f

Please sign in to comment.