{"record":{"id":"024c0a45a518c7c5","repo":"thanos-io/thanos","slug":"preparing-s-command-failed","errorCode":null,"errorMessage":"preparing %s command failed","messagePattern":"preparing (.+?) command failed","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/thanos/main.go","lineNumber":143,"sourceCode":"\t\tctx, cancel := context.WithCancel(ctx)\n\t\tg.Add(func() error {\n\t\t\t<-ctx.Done()\n\t\t\treturn ctx.Err()\n\t\t}, func(error) {\n\t\t\tif closer != nil {\n\t\t\t\tif err := closer.Close(); err != nil {\n\t\t\t\t\tlevel.Warn(logger).Log(\"msg\", \"closing tracer failed\", \"err\", err)\n\t\t\t\t}\n\t\t\t}\n\t\t\tcancel()\n\t\t})\n\t}\n\t// Create a signal channel to dispatch reload events to sub-commands.\n\treloadCh := make(chan struct{}, 1)\n\n\tif err := setup(&g, logger, metrics, tracer, reloadCh, *logLevel == \"debug\"); err != nil {\n\t\t// Use %+v for github.com/pkg/errors error to print with stack.\n\t\tlevel.Error(logger).Log(\"err\", fmt.Sprintf(\"%+v\", errors.Wrapf(err, \"preparing %s command failed\", cmd)))\n\t\tos.Exit(1)\n\t}\n\n\t// Listen for termination signals.\n\t{\n\t\tcancel := make(chan struct{})\n\t\tg.Add(func() error {\n\t\t\treturn interrupt(logger, cancel)\n\t\t}, func(error) {\n\t\t\tclose(cancel)\n\t\t})\n\t}\n\n\t// Listen for reload signals.\n\t{\n\t\tcancel := make(chan struct{})\n\t\tg.Add(func() error {\n\t\t\treturn reload(logger, cancel, reloadCh)","sourceCodeStart":125,"sourceCodeEnd":161,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/cmd/thanos/main.go#L125-L161","documentation":"This is a top-level wrapper in Thanos's main(): when the per-subcommand setup function (flag validation, config parsing, component wiring) returns an error, main wraps it with errors.Wrapf(err, \"preparing %s command failed\", cmd) and logs it with a full stack trace before exiting with status 1. It means the chosen Thanos subcommand (e.g. query, sidecar) failed during initialization, before any run group started.","triggerScenarios":"Running any `thanos <subcommand>` where the cmd.Setup callback invoked by setup() returns a non-nil error: invalid flags, unparseable label/relabel config, failed request-logging config parse, bad TLS paths, etc.","commonSituations":"Typo in --store or --endpoint flags, malformed YAML in --request-log-config, invalid relabel expressions, missing TLS cert files, or incompatible flag combinations when launching Thanos in Kubernetes/Docker.","solutions":["Read the wrapped inner error in the logged stack trace (%+v prints the full chain); it names the exact setup step that failed.","Re-run the command with --log.level=debug for more context.","Validate all flag values and config files (labels, relabel configs, request-log config) against Thanos documentation.","Fix the underlying misconfiguration and restart the command."],"exampleFix":"// before\nthanos query --selector-label='}'   // malformed label flag\n// after\nthanos query --selector-label='cluster=\"eu1\"'","handlingStrategy":"validation","validationCode":"// Pre-validate flags/config before launching thanos\nif cfg, err := yaml.Marshal(myConfig); err != nil || len(cfg) == 0 {\n    return fmt.Errorf(\"invalid thanos config: %w\", err)\n}\n// smoke-test the command\nif out, err := exec.Command(\"thanos\", cmd, \"--help\").CombinedOutput(); err != nil {\n    return fmt.Errorf(\"thanos %s unavailable: %s\", cmd, out)\n}","typeGuard":null,"tryCatchPattern":"// In a wrapper script\nif ! thanos query --log.level=debug ...; then\n  echo \"setup failed; see wrapped inner error above\" >&2\n  exit 1\nfi","preventionTips":["Always read the innermost error of the logged %+v stack, not just the wrapper message","Lint Kubernetes/Helm manifests so quoted flag values survive templating","Run thanos with --log.level=debug during first deployment"],"tags":["cli","setup","configuration"],"backgroundTag":"invalid-cli-argument","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}