cloudflare/cloudflared · error

error generating %s: %v

Error message

error generating %s: %v

What it means

This error is returned by ServiceTemplate.Generate when template parsing succeeded but executing the template against ServiceTemplateArgs failed. At this point the output buffer is being rendered but a runtime template error occurred (wrong field name, wrong pipe type, writer error). The wrapper includes the template path and the underlying execution error.

Source

Thrown at cmd/cloudflared/service_template.go:51

}

func (st *ServiceTemplate) Generate(args *ServiceTemplateArgs) error {
	tmpl, err := template.New(st.Path).Parse(st.Content)
	if err != nil {
		return fmt.Errorf("error generating %s template: %v", st.Path, err)
	}
	resolvedPath, err := st.ResolvePath()
	if err != nil {
		return err
	}
	if _, err = os.Stat(resolvedPath); err == nil {
		return errors.New(serviceAlreadyExistsWarn(resolvedPath))
	}

	var buffer bytes.Buffer
	err = tmpl.Execute(&buffer, args)
	if err != nil {
		return fmt.Errorf("error generating %s: %v", st.Path, err)
	}
	fileMode := os.FileMode(0o644)
	if st.FileMode != 0 {
		fileMode = st.FileMode
	}

	plistFolder := filepath.Dir(resolvedPath)
	err = os.MkdirAll(plistFolder, 0o755)
	if err != nil {
		return fmt.Errorf("error creating %s: %v", plistFolder, err)
	}

	err = os.WriteFile(resolvedPath, buffer.Bytes(), fileMode)
	if err != nil {
		return fmt.Errorf("error writing %s: %v", resolvedPath, err)
	}
	return nil
}

View on GitHub (pinned to 2253eeeb25)

Solutions

  1. Read the wrapped %v error for the exact field/pipeline that failed at execution
  2. Ensure every {{ .Field }} in the template exists on ServiceTemplateArgs with a compatible type
  3. Fix nil pointer fields in the ServiceTemplateArgs passed to Generate
  4. Test template execution locally with the same args struct before install

Example fix

// before
tmpl.Execute(&buffer, args) // fails: {{ .LogFile }} not in args
// after
args.LogFile = "/var/log/cloudflared.log"
err = tmpl.Execute(&buffer, args)
Defensive patterns

Strategy: validation

Validate before calling

// Pre-render the template with the same args to catch execution errors early
var probe bytes.Buffer
if err := tmpl.Execute(&probe, args); err != nil {
	return fmt.Errorf("template args incompatible: %w", err)
}

Type guard

func hasRequiredArgs(args *ServiceTemplateArgs, fields ...string) bool {
	v := reflect.ValueOf(args).Elem()
	for _, f := range fields {
		if v.FieldByName(f).IsZero() {
			return false
		}
	}
	return true
}

Try / catch

if err := st.Generate(args); err != nil {
	var execErr error
	if strings.HasPrefix(err.Error(), "error generating "+st.Path+":") {
		execErr = err
		// inspect wrapped cause for missing field / nil deref
	}
	return execErr
}

Prevention

When it happens

Trigger: Calling Generate() with a ServiceTemplateArgs value whose fields do not match the template's references — e.g. the template accesses {{ .SomeField }} that does not exist on ServiceTemplateArgs, or a method invocation on a nil/zero-value field. Raised from installLaunchd on macOS.

Common situations: Upgrading cloudflared where the built-in template gained new field references but args construction is stale; custom templates referencing misspelled args fields; passing a partially-initialized ServiceTemplateArgs (nil pointer deref inside a template action).

Related errors


AI-assisted analysis of cloudflare/cloudflared@2253eeeb25 (2026-09-06). Data as JSON: /api/errors/f3c1fd3c2969b6bf. Report an issue: GitHub.