aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2020-02-25 14:58:30 +0000
committerBryan C. Mills <bcmills@google.com>2020-02-25 15:43:19 +0000
commit987e4e892325ea91a15a34f7a858ab007fea39c4 (patch)
treeb5b607d959ac8745d18b5a02ec76b2e58e6a7e90 /src
parentb2696fde403a5b059936ac6dd22b6ec9a899e084 (diff)
downloadgo-987e4e892325ea91a15a34f7a858ab007fea39c4.tar.xz
Revert "Revert "cmd/go/internal/modload: record the replacement for the module containing package main in BuildInfo""
This reverts CL 220722. Reason for revert: rolling forward with fix. Fixes #37392 Change-Id: Iba8b0c645267777fbb7019976292d691a10b906a Reviewed-on: https://go-review.googlesource.com/c/go/+/220898 Run-TryBot: Bryan C. Mills <bcmills@google.com> Reviewed-by: Jay Conrod <jayconrod@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/go/internal/modload/build.go30
-rw-r--r--src/cmd/go/testdata/mod/example.com_printversion_v0.1.0.txt6
-rw-r--r--src/cmd/go/testdata/mod/example.com_printversion_v1.0.0.txt6
-rw-r--r--src/cmd/go/testdata/script/version_replace.txt33
-rw-r--r--src/runtime/debug/mod.go51
5 files changed, 90 insertions, 36 deletions
diff --git a/src/cmd/go/internal/modload/build.go b/src/cmd/go/internal/modload/build.go
index 6fa47d7400..454dbf28cf 100644
--- a/src/cmd/go/internal/modload/build.go
+++ b/src/cmd/go/internal/modload/build.go
@@ -207,6 +207,7 @@ func PackageBuildInfo(path string, deps []string) string {
if isStandardImportPath(path) || !Enabled() {
return ""
}
+
target := mustFindModule(path, path)
mdeps := make(map[module.Version]bool)
for _, dep := range deps {
@@ -223,26 +224,25 @@ func PackageBuildInfo(path string, deps []string) string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "path\t%s\n", path)
- tv := target.Version
- if tv == "" {
- tv = "(devel)"
- }
- fmt.Fprintf(&buf, "mod\t%s\t%s\t%s\n", target.Path, tv, modfetch.Sum(target))
- for _, mod := range mods {
- mv := mod.Version
+
+ writeEntry := func(token string, m module.Version) {
+ mv := m.Version
if mv == "" {
mv = "(devel)"
}
- r := Replacement(mod)
- h := ""
- if r.Path == "" {
- h = "\t" + modfetch.Sum(mod)
- }
- fmt.Fprintf(&buf, "dep\t%s\t%s%s\n", mod.Path, mv, h)
- if r.Path != "" {
- fmt.Fprintf(&buf, "=>\t%s\t%s\t%s\n", r.Path, r.Version, modfetch.Sum(r))
+ fmt.Fprintf(&buf, "%s\t%s\t%s", token, m.Path, mv)
+ if r := Replacement(m); r.Path == "" {
+ fmt.Fprintf(&buf, "\t%s\n", modfetch.Sum(m))
+ } else {
+ fmt.Fprintf(&buf, "\n=>\t%s\t%s\t%s\n", r.Path, r.Version, modfetch.Sum(r))
}
}
+
+ writeEntry("mod", target)
+ for _, mod := range mods {
+ writeEntry("dep", mod)
+ }
+
return buf.String()
}
diff --git a/src/cmd/go/testdata/mod/example.com_printversion_v0.1.0.txt b/src/cmd/go/testdata/mod/example.com_printversion_v0.1.0.txt
index bae8b13d47..606322ac86 100644
--- a/src/cmd/go/testdata/mod/example.com_printversion_v0.1.0.txt
+++ b/src/cmd/go/testdata/mod/example.com_printversion_v0.1.0.txt
@@ -21,7 +21,13 @@ func main() {
info, _ := debug.ReadBuildInfo()
fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
+ if r := info.Main.Replace; r != nil {
+ fmt.Fprintf(os.Stdout, "\t(replaced by %s %s)\n", r.Path, r.Version)
+ }
for _, m := range info.Deps {
fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
+ if r := m.Replace; r != nil {
+ fmt.Fprintf(os.Stdout, "\t(replaced by %s %s)\n", r.Path, r.Version)
+ }
}
}
diff --git a/src/cmd/go/testdata/mod/example.com_printversion_v1.0.0.txt b/src/cmd/go/testdata/mod/example.com_printversion_v1.0.0.txt
index 246741821a..b9b71e9538 100644
--- a/src/cmd/go/testdata/mod/example.com_printversion_v1.0.0.txt
+++ b/src/cmd/go/testdata/mod/example.com_printversion_v1.0.0.txt
@@ -29,7 +29,13 @@ func main() {
info, _ := debug.ReadBuildInfo()
fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
+ if r := info.Main.Replace; r != nil {
+ fmt.Fprintf(os.Stdout, "\t(replaced by %s %s)\n", r.Path, r.Version)
+ }
for _, m := range info.Deps {
fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
+ if r := m.Replace; r != nil {
+ fmt.Fprintf(os.Stdout, "\t(replaced by %s %s)\n", r.Path, r.Version)
+ }
}
}
diff --git a/src/cmd/go/testdata/script/version_replace.txt b/src/cmd/go/testdata/script/version_replace.txt
new file mode 100644
index 0000000000..b657086f09
--- /dev/null
+++ b/src/cmd/go/testdata/script/version_replace.txt
@@ -0,0 +1,33 @@
+[short] skip
+
+go mod download example.com/printversion@v0.1.0 example.com/printversion@v1.0.0
+
+go install example.com/printversion
+
+go run example.com/printversion
+cmp stdout out.txt
+
+go version -m $GOPATH/bin/printversion$GOEXE
+stdout '^.*[/\\]bin[/\\]printversion'$GOEXE': .*$'
+stdout '^ path example.com/printversion$'
+stdout '^ mod example.com/printversion v0.1.0$'
+stdout '^ => example.com/printversion v1.0.0 h1:.*$'
+stdout '^ dep example.com/version v1.0.0$'
+stdout '^ => example.com/version v1.0.1 h1:.*$'
+
+-- go.mod --
+module golang.org/issue/37392
+go 1.14
+require (
+ example.com/printversion v0.1.0
+)
+replace (
+ example.com/printversion => example.com/printversion v1.0.0
+ example.com/version v1.0.0 => example.com/version v1.0.1
+)
+-- out.txt --
+path is example.com/printversion
+main is example.com/printversion v0.1.0
+ (replaced by example.com/printversion v1.0.0)
+using example.com/version v1.0.0
+ (replaced by example.com/version v1.0.1)
diff --git a/src/runtime/debug/mod.go b/src/runtime/debug/mod.go
index 837cd689a0..0381bdcc53 100644
--- a/src/runtime/debug/mod.go
+++ b/src/runtime/debug/mod.go
@@ -47,9 +47,27 @@ func readBuildInfo(data string) (*BuildInfo, bool) {
repLine = "=>\t"
)
- info := &BuildInfo{}
+ readEntryFirstLine := func(elem []string) (Module, bool) {
+ if len(elem) != 2 && len(elem) != 3 {
+ return Module{}, false
+ }
+ sum := ""
+ if len(elem) == 3 {
+ sum = elem[2]
+ }
+ return Module{
+ Path: elem[0],
+ Version: elem[1],
+ Sum: sum,
+ }, true
+ }
- var line string
+ var (
+ info = &BuildInfo{}
+ last *Module
+ line string
+ ok bool
+ )
// Reverse of cmd/go/internal/modload.PackageBuildInfo
for len(data) > 0 {
i := strings.IndexByte(data, '\n')
@@ -63,42 +81,33 @@ func readBuildInfo(data string) (*BuildInfo, bool) {
info.Path = elem
case strings.HasPrefix(line, modLine):
elem := strings.Split(line[len(modLine):], "\t")
- if len(elem) != 3 {
+ last = &info.Main
+ *last, ok = readEntryFirstLine(elem)
+ if !ok {
return nil, false
}
- info.Main = Module{
- Path: elem[0],
- Version: elem[1],
- Sum: elem[2],
- }
case strings.HasPrefix(line, depLine):
elem := strings.Split(line[len(depLine):], "\t")
- if len(elem) != 2 && len(elem) != 3 {
+ last = new(Module)
+ info.Deps = append(info.Deps, last)
+ *last, ok = readEntryFirstLine(elem)
+ if !ok {
return nil, false
}
- sum := ""
- if len(elem) == 3 {
- sum = elem[2]
- }
- info.Deps = append(info.Deps, &Module{
- Path: elem[0],
- Version: elem[1],
- Sum: sum,
- })
case strings.HasPrefix(line, repLine):
elem := strings.Split(line[len(repLine):], "\t")
if len(elem) != 3 {
return nil, false
}
- last := len(info.Deps) - 1
- if last < 0 {
+ if last == nil {
return nil, false
}
- info.Deps[last].Replace = &Module{
+ last.Replace = &Module{
Path: elem[0],
Version: elem[1],
Sum: elem[2],
}
+ last = nil
}
}
return info, true