@@ -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+
88113func packageSummaries (snapshot * cache.Snapshot , pkgs immutable.Map [cache.PackageID , cache.PackagePath ]) []string {
89114 var summaries []string
90115 for id := range pkgs .All () {
0 commit comments