Search code examples
gopprof

exec.Command does not register error from Go's own pprof tool


Here is my code:

cmd := exec.Command("go", "tool", "pprof", "-dot", "-lines", "http://google.com")
out, err := cmd.Output()
if err != nil {
    panic(err)
}
println(string(out))

When I run the exact same command in my console, I see:

$ go tool pprof -dot -lines http://google.com 
Fetching profile from http://google.com/profilez
Please wait... (30s)
server response: 404 Not Found 

However, my go program does not register that this is an error. Oddly, the variable out prints as an empty string and err is nil. What is going on?

To clarify, I am profiling http://google.com to purposefully create an error. I would normally profile a real Go application.


Solution

  • The text

    Fetching profile from http://google.com/profilez
    Please wait... (30s)
    server response: 404 Not Found 
    

    is written to stderr. Your program captures stdout, which is empty. Consider calling:

    out, err := cmd.CombinedOutput()
    

    to grab both stdout and stderr.

    cmd.Output() and cmd.CombinedOutput() return err == nil because the command exits with status zero. Perhaps an issue should be filed requesting that the command exit with non-zero status.