Skip to content

os/exec: document that LookPath result may only be valid relative to current directory #3622

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
alexbrainman opened this issue May 15, 2012 · 15 comments
Milestone

Comments

@alexbrainman
Copy link
Member

This program should work, but fails

package main

import (
    "fmt"
    "log"
    "os"
    "os/exec"
    "path/filepath"
    "runtime"
)

func main() {
    var prog string
    if runtime.GOOS != "windows" {
        prog = "date"
        // add . at the front of our PATH,
        // windows starts search in the current directory anyway
        p := os.Getenv("PATH")
        err := os.Setenv("PATH", ".:"+p)
        if err != nil {
            log.Fatal(err)
        }
    } else {
        prog = "ipconfig"
    }
    // find our executable
    path, err := exec.LookPath(prog)
    if err != nil {
        log.Fatal(err)
    }
    dir, _ := filepath.Split(path)
    // chdir to where our executable lives
    err = os.Chdir(dir)
    if err != nil {
        log.Fatal(err)
    }
    // find our executable again
    path, err = exec.LookPath(prog)
    if err != nil {
        log.Fatal(err)
    }
    // run our executable with /tmp directory as current
    cmd := exec.Command(path)
    cmd.Dir = os.TempDir()
    out, err := cmd.CombinedOutput()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("out=%v\n", string(out))
}

I am not sure where things should be changed.

Alex
@bradfitz
Copy link
Contributor

Comment 2:

Your summary says "relative path returned by LookPath".
The whole point of LookPath is to return an absolute path.

@alexbrainman
Copy link
Member Author

Comment 3:

Well, it is returning relative path in some situations (see my sample program). If that
is incorrect, lets change it.
Alex

@rsc
Copy link
Contributor

rsc commented May 24, 2012

Comment 4:

it can return a relative path when . is in your path, i think

@alexbrainman
Copy link
Member Author

Comment 5:

That is, in fact, default on Windows. Windows always look in the current directory first.
Alex

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 6:

I think LookPath returning a relative path is okay. The fix here is that if you're going
to do a Chdir and prog is a relative path, make it an absolute path before the Chdir.
But that is a fix in func main. I don't know whether there's anything in the tree to fix
but docs.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 7:

Labels changed: added priority-later, removed priority-triage.

@rsc
Copy link
Contributor

rsc commented Sep 12, 2012

Comment 8:

Labels changed: added go1.1maybe.

@alexbrainman
Copy link
Member Author

Comment 9:

@rsc,
>>> ... The fix here is that if you're going to do a Chdir and prog is a relative path,
make it an absolute path before the Chdir ...
I am slow, so, please, send amended program and I will try it. Thank you.
Alex

@rsc
Copy link
Contributor

rsc commented Sep 13, 2012

Comment 10:

The fixed program would go something like
    // find our executable
    path, err := exec.LookPath(prog)
    if err != nil {
        log.Fatal(err)
    }
    // get absolute path so we can run it from /tmp
        path, err = filepath.Abs(path)
        if err != nil {
                log.Fatal(err)
        }
    // run our executable with /tmp directory as current
    cmd := exec.Command(path)
    cmd.Dir = os.TempDir()
    out, err := cmd.CombinedOutput()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("out=%v\n", string(out))

@alexbrainman
Copy link
Member Author

Comment 11:

I see. Are you saying that result of successful exec.LookPath is not always ready for
consumption by exec.Command? Then we need to document that and also, probably, be able
to explain why it is so. :-(
Alex

@rsc
Copy link
Contributor

rsc commented Sep 13, 2012

Comment 12:

relative to current directory

@rsc
Copy link
Contributor

rsc commented Dec 10, 2012

Comment 14:

Labels changed: added size-m.

@rsc
Copy link
Contributor

rsc commented Mar 12, 2013

Comment 15:

[The time for maybe has passed.]

Labels changed: removed go1.1maybe.

@rsc
Copy link
Contributor

rsc commented Jul 30, 2013

Comment 16:

Labels changed: added go1.2.

@robpike
Copy link
Contributor

robpike commented Aug 15, 2013

Comment 17:

This issue was closed by revision a41a5bb.

Status changed to Fixed.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants