go/internal/cgo: set pkgdir with -srcdir instead of CWD

If pkgdir uses a symlink (e.g., how the macOS builders are currently
setup for their GOROOT, due to how /var and /tmp work there), then
changing into that directory to invoke cgo means that cgo will only be
able to see the actual directory it was run from. This can lead to
inconsistencies with code that expects to see the original
symlink-based path.

This CL changes go/internal/cgo to instead use cgo's -srcdir flag so
that cgo can emit //line directives with the symlink paths instead.

Fixes golang/go#44339.

Change-Id: I3cbee6753454922711c8c70d7c64a02565b8bb4c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/293250
gopls-CI: kokoro <noreply+kokoro@google.com>
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/go/internal/cgo/cgo.go b/go/internal/cgo/cgo.go
index 9772504..d9074ea 100644
--- a/go/internal/cgo/cgo.go
+++ b/go/internal/cgo/cgo.go
@@ -57,13 +57,14 @@
 	"go/build"
 	"go/parser"
 	"go/token"
-	exec "golang.org/x/sys/execabs"
 	"io/ioutil"
 	"log"
 	"os"
 	"path/filepath"
 	"regexp"
 	"strings"
+
+	exec "golang.org/x/sys/execabs"
 )
 
 // ProcessFiles invokes the cgo preprocessor on bp.CgoFiles, parses
@@ -159,14 +160,13 @@
 	}
 
 	args := stringList(
-		"go", "tool", "cgo", "-objdir", tmpdir, cgoflags, "--",
+		"go", "tool", "cgo", "-srcdir", pkgdir, "-objdir", tmpdir, cgoflags, "--",
 		cgoCPPFLAGS, cgoexeCFLAGS, cgoFiles,
 	)
 	if false {
-		log.Printf("Running cgo for package %q: %s (dir=%s)", bp.ImportPath, args, pkgdir)
+		log.Printf("Running cgo for package %q: %s", bp.ImportPath, args)
 	}
 	cmd := exec.Command(args[0], args[1:]...)
-	cmd.Dir = pkgdir
 	cmd.Stdout = os.Stderr
 	cmd.Stderr = os.Stderr
 	if err := cmd.Run(); err != nil {
diff --git a/go/internal/gcimporter/bexport_test.go b/go/internal/gcimporter/bexport_test.go
index 48e99d6..702278e 100644
--- a/go/internal/gcimporter/bexport_test.go
+++ b/go/internal/gcimporter/bexport_test.go
@@ -118,15 +118,8 @@
 
 func fileLine(fset *token.FileSet, obj types.Object) string {
 	posn := fset.Position(obj.Pos())
-	filename := strings.ReplaceAll(posn.Filename, "$GOROOT", build.Default.GOROOT)
-	if runtime.GOOS == "darwin" {
-		// cmd/compile and go/loader are inconsistent about which spelling
-		// to use in os/signal/internal/pty's position information.
-		// TODO(mdempsky): Investigate why they're inconsistent, and why
-		// for only that one package (golang.org/issue/44339).
-		filename = strings.ReplaceAll(filename, "/private/var/folders/", "/var/folders/")
-	}
-	return fmt.Sprintf("%s:%d", filepath.Clean(filename), posn.Line)
+	filename := filepath.Clean(strings.ReplaceAll(posn.Filename, "$GOROOT", runtime.GOROOT()))
+	return fmt.Sprintf("%s:%d", filename, posn.Line)
 }
 
 // equalObj reports how x and y differ.  They are assumed to belong to