golang/go · error
invalid pattern syntax
Error message
invalid pattern syntax
What it means
Thrown while parsing each `//go:embed` pattern in embedGoFiles (the loop at line 2170). A pattern (after stripping an optional `all:` prefix) must (a) be accepted by path.Match as a valid glob and (b) pass validEmbedPattern (non-empty, no leading slash, no backslash, no `..`, no Windows drive). If either fails, the build aborts with this generic message. The TODO above it notes position info is missing, so the offending pattern is not named.
Source
Thrown at src/cmd/go/internal/load/pkg.go:2176
err = &EmbedError{
Pattern: pattern,
Err: err,
}
}
}()
// TODO(rsc): All these messages need position information for better error reports.
pmap = make(map[string][]string)
have := make(map[string]int)
dirOK := make(map[string]bool)
pid := 0 // pattern ID, to allow reuse of have map
for _, pattern = range patterns {
pid++
glob, all := strings.CutPrefix(pattern, "all:")
// Check pattern is valid for //go:embed.
if _, err := pathpkg.Match(glob, ""); err != nil || !validEmbedPattern(glob) {
return nil, nil, fmt.Errorf("invalid pattern syntax")
}
// Glob to find matches.
match, err := fsys.Glob(str.QuoteGlob(str.WithFilePathSeparator(pkgdir)) + filepath.FromSlash(glob))
if err != nil {
return nil, nil, err
}
// Filter list of matches down to the ones that will still exist when
// the directory is packaged up as a module. (If p.Dir is in the module cache,
// only those files exist already, but if p.Dir is in the current module,
// then there may be other things lying around, like symbolic links or .git directories.)
var list []string
for _, file := range match {
// relative path to p.Dir which begins without prefix slash
rel := filepath.ToSlash(str.TrimFilePathPrefix(file, pkgdir))
what := "file"View on GitHub (pinned to b6b368adc5)
Solutions
- Inspect every //go:embed line in the package and make each pattern a non-empty relative glob (e.g. `static/*.html`, `templates`).
- Remove leading slashes, backslashes, and `..` segments — embed patterns are relative to the package directory and cannot escape it.
- If using `all:`, ensure a real pattern follows the prefix (e.g. `all:assets`).
- Validate the pattern locally with `go vet ./...` after fixing; vet reports the file:line of the directive.
Example fix
// before //go:embed / // after //go:embed static
Defensive patterns
Strategy: validation
Validate before calling
// Validate an embed pattern before relying on `go build` to catch it.
package embedcheck
import (
"errors"
"path"
"strings"
)
// validEmbedPattern mirrors cmd/go's rules: non-empty, no leading '/',
// no backslash, no '..'. Returns nil if acceptable.
func CheckEmbedPattern(p string) error {
if p == "" {
return errors.New("embed pattern is empty")
}
if strings.Contains(p, "\\") {
return errors.New("embed pattern must not contain a backslash")
}
if strings.HasPrefix(p, "/") {
return errors.New("embed pattern must not be absolute")
}
for _, seg := range strings.Split(p, "/") {
if seg == ".." || seg == "" {
return errors.New("embed pattern has empty or '..' segment")
}
}
if _, err := path.Match(p, ""); err != nil {
return errors.New("embed pattern is not a valid glob: " + err.Error())
}
return nil
} Prevention
- Run `go vet ./...` after editing any //go:embed directive — vet reports the file:line, unlike the build error.
- Add a unit test that asserts every embed pattern you intend to use resolves to at least one file under the package dir.
When it happens
Trigger: Writing `//go:embed ` (empty), `//go:embed [bad`, `//go:embed ../escape`, `//go:embed \bs`, or `//go:embed all:` (empty after prefix). The check runs pathpkg.Match(glob,"") first, so syntactically bad glob classes like an unmatched `[` are caught.
Common situations: Hand-typing embed directives; copy-paste leaving a trailing pattern empty; editor auto-formatting stripping the path; using a leading `/` assuming it is repo-root relative; CI building on Windows where a backslash slips in.
Related errors
- cannot embed %s %s: in different module
- no matching files found
- cannot embed %s %s: in non-directory %s
- cannot embed %s %s: invalid name %s
- cannot embed %s %s: in invalid directory %s
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/b3042d78e30d8b24.
Report an issue: GitHub.