-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Description
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 of Join is just cmd, and therefore exec.Command will do a PATH 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 use filepath.Separator manually, writing exec.Command("." + string(filepath.Separator) + cmd) which is hideous. The third is to create the Command directly. The fourth is to use an absolute path via filepath.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.