// The compiler hides the exact value of $GOROOT
// when building things in GOROOT.
// Assume b.WorkDir is being trimmed properly.
+ // When -trimpath is used with a package built from the module cache,
+ // use the module path and version instead of the directory.
if !p.Goroot && !cfg.BuildTrimpath && !strings.HasPrefix(p.Dir, b.WorkDir) {
fmt.Fprintf(h, "dir %s\n", p.Dir)
+ } else if cfg.BuildTrimpath && p.Module != nil {
+ fmt.Fprintf(h, "module %s@%s\n", p.Module.Path, p.Module.Version)
}
fmt.Fprintf(h, "goos %s goarch %s\n", cfg.Goos, cfg.Goarch)
fmt.Fprintf(h, "import %q\n", p.ImportPath)
--- /dev/null
+Module with a function that prints file name for the top stack frame.
+Different versions of this module are identical, but they should return
+different file names with -trimpath.
+-- .mod --
+module example.com/stack
+
+go 1.14
+-- .info --
+{"Version":"v1.0.0"}
+-- stack.go --
+package stack
+
+import "runtime"
+
+func TopFile() string {
+ _, file, _, _ := runtime.Caller(0)
+ return file
+}
--- /dev/null
+Module with a function that prints file name for the top stack frame.
+Different versions of this module are identical, but they should return
+different file names with -trimpath.
+-- .mod --
+module example.com/stack
+
+go 1.14
+-- .info --
+{"Version":"v1.0.1"}
+-- stack.go --
+package stack
+
+import "runtime"
+
+func TopFile() string {
+ _, file, _, _ := runtime.Caller(0)
+ return file
+}
+[short] skip
env GO111MODULE=on
# Set up fresh GOCACHE.
stderr '(compile|gccgo)( |\.exe)'
stderr 'link( |\.exe)'
+# Two distinct versions of the same module with identical content should
+# still be cached separately.
+# Verifies golang.org/issue/35412.
+go get -d example.com/stack@v1.0.0
+go run -trimpath printstack.go
+stdout '^example.com/stack@v1.0.0/stack.go$'
+go get -d example.com/stack@v1.0.1
+go run -trimpath printstack.go
+stdout '^example.com/stack@v1.0.1/stack.go$'
+
-- $WORK/hello.go --
package main
func main() { println("hello") }
+-- $WORK/printstack.go --
+// +build ignore
+
+package main
+
+import (
+ "fmt"
+
+ "example.com/stack"
+)
+
+func main() {
+ fmt.Println(stack.TopFile())
+}
-- $WORK/go.mod --
module m
+
+go 1.14