{"record":{"id":"100e3b5a949da1d7","repo":"vitessio/vitess","slug":"failed-to-execute-template-w","errorCode":null,"errorMessage":"failed to execute template: %w","messagePattern":"failed to execute template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtadmin/cluster/discovery/discovery_consul.go","lineNumber":179,"sourceCode":"\t\treturn nil, fmt.Errorf(\"failed to parse vtctld host address template %s: %w\", *vtctldAddrTmplStr, err)\n\t}\n\n\treturn disco, nil\n}\n\nfunc generateConsulDatacenter(component string, cluster *vtadminpb.Cluster, tmplStr string) (string, error) {\n\ttmpl, err := template.New(\"consul-\" + component + \"-datacenter-\" + cluster.Id).Parse(tmplStr)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"error parsing template %s: %w\", tmplStr, err)\n\t}\n\n\tdc, err := textutil.ExecuteTemplate(tmpl, &struct {\n\t\tCluster *vtadminpb.Cluster\n\t}{\n\t\tCluster: cluster,\n\t})\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to execute template: %w\", err)\n\t}\n\n\treturn dc, nil\n}\n\n// DiscoverVTGate is part of the Discovery interface.\nfunc (c *ConsulDiscovery) DiscoverVTGate(ctx context.Context, tags []string) (*vtadminpb.VTGate, error) {\n\tspan, ctx := trace.NewSpan(ctx, \"ConsulDiscovery.DiscoverVTGate\")\n\tdefer span.Finish()\n\n\texecuteFQDNTemplate := true\n\n\treturn c.discoverVTGate(ctx, tags, executeFQDNTemplate)\n}\n\n// discoverVTGate calls discoverVTGates and then returns a random VTGate from\n// the result. see discoverVTGates for further documentation.\nfunc (c *ConsulDiscovery) discoverVTGate(ctx context.Context, tags []string, executeFQDNTemplate bool) (*vtadminpb.VTGate, error) {","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtadmin/cluster/discovery/discovery_consul.go#L161-L197","documentation":"generateConsulDatacenter executes a successfully-parsed datacenter template against a struct containing the cluster proto via textutil.ExecuteTemplate. If execution fails (data mismatch, nil field access, invalid pipeline at render time), this error wraps the runtime failure. Unlike error 898 this is a template execution error, not a parse error.","triggerScenarios":"Calling NewConsul with a datacenter template that parses but fails during ExecuteTemplate — e.g. the template calls a method/field on a nil value or has a runtime-invalid pipeline given the vtadminpb.Cluster data.","commonSituations":"Templates guarded with {{ if }} on fields that are nil pointers in the Cluster proto, method calls not defined on proto types, cluster proto fields left unset in the request.","solutions":["Remove or guard field accesses that can be nil (wrap with {{ if .Cluster.SomeField }}...{{ end }}).","Only use plain fields of vtadminpb.Cluster (Id, Names, etc.) rather than methods or deep pointer chains.","Read the wrapped error, which names the exact template line where execution failed.","Ensure the vtadminpb.Cluster passed to discovery.New is fully populated, not a zero-value proto."],"exampleFix":"// before\n--vtgate-datacenter-template=\"{{ .Cluster.ProxyTopology.Address }}\" // panics if ProxyTopology nil\n// after\n--vtgate-datacenter-template=\"{{ .Cluster.Id }}\"","handlingStrategy":"validation","validationCode":"// execute a dry-run render before NewConsul\ntmpl, err := template.New(\"dry\").Parse(tmplStr)\nif err == nil {\n    var buf bytes.Buffer\n    err = tmpl.Execute(&buf, struct{ Cluster *vtadminpb.Cluster }{Cluster: cluster})\n}","typeGuard":null,"tryCatchPattern":"disco, err := NewConsul(cluster, args)\nif err != nil && strings.Contains(err.Error(), \"failed to execute template\") {\n    log.Fatalf(\"template renders invalid data: %v\", err)\n}","preventionTips":["Guard potentially-nil proto fields with {{ if }} blocks before accessing them.","Ensure the vtadminpb.Cluster passed to discovery.New is fully populated.","Prefer plain fields over method calls in templates; proto wrappers may not expose methods.","Dry-run render templates with a sample Cluster in tests."],"tags":["go","template","consul","datacenter","execution"],"backgroundTag":"template-execution-failed","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}