Skip to content

os/exec: Cmd changes absolute path to relative when using go #28356

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
WillDrug opened this issue Oct 24, 2018 · 1 comment
Closed

os/exec: Cmd changes absolute path to relative when using go #28356

WillDrug opened this issue Oct 24, 2018 · 1 comment

Comments

@WillDrug
Copy link

WillDrug commented Oct 24, 2018

Using go version go1.11.1 linux/amd64 with default env I run the following code (simplified for the ticket, added extra fmt.Print*)

cmdArgs = <-stdin
log.Printf("Got %v to run", cmdArgs)
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
ret, err = cmd.CombinedOutput()
fmt.Println(ret)

Expected result --

Got [go build -o=/abs/path/to/output/HelloWorld -a /abs/path/to/code/HelloWorld/] to run

Actual result --

Got [go build -o=/abs/path/to/output/HelloWorld -a /abs/path/to/code/HelloWorld/] to run
go: directory ../HelloWorld/ outside available modules

Somehow, exec.Command transforms an absolute build path to relative (specifically, relative to current software working dir), making me think that when the program runs, additional go execs have the same execution environment.

Appending cmd.Dir = "/abs/path/to/code/HelloWorld/" fixes the issue, but I would like to get around this.

P.S. Since build path is absolute, obviously, I'm using go mod.

@ianlancetaylor ianlancetaylor changed the title os/exec.Cmd changes absolute path to relative when using go os/exec: Cmd changes absolute path to relative when using go Oct 24, 2018
@ianlancetaylor
Copy link
Member

The go tool prints a path as relative if that is shorter. See https://golang.org/src/cmd/go/internal/base/path.go#L23 . This isn't a bug in os/exec. I don't think it's a bug at all. Closing. Please comment if you disagree. If you do disagree, please provide a complete program that shows the problem, not a fragment; thanks.

@golang golang locked and limited conversation to collaborators Oct 24, 2019
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

3 participants