aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid Chase <drchase@google.com>2023-03-12 18:19:57 +0100
committerDavid Chase <drchase@google.com>2023-03-14 09:07:58 +0000
commita54fe8a270aef105dc5fbdca4eb90227587361e9 (patch)
treece51c009bfef03d214e5bbc22fd19f6aea59173d /src
parentdd009b2f1a3a781c9c23c852402deb1039513d04 (diff)
downloadgo-a54fe8a270aef105dc5fbdca4eb90227587361e9.tar.xz
cmd/go: restore 'extend path shortening to all paths in messages'
The problem requiring a revert was creation (somehow) of /-containing paths in compiler messages on Windows. The new code deals with this existing-but-unexpected behavior. original was CL 465805 revert was CL 473795 this is the original, plus a correction for unexpected paths on Windows. Change-Id: I786e875e704c2d7018c8248960f2ff7188cac3ef Reviewed-on: https://go-review.googlesource.com/c/go/+/474015 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: David Chase <drchase@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/go/internal/work/exec.go36
-rw-r--r--src/cmd/go/testdata/script/build_shorten_pkg.txt81
2 files changed, 112 insertions, 5 deletions
diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
index b22e8b6e95..e3cd9972ee 100644
--- a/src/cmd/go/internal/work/exec.go
+++ b/src/cmd/go/internal/work/exec.go
@@ -2239,13 +2239,39 @@ func (e *prefixSuffixError) ImportPath() string {
func formatOutput(workDir, dir, importPath, desc, out string) *prefixSuffixError {
prefix := "# " + desc
suffix := "\n" + out
- if reldir := base.ShortPath(dir); reldir != dir {
- suffix = strings.ReplaceAll(suffix, " "+dir, " "+reldir)
- suffix = strings.ReplaceAll(suffix, "\n"+dir, "\n"+reldir)
- suffix = strings.ReplaceAll(suffix, "\n\t"+dir, "\n\t"+reldir)
- }
+
suffix = strings.ReplaceAll(suffix, " "+workDir, " $WORK")
+ for {
+ // Note that dir starts out long, something like
+ // /foo/bar/baz/root/a
+ // The target string to be reduced is something like
+ // (blah-blah-blah) /foo/bar/baz/root/sibling/whatever.go:blah:blah
+ // /foo/bar/baz/root/a doesn't match /foo/bar/baz/root/sibling, but the prefix
+ // /foo/bar/baz/root does. And there may be other niblings sharing shorter
+ // prefixes, the only way to find them is to look.
+ // This doesn't always produce a relative path --
+ // /foo is shorter than ../../.., for example.
+ //
+ if reldir := base.ShortPath(dir); reldir != dir {
+ suffix = strings.ReplaceAll(suffix, " "+dir, " "+reldir)
+ suffix = strings.ReplaceAll(suffix, "\n"+dir, "\n"+reldir)
+ suffix = strings.ReplaceAll(suffix, "\n\t"+dir, "\n\t"+reldir)
+ if filepath.Separator == '\\' {
+ // Don't know why, sometimes this comes out with slashes, not backslashes.
+ wdir := strings.ReplaceAll(dir, "\\", "/")
+ suffix = strings.ReplaceAll(suffix, " "+wdir, " "+reldir)
+ suffix = strings.ReplaceAll(suffix, "\n"+wdir, "\n"+reldir)
+ suffix = strings.ReplaceAll(suffix, "\n\t"+wdir, "\n\t"+reldir)
+ }
+ }
+ dirP := filepath.Dir(dir)
+ if dir == dirP {
+ break
+ }
+ dir = dirP
+ }
+
return &prefixSuffixError{importPath: importPath, prefix: prefix, suffix: suffix}
}
diff --git a/src/cmd/go/testdata/script/build_shorten_pkg.txt b/src/cmd/go/testdata/script/build_shorten_pkg.txt
new file mode 100644
index 0000000000..38672b65df
--- /dev/null
+++ b/src/cmd/go/testdata/script/build_shorten_pkg.txt
@@ -0,0 +1,81 @@
+[short] skip
+
+# This test may go away when the loopvar experiment goes away.
+# Accurate reporting of notable loops in the presence of inlining
+# can create warnings in sibling directories, and it's nice if those
+# can be trimmed like subdirectory paths are.
+
+env GOEXPERIMENT=loopvar
+go build -gcflags=inlines/a=-d=loopvar=2 .
+stderr ^\.[\\/]b[\\/]b\.go:12:6:.*loop.inlined.into.a[\\/]a\.go
+stderr ^\.[\\/]b[\\/]b\.go:12:9:.*loop.inlined.into.a[\\/]a\.go
+
+-- go.mod --
+module inlines
+
+go 1.21
+-- a/a.go --
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package a
+
+import "inlines/b"
+
+func F() []*int {
+ var s []*int
+ for i := 0; i < 10; i++ {
+ s = append(s, &i)
+ }
+ return s
+}
+
+func Fb() []*int {
+ bf, _ := b.F()
+ return bf
+}
+-- b/b.go --
+package b
+
+var slice = []int{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}
+
+func F() ([]*int, []*int) {
+ return g()
+}
+
+func g() ([]*int, []*int) {
+ var s []*int
+ var t []*int
+ for i, j := range slice {
+ s = append(s, &i)
+ t = append(t, &j)
+ }
+ return s[:len(s)-1], t
+}
+-- main.go --
+package main
+
+import (
+ "fmt"
+ "inlines/a"
+ "inlines/b"
+)
+
+func sum(s []*int) int {
+ sum := 0
+ for _, pi := range s {
+ sum += *pi
+ }
+ return sum
+}
+
+func main() {
+ af := a.F()
+ bf, _ := b.F()
+ abf := a.Fb()
+
+ saf, sbf, sabf := sum(af), sum(bf), sum(abf)
+
+ fmt.Printf("af, bf, abf sums = %d, %d, %d\n", saf, sbf, sabf)
+}