Skip to content

Commit

Permalink
cmd/go, embed: exclude .* and _* from embedded directory trees
Browse files Browse the repository at this point in the history
Discussion on #42328 led to a decision to exclude files matching
.* and _* from embedded directory results when embedding an
entire directory tree.

This CL implements that new behavior.

Fixes #42328.

Change-Id: I6188994e96348b3449c7d9d3d0d181cfbf2d4db1
Reviewed-on: https://go-review.googlesource.com/c/go/+/275092
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
  • Loading branch information
rsc committed Dec 4, 2020
1 parent b67b7dd commit 37588ff
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/cmd/go/internal/load/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1998,6 +1998,16 @@ func (p *Package) resolveEmbed(patterns []string) (files []string, pmap map[stri
return err
}
rel := filepath.ToSlash(path[len(p.Dir)+1:])
name := info.Name()
if path != file && (isBadEmbedName(name) || name[0] == '.' || name[0] == '_') {
// 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.
if info.IsDir() {
return fs.SkipDir
}
return nil
}
if info.IsDir() {
if _, err := fsys.Stat(filepath.Join(path, "go.mod")); err == nil {
return filepath.SkipDir
Expand All @@ -2007,10 +2017,6 @@ func (p *Package) resolveEmbed(patterns []string) (files []string, pmap map[stri
if !info.Mode().IsRegular() {
return nil
}
if isBadEmbedName(info.Name()) {
// Ignore bad names, assuming they won't go into modules.
return nil
}
count++
if have[rel] != pid {
have[rel] = pid
Expand Down Expand Up @@ -2050,6 +2056,9 @@ func validEmbedPattern(pattern string) bool {
// as existing for embedding.
func isBadEmbedName(name string) bool {
switch name {
// Empty string should be impossible but make it bad.
case "":
return true
// Version control directories won't be present in module.
case ".bzr", ".hg", ".git", ".svn":
return true
Expand Down
5 changes: 4 additions & 1 deletion src/embed/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@
// as Go double-quoted or back-quoted string literals.
//
// If a pattern names a directory, all files in the subtree rooted at that directory are
// embedded (recursively), so the variable in the above example is equivalent to:
// embedded (recursively), except that files with names beginning with ‘.’ or ‘_’
// are excluded. So the variable in the above example is almost equivalent to:
//
// // content is our static web server content.
// //go:embed image template html/index.html
// var content embed.FS
//
// The difference is that ‘image/*’ embeds ‘image/.tempfile’ while ‘image’ does not.
//
// 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.
// Similarly, it can be used with both global and function-local variables,
Expand Down
21 changes: 21 additions & 0 deletions src/embed/internal/embedtest/embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,24 @@ func TestDir(t *testing.T) {
testDir(t, all, "testdata/i/j", "k/")
testDir(t, all, "testdata/i/j/k", "k8s.txt")
}

func TestHidden(t *testing.T) {
//go:embed testdata
var dir embed.FS

//go:embed testdata/*
var star embed.FS

t.Logf("//go:embed testdata")

testDir(t, dir, "testdata",
"ascii.txt", "glass.txt", "hello.txt", "i/", "ken.txt")

t.Logf("//go:embed testdata/*")

testDir(t, star, "testdata",
".hidden/", "_hidden/", "ascii.txt", "glass.txt", "hello.txt", "i/", "ken.txt")

testDir(t, star, "testdata/.hidden",
"fortune.txt", "more/") // but not .more or _more
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define struct union /* Great space saver */
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define struct union /* Great space saver */
2 changes: 2 additions & 0 deletions src/embed/internal/embedtest/testdata/.hidden/fortune.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WARNING: terminal is not fully functional
- (press RETURN)
1 change: 1 addition & 0 deletions src/embed/internal/embedtest/testdata/.hidden/more/tip.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#define struct union /* Great space saver */
2 changes: 2 additions & 0 deletions src/embed/internal/embedtest/testdata/_hidden/fortune.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
WARNING: terminal is not fully functional
- (press RETURN)

0 comments on commit 37588ff

Please sign in to comment.