{"record":{"id":"f586379b5a4f0804","repo":"OpenNHP/opennhp","slug":"s","errorCode":null,"errorMessage":"%s","messagePattern":"%s","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/utils/cmd.go","lineNumber":33,"sourceCode":"\tdefer cancel()\n\tc := make(chan string)\n\tdefer close(c)\n\tvar stderr bytes.Buffer\n\tvar stdout bytes.Buffer\n\n\tcmd := exec.CommandContext(ctx, command, args...) //nolint:gosec // G204: Command args passed as separate parameters, not shell string\n\tcmd.Stderr = &stderr\n\tcmd.Stdout = &stdout\n\tif len(in) > 0 {\n\t\tcmd.Stdin = strings.NewReader(in)\n\t}\n\terr := cmd.Run()\n\tif err != nil {\n\t\treturn \"\", cmd.String(), err\n\t}\n\tif stderr.String() != \"\" {\n\t\tlog.Println(stderr.String())\n\t\treturn \"\", cmd.String(), fmt.Errorf(\"%s\", stderr.String())\n\t}\n\n\tres := strings.Replace(stdout.String(), \"\\n\", \"\", -1)\n\treturn res, cmd.String(), nil\n}\n","sourceCodeStart":15,"sourceCodeEnd":39,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/utils/cmd.go#L15-L39","documentation":"utils.Run executes an external command and captures stdout/stderr. If the command exits with an error it returns that error; if the command apparently succeeded but wrote anything to stderr, Run logs the stderr and fails the call with an error whose message is the raw stderr text (fmt.Errorf(\"%s\", ...)). Any non-empty stderr is therefore treated as a failure even when the exit code was 0.","triggerScenarios":"A wrapped command writes warnings or informational messages to stderr while still producing correct stdout; or the command actually fails and cmd.Run() returns nil only in the exit-0-but-noisy case. The caller receives an error containing the tool's own stderr output as the message.","commonSituations":"CLI tools that print deprecation warnings or progress bars to stderr; scripts invoked by nhp that always emit a banner on stderr; tools whose success output partially goes to stderr.","solutions":["Run the command manually and inspect its stderr to see the actual message returned in the error","Fix the invoked command/tool so it does not write to stderr on success (redirect its stderr to stdout or /dev/null in the wrapper script)","If the stderr output is benign and you control the code, check the error string or switch to calling cmd.Run() directly with your own exit-code-only policy"],"exampleFix":"// before (called via utils.Run, fails on any stderr)\nout, _, err := utils.Run(\"tool\", \"arg\")\n// after (tolerate warnings, keep output)\ncmd := exec.Command(\"tool\", \"arg\")\nvar stderr bytes.Buffer\ncmd.Stderr = &stderr\nerr := cmd.Run()\n// inspect stderr only if err != nil","handlingStrategy":"validation","validationCode":"var b bytes.Buffer\ncmd := exec.Command(\"tool\", \"args\")\ncmd.Stderr = &b\nif err := cmd.Run(); err != nil { return err }\nif b.Len() > 0 { log.Printf(\"tool stderr: %s\", b.String()) } // handle warnings explicitly before using utils.Run","typeGuard":null,"tryCatchPattern":"out, cmdStr, err := utils.Run(\"tool\", \"arg\")\nif err != nil {\n    log.Printf(\"command %s failed with stderr: %v\", cmdStr, err)\n    return err\n}","preventionTips":["Test every wrapped command manually and check whether it writes to stderr on success","Silence or redirect benign stderr output in wrapper scripts (2>/dev/null) when success output is on stdout","Prefer checking exit codes over stderr content when wrapping third-party CLIs"],"tags":["go","exec","stderr","subprocess"],"backgroundTag":"command-execution-failed","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}