Skip to content

os/exec: does not report error for exec.Command when the file does not exist #23437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dongweigogo opened this issue Jan 13, 2018 · 5 comments
Closed

Comments

@dongweigogo
Copy link

dongweigogo commented Jan 13, 2018

go 1.8.4

In running an exec.Command against files, Go does not report any error when the file does not exist. A simple example as below:

``

    cmd:=exec.Command("grep", "abc", "test.txt")     //actually, "test.txt" does not exist
    so,err1:=cmd.StdoutPipe()
    fmt.Println(err1)
    cmd.Start()
    buf,err2:=ioutil.ReadAll(so)
    fmt.Println(string(buf), err2)
    cmd.Wait()

``
Both err1 and err2 are nil, and no error is shown. It looks like the program runs successfully, but actually not.

@davecheney
Copy link
Contributor

Go does not report any error because you are ignoring the error from cmd.Start() and `cmd.Wait().

I am going to close this issue as this appears to be a request for support, not a bug report. We only use our bug tracker for tracking bugs and tracking proposals going through the Proposal Process.

Please see https://golang.org/wiki/Questions for good places to ask questions.

@dongweigogo
Copy link
Author

dongweigogo commented Jan 13, 2018

@davecheney , well, the cmd.Start() still does not report any error, but cmd.Wait() does. It looks weird, why do we just report it at the begining, in Start but not in Wait?

@as
Copy link
Contributor

as commented Jan 13, 2018

If cmd.Start doesn't report an error when the file doesn't exist, are there any conditions in which is reports an error?

@bradfitz
Copy link
Contributor

@as, yes, Start returns an error if the process fails to start. Wait returns an error if the process fails to finish. It doesn't know that test.txt is a filename vs any other arbitrary string. It's just an argument to grep, for grep to deal with.

Let's move discussion to golang-nuts.

@mikioh mikioh changed the title go does not report error for exec.Command when the file does not exist os/exec: does not report error for exec.Command when the file does not exist Jan 13, 2018
@as
Copy link
Contributor

as commented Jan 14, 2018

@bradfitz

My bad, the title of the issue made me think that file was referring to the grep binary itself.

@golang golang locked as resolved and limited conversation to collaborators Jan 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants