Skip to content

Commit 574c286

Browse files
author
Jay Conrod
committed
cmd/go: trim paths from vendored packages with -trimpath
In CL 199821, we stopped setting the module directory for vendored packages when -mod=vendor is used. This broke -trimpath, since we replace the module directory with a string derived from the module path and version. A comment in CL 202977 makes it clear that the module directory should not be set. With this change, -trimpath falls back to replacing the package directory with the package path if the module directory is not set. We also fall back to replacing the package directory if the module version is not set to avoid adding a meaningless @ only for the main module. As a consequence of this change, file names in vendored packages will not have module versions, so file names will be a little different between -mod=mod and -mod=vendor. Fixes #36566 Change-Id: I0e9cd76d36a2028a49d0b6697ea9a9b3140d7ff3 Reviewed-on: https://go-review.googlesource.com/c/go/+/214945 Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 998cbe2 commit 574c286

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/cmd/go/internal/work/gc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func (a *Action) trimpath() string {
227227
// For "go build -trimpath", rewrite package source directory
228228
// to a file system-independent path (just the import path).
229229
if cfg.BuildTrimpath {
230-
if m := a.Package.Module; m != nil {
230+
if m := a.Package.Module; m != nil && m.Dir != "" && m.Version != "" {
231231
rewrite += ";" + m.Dir + "=>" + m.Path + "@" + m.Version
232232
} else {
233233
rewrite += ";" + a.Package.Dir + "=>" + a.Package.ImportPath
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Check that when -trimpath and -mod=vendor are used together,
2+
# paths in vendored packages are properly trimmed.
3+
# Verifies golang.org/issue/36566.
4+
5+
[short] skip
6+
7+
# Only the main module has a root directory in vendor mode.
8+
go mod vendor
9+
go list -f {{.Module.Dir}} example.com/main
10+
stdout $PWD
11+
go list -f {{.Module.Dir}} example.com/stack
12+
! stdout .
13+
14+
# The program prints a file name from a vendored package.
15+
# Without -trimpath, the name should include the vendor directory.
16+
go run main.go
17+
stdout vendor
18+
19+
# With -trimpath, everything before the package path should be trimmed.
20+
# Unlike with -mod=mod, we don't include versions as part of the module name.
21+
go run -trimpath main.go
22+
stdout example.com/stack/stack.go
23+
24+
-- go.mod --
25+
module example.com/main
26+
27+
require example.com/stack v1.0.0
28+
29+
-- main.go --
30+
package main
31+
32+
import (
33+
"fmt"
34+
35+
"example.com/stack"
36+
)
37+
38+
func main() {
39+
fmt.Println(stack.TopFile())
40+
}

0 commit comments

Comments
 (0)