1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
18 "golang.org/x/tools/internal/event"
19 "golang.org/x/tools/internal/jsonrpc2"
20 "golang.org/x/tools/internal/lsp/debug"
21 "golang.org/x/tools/internal/lsp/protocol"
22 "golang.org/x/tools/internal/lsp/source"
23 "golang.org/x/tools/internal/span"
24 errors "golang.org/x/xerrors"
27 func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitialize) (*protocol.InitializeResult, error) {
29 if s.state >= serverInitializing {
30 defer s.stateMu.Unlock()
31 return nil, errors.Errorf("%w: initialize called while server in %v state", jsonrpc2.ErrInvalidRequest, s.state)
33 s.state = serverInitializing
36 s.progress.supportsWorkDoneProgress = params.Capabilities.Window.WorkDoneProgress
38 options := s.session.Options()
39 defer func() { s.session.SetOptions(options) }()
41 if err := s.handleOptionResults(ctx, source.SetOptions(options, params.InitializationOptions)); err != nil {
44 options.ForClientCapabilities(params.Capabilities)
46 folders := params.WorkspaceFolders
47 if len(folders) == 0 {
48 if params.RootURI != "" {
49 folders = []protocol.WorkspaceFolder{{
50 URI: string(params.RootURI),
51 Name: path.Base(params.RootURI.SpanURI().Filename()),
55 for _, folder := range folders {
56 uri := span.URIFromURI(folder.URI)
60 s.pendingFolders = append(s.pendingFolders, folder)
62 // gopls only supports URIs with a file:// scheme, so if we have no
63 // workspace folders with a supported scheme, fail to initialize.
64 if len(folders) > 0 && len(s.pendingFolders) == 0 {
65 return nil, fmt.Errorf("unsupported URI schemes: %v (gopls only supports file URIs)", folders)
68 var codeActionProvider interface{} = true
69 if ca := params.Capabilities.TextDocument.CodeAction; len(ca.CodeActionLiteralSupport.CodeActionKind.ValueSet) > 0 {
70 // If the client has specified CodeActionLiteralSupport,
71 // send the code actions we support.
73 // Using CodeActionOptions is only valid if codeActionLiteralSupport is set.
74 codeActionProvider = &protocol.CodeActionOptions{
75 CodeActionKinds: s.getSupportedCodeActions(),
78 var renameOpts interface{} = true
79 if r := params.Capabilities.TextDocument.Rename; r.PrepareSupport {
80 renameOpts = protocol.RenameOptions{
81 PrepareProvider: r.PrepareSupport,
85 if st := params.Capabilities.TextDocument.SemanticTokens; st != nil {
86 rememberToks(st.TokenTypes, st.TokenModifiers)
89 goplsVer := &bytes.Buffer{}
90 debug.PrintVersionInfo(ctx, goplsVer, true, debug.PlainText)
92 return &protocol.InitializeResult{
93 Capabilities: protocol.ServerCapabilities{
94 CallHierarchyProvider: true,
95 CodeActionProvider: codeActionProvider,
96 CompletionProvider: protocol.CompletionOptions{
97 TriggerCharacters: []string{"."},
99 DefinitionProvider: true,
100 TypeDefinitionProvider: true,
101 ImplementationProvider: true,
102 DocumentFormattingProvider: true,
103 DocumentSymbolProvider: true,
104 WorkspaceSymbolProvider: true,
105 ExecuteCommandProvider: protocol.ExecuteCommandOptions{
106 Commands: options.SupportedCommands,
108 FoldingRangeProvider: true,
110 DocumentHighlightProvider: true,
111 DocumentLinkProvider: protocol.DocumentLinkOptions{},
112 ReferencesProvider: true,
113 RenameProvider: renameOpts,
114 SignatureHelpProvider: protocol.SignatureHelpOptions{
115 TriggerCharacters: []string{"(", ","},
117 TextDocumentSync: &protocol.TextDocumentSyncOptions{
118 Change: protocol.Incremental,
120 Save: protocol.SaveOptions{
124 Workspace: protocol.WorkspaceGn{
125 WorkspaceFolders: protocol.WorkspaceFoldersGn{
127 ChangeNotifications: "workspace/didChangeWorkspaceFolders",
132 Name string `json:"name"`
133 Version string `json:"version,omitempty"`
136 Version: goplsVer.String(),
141 func (s *Server) initialized(ctx context.Context, params *protocol.InitializedParams) error {
143 if s.state >= serverInitialized {
144 defer s.stateMu.Unlock()
145 return errors.Errorf("%w: initalized called while server in %v state", jsonrpc2.ErrInvalidRequest, s.state)
147 s.state = serverInitialized
150 for _, not := range s.notifications {
151 s.client.ShowMessage(ctx, not)
153 s.notifications = nil
155 options := s.session.Options()
156 defer func() { s.session.SetOptions(options) }()
158 if err := s.addFolders(ctx, s.pendingFolders); err != nil {
161 s.pendingFolders = nil
163 if options.ConfigurationSupported && options.DynamicConfigurationSupported {
164 registrations := []protocol.Registration{
166 ID: "workspace/didChangeConfiguration",
167 Method: "workspace/didChangeConfiguration",
170 ID: "workspace/didChangeWorkspaceFolders",
171 Method: "workspace/didChangeWorkspaceFolders",
174 if options.SemanticTokens {
175 registrations = append(registrations, semanticTokenRegistrations()...)
178 if err := s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
179 Registrations: registrations,
187 func (s *Server) addFolders(ctx context.Context, folders []protocol.WorkspaceFolder) error {
188 originalViews := len(s.session.Views())
189 viewErrors := make(map[span.URI]error)
191 var wg sync.WaitGroup
192 if s.session.Options().VerboseWorkDoneProgress {
193 work := s.progress.start(ctx, DiagnosticWorkTitle(FromInitialWorkspaceLoad), "Calculating diagnostics for initial workspace load...", nil, nil)
201 dirsToWatch := map[span.URI]struct{}{}
202 for _, folder := range folders {
203 uri := span.URIFromURI(folder.URI)
204 // Ignore non-file URIs.
208 work := s.progress.start(ctx, "Setting up workspace", "Loading packages...", nil, nil)
209 snapshot, release, err := s.addView(ctx, folder.Name, uri)
211 viewErrors[uri] = err
212 work.end(fmt.Sprintf("Error loading packages: %s", err))
215 var swg sync.WaitGroup
219 snapshot.AwaitInitialized(ctx)
220 work.end("Finished loading packages.")
223 for _, dir := range snapshot.WorkspaceDirectories(ctx) {
224 dirsToWatch[dir] = struct{}{}
227 // Print each view's environment.
228 buf := &bytes.Buffer{}
229 if err := snapshot.WriteEnv(ctx, buf); err != nil {
230 viewErrors[uri] = err
233 event.Log(ctx, buf.String())
235 // Diagnose the newly created view.
238 s.diagnoseDetached(snapshot)
244 // Register for file watching notifications, if they are supported.
245 s.watchedDirectoriesMu.Lock()
246 err := s.registerWatchedDirectoriesLocked(ctx, dirsToWatch)
247 s.watchedDirectoriesMu.Unlock()
251 if len(viewErrors) > 0 {
252 errMsg := fmt.Sprintf("Error loading workspace folders (expected %v, got %v)\n", len(folders), len(s.session.Views())-originalViews)
253 for uri, err := range viewErrors {
254 errMsg += fmt.Sprintf("failed to load view for %s: %v\n", uri, err)
256 return s.client.ShowMessage(ctx, &protocol.ShowMessageParams{
257 Type: protocol.Error,
264 // updateWatchedDirectories compares the current set of directories to watch
265 // with the previously registered set of directories. If the set of directories
266 // has changed, we unregister and re-register for file watching notifications.
267 // updatedSnapshots is the set of snapshots that have been updated.
268 func (s *Server) updateWatchedDirectories(ctx context.Context, updatedSnapshots map[source.View]source.Snapshot) error {
269 dirsToWatch := map[span.URI]struct{}{}
270 seenViews := map[source.View]struct{}{}
272 // Collect all of the workspace directories from the updated snapshots.
273 for _, snapshot := range updatedSnapshots {
274 seenViews[snapshot.View()] = struct{}{}
275 for _, dir := range snapshot.WorkspaceDirectories(ctx) {
276 dirsToWatch[dir] = struct{}{}
279 // Not all views were necessarily updated, so check the remaining views.
280 for _, view := range s.session.Views() {
281 if _, ok := seenViews[view]; ok {
284 snapshot, release := view.Snapshot(ctx)
285 for _, dir := range snapshot.WorkspaceDirectories(ctx) {
286 dirsToWatch[dir] = struct{}{}
291 s.watchedDirectoriesMu.Lock()
292 defer s.watchedDirectoriesMu.Unlock()
294 // Nothing to do if the set of workspace directories is unchanged.
295 if equalURISet(s.watchedDirectories, dirsToWatch) {
299 // If the set of directories to watch has changed, register the updates and
300 // unregister the previously watched directories. This ordering avoids a
301 // period where no files are being watched. Still, if a user makes on-disk
302 // changes before these updates are complete, we may miss them for the new
304 if s.watchRegistrationCount > 0 {
305 prevID := s.watchRegistrationCount - 1
306 if err := s.registerWatchedDirectoriesLocked(ctx, dirsToWatch); err != nil {
309 return s.client.UnregisterCapability(ctx, &protocol.UnregistrationParams{
310 Unregisterations: []protocol.Unregistration{{
311 ID: watchedFilesCapabilityID(prevID),
312 Method: "workspace/didChangeWatchedFiles",
319 func watchedFilesCapabilityID(id uint64) string {
320 return fmt.Sprintf("workspace/didChangeWatchedFiles-%d", id)
323 func equalURISet(m1, m2 map[span.URI]struct{}) bool {
324 if len(m1) != len(m2) {
336 // registerWatchedDirectoriesLocked sends the workspace/didChangeWatchedFiles
337 // registrations to the client and updates s.watchedDirectories.
338 func (s *Server) registerWatchedDirectoriesLocked(ctx context.Context, dirs map[span.URI]struct{}) error {
339 if !s.session.Options().DynamicWatchedFilesSupported {
342 for k := range s.watchedDirectories {
343 delete(s.watchedDirectories, k)
345 // Work-around microsoft/vscode#100870 by making sure that we are,
346 // at least, watching the user's entire workspace. This will still be
347 // applied to every folder in the workspace.
348 watchers := []protocol.FileSystemWatcher{{
349 GlobPattern: "**/*.{go,mod,sum}",
350 Kind: float64(protocol.WatchChange + protocol.WatchDelete + protocol.WatchCreate),
352 for dir := range dirs {
353 filename := dir.Filename()
354 // If the directory is within a workspace folder, we're already
355 // watching it via the relative path above.
356 for _, view := range s.session.Views() {
357 if isSubdirectory(view.Folder().Filename(), filename) {
361 // If microsoft/vscode#100870 is resolved before
362 // microsoft/vscode#104387, we will need a work-around for Windows
363 // drive letter casing.
364 watchers = append(watchers, protocol.FileSystemWatcher{
365 GlobPattern: fmt.Sprintf("%s/**/*.{go,mod,sum}", filename),
366 Kind: float64(protocol.WatchChange + protocol.WatchDelete + protocol.WatchCreate),
369 if err := s.client.RegisterCapability(ctx, &protocol.RegistrationParams{
370 Registrations: []protocol.Registration{{
371 ID: watchedFilesCapabilityID(s.watchRegistrationCount),
372 Method: "workspace/didChangeWatchedFiles",
373 RegisterOptions: protocol.DidChangeWatchedFilesRegistrationOptions{
380 s.watchRegistrationCount++
382 for dir := range dirs {
383 s.watchedDirectories[dir] = struct{}{}
388 func isSubdirectory(root, leaf string) bool {
389 rel, err := filepath.Rel(root, leaf)
390 return err == nil && !strings.HasPrefix(rel, "..")
393 func (s *Server) fetchConfig(ctx context.Context, name string, folder span.URI, o *source.Options) error {
394 if !s.session.Options().ConfigurationSupported {
397 v := protocol.ParamConfiguration{
398 ConfigurationParams: protocol.ConfigurationParams{
399 Items: []protocol.ConfigurationItem{{
400 ScopeURI: string(folder),
403 ScopeURI: string(folder),
404 Section: fmt.Sprintf("gopls-%s", name),
408 configs, err := s.client.Configuration(ctx, &v)
410 return fmt.Errorf("failed to get workspace configuration from client (%s): %v", folder, err)
412 for _, config := range configs {
413 if err := s.handleOptionResults(ctx, source.SetOptions(o, config)); err != nil {
420 func (s *Server) eventuallyShowMessage(ctx context.Context, msg *protocol.ShowMessageParams) error {
422 defer s.stateMu.Unlock()
423 if s.state == serverInitialized {
424 return s.client.ShowMessage(ctx, msg)
426 s.notifications = append(s.notifications, msg)
430 func (s *Server) handleOptionResults(ctx context.Context, results source.OptionResults) error {
431 for _, result := range results {
432 if result.Error != nil {
433 msg := &protocol.ShowMessageParams{
434 Type: protocol.Error,
435 Message: result.Error.Error(),
437 if err := s.eventuallyShowMessage(ctx, msg); err != nil {
441 switch result.State {
442 case source.OptionUnexpected:
443 msg := &protocol.ShowMessageParams{
444 Type: protocol.Error,
445 Message: fmt.Sprintf("unexpected gopls setting %q", result.Name),
447 if err := s.eventuallyShowMessage(ctx, msg); err != nil {
450 case source.OptionDeprecated:
451 msg := fmt.Sprintf("gopls setting %q is deprecated", result.Name)
452 if result.Replacement != "" {
453 msg = fmt.Sprintf("%s, use %q instead", msg, result.Replacement)
455 if err := s.eventuallyShowMessage(ctx, &protocol.ShowMessageParams{
456 Type: protocol.Warning,
466 // beginFileRequest checks preconditions for a file-oriented request and routes
468 // We don't want to return errors for benign conditions like wrong file type,
469 // so callers should do if !ok { return err } rather than if err != nil.
470 func (s *Server) beginFileRequest(ctx context.Context, pURI protocol.DocumentURI, expectKind source.FileKind) (source.Snapshot, source.VersionedFileHandle, bool, func(), error) {
471 uri := pURI.SpanURI()
473 // Not a file URI. Stop processing the request, but don't return an error.
474 return nil, nil, false, func() {}, nil
476 view, err := s.session.ViewOf(uri)
478 return nil, nil, false, func() {}, err
480 snapshot, release := view.Snapshot(ctx)
481 fh, err := snapshot.GetVersionedFile(ctx, uri)
484 return nil, nil, false, func() {}, err
486 if expectKind != source.UnknownKind && fh.Kind() != expectKind {
487 // Wrong kind of file. Nothing to do.
489 return nil, nil, false, func() {}, nil
491 return snapshot, fh, true, release, nil
494 func (s *Server) shutdown(ctx context.Context) error {
496 defer s.stateMu.Unlock()
497 if s.state < serverInitialized {
498 event.Log(ctx, "server shutdown without initialization")
500 if s.state != serverShutDown {
501 // drop all the active views
502 s.session.Shutdown(ctx)
503 s.state = serverShutDown
508 func (s *Server) exit(ctx context.Context) error {
510 defer s.stateMu.Unlock()
512 // TODO: We need a better way to find the conn close method.
513 s.client.(io.Closer).Close()
515 if s.state != serverShutDown {
516 // TODO: We should be able to do better than this.
519 // we don't terminate the process on a normal exit, we just allow it to
520 // close naturally if needed after the connection is closed.