If a package has a module with a version, the package's directory is
replaced with the module path and version, followed by the package's
path within the module.
This is a follow up to CL 214945. We no longer check whether the
module has a directory (with -mod=vendor, it does not).
Updates #36566
Change-Id: I5bc952b13bc7b4659f58ee555bd6c6a087eb7792
Reviewed-on: https://go-review.googlesource.com/c/go/+/215940
Run-TryBot: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
// For "go build -trimpath", rewrite package source directory
// to a file system-independent path (just the import path).
if cfg.BuildTrimpath {
- if m := a.Package.Module; m != nil && m.Dir != "" && m.Version != "" {
- rewrite += ";" + m.Dir + "=>" + m.Path + "@" + m.Version
+ if m := a.Package.Module; m != nil && m.Version != "" {
+ rewrite += ";" + a.Package.Dir + "=>" + m.Path + "@" + m.Version + strings.TrimPrefix(a.Package.ImportPath, m.Path)
} else {
rewrite += ";" + a.Package.Dir + "=>" + a.Package.ImportPath
}
stdout vendor
# With -trimpath, everything before the package path should be trimmed.
-# Unlike with -mod=mod, we don't include versions as part of the module name.
+# As with -mod=mod, the version should appear as part of the module path.
go run -trimpath main.go
-stdout example.com/stack/stack.go
+stdout example.com/stack@v1.0.0/stack.go
-- go.mod --
module example.com/main