Skip to content

os/exec: Command does a LookPath in the wrong place. #7228

@gopherbot

Description

@gopherbot

by david@scraperwiki.com:

What steps will reproduce the problem?
If possible, include a link to a program on play.golang.org.

http://play.golang.org/p/3MjLGe5gFd

If I am planning to set the .Dir property of a Cmd object before calling .Run(), it
probably will not work because exec.Command() curses the Cmd object.

If the filename of a binary passed to exec.Command() does not exist then the returned
Cmd object is cursed and will not .Run() even if Dir is assigned before .Run() is called.

In the example in the code, exec.Command("./ls") followed by .Dir =
"/bin" followed by .Run() does not work, even though /bin/ls exists. (first
.Run() invocation in the code).

Internally, what is happening is that exec.Command() is calling LookPath and setting the
.err property. However, if "./ls" exists, then the LookPath will succeed, and
the .Run() will work, and it will run "/bin/ls" instead of "./ls"
(second invocation in the code).

The upshot is that exec.Command() should not be doing the LookPath. The LookPath should
be done only when .Run() is called, because .Dir may change.


What is the expected output?

We should see:
<a listing of the /bin directory>
<date and time> error: <nil>
<a listing of the /bin directory>
<date and time> error: <nil>

(note that this is the same thing twice)


What do you see instead?

2014/01/28 17:02:57 error: exec: "./ls": stat ./ls: no such file or directory
<a listing of the /bin directory>
<date and time> error: <nil>

(note that this has an error message followed by one part of the expected output)



Which compiler are you using (5g, 6g, 8g, gccgo)?

WAT?

Which operating system are you using?

$ uname -a
Linux ash 3.8.0-32-generic #47~precise1-Ubuntu SMP Wed Oct 2 16:19:35 UTC 2013 x86_64
x86_64 x86_64 GNU/Linux


Which version are you using?  (run 'go version')

$ go version
go version go1.1.2 linux/amd64

Please provide any additional information below.

Note that the sandbox provided by the go playground doesn't have a "/bin"
directory, but we can still see evidence of the buggy behaviour, because we get two
different error messages.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions