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
os/exec: add convenient, portable way to execute "./foo" (foo in working directory) #20308
Comments
What about |
Ooh, missed that one. Still feels a little hacky, but it's better than the others! |
Another option, which I just now noticed, is to create the
From https://golang.org/pkg/os/exec/#Cmd:
Edit: |
Personally, I don't feel like we need a helper function in os/exec that depends on os.Getwd |
I've only needed to do this a few times and I just used a full path with a directory like I feel like adding extra API or magic to |
This seems like a non-problem. People can write "./foo", which works everywhere already, or they can use a full path with os.Getwd, as @mvdan suggests. If this really happens a lot, then instead of
define
and write |
exec.Command
documentation states: "If name contains no path separators, Command uses LookPath to resolve name to a complete path if possible. Otherwise it uses name directly as Path."filepath.Join
documentation states: "Join calls Clean on the result"filepath.Clean
documentation states: "2. Eliminate each . path name element (the current directory)."The result is that the most obvious way to execute a command in the working directory,
exec.Command(filepath.Join(".", cmd))
is buggy. The result ofJoin
is justcmd
, and thereforeexec.Command
will do aPATH
lookup before running the command.There are four reasonable workarounds. The first is to specify the "/" explicitly and just write
exec.Command("./" + cmd)
and hope it works. The second is to usefilepath.Separator
manually, writingexec.Command("." + string(filepath.Separator) + cmd)
which is hideous. The third is to create theCommand
directly. The fourth is to use an absolute path viafilepath.Abs
, but it requires adding an extra error check.There should be some facility somewhere to let you do this with less of a headache. Whether this is a new method in the filepath package which allows for creation of paths with a leading "./" or a function in exec to create a Command that does not do
PATH
resolution, I don't think it is important.The text was updated successfully, but these errors were encountered: