{"record":{"id":"9210167808a36c5e","repo":"docker/cli","slug":"template-parsing-error-w","errorCode":null,"errorMessage":"template parsing error: %w","messagePattern":"template parsing error: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/formatter/formatter.go","lineNumber":88,"sourceCode":"\n// Context contains information required by the formatter to print the output as desired.\ntype Context struct {\n\t// Output is the output stream to which the formatted string is written.\n\tOutput io.Writer\n\t// Format is used to choose raw, table or custom format for the output.\n\tFormat Format\n\t// Trunc when set to true will truncate the output of certain fields such as Container ID.\n\tTrunc bool\n\n\t// internal element\n\theader any\n\tbuffer *bytes.Buffer\n}\n\nfunc (c *Context) parseFormat() (*template.Template, error) {\n\ttmpl, err := templates.Parse(c.Format.templateString())\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"template parsing error: %w\", err)\n\t}\n\treturn tmpl, nil\n}\n\nfunc (c *Context) postFormat(tmpl *template.Template, subContext SubContext) {\n\tout := c.Output\n\tif out == nil {\n\t\tout = io.Discard\n\t}\n\tif !c.Format.IsTable() {\n\t\t_, _ = c.buffer.WriteTo(out)\n\t\treturn\n\t}\n\n\t// Write column-headers and rows to the tab-writer buffer, then flush the output.\n\ttw := tabwriter.NewWriter(out, 10, 1, 3, ' ', 0)\n\t_ = tmpl.Funcs(templates.HeaderFunctions).Execute(tw, subContext.FullHeader())\n\t_, _ = tw.Write([]byte{'\\n'})","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/formatter/formatter.go#L70-L106","documentation":"Returned by Context.parseFormat (formatter.go:88) when templates.Parse fails on the Go text/template string derived from the user's --format flag. This is a compile-time template error: the syntax is invalid Go template, an unknown function is referenced, or delimiters are mismatched. The error occurs before any data is rendered, so it is purely about the template grammar.","triggerScenarios":"Running any docker command with --format '<invalid template>', e.g., unbalanced actions '{{.ID', referencing a function that is not registered in the templates package, using wrong delimiters, or a stray '}}'. Also triggered by '--format json' variants that are actually custom templates but malformed.","commonSituations":"Shell-quoting mistakes that mangle the template (missing quotes around braces); using functions available in Go's text/template but not whitelisted by docker/cli/templates; copy-pasting a template that uses Sprig functions (not available here); typos in field accessor chains.","solutions":["Validate the template locally with Go: 'go run' a tiny text/template snippet, or test with a simpler field like '--format {{.ID}}'.","Check shell quoting - wrap the format in single quotes to prevent shell expansion of braces.","Use only functions provided by github.com/docker/cli/templates (json, id, etc.); avoid Sprig-specific helpers.","Start from a known-good format (e.g., '{{json .}}') and add complexity incrementally to isolate the syntax error."],"exampleFix":"# before\ndocker ps --format {{.ID {{.Names}}\n# after\ndocker ps --format '{{.ID}} {{.Names}}'","handlingStrategy":"validation","validationCode":"// Validate a format string parses before running the command.\nimport \"github.com/docker/cli/templates\"\n\nfunc validateFormat(format string) error {\n\t_, err := templates.Parse(formatter.Format(format).templateString())\n\treturn err\n}","typeGuard":null,"tryCatchPattern":"if err := cmd.Execute(); err != nil {\n\tif strings.Contains(err.Error(), \"template parsing error\") {\n\t\t// the format string is syntactically invalid; fix the --format flag\n\t}\n}","preventionTips":["Single-quote the --format value to protect braces from the shell.","Test templates against '{{json .}}' output first to learn available fields.","Use only functions exported by github.com/docker/cli/templates."],"tags":["formatter","template","format-flag","syntax"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}