Skip to content
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: Getwd returns wrong path format in MinGW #43944

Open
enihcam opened this issue Jan 27, 2021 · 13 comments
Open

os: Getwd returns wrong path format in MinGW #43944

enihcam opened this issue Jan 27, 2021 · 13 comments

Comments

@enihcam
Copy link

@enihcam enihcam commented Jan 27, 2021

What version of Go are you using (go version)?

$ go version
go1.15.7 windows/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\guest\AppData\Local\go-build
set GOENV=C:\Users\guest\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\guest\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\guest\go
set GOPRIVATE=
set GOPROXY=
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
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
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\guest\AppData\Local\Temp\go-build066057222=/tmp/go-build -gno-record-gcc-switches

What did you do?

run exec.Command(filepath.Join(os.Getwd(), 'app') under:

~ $ uname -a
MINGW64_NT-10.0-17763 dell 3.1.7-340.x86_64 2020-10-23 13:08 UTC x86_64 Msys
~ $ pwd
/c/Users/guest

What did you expect to see?

app output

What did you see instead?

exec: "C:\\Users\\guest\\app": file does not exist

@seankhliao seankhliao changed the title [MINGW] incorrect folder format returned by os.Getwd() os: Getwd returns wrong path format in mngw Jan 27, 2021
@ALTree ALTree changed the title os: Getwd returns wrong path format in mngw os: Getwd returns wrong path format in MinGW Jan 27, 2021
@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Jan 27, 2021

Is the issue the printed path shows "C:\\Users\\guest\\app" or is the issue that it gives a error?

Because I cannot reproduce the error on msys on windows if app is actually in the working directory.

@enihcam
Copy link
Author

@enihcam enihcam commented Jan 27, 2021

@AlexRouSg @seankhliao here is how i reproduce the problem:

~ $ ls app
app*
~ $ pwd
/c/Users/guest

now try this code:

func main() {
	dir, _ := os.Getwd()
	ret := exec.Command(filepath.Join(dir, "app"))
	fmt.Println(ret.Output())
}

i got

[] exec: "C:\\Users\\guest\\app": file does not exist

my expectation would be:

  1. os.Getwd() returns /c/Users/guest instead of C:\\Users\\guest, i.e. same as the output from bash cmd pwd
  2. correspondingly, filepath.Join() returns /c/Users/guest/app
  3. execute /c/Users/guest/app.

thanks.

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Jan 27, 2021

There is nothing wrong with os.Getwd() and related functions returning a different format other than cosmetics.
You can even run cd C:\\Users\\guest on msys

This is a issue of exec not being able to run programs without the .exe extension.
i.e. it will work if you rename app to app.exe (only the filename, you do not have to change the code)

cc @alexbrainman for windows

@enihcam
Copy link
Author

@enihcam enihcam commented Jan 27, 2021

@AlexRouSg i see. thanks.

@seankhliao
Copy link
Contributor

@seankhliao seankhliao commented Jan 27, 2021

should this be closed as working as intended?

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Jan 27, 2021

@seankhliao
No idea if it's intentional

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Jan 27, 2021

maybe change the title to: exec: Unable to run programs without .exe extension on windows?

@enihcam
Copy link
Author

@enihcam enihcam commented Jan 27, 2021

maybe change the title to: exec: Unable to run programs without .exe extension on windows?

however the app (without .exe) runs under MinGW. i thought Go should be more UNIX-ish? :)

@alexbrainman
Copy link
Member

@alexbrainman alexbrainman commented Jan 28, 2021

What did you expect to see?

app output

What did you see instead?

exec: "C:\\Users\\guest\\app": file does not exist

@enihcam looks like you are using Cygwin shell.

Go is not supported to run under Cygwin. If you want to run program built with Go for Windows, just use plain Windows.

Alex

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Jan 28, 2021

@alexbrainman

Issue is not cygwin related, author had a file named app with no extension and exec is unable to run it.
Changing the filename on disk from app to app.exe runs.

@alexbrainman
Copy link
Member

@alexbrainman alexbrainman commented Feb 5, 2021

@AlexRouSg

Issue is not cygwin related,

I disagree. This code

exec.Command(filepath.Join(os.Getwd(), 'app')

does not produce this error message

exec: "C:\\Users\\guest\\app": file does not exist

if you run this program under cmd.exe.

I am guessing here. If you really want my help. I need proper repro (step by step instructions on how to reproduce this issue), not bits of code and bits of error message.

Alex

@AlexRouSg
Copy link
Contributor

@AlexRouSg AlexRouSg commented Feb 5, 2021

code:

package main

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

func main() {
	dir, _ := os.Getwd()
	cmd := exec.Command(filepath.Join(dir, "app"))
	err := cmd.Run()

	if err != nil {
		fmt.Println(err)
	}
}

cmd cmmands and output:

C:\Users\alexr\go-workspace\src\test>dir
 Directory of C:\Users\alexr\go-workspace\src\test

06/02/2021  05:24 AM    <DIR>          .
06/02/2021  05:24 AM    <DIR>          ..
27/01/2021  09:26 PM         2,137,600 app.exe
27/01/2021  09:41 PM               161 test.go
               2 File(s)      2,137,761 bytes
               2 Dir(s)  1,557,246,210,048 bytes free

C:\Users\alexr\go-workspace\src\test>go run test.go

C:\Users\alexr\go-workspace\src\test>rename app.exe app

C:\Users\alexr\go-workspace\src\test>dir
 Directory of C:\Users\alexr\go-workspace\src\test

06/02/2021  05:25 AM    <DIR>          .
06/02/2021  05:25 AM    <DIR>          ..
27/01/2021  09:26 PM         2,137,600 app
27/01/2021  09:41 PM               161 test.go
               2 File(s)      2,137,761 bytes
               2 Dir(s)  1,557,246,099,456 bytes free

C:\Users\alexr\go-workspace\src\test>go run test.go
exec: "C:\\Users\\alexr\\go-workspace\\src\\test\\app": file does not exist

C:\Users\alexr\go-workspace\src\test>
@alexbrainman
Copy link
Member

@alexbrainman alexbrainman commented Feb 7, 2021

@AlexRouSg thanks for repro steps.

I agree, that your program should be able to run without the error as per

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.

I don't agree or disagree with documentation. But that is how I read the documentation.

But real code also applies PATHEXT matches to your path before accepting it. That is why it fails.

I am not sure what should be done here. Perhaps we can relax this scenario to execute file path if it exists.

Alex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants