You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is as expected given #50599 (see the “Limitations” section of that proposal).
Because you have explicitly set cmd.Env to a non-nil slice, the PWD environment variable is not automatically updated to match cmd.Dir — instead, it retains the value that was set by the command cd /tmp/reprex/runner.
When the program invokes go run /tmp/reprex/symlink, the go command is run in the directory referred to by /tmp/reprex/symlink, but with a PWD that does not refer to that directory, so it cannot use the PWD variable to determine the working directory. It falls back to computing the working directory by walking the filesystem upward, which gives a path that does not involve symlinks (such as /tmp/reprex/orig). Because that directory contains a go.mod file, that is used as the location of the main module. You are requesting to run the program contained in /tmp/reprex/symlink, which is not lexically in the directory /tmp/reprex/orig, so it is considered to be outside of the main module.
The go command compares directories lexically because to do otherwise is not only much more expensive, but also potentially confusing — for example, it could cause error messages to refer to some directory other than the one you actually provided.
The fix is for your program to make use of #50599 to set PWD to correctly match the command's working directory (as required by POSIX):
cmd:=exec.Command("go", "run", src)
cmd.Dir=srccmd.Env=cmd.Environ() // Call after setting cmd.Dir to get the correct PWD.cmd.Stdout=os.Stdoutcmd.Stderr=os.Stderr
changed the title
cmd/go: go build (etc.) doesn't work with symlinks when invoked via os/exec.Command
cmd/go: go build (etc.) doesn't work with symlinks when invoked via os/exec.Command with mismatched PWD
Oct 23, 2023