Skip to content

Commit 987e4e8

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> Reviewed-by: Jay Conrod <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent b2696fd commit 987e4e8

File tree

5 files changed

+90
-36
lines changed

5 files changed

+90
-36
lines changed

src/cmd/go/internal/modload/build.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ func PackageBuildInfo(path string, deps []string) string {
207207
if isStandardImportPath(path) || !Enabled() {
208208
return ""
209209
}
210+
210211
target := mustFindModule(path, path)
211212
mdeps := make(map[module.Version]bool)
212213
for _, dep := range deps {
@@ -223,26 +224,25 @@ func PackageBuildInfo(path string, deps []string) string {
223224

224225
var buf bytes.Buffer
225226
fmt.Fprintf(&buf, "path\t%s\n", path)
226-
tv := target.Version
227-
if tv == "" {
228-
tv = "(devel)"
229-
}
230-
fmt.Fprintf(&buf, "mod\t%s\t%s\t%s\n", target.Path, tv, modfetch.Sum(target))
231-
for _, mod := range mods {
232-
mv := mod.Version
227+
228+
writeEntry := func(token string, m module.Version) {
229+
mv := m.Version
233230
if mv == "" {
234231
mv = "(devel)"
235232
}
236-
r := Replacement(mod)
237-
h := ""
238-
if r.Path == "" {
239-
h = "\t" + modfetch.Sum(mod)
240-
}
241-
fmt.Fprintf(&buf, "dep\t%s\t%s%s\n", mod.Path, mv, h)
242-
if r.Path != "" {
243-
fmt.Fprintf(&buf, "=>\t%s\t%s\t%s\n", r.Path, r.Version, modfetch.Sum(r))
233+
fmt.Fprintf(&buf, "%s\t%s\t%s", token, m.Path, mv)
234+
if r := Replacement(m); r.Path == "" {
235+
fmt.Fprintf(&buf, "\t%s\n", modfetch.Sum(m))
236+
} else {
237+
fmt.Fprintf(&buf, "\n=>\t%s\t%s\t%s\n", r.Path, r.Version, modfetch.Sum(r))
244238
}
245239
}
240+
241+
writeEntry("mod", target)
242+
for _, mod := range mods {
243+
writeEntry("dep", mod)
244+
}
245+
246246
return buf.String()
247247
}
248248

src/cmd/go/testdata/mod/example.com_printversion_v0.1.0.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ func main() {
2121
info, _ := debug.ReadBuildInfo()
2222
fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
2323
fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
24+
if r := info.Main.Replace; r != nil {
25+
fmt.Fprintf(os.Stdout, "\t(replaced by %s %s)\n", r.Path, r.Version)
26+
}
2427
for _, m := range info.Deps {
2528
fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
29+
if r := m.Replace; r != nil {
30+
fmt.Fprintf(os.Stdout, "\t(replaced by %s %s)\n", r.Path, r.Version)
31+
}
2632
}
2733
}

src/cmd/go/testdata/mod/example.com_printversion_v1.0.0.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ func main() {
2929
info, _ := debug.ReadBuildInfo()
3030
fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
3131
fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
32+
if r := info.Main.Replace; r != nil {
33+
fmt.Fprintf(os.Stdout, "\t(replaced by %s %s)\n", r.Path, r.Version)
34+
}
3235
for _, m := range info.Deps {
3336
fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
37+
if r := m.Replace; r != nil {
38+
fmt.Fprintf(os.Stdout, "\t(replaced by %s %s)\n", r.Path, r.Version)
39+
}
3440
}
3541
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[short] skip
2+
3+
go mod download example.com/[email protected] example.com/[email protected]
4+
5+
go install example.com/printversion
6+
7+
go run example.com/printversion
8+
cmp stdout out.txt
9+
10+
go version -m $GOPATH/bin/printversion$GOEXE
11+
stdout '^.*[/\\]bin[/\\]printversion'$GOEXE': .*$'
12+
stdout '^ path example.com/printversion$'
13+
stdout '^ mod example.com/printversion v0.1.0$'
14+
stdout '^ => example.com/printversion v1.0.0 h1:.*$'
15+
stdout '^ dep example.com/version v1.0.0$'
16+
stdout '^ => example.com/version v1.0.1 h1:.*$'
17+
18+
-- go.mod --
19+
module golang.org/issue/37392
20+
go 1.14
21+
require (
22+
example.com/printversion v0.1.0
23+
)
24+
replace (
25+
example.com/printversion => example.com/printversion v1.0.0
26+
example.com/version v1.0.0 => example.com/version v1.0.1
27+
)
28+
-- out.txt --
29+
path is example.com/printversion
30+
main is example.com/printversion v0.1.0
31+
(replaced by example.com/printversion v1.0.0)
32+
using example.com/version v1.0.0
33+
(replaced by example.com/version v1.0.1)

src/runtime/debug/mod.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,27 @@ func readBuildInfo(data string) (*BuildInfo, bool) {
4747
repLine = "=>\t"
4848
)
4949

50-
info := &BuildInfo{}
50+
readEntryFirstLine := func(elem []string) (Module, bool) {
51+
if len(elem) != 2 && len(elem) != 3 {
52+
return Module{}, false
53+
}
54+
sum := ""
55+
if len(elem) == 3 {
56+
sum = elem[2]
57+
}
58+
return Module{
59+
Path: elem[0],
60+
Version: elem[1],
61+
Sum: sum,
62+
}, true
63+
}
5164

52-
var line string
65+
var (
66+
info = &BuildInfo{}
67+
last *Module
68+
line string
69+
ok bool
70+
)
5371
// Reverse of cmd/go/internal/modload.PackageBuildInfo
5472
for len(data) > 0 {
5573
i := strings.IndexByte(data, '\n')
@@ -63,42 +81,33 @@ func readBuildInfo(data string) (*BuildInfo, bool) {
6381
info.Path = elem
6482
case strings.HasPrefix(line, modLine):
6583
elem := strings.Split(line[len(modLine):], "\t")
66-
if len(elem) != 3 {
84+
last = &info.Main
85+
*last, ok = readEntryFirstLine(elem)
86+
if !ok {
6787
return nil, false
6888
}
69-
info.Main = Module{
70-
Path: elem[0],
71-
Version: elem[1],
72-
Sum: elem[2],
73-
}
7489
case strings.HasPrefix(line, depLine):
7590
elem := strings.Split(line[len(depLine):], "\t")
76-
if len(elem) != 2 && len(elem) != 3 {
91+
last = new(Module)
92+
info.Deps = append(info.Deps, last)
93+
*last, ok = readEntryFirstLine(elem)
94+
if !ok {
7795
return nil, false
7896
}
79-
sum := ""
80-
if len(elem) == 3 {
81-
sum = elem[2]
82-
}
83-
info.Deps = append(info.Deps, &Module{
84-
Path: elem[0],
85-
Version: elem[1],
86-
Sum: sum,
87-
})
8897
case strings.HasPrefix(line, repLine):
8998
elem := strings.Split(line[len(repLine):], "\t")
9099
if len(elem) != 3 {
91100
return nil, false
92101
}
93-
last := len(info.Deps) - 1
94-
if last < 0 {
102+
if last == nil {
95103
return nil, false
96104
}
97-
info.Deps[last].Replace = &Module{
105+
last.Replace = &Module{
98106
Path: elem[0],
99107
Version: elem[1],
100108
Sum: elem[2],
101109
}
110+
last = nil
102111
}
103112
}
104113
return info, true

0 commit comments

Comments
 (0)