Skip to content

Commit c5643e9

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/server: fix two bugs related to dynamic configuration
Fix two bugs related to dynamic configuration, that combine to prevent several clients from correctly configuring gopls, as reported in golang/go#65519 (Eglot), slack (lsp-mode), and team chat (Helix). The first bug has existed ~forever: when we make a workspace/configuration request in response to a didChangeConfiguration notification, we attempt to fetch the "global" settings by passing "scopeURI": "". The LSP spec defines "scopeURI" as a nullable URI: https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#configurationItem Apparently, Javascript-based clients such as VS Code and coc.nvim (the two clients I regularly test with) will happily accept "" as a falsy value, and so this global query works. However, this query fails in Eglot (and likely other clients), causing didChangeConfiguration not to work. The second bug is new: When adding a new workspace folder we were failing to overwrite opts with the correct value (:= vs =, alas). This initial query had been masking the bug described above in Emacs, whereas in VS Code the (incorrectly) successful workspace/configuration request above masked the new bug. Since they both fail in Eglot, they are revealed. The fake editor is updated to reject the "" scope, highlighting the first bug. A new integration test is added to exercise the second bug, by way of a new integration test option to add per-folder configuration. Additionally, a marker test is added to exercise static configuration, which is when the client does not support the configuration capability at all. This wasn't actually broken, as first suspected, but the test is useful to include anyway, as we had no tests for this client behavior. Fixes golang/go#65519 Change-Id: Ie7170e3a26001546d4e334b83e6e73cd4ade10d8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/563475 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Robert Findley <rfindley@google.com>
1 parent 50b4f1b commit c5643e9

File tree

6 files changed

+148
-8
lines changed

6 files changed

+148
-8
lines changed

gopls/internal/server/general.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ func (s *server) newFolder(ctx context.Context, folder protocol.DocumentURI, nam
473473
return nil, fmt.Errorf("failed to get workspace configuration from client (%s): %v", folder, err)
474474
}
475475

476-
opts := opts.Clone()
476+
opts = opts.Clone()
477477
for _, config := range configs {
478478
if err := s.handleOptionResults(ctx, settings.SetOptions(opts, config)); err != nil {
479479
return nil, err
@@ -493,15 +493,23 @@ func (s *server) newFolder(ctx context.Context, folder protocol.DocumentURI, nam
493493
}, nil
494494
}
495495

496+
// fetchFolderOptions makes a workspace/configuration request for the given
497+
// folder, and populates options with the result.
498+
//
499+
// If folder is "", fetchFolderOptions makes an unscoped request.
496500
func (s *server) fetchFolderOptions(ctx context.Context, folder protocol.DocumentURI) (*settings.Options, error) {
497501
opts := s.Options()
498502
if !opts.ConfigurationSupported {
499503
return opts, nil
500504
}
501-
scope := string(folder)
505+
var scopeURI *string
506+
if folder != "" {
507+
scope := string(folder)
508+
scopeURI = &scope
509+
}
502510
configs, err := s.client.Configuration(ctx, &protocol.ParamConfiguration{
503511
Items: []protocol.ConfigurationItem{{
504-
ScopeURI: &scope,
512+
ScopeURI: scopeURI,
505513
Section: "gopls",
506514
}},
507515
},

gopls/internal/test/integration/fake/client.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ func (c *Client) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFolder,
9999
func (c *Client) Configuration(_ context.Context, p *protocol.ParamConfiguration) ([]interface{}, error) {
100100
results := make([]interface{}, len(p.Items))
101101
for i, item := range p.Items {
102+
if item.ScopeURI != nil && *item.ScopeURI == "" {
103+
return nil, fmt.Errorf(`malformed ScopeURI ""`)
104+
}
102105
if item.Section == "gopls" {
103106
config := c.editor.Config()
104-
results[i] = makeSettings(c.editor.sandbox, config)
107+
results[i] = makeSettings(c.editor.sandbox, config, item.ScopeURI)
105108
}
106109
}
107110
return results, nil

gopls/internal/test/integration/fake/editor.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ type EditorConfig struct {
110110
FileAssociations map[string]string
111111

112112
// Settings holds user-provided configuration for the LSP server.
113-
Settings map[string]interface{}
113+
Settings map[string]any
114+
115+
// FolderSettings holds user-provided per-folder configuration, if any.
116+
//
117+
// It maps each folder (as a relative path to the sandbox workdir) to its
118+
// configuration mapping (like Settings).
119+
FolderSettings map[string]map[string]any
114120

115121
// CapabilitiesJSON holds JSON client capabilities to overlay over the
116122
// editor's default client capabilities.
@@ -216,7 +222,7 @@ func (e *Editor) Client() *Client {
216222
}
217223

218224
// makeSettings builds the settings map for use in LSP settings RPCs.
219-
func makeSettings(sandbox *Sandbox, config EditorConfig) map[string]interface{} {
225+
func makeSettings(sandbox *Sandbox, config EditorConfig, scopeURI *protocol.URI) map[string]any {
220226
env := make(map[string]string)
221227
for k, v := range sandbox.GoEnv() {
222228
env[k] = v
@@ -229,7 +235,7 @@ func makeSettings(sandbox *Sandbox, config EditorConfig) map[string]interface{}
229235
env[k] = v
230236
}
231237

232-
settings := map[string]interface{}{
238+
settings := map[string]any{
233239
"env": env,
234240

235241
// Use verbose progress reporting so that integration tests can assert on
@@ -248,6 +254,28 @@ func makeSettings(sandbox *Sandbox, config EditorConfig) map[string]interface{}
248254
settings[k] = v
249255
}
250256

257+
// If the server is requesting configuration for a specific scope, apply
258+
// settings for the nearest folder that has customized settings, if any.
259+
if scopeURI != nil {
260+
var (
261+
scopePath = protocol.DocumentURI(*scopeURI).Path()
262+
closestDir string // longest dir with settings containing the scope, if any
263+
closestSettings map[string]any // settings for that dir, if any
264+
)
265+
for relPath, settings := range config.FolderSettings {
266+
dir := sandbox.Workdir.AbsPath(relPath)
267+
if strings.HasPrefix(scopePath+string(filepath.Separator), dir+string(filepath.Separator)) && len(dir) > len(closestDir) {
268+
closestDir = dir
269+
closestSettings = settings
270+
}
271+
}
272+
if closestSettings != nil {
273+
for k, v := range closestSettings {
274+
settings[k] = v
275+
}
276+
}
277+
}
278+
251279
return settings
252280
}
253281

@@ -261,7 +289,7 @@ func (e *Editor) initialize(ctx context.Context) error {
261289
Version: "v1.0.0",
262290
}
263291
}
264-
params.InitializationOptions = makeSettings(e.sandbox, config)
292+
params.InitializationOptions = makeSettings(e.sandbox, config, nil)
265293
params.WorkspaceFolders = makeWorkspaceFolders(e.sandbox, config.WorkspaceFolders)
266294

267295
capabilities, err := clientCapabilities(config)

gopls/internal/test/integration/misc/configuration_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,48 @@ var FooErr = errors.New("foo")
4949
})
5050
}
5151

52+
// Test that clients can configure per-workspace configuration, which is
53+
// queried via the scopeURI of a workspace/configuration request.
54+
// (this was broken in golang/go#65519).
55+
func TestWorkspaceConfiguration(t *testing.T) {
56+
const files = `
57+
-- go.mod --
58+
module example.com/config
59+
60+
go 1.18
61+
62+
-- a/a.go --
63+
package a
64+
65+
import "example.com/config/b"
66+
67+
func _() {
68+
_ = b.B{2}
69+
}
70+
71+
-- b/b.go --
72+
package b
73+
74+
type B struct {
75+
F int
76+
}
77+
`
78+
79+
WithOptions(
80+
WorkspaceFolders("a"),
81+
FolderSettings(map[string]Settings{
82+
"a": {
83+
"analyses": map[string]bool{
84+
"composites": false,
85+
},
86+
},
87+
}),
88+
).Run(t, files, func(t *testing.T, env *Env) {
89+
env.OpenFile("a/a.go")
90+
env.AfterChange(NoDiagnostics())
91+
})
92+
}
93+
5294
// TestMajorOptionsChange is like TestChangeConfiguration, but modifies an
5395
// an open buffer before making a major (but inconsequential) change that
5496
// causes gopls to recreate the view.

gopls/internal/test/integration/options.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,29 @@ func WorkspaceFolders(relFolders ...string) RunOption {
104104
// Use an empty non-nil slice to signal explicitly no folders.
105105
relFolders = []string{}
106106
}
107+
107108
return optionSetter(func(opts *runConfig) {
108109
opts.editor.WorkspaceFolders = relFolders
109110
})
110111
}
111112

113+
// FolderSettings defines per-folder workspace settings, keyed by relative path
114+
// to the folder.
115+
//
116+
// Use in conjunction with WorkspaceFolders to have different settings for
117+
// different folders.
118+
func FolderSettings(folderSettings map[string]Settings) RunOption {
119+
// Re-use the Settings type, for symmetry, but translate back into maps for
120+
// the editor config.
121+
folders := make(map[string]map[string]any)
122+
for k, v := range folderSettings {
123+
folders[k] = v
124+
}
125+
return optionSetter(func(opts *runConfig) {
126+
opts.editor.FolderSettings = folders
127+
})
128+
}
129+
112130
// EnvVars sets environment variables for the LSP session. When applying these
113131
// variables to the session, the special string $SANDBOX_WORKDIR is replaced by
114132
// the absolute path to the sandbox working directory.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
This test confirms that gopls honors configuration even if the client does not
2+
support dynamic configuration.
3+
4+
-- capabilities.json --
5+
{
6+
"configuration": false
7+
}
8+
9+
-- settings.json --
10+
{
11+
"usePlaceholders": true,
12+
"analyses": {
13+
"composites": false
14+
}
15+
}
16+
17+
-- go.mod --
18+
module example.com/config
19+
20+
go 1.18
21+
22+
-- a/a.go --
23+
package a
24+
25+
import "example.com/config/b"
26+
27+
func Identity[P ~int](p P) P { //@item(Identity, "Identity", "", "")
28+
return p
29+
}
30+
31+
func _() {
32+
_ = b.B{2}
33+
_ = Identi //@snippet(" //", Identity, "Identity(${1:p P})"), diag("Ident", re"(undefined|undeclared)")
34+
}
35+
36+
-- b/b.go --
37+
package b
38+
39+
type B struct {
40+
F int
41+
}

0 commit comments

Comments
 (0)