Skip to content

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

@WillDrug

Description

@WillDrug

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions