{"record":{"id":"1c608d3e4decaf55","repo":"ent/ent","slug":"entc-load-format-template-w","errorCode":null,"errorMessage":"entc/load: format template: %w","messagePattern":"entc/load: format template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"entc/load/load.go","lineNumber":86,"sourceCode":"\t\treturn nil, fmt.Errorf(\"entc/load: parse schema dir: %w\", err)\n\t}\n\tif len(c.Names) == 0 {\n\t\treturn nil, fmt.Errorf(\"entc/load: no schema found in: %s\", c.Path)\n\t}\n\tvar b bytes.Buffer\n\terr = buildTmpl.ExecuteTemplate(&b, \"main\", struct {\n\t\t*Config\n\t\tPackage string\n\t}{\n\t\tConfig:  c,\n\t\tPackage: spec.PkgPath,\n\t})\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"entc/load: execute template: %w\", err)\n\t}\n\tbuf, err := format.Source(b.Bytes())\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"entc/load: format template: %w\", err)\n\t}\n\tif err := os.MkdirAll(\".entc\", os.ModePerm); err != nil {\n\t\treturn nil, err\n\t}\n\ttarget := fmt.Sprintf(\".entc/%s.go\", filename(spec.PkgPath))\n\tif err := os.WriteFile(target, buf, 0644); err != nil {\n\t\treturn nil, fmt.Errorf(\"entc/load: write file %s: %w\", target, err)\n\t}\n\tdefer os.RemoveAll(\".entc\")\n\tout, err := gorun(target, c.BuildFlags)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tfor _, line := range strings.Split(out, \"\\n\") {\n\t\tschema, err := UnmarshalSchema([]byte(line))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"entc/load: unmarshal schema %s: %w\", line, err)\n\t\t}","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/ent/ent/blob/69d5d4deb19599f129166634e09d33addcf3f2cc/entc/load/load.go#L68-L104","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\ncfg := &load.Config{Path: \"./ent/schema\", BuildFlags: []string{\"-tags\", \"ignored_tag\"}}\ngraph, err := load.Load(cfg)\n// after\ncfg := &load.Config{Path: \"./ent/schema\"}\ngraph, err := load.Load(cfg)\nif err != nil {\n    log.Fatalf(\"check schema package compiles: %v\", err)\n}","handlingStrategy":"try-catch","validationCode":"// before calling Load\ncmd := exec.Command(\"go\", \"build\", \"./...\")\nif out, err := cmd.CombinedOutput(); err != nil {\n    return fmt.Errorf(\"schema package does not compile: %v\\n%s\", err, out)\n}","typeGuard":"func isValidGoSource(dir string) bool {\n    fset := token.NewFileSet()\n    pkgs, err := parser.ParseDir(fset, dir, nil, parser.AllErrors)\n    return err == nil && len(pkgs) > 0\n}","tryCatchPattern":"spec, err := load.Load(cfg)\nif err != nil {\n    var fmterr = strings.Contains(err.Error(), \"format template\")\n    if fmterr {\n        // regenerate schema dir / check Go toolchain version\n    }\n    return fmt.Errorf(\"entc load failed: %w\", err)\n}","preventionTips":["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"],"tags":["gofmt","code-generation","ent"],"backgroundTag":"generated-code-format-failed","analyzedSha":"69d5d4deb19599f129166634e09d33addcf3f2cc","analyzedAt":"2026-09-03T16:51:39.799Z","contentChangedAt":"2026-09-03T16:51:39.799Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}