mirror of
https://github.com/golang/go
synced 2024-11-23 09:30:03 -07:00
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 <jayconrod@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Michael Matloob <matloob@golang.org>
This commit is contained in:
parent
998cbe2983
commit
574c286607
@ -227,7 +227,7 @@ 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 {
|
||||
if m := a.Package.Module; m != nil && m.Dir != "" && m.Version != "" {
|
||||
rewrite += ";" + m.Dir + "=>" + m.Path + "@" + m.Version
|
||||
} else {
|
||||
rewrite += ";" + a.Package.Dir + "=>" + a.Package.ImportPath
|
||||
|
40
src/cmd/go/testdata/script/mod_vendor_trimpath.txt
vendored
Normal file
40
src/cmd/go/testdata/script/mod_vendor_trimpath.txt
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
# Check that when -trimpath and -mod=vendor are used together,
|
||||
# paths in vendored packages are properly trimmed.
|
||||
# Verifies golang.org/issue/36566.
|
||||
|
||||
[short] skip
|
||||
|
||||
# Only the main module has a root directory in vendor mode.
|
||||
go mod vendor
|
||||
go list -f {{.Module.Dir}} example.com/main
|
||||
stdout $PWD
|
||||
go list -f {{.Module.Dir}} example.com/stack
|
||||
! stdout .
|
||||
|
||||
# The program prints a file name from a vendored package.
|
||||
# Without -trimpath, the name should include the vendor directory.
|
||||
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.
|
||||
go run -trimpath main.go
|
||||
stdout example.com/stack/stack.go
|
||||
|
||||
-- go.mod --
|
||||
module example.com/main
|
||||
|
||||
require example.com/stack v1.0.0
|
||||
|
||||
-- main.go --
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"example.com/stack"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(stack.TopFile())
|
||||
}
|
Loading…
Reference in New Issue
Block a user