Skip to content

Commit be0c89d

Browse files
committed
internal/lsp/fake: check for file changes after running the Go command
Our fake.Workdir generates synthetic file events for "watched" files when using its file API, but we have no such hooks for file changes originating from an external process, in this case the go command. Previously, we detected a file event by checking go command logs to see if a go.mod file was created. This is bound to be fragile, so we replace this with a helper function that walks the working directory looking for changes. Change-Id: I2c2176099b97f3a3b25f4aed2b18de13ae05544a Reviewed-on: https://go-review.googlesource.com/c/tools/+/232637 Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 88e38c1 commit be0c89d

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

internal/lsp/fake/sandbox.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"strings"
1414

1515
"golang.org/x/tools/internal/gocommand"
16-
"golang.org/x/tools/internal/lsp/protocol"
1716
"golang.org/x/tools/txtar"
1817
)
1918

@@ -121,21 +120,14 @@ func (sb *Sandbox) RunGoCommand(ctx context.Context, verb string, args ...string
121120
Env: sb.GoEnv(),
122121
}
123122
gocmdRunner := &gocommand.Runner{}
124-
_, stderr, _, err := gocmdRunner.RunRaw(ctx, inv)
123+
_, _, _, err := gocmdRunner.RunRaw(ctx, inv)
125124
if err != nil {
126125
return err
127126
}
128-
// Hardcoded "file watcher": If the command executed was "go mod init",
129-
// send a file creation event for a go.mod in the working directory.
130-
if strings.HasPrefix(stderr.String(), "go: creating new go.mod") {
131-
modpath := filepath.Join(sb.Workdir.workdir, "go.mod")
132-
sb.Workdir.sendEvents(ctx, []FileEvent{{
133-
Path: modpath,
134-
ProtocolEvent: protocol.FileEvent{
135-
URI: toURI(modpath),
136-
Type: protocol.Created,
137-
},
138-
}})
127+
// Since running a go command may result in changes to workspace files,
128+
// check if we need to send any any "watched" file events.
129+
if err := sb.Workdir.CheckForFileChanges(ctx); err != nil {
130+
return fmt.Errorf("checking for file changes: %w", err)
139131
}
140132
return nil
141133
}

0 commit comments

Comments
 (0)