@@ -11,6 +11,8 @@ import (
11
11
"io"
12
12
"os"
13
13
"path"
14
+ "path/filepath"
15
+ "strings"
14
16
"sync"
15
17
16
18
"golang.org/x/tools/internal/event"
@@ -46,6 +48,10 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitializ
46
48
if params .RootURI != "" && ! params .RootURI .SpanURI ().IsFile () {
47
49
return nil , fmt .Errorf ("unsupported URI scheme: %v (gopls only supports file URIs)" , params .RootURI )
48
50
}
51
+ if params .RootURI != "" {
52
+ s .rootURI = params .RootURI .SpanURI ()
53
+ }
54
+
49
55
for _ , folder := range params .WorkspaceFolders {
50
56
uri := span .URIFromURI (folder .URI )
51
57
if ! uri .IsFile () {
@@ -144,20 +150,6 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
144
150
options := s .session .Options ()
145
151
defer func () { s .session .SetOptions (options ) }()
146
152
147
- var registrations []protocol.Registration
148
- if options .ConfigurationSupported && options .DynamicConfigurationSupported {
149
- registrations = append (registrations ,
150
- protocol.Registration {
151
- ID : "workspace/didChangeConfiguration" ,
152
- Method : "workspace/didChangeConfiguration" ,
153
- },
154
- protocol.Registration {
155
- ID : "workspace/didChangeWorkspaceFolders" ,
156
- Method : "workspace/didChangeWorkspaceFolders" ,
157
- },
158
- )
159
- }
160
-
161
153
// TODO: this event logging may be unnecessary.
162
154
// The version info is included in the initialize response.
163
155
buf := & bytes.Buffer {}
@@ -169,9 +161,18 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
169
161
}
170
162
s .pendingFolders = nil
171
163
172
- if len ( registrations ) > 0 {
164
+ if options . ConfigurationSupported && options . DynamicConfigurationSupported {
173
165
if err := s .client .RegisterCapability (ctx , & protocol.RegistrationParams {
174
- Registrations : registrations ,
166
+ Registrations : []protocol.Registration {
167
+ {
168
+ ID : "workspace/didChangeConfiguration" ,
169
+ Method : "workspace/didChangeConfiguration" ,
170
+ },
171
+ {
172
+ ID : "workspace/didChangeWorkspaceFolders" ,
173
+ Method : "workspace/didChangeWorkspaceFolders" ,
174
+ },
175
+ },
175
176
}); err != nil {
176
177
return err
177
178
}
@@ -322,10 +323,25 @@ func (s *Server) registerWatchedDirectoriesLocked(ctx context.Context, dirs map[
322
323
for k := range s .watchedDirectories {
323
324
delete (s .watchedDirectories , k )
324
325
}
325
- var watchers []protocol.FileSystemWatcher
326
+ // Work-around microsoft/vscode#100870 by making sure that we are,
327
+ // at least, watching the user's entire workspace. This will still be
328
+ // applied to every folder in the workspace.
329
+ watchers := []protocol.FileSystemWatcher {{
330
+ GlobPattern : "**/*.{go,mod,sum}" ,
331
+ Kind : float64 (protocol .WatchChange + protocol .WatchDelete + protocol .WatchCreate ),
332
+ }}
326
333
for dir := range dirs {
334
+ filename := dir .Filename ()
335
+ // If the directory is within the root URI, we're already watching it
336
+ // via the relative path above.
337
+ if isSubdirectory (s .rootURI .Filename (), filename ) {
338
+ continue
339
+ }
340
+ // If microsoft/vscode#100870 is resolved before
341
+ // microsoft/vscode#104387, we will need a work-around for Windows
342
+ // drive letter casing.
327
343
watchers = append (watchers , protocol.FileSystemWatcher {
328
- GlobPattern : fmt .Sprintf ("%s/**/*.{go,mod,sum}" , dir ),
344
+ GlobPattern : fmt .Sprintf ("%s/**/*.{go,mod,sum}" , filename ),
329
345
Kind : float64 (protocol .WatchChange + protocol .WatchDelete + protocol .WatchCreate ),
330
346
})
331
347
}
@@ -348,6 +364,11 @@ func (s *Server) registerWatchedDirectoriesLocked(ctx context.Context, dirs map[
348
364
return nil
349
365
}
350
366
367
+ func isSubdirectory (root , leaf string ) bool {
368
+ rel , err := filepath .Rel (root , leaf )
369
+ return err == nil && ! strings .HasPrefix (rel , ".." )
370
+ }
371
+
351
372
func (s * Server ) fetchConfig (ctx context.Context , name string , folder span.URI , o * source.Options ) error {
352
373
if ! s .session .Options ().ConfigurationSupported {
353
374
return nil
0 commit comments