-
Notifications
You must be signed in to change notification settings - Fork 18k
os/exec: Cmd.Dir is not checked when looking for executable on Windows #21675
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
Comments
@atn832 thank you for the report! However, your bug report has me a little confused: // says it cannot find list.bat in the path.
runScript("subfolder", "list.bat")
// correctly runs .\subfolder\list2.bat from .\subfolder
runScript("subfolder", "list2.bat") in that snippet adapted from your bug report, both Also we have tests that set Dir that always run on every change, even on the Windows testing environments:
I don't have a Windows computer yet to reproduce this, I was just asking some questions. |
The difference between list.bat and list2.bat is that i added a dummy list2.bat in the executable's folder (parent of subfolder), as I specified right under "What did you do?". So my file structure was like so:
It seems that when test.exe checks to see if list2.bat exists, it will find one in test.exe's folder, so it will satisfy the check, then it will actually execute subfolder\list2.bat (as expected since cmd.Dir = "subfolder"). For list.bat, I guess it looks in the PATH environment variable, then in the executable's folder. Since it can't find it, it throws an error, even though it should really look for .\subfolder\list.bat. It looks like the tests in lp_windows_test.go runs "cmd", which is in in one of the folders in the PATH environment variable. and exec_test.go uses the file's absolute path ( |
Thanks for the response @atn832, I'll defer to @alexbrainman. I also don't have access to a Windows computer so couldn't dig through more. |
@atn832 I think this is working as expected.
This is a result of you running this line: runScript("subfolder", "list.bat") This sets Command's name parameter to "list.bat". According to https://golang.org/pkg/os/exec/#Command "... 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. ..." In your case it uses LookPath to search for "list.bat" (including current directory). It fails to find it, so you get that error message. Alex |
@atn832 |
Thank you all! I will try tonight. I also tested on Linux and Mac, with a subfolder in cmd.Dir and simply "./somescript.sh" (without prepending the subfolder) and it worked fine. Maybe because it contains a separator? |
@atn832 EDIT: |
Ok so I have tested both options + one other:
So I will use option 1, which is the same as what I used for Mac/Linux. Thank you! |
@atn832 As in the playground link, to get the path to where is the executable is.
Then do something like |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.9 windows/amd64
Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?(windows 10)
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\atn\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
What did you do?
I created the following files in the folder of this source code file:
.\subfolder\list.bat: file content is simply "dir"
.\subfolder\list2.bat: file content is simply "dir"
.\list2.bat: file content is empty, or can be anything
Recipe:
What did you expect to see?
I expect list.bat and list2.bat to list the content of subfolder.
What did you see instead?
list.bat is "not found", even though it is indeed in subfolder
EDIT: I've just go-fmt'd the source code as at https://play.golang.org/p/LDKmGQpxRH and also added the markdown tag for Go source code.
The text was updated successfully, but these errors were encountered: