loader: support "all:" prefix in //go:embed patterns

The "all:" prefix in //go:embed directives instructs the compiler to
  include hidden files (starting with "." or "_") when embedding a
  directory. Previously, the prefix was passed literally to path.Match,
  which would never match.

  Introduce an embedPattern struct that parses and validates the pattern
  at construction time via newEmbedPattern(). The "all:" prefix is
  stripped once and stored along with the glob pattern, avoiding repeated
  string prefix checks. Pattern validation is absorbed into the
  constructor, guaranteeing that any embedPattern value is valid and
  eliminating the need for separate validation loops.

  Fixes embedding with patterns like "//go:embed all:static".
This commit is contained in:
Abhishek Goyal
2026-02-21 21:25:11 +05:30
committed by deadprogram
parent 27e5baeb57
commit 44b0c79eaf
3 changed files with 71 additions and 45 deletions
+10 -4
View File
@@ -20,6 +20,10 @@ var (
//go:embed a/b/.hidden
var hidden string
// A test to check that hidden files ARE included when using "all:" prefix.
//go:embed all:a
var allFiles embed.FS
var helloStringBytes = []byte(helloString)
func main() {
@@ -27,11 +31,13 @@ func main() {
println("bytes:", strings.TrimSpace(string(helloBytes)))
println("[]byte(string):", strings.TrimSpace(string(helloStringBytes)))
println("files:")
readFiles(".")
readFiles(".", files)
println("all:a files (should include .hidden):")
readFiles(".", allFiles)
}
func readFiles(dir string) {
entries, err := files.ReadDir(dir)
func readFiles(dir string, fs embed.FS) {
entries, err := fs.ReadDir(dir)
if err != nil {
println(err.Error())
return
@@ -43,7 +49,7 @@ func readFiles(dir string) {
}
println("-", entryPath)
if entry.IsDir() {
readFiles(entryPath)
readFiles(entryPath, fs)
}
}
}
+6
View File
@@ -7,3 +7,9 @@ files:
- a/b/bar.txt
- a/b/foo.txt
- hello.txt
all:a files (should include .hidden):
- a
- a/b
- a/b/.hidden
- a/b/bar.txt
- a/b/foo.txt