{"record":{"id":"6fcf23c3bba4a45f","repo":"ent/ent","slug":"executing-template-s-w","errorCode":null,"errorMessage":"executing template %s: %w","messagePattern":"executing template (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/internal/base/base.go","lineNumber":294,"sourceCode":"\tcobra.CheckErr(cmd.MarkFlagRequired(\"dialect\"))\n\treturn cmd\n}\n\n// newEnv create a new environment for ent codegen.\nfunc newEnv(target string, names []string, tmpl *template.Template) error {\n\tif err := createDir(target); err != nil {\n\t\treturn fmt.Errorf(\"create dir %s: %w\", target, err)\n\t}\n\tfor _, name := range names {\n\t\tif err := gen.ValidSchemaName(name); err != nil {\n\t\t\treturn fmt.Errorf(\"new schema %s: %w\", name, err)\n\t\t}\n\t\tif fileExists(target, name) {\n\t\t\treturn fmt.Errorf(\"new schema %s: already exists\", name)\n\t\t}\n\t\tb := bytes.NewBuffer(nil)\n\t\tif err := tmpl.Execute(b, name); err != nil {\n\t\t\treturn fmt.Errorf(\"executing template %s: %w\", name, err)\n\t\t}\n\t\tnewFileTarget := filepath.Join(target, strings.ToLower(name+\".go\"))\n\t\tif err := os.WriteFile(newFileTarget, b.Bytes(), 0644); err != nil {\n\t\t\treturn fmt.Errorf(\"writing file %s: %w\", newFileTarget, err)\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc createDir(target string) error {\n\t_, err := os.Stat(target)\n\tif err == nil || !os.IsNotExist(err) {\n\t\treturn err\n\t}\n\tif err := os.MkdirAll(target, os.ModePerm); err != nil {\n\t\treturn fmt.Errorf(\"creating schema directory: %w\", err)\n\t}\n\tif target != defaultSchema {","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/ent/ent/blob/69d5d4deb19599f129166634e09d33addcf3f2cc/cmd/internal/base/base.go#L276-L312","documentation":"After the name checks, newEnv executes the parsed template with the schema name as data to render the file contents. If tmpl.Execute fails (template runtime error, bad action against a string data argument), this wrapped error is returned. Unlike parse errors (113), this happens during rendering.","triggerScenarios":"A user-supplied --template that parses but fails at execution, e.g. calls a function not in gen.Funcs or indexes into data that doesn't exist; template actions that error on a plain string input.","commonSituations":"Custom scaffolding templates written for a different data shape (expecting structs, receiving a string name); calling missing or misspelled template funcs; copying templates from other generators.","solutions":["Read the wrapped exec error to find the failing template action and fix it.","Ensure the template only uses funcs available in gen.Funcs and treats the data as the schema name string.","Test the template with template.New().Funcs(gen.Funcs).Parse(...).Execute on the name before using it with ent new."],"exampleFix":"// before (template)\n{{ .Name | upper }}\n// after\n{{ . }}","handlingStrategy":"validation","validationCode":"t := template.New(\"schema\").Funcs(gen.Funcs)\nif _, err := t.Parse(tmplSrc); err != nil {\n    return err\n}\nvar buf bytes.Buffer\nif err := t.Execute(&buf, \"Probe\"); err != nil {\n    return fmt.Errorf(\"template fails at exec: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := runNewCmd(); err != nil {\n    if strings.Contains(err.Error(), \"executing template\") {\n        // fix the failing action reported in the wrapped error\n    }\n}","preventionTips":["Treat the template data as the schema name string only; don't index into it like a struct.","Use only funcs present in gen.Funcs.","Dry-run templates with a probe Execute before wiring them into ent new."],"tags":["ent","template","codegen","cli"],"backgroundTag":"template-execution-error","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"}