ent/ent · error

executing template %s: %w

Error message

executing template %s: %w

What it means

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.

Source

Thrown at cmd/internal/base/base.go:294

	cobra.CheckErr(cmd.MarkFlagRequired("dialect"))
	return cmd
}

// newEnv create a new environment for ent codegen.
func newEnv(target string, names []string, tmpl *template.Template) error {
	if err := createDir(target); err != nil {
		return fmt.Errorf("create dir %s: %w", target, err)
	}
	for _, name := range names {
		if err := gen.ValidSchemaName(name); err != nil {
			return fmt.Errorf("new schema %s: %w", name, err)
		}
		if fileExists(target, name) {
			return fmt.Errorf("new schema %s: already exists", name)
		}
		b := bytes.NewBuffer(nil)
		if err := tmpl.Execute(b, name); err != nil {
			return fmt.Errorf("executing template %s: %w", name, err)
		}
		newFileTarget := filepath.Join(target, strings.ToLower(name+".go"))
		if err := os.WriteFile(newFileTarget, b.Bytes(), 0644); err != nil {
			return fmt.Errorf("writing file %s: %w", newFileTarget, err)
		}
	}
	return nil
}

func createDir(target string) error {
	_, err := os.Stat(target)
	if err == nil || !os.IsNotExist(err) {
		return err
	}
	if err := os.MkdirAll(target, os.ModePerm); err != nil {
		return fmt.Errorf("creating schema directory: %w", err)
	}
	if target != defaultSchema {

View on GitHub (pinned to 69d5d4deb1)

Solutions

  1. Read the wrapped exec error to find the failing template action and fix it.
  2. Ensure the template only uses funcs available in gen.Funcs and treats the data as the schema name string.
  3. Test the template with template.New().Funcs(gen.Funcs).Parse(...).Execute on the name before using it with ent new.

Example fix

// before (template)
{{ .Name | upper }}
// after
{{ . }}
Defensive patterns

Strategy: validation

Validate before calling

t := template.New("schema").Funcs(gen.Funcs)
if _, err := t.Parse(tmplSrc); err != nil {
    return err
}
var buf bytes.Buffer
if err := t.Execute(&buf, "Probe"); err != nil {
    return fmt.Errorf("template fails at exec: %w", err)
}

Try / catch

if err := runNewCmd(); err != nil {
    if strings.Contains(err.Error(), "executing template") {
        // fix the failing action reported in the wrapped error
    }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of ent/ent@69d5d4deb1 (2026-09-03). Data as JSON: /api/errors/6fcf23c3bba4a45f. Report an issue: GitHub.