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
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
// 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
// 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,
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
+}
--- /dev/null
+#define struct union /* Great space saver */
--- /dev/null
+#define struct union /* Great space saver */
--- /dev/null
+WARNING: terminal is not fully functional
+ - (press RETURN)
--- /dev/null
+#define struct union /* Great space saver */
--- /dev/null
+WARNING: terminal is not fully functional
+ - (press RETURN)