]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: unify trimpath logic for -mod=vendor, -mod=mod
authorJay Conrod <jayconrod@google.com>
Wed, 22 Jan 2020 23:46:11 +0000 (15:46 -0800)
committerJay Conrod <jayconrod@google.com>
Thu, 23 Jan 2020 16:52:00 +0000 (16:52 +0000)
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>
src/cmd/go/internal/work/gc.go
src/cmd/go/testdata/script/mod_vendor_trimpath.txt

index daa88d3114356bafe3998c2ac62f1d880f3f5006..7d17c0c01eda8c29099f0b3d96b8e1028df141d0 100644 (file)
@@ -227,8 +227,8 @@ func (a *Action) trimpath() string {
        // 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
                }
index 241a3036751e5f4ceeecf76dd4efbe98528f1ceb..c419fc9793d9bfb36b642a5411593491f075bb43 100644 (file)
@@ -17,9 +17,9 @@ go run main.go
 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