{"record":{"id":"48bd4c312437b575","repo":"mikefarah/yq","slug":"system-command-v-failed-w-stderr-v","errorCode":null,"errorMessage":"system command '%v' failed: %w\nstderr: %v","messagePattern":"system command '(.+?)' failed: %w\nstderr: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/operator_system.go","lineNumber":130,"sourceCode":"\n\t\tvar stdin bytes.Buffer\n\t\tencoded, err := encodeToYamlString(candidate)\n\t\tif err != nil {\n\t\t\treturn Context{}, err\n\t\t}\n\t\tstdin.WriteString(encoded)\n\n\t\t// #nosec G204 - intentional: user must explicitly enable this operator\n\t\tcmd := exec.Command(command, args...)\n\t\tcmd.Stdin = &stdin\n\t\tvar stderr bytes.Buffer\n\t\tcmd.Stderr = &stderr\n\n\t\toutput, err := cmd.Output()\n\t\tif err != nil {\n\t\t\tstderrStr := strings.TrimSpace(stderr.String())\n\t\t\tif stderrStr != \"\" {\n\t\t\t\treturn Context{}, fmt.Errorf(\"system command '%v' failed: %w\\nstderr: %v\", command, err, stderrStr)\n\t\t\t}\n\t\t\treturn Context{}, fmt.Errorf(\"system command '%v' failed: %w\", command, err)\n\t\t}\n\n\t\tresult := string(output)\n\t\tif strings.HasSuffix(result, \"\\r\\n\") {\n\t\t\tresult = result[:len(result)-2]\n\t\t} else if strings.HasSuffix(result, \"\\n\") {\n\t\t\tresult = result[:len(result)-1]\n\t\t}\n\t\tnewNode := candidate.CreateReplacement(ScalarNode, \"!!str\", result)\n\t\tresults.PushBack(newNode)\n\t}\n\n\treturn context.ChildContext(results), nil\n}\n","sourceCodeStart":112,"sourceCodeEnd":147,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/operator_system.go#L112-L147","documentation":"The external command launched by the `system` operator exited with a non-zero status, and it wrote something to stderr. The error wraps the underlying exec error (typically *exec.ExitError) and appends the trimmed stderr so the caller can see why the command failed.","triggerScenarios":"`system(\"grep\"; [\"pattern\"])` where grep finds nothing (exit 1) and prints to stderr, or any command that fails — bad flags, missing files, permission errors — while emitting stderr output.","commonSituations":"Commands assuming a working directory or environment that differs in the yq run, tools like ssh/git/curl writing warnings to stderr on non-fatal errors, or legitimately failing commands (grep no-match, test false) whose non-zero exit codes yq treats as failures.","solutions":["Read the stderr text appended to the error message to diagnose the command's failure","Fix the underlying command arguments or input data so the command exits 0","If the command uses non-zero exits as signals (e.g. grep), wrap it so it exits 0: `system(\"sh\", \"-c\", \"grep p file || true\")` style, or redirect stderr if the noise is benign","Check the working directory/environment; the command runs in yq's cwd with the current node piped to stdin"],"exampleFix":"// before: grep exits 1 on no match\nsystem(\"grep\"; [\"needle\", \"haystack.txt\"])\n// after: tolerate no-match\nsystem(\"sh\", \"-c\", \"grep needle haystack.txt || true\")","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Go API usage\nout, err := yqEval(\"system(\\\"tool\\\")\", doc)\nif err != nil {\n    var exitErr *exec.ExitError\n    if strings.Contains(err.Error(), \"failed:\") && strings.Contains(err.Error(), \"stderr:\") {\n        // parse stderr portion after \"stderr:\" for the command's diagnostics\n    } else if errors.As(err, exitErr) { /* exit code at exitErr.ExitCode() */ }\n}","preventionTips":["Treat any non-zero child exit as an error path in scripts using system()","Make commands exit 0 on expected no-op cases (|| true) when failures are signals","Ensure the command's required files/env/cwd are present where yq runs"],"tags":["yq","system-operator","exec","stderr","process-exit"],"backgroundTag":"command-exit-nonzero","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}