ent/ent · error
entc/load: format template: %w
Error message
entc/load: format template: %w
What it means
entc/load.Load generates a temporary Go file from the user's schema package via a template, then runs gofmt's format.Source on it before compiling. This error means the generated code was not valid, parseable Go, so formatting failed. Since the template itself is fixed, a failure here almost always means something about the schema package or build environment made the rendered template malformed.
Source
Thrown at entc/load/load.go:86
return nil, fmt.Errorf("entc/load: parse schema dir: %w", err)
}
if len(c.Names) == 0 {
return nil, fmt.Errorf("entc/load: no schema found in: %s", c.Path)
}
var b bytes.Buffer
err = buildTmpl.ExecuteTemplate(&b, "main", struct {
*Config
Package string
}{
Config: c,
Package: spec.PkgPath,
})
if err != nil {
return nil, fmt.Errorf("entc/load: execute template: %w", err)
}
buf, err := format.Source(b.Bytes())
if err != nil {
return nil, fmt.Errorf("entc/load: format template: %w", err)
}
if err := os.MkdirAll(".entc", os.ModePerm); err != nil {
return nil, err
}
target := fmt.Sprintf(".entc/%s.go", filename(spec.PkgPath))
if err := os.WriteFile(target, buf, 0644); err != nil {
return nil, fmt.Errorf("entc/load: write file %s: %w", target, err)
}
defer os.RemoveAll(".entc")
out, err := gorun(target, c.BuildFlags)
if err != nil {
return nil, err
}
for _, line := range strings.Split(out, "\n") {
schema, err := UnmarshalSchema([]byte(line))
if err != nil {
return nil, fmt.Errorf("entc/load: unmarshal schema %s: %w", line, err)
}View on GitHub (pinned to 69d5d4deb1)
Solutions
- Run `go build ./...` and `gofmt -l .` on the schema package to confirm the user's own code parses cleanly
- Ensure the Go toolchain used to run entc is compatible with the syntax used in schema files (update Go or lower language version in go.mod)
- Remove stale generated artifacts (e.g. old .entc or ent/runtime.go) and regenerate
- Retry without custom Config.BuildFlags to rule out flag-induced bad loads
Example fix
// before
cfg := &load.Config{Path: "./ent/schema", BuildFlags: []string{"-tags", "ignored_tag"}}
graph, err := load.Load(cfg)
// after
cfg := &load.Config{Path: "./ent/schema"}
graph, err := load.Load(cfg)
if err != nil {
log.Fatalf("check schema package compiles: %v", err)
} Defensive patterns
Strategy: try-catch
Validate before calling
// before calling Load
cmd := exec.Command("go", "build", "./...")
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("schema package does not compile: %v\n%s", err, out)
} Type guard
func isValidGoSource(dir string) bool {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, dir, nil, parser.AllErrors)
return err == nil && len(pkgs) > 0
} Try / catch
spec, err := load.Load(cfg)
if err != nil {
var fmterr = strings.Contains(err.Error(), "format template")
if fmterr {
// regenerate schema dir / check Go toolchain version
}
return fmt.Errorf("entc load failed: %w", err)
} Prevention
- Keep `go build ./...` green before running ent codegen
- Pin entgo.io/ent and the Go toolchain versions in CI
- Delete stale generated files before regenerating
- Avoid exotic syntax in schema files not supported by your pinned Go version
When it happens
Trigger: Calling entc/load.Load (or entc.Load) where the rendered schema-spec template produces syntactically invalid Go — e.g. the schema package contains declarations the template cannot render correctly, or the go/packages load produced a stale/incomplete types package that renders bad code.
Common situations: Go version mismatch between the toolchain and the loaded package's language features (newer syntax the parser rejects), corrupted or partially-generated schema directories, build flags that exclude files the template expects, or vendoring/module issues yielding an empty package.
Related errors
- invalid default value for enum field %q
- foreign-key was not found for edge %q of type %s
- edge %q is unique
- edge %q is not-unique
- StorageKey.Table is allowed only for M2M edges (got %s)
AI-assisted analysis of ent/ent@69d5d4deb1 (2026-09-03).
Data as JSON: /api/errors/1c608d3e4decaf55.
Report an issue: GitHub.