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>
diff --git a/gopls/internal/server/general.go b/gopls/internal/server/general.go
index 9f5e2b0..cdaee1b 100644
--- a/gopls/internal/server/general.go
+++ b/gopls/internal/server/general.go
@@ -473,7 +473,7 @@
return nil, fmt.Errorf("failed to get workspace configuration from client (%s): %v", folder, err)
}
- opts := opts.Clone()
+ opts = opts.Clone()
for _, config := range configs {
if err := s.handleOptionResults(ctx, settings.SetOptions(opts, config)); err != nil {
return nil, err
@@ -493,15 +493,23 @@
}, nil
}
+// fetchFolderOptions makes a workspace/configuration request for the given
+// folder, and populates options with the result.
+//
+// If folder is "", fetchFolderOptions makes an unscoped request.
func (s *server) fetchFolderOptions(ctx context.Context, folder protocol.DocumentURI) (*settings.Options, error) {
opts := s.Options()
if !opts.ConfigurationSupported {
return opts, nil
}
- scope := string(folder)
+ var scopeURI *string
+ if folder != "" {
+ scope := string(folder)
+ scopeURI = &scope
+ }
configs, err := s.client.Configuration(ctx, &protocol.ParamConfiguration{
Items: []protocol.ConfigurationItem{{
- ScopeURI: &scope,
+ ScopeURI: scopeURI,
Section: "gopls",
}},
},
diff --git a/gopls/internal/test/integration/fake/client.go b/gopls/internal/test/integration/fake/client.go
index 2859d9b..f940821 100644
--- a/gopls/internal/test/integration/fake/client.go
+++ b/gopls/internal/test/integration/fake/client.go
@@ -99,9 +99,12 @@
func (c *Client) Configuration(_ context.Context, p *protocol.ParamConfiguration) ([]interface{}, error) {
results := make([]interface{}, len(p.Items))
for i, item := range p.Items {
+ if item.ScopeURI != nil && *item.ScopeURI == "" {
+ return nil, fmt.Errorf(`malformed ScopeURI ""`)
+ }
if item.Section == "gopls" {
config := c.editor.Config()
- results[i] = makeSettings(c.editor.sandbox, config)
+ results[i] = makeSettings(c.editor.sandbox, config, item.ScopeURI)
}
}
return results, nil
diff --git a/gopls/internal/test/integration/fake/editor.go b/gopls/internal/test/integration/fake/editor.go
index e2eec07..7c15106 100644
--- a/gopls/internal/test/integration/fake/editor.go
+++ b/gopls/internal/test/integration/fake/editor.go
@@ -110,7 +110,13 @@
FileAssociations map[string]string
// Settings holds user-provided configuration for the LSP server.
- Settings map[string]interface{}
+ Settings map[string]any
+
+ // FolderSettings holds user-provided per-folder configuration, if any.
+ //
+ // It maps each folder (as a relative path to the sandbox workdir) to its
+ // configuration mapping (like Settings).
+ FolderSettings map[string]map[string]any
// CapabilitiesJSON holds JSON client capabilities to overlay over the
// editor's default client capabilities.
@@ -216,7 +222,7 @@
}
// makeSettings builds the settings map for use in LSP settings RPCs.
-func makeSettings(sandbox *Sandbox, config EditorConfig) map[string]interface{} {
+func makeSettings(sandbox *Sandbox, config EditorConfig, scopeURI *protocol.URI) map[string]any {
env := make(map[string]string)
for k, v := range sandbox.GoEnv() {
env[k] = v
@@ -229,7 +235,7 @@
env[k] = v
}
- settings := map[string]interface{}{
+ settings := map[string]any{
"env": env,
// Use verbose progress reporting so that integration tests can assert on
@@ -248,6 +254,28 @@
settings[k] = v
}
+ // If the server is requesting configuration for a specific scope, apply
+ // settings for the nearest folder that has customized settings, if any.
+ if scopeURI != nil {
+ var (
+ scopePath = protocol.DocumentURI(*scopeURI).Path()
+ closestDir string // longest dir with settings containing the scope, if any
+ closestSettings map[string]any // settings for that dir, if any
+ )
+ for relPath, settings := range config.FolderSettings {
+ dir := sandbox.Workdir.AbsPath(relPath)
+ if strings.HasPrefix(scopePath+string(filepath.Separator), dir+string(filepath.Separator)) && len(dir) > len(closestDir) {
+ closestDir = dir
+ closestSettings = settings
+ }
+ }
+ if closestSettings != nil {
+ for k, v := range closestSettings {
+ settings[k] = v
+ }
+ }
+ }
+
return settings
}
@@ -261,7 +289,7 @@
Version: "v1.0.0",
}
}
- params.InitializationOptions = makeSettings(e.sandbox, config)
+ params.InitializationOptions = makeSettings(e.sandbox, config, nil)
params.WorkspaceFolders = makeWorkspaceFolders(e.sandbox, config.WorkspaceFolders)
capabilities, err := clientCapabilities(config)
diff --git a/gopls/internal/test/integration/misc/configuration_test.go b/gopls/internal/test/integration/misc/configuration_test.go
index 2d39dc9..fff4576 100644
--- a/gopls/internal/test/integration/misc/configuration_test.go
+++ b/gopls/internal/test/integration/misc/configuration_test.go
@@ -49,6 +49,48 @@
})
}
+// Test that clients can configure per-workspace configuration, which is
+// queried via the scopeURI of a workspace/configuration request.
+// (this was broken in golang/go#65519).
+func TestWorkspaceConfiguration(t *testing.T) {
+ const files = `
+-- go.mod --
+module example.com/config
+
+go 1.18
+
+-- a/a.go --
+package a
+
+import "example.com/config/b"
+
+func _() {
+ _ = b.B{2}
+}
+
+-- b/b.go --
+package b
+
+type B struct {
+ F int
+}
+`
+
+ WithOptions(
+ WorkspaceFolders("a"),
+ FolderSettings(map[string]Settings{
+ "a": {
+ "analyses": map[string]bool{
+ "composites": false,
+ },
+ },
+ }),
+ ).Run(t, files, func(t *testing.T, env *Env) {
+ env.OpenFile("a/a.go")
+ env.AfterChange(NoDiagnostics())
+ })
+}
+
// TestMajorOptionsChange is like TestChangeConfiguration, but modifies an
// an open buffer before making a major (but inconsequential) change that
// causes gopls to recreate the view.
diff --git a/gopls/internal/test/integration/options.go b/gopls/internal/test/integration/options.go
index 274b9e4..a6c394e 100644
--- a/gopls/internal/test/integration/options.go
+++ b/gopls/internal/test/integration/options.go
@@ -104,11 +104,29 @@
// Use an empty non-nil slice to signal explicitly no folders.
relFolders = []string{}
}
+
return optionSetter(func(opts *runConfig) {
opts.editor.WorkspaceFolders = relFolders
})
}
+// FolderSettings defines per-folder workspace settings, keyed by relative path
+// to the folder.
+//
+// Use in conjunction with WorkspaceFolders to have different settings for
+// different folders.
+func FolderSettings(folderSettings map[string]Settings) RunOption {
+ // Re-use the Settings type, for symmetry, but translate back into maps for
+ // the editor config.
+ folders := make(map[string]map[string]any)
+ for k, v := range folderSettings {
+ folders[k] = v
+ }
+ return optionSetter(func(opts *runConfig) {
+ opts.editor.FolderSettings = folders
+ })
+}
+
// EnvVars sets environment variables for the LSP session. When applying these
// variables to the session, the special string $SANDBOX_WORKDIR is replaced by
// the absolute path to the sandbox working directory.
diff --git a/gopls/internal/test/marker/testdata/configuration/static.txt b/gopls/internal/test/marker/testdata/configuration/static.txt
new file mode 100644
index 0000000..c84b55d
--- /dev/null
+++ b/gopls/internal/test/marker/testdata/configuration/static.txt
@@ -0,0 +1,41 @@
+This test confirms that gopls honors configuration even if the client does not
+support dynamic configuration.
+
+-- capabilities.json --
+{
+ "configuration": false
+}
+
+-- settings.json --
+{
+ "usePlaceholders": true,
+ "analyses": {
+ "composites": false
+ }
+}
+
+-- go.mod --
+module example.com/config
+
+go 1.18
+
+-- a/a.go --
+package a
+
+import "example.com/config/b"
+
+func Identity[P ~int](p P) P { //@item(Identity, "Identity", "", "")
+ return p
+}
+
+func _() {
+ _ = b.B{2}
+ _ = Identi //@snippet(" //", Identity, "Identity(${1:p P})"), diag("Ident", re"(undefined|undeclared)")
+}
+
+-- b/b.go --
+package b
+
+type B struct {
+ F int
+}