Files
Abhishek Goyal 44b0c79eaf 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".
2026-04-05 14:15:40 +02:00

56 lines
1.1 KiB
Go

package main
import (
"embed"
"strings"
)
//go:embed a hello.txt
var files embed.FS
var (
//go:embed "hello.*"
helloString string
//go:embed hello.txt
helloBytes []byte
)
// A test to check that hidden files are not included when matching a directory.
//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() {
println("string:", strings.TrimSpace(helloString))
println("bytes:", strings.TrimSpace(string(helloBytes)))
println("[]byte(string):", strings.TrimSpace(string(helloStringBytes)))
println("files:")
readFiles(".", files)
println("all:a files (should include .hidden):")
readFiles(".", allFiles)
}
func readFiles(dir string, fs embed.FS) {
entries, err := fs.ReadDir(dir)
if err != nil {
println(err.Error())
return
}
for _, entry := range entries {
entryPath := entry.Name()
if dir != "." {
entryPath = dir + "/" + entryPath
}
println("-", entryPath)
if entry.IsDir() {
readFiles(entryPath, fs)
}
}
}