Skip to content

Commit 240a4dc

Browse files
committed
gopls/internal/lsp/cmd: add instrumentation for TestFix
To help pin down golang/go#59475, add some additional debug output. For golang/go#59475 Change-Id: Ibed0743bcc00a51c0fa56a15fb7fe1be88ec403c Reviewed-on: https://go-review.googlesource.com/c/tools/+/486799 Reviewed-by: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> Run-TryBot: Robert Findley <rfindley@google.com>
1 parent ed3ff1b commit 240a4dc

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

gopls/internal/lsp/cmd/suggested_fix.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"flag"
1010
"fmt"
1111
"io/ioutil"
12+
"log"
1213
"os"
1314

1415
"golang.org/x/tools/gopls/internal/lsp/protocol"
@@ -41,11 +42,16 @@ fix-flags:
4142
printFlagDefaults(f)
4243
}
4344

45+
const DebugSuggestedFixEnvVar = "_DEBUG_SUGGESTED_FIX"
46+
4447
// Run performs diagnostic checks on the file specified and either;
4548
// - if -w is specified, updates the file in place;
4649
// - if -d is specified, prints out unified diffs of the changes; or
4750
// - otherwise, prints the new versions to stdout.
4851
func (s *suggestedFix) Run(ctx context.Context, args ...string) error {
52+
// For debugging golang/go#59475, enable some additional output.
53+
var debug = os.Getenv(DebugSuggestedFixEnvVar) == "true"
54+
4955
if len(args) < 1 {
5056
return tool.CommandLineErrorf("fix expects at least 1 argument")
5157
}
@@ -84,6 +90,9 @@ func (s *suggestedFix) Run(ctx context.Context, args ...string) error {
8490
// LSP requires a slice, not a nil.
8591
file.diagnostics = []protocol.Diagnostic{}
8692
}
93+
if debug {
94+
log.Printf("file diagnostics: %#v", file.diagnostics)
95+
}
8796
p := protocol.CodeActionParams{
8897
TextDocument: protocol.TextDocumentIdentifier{
8998
URI: protocol.URIFromSpanURI(uri),
@@ -98,6 +107,10 @@ func (s *suggestedFix) Run(ctx context.Context, args ...string) error {
98107
if err != nil {
99108
return fmt.Errorf("%v: %v", from, err)
100109
}
110+
if debug {
111+
log.Printf("code actions: %#v", actions)
112+
}
113+
101114
var edits []protocol.TextEdit
102115
for _, a := range actions {
103116
if a.Command != nil {

gopls/internal/lsp/cmd/test/integration_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ func f() (int, string) { return 0, "" }
712712
res.checkExit(true)
713713
got := res.stdout
714714
if got != want {
715-
t.Errorf("fix: got <<%s>>, want <<%s>>", got, want)
715+
t.Errorf("fix: got <<%s>>, want <<%s>>\nstderr:\n%s", got, want, res.stderr)
716716
}
717717
}
718718
// TODO(adonovan): more tests:
@@ -800,16 +800,19 @@ func gopls(t *testing.T, dir string, args ...string) *result {
800800
t.Fatalf("dir is not absolute: %s", dir)
801801
}
802802

803-
cmd := exec.Command(os.Args[0], args...)
804-
cmd.Env = append(os.Environ(), "ENTRYPOINT=goplsMain")
805-
cmd.Dir = dir
806-
cmd.Stdout = new(bytes.Buffer)
807-
cmd.Stderr = new(bytes.Buffer)
803+
goplsCmd := exec.Command(os.Args[0], args...)
804+
goplsCmd.Env = append(os.Environ(),
805+
"ENTRYPOINT=goplsMain",
806+
fmt.Sprintf("%s=true", cmd.DebugSuggestedFixEnvVar),
807+
)
808+
goplsCmd.Dir = dir
809+
goplsCmd.Stdout = new(bytes.Buffer)
810+
goplsCmd.Stderr = new(bytes.Buffer)
808811

809-
cmdErr := cmd.Run()
812+
cmdErr := goplsCmd.Run()
810813

811-
stdout := strings.ReplaceAll(fmt.Sprint(cmd.Stdout), dir, ".")
812-
stderr := strings.ReplaceAll(fmt.Sprint(cmd.Stderr), dir, ".")
814+
stdout := strings.ReplaceAll(fmt.Sprint(goplsCmd.Stdout), dir, ".")
815+
stderr := strings.ReplaceAll(fmt.Sprint(goplsCmd.Stderr), dir, ".")
813816
exitcode := 0
814817
if cmdErr != nil {
815818
if exitErr, ok := cmdErr.(*exec.ExitError); ok {

0 commit comments

Comments
 (0)