Skip to content

Commit 60b6bcd

Browse files
golopotgopherbot
authored andcommitted
gopls/rename: include filename in error message
Error messages now display the filename along with the package name when a rename is blocked. Fixes golang/go#69563 Change-Id: I00277281a78134271f7a0a7e8497a5863e48b92d Reviewed-on: https://go-review.googlesource.com/c/tools/+/615686 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent 2848ab8 commit 60b6bcd

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

gopls/internal/golang/rename_check.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -866,9 +866,17 @@ func (r *renamer) satisfy() map[satisfy.Constraint]bool {
866866
//
867867
// Only proceed if all packages have no errors.
868868
if len(pkg.ParseErrors()) > 0 || len(pkg.TypeErrors()) > 0 {
869+
var filename string
870+
if len(pkg.ParseErrors()) > 0 {
871+
err := pkg.ParseErrors()[0][0]
872+
filename = filepath.Base(err.Pos.Filename)
873+
} else if len(pkg.TypeErrors()) > 0 {
874+
err := pkg.TypeErrors()[0]
875+
filename = filepath.Base(err.Fset.File(err.Pos).Name())
876+
}
869877
r.errorf(token.NoPos, // we don't have a position for this error.
870-
"renaming %q to %q not possible because %q has errors",
871-
r.from, r.to, pkg.Metadata().PkgPath)
878+
"renaming %q to %q not possible because %q in %q has errors",
879+
r.from, r.to, filename, pkg.Metadata().PkgPath)
872880
return nil
873881
}
874882
f.Find(pkg.TypesInfo(), pkg.Syntax())

gopls/internal/test/marker/testdata/rename/bad.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,19 @@ package bad
1111
type myStruct struct {
1212
}
1313

14-
func (s *myStruct) sFunc() bool { //@renameerr("sFunc", "rFunc", re"not possible")
14+
func (s *myStruct) sFunc() bool { //@renameerr("sFunc", "rFunc", "not possible because \"bad.go\" in \"golang.org/lsptests/bad\" has errors")
1515
return s.Bad //@diag("Bad", re"no field or method")
1616
}
1717

1818
-- bad_test.go --
1919
package bad
20+
21+
22+
-- badsyntax/badsyntax.go --
23+
package badsyntax
24+
25+
type S struct {}
26+
27+
func (s *S) sFunc() bool { //@renameerr("sFunc", "rFunc", "not possible because \"badsyntax.go\" in \"golang.org/lsptests/bad/badsyntax\" has errors")
28+
# //@diag("#", re"expected statement, found")
29+
}

0 commit comments

Comments
 (0)