Skip to content

Commit 9563af6

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/mcp: include module paths in workspace summaries
When working on modules with a /vN suffix, module paths help the model produce accurate import paths. For golang/go#73580 Change-Id: I979e933ac8bfcfa5133643c3551a5777eb751747 Reviewed-on: https://go-review.googlesource.com/c/tools/+/687295 Reviewed-by: Madeline Kalil <mkalil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Findley <rfindley@google.com>
1 parent 88a4eb3 commit 9563af6

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

gopls/internal/mcp/workspace.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
"bytes"
99
"context"
1010
"fmt"
11+
"io"
1112
"strings"
1213

1314
"slices"
1415

1516
"golang.org/x/tools/gopls/internal/cache"
17+
"golang.org/x/tools/gopls/internal/protocol"
1618
"golang.org/x/tools/gopls/internal/util/immutable"
1719
"golang.org/x/tools/internal/mcp"
1820
)
@@ -60,15 +62,11 @@ func (h *handler) workspaceHandler(ctx context.Context, _ *mcp.ServerSession, _
6062

6163
case cache.GoModView:
6264
fmt.Fprintf(&summary, "The `%s` directory uses Go modules, with the following main modules:\n", dir)
63-
for _, m := range v.ModFiles() {
64-
fmt.Fprintf(&summary, "\t%s\n", m.Path())
65-
}
65+
summarizeModFiles(ctx, &summary, snapshot)
6666

6767
case cache.GoWorkView:
6868
fmt.Fprintf(&summary, "The `%s` directory is in the go workspace defined by `%s`, with the following main modules:\n", dir, v.GoWork().Path())
69-
for _, m := range v.ModFiles() {
70-
fmt.Fprintf(&summary, "\t%s\n", m.Path())
71-
}
69+
summarizeModFiles(ctx, &summary, snapshot)
7270

7371
case cache.AdHocView:
7472
fmt.Fprintf(&summary, "The `%s` directory is an ad-hoc Go package, not in a Go module.\n", dir)
@@ -85,6 +83,33 @@ func (h *handler) workspaceHandler(ctx context.Context, _ *mcp.ServerSession, _
8583
return textResult(summary.String()), nil
8684
}
8785

86+
func summarizeModFiles(ctx context.Context, w io.Writer, snapshot *cache.Snapshot) {
87+
v := snapshot.View()
88+
for _, m := range v.ModFiles() {
89+
if modPath, err := modulePath(ctx, snapshot, m); err != nil {
90+
// Fall back on just the go.mod file.
91+
fmt.Fprintf(w, "\t%s\n", m.Path())
92+
} else {
93+
fmt.Fprintf(w, "\t%s (module %s)\n", m.Path(), modPath)
94+
}
95+
}
96+
}
97+
98+
func modulePath(ctx context.Context, snapshot *cache.Snapshot, uri protocol.DocumentURI) (string, error) {
99+
fh, err := snapshot.ReadFile(ctx, uri)
100+
if err != nil {
101+
return "", fmt.Errorf("Reading %s: %v", uri, err)
102+
}
103+
pmf, err := snapshot.ParseMod(ctx, fh)
104+
if err != nil {
105+
return "", fmt.Errorf("parsing modfile: %v", err)
106+
}
107+
if pmf.File == nil || pmf.File.Module == nil {
108+
return "", fmt.Errorf("malformed modfile")
109+
}
110+
return pmf.File.Module.Mod.Path, nil
111+
}
112+
88113
func packageSummaries(snapshot *cache.Snapshot, pkgs immutable.Map[cache.PackageID, cache.PackagePath]) []string {
89114
var summaries []string
90115
for id := range pkgs.All() {

gopls/internal/test/marker/testdata/mcptools/workspace.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ package b
3232

3333
-- @workspace --
3434
The `$WORKDIR` directory is in the go workspace defined by `$WORKDIR/go.work`, with the following main modules:
35-
$WORKDIR/a/go.mod
36-
$WORKDIR/b/go.mod
35+
$WORKDIR/a/go.mod (module example.com/a)
36+
$WORKDIR/b/go.mod (module example.com/b)
3737

0 commit comments

Comments
 (0)