Skip to content

Commit

Permalink
gopls/internal/server: fix two bugs related to dynamic configuration
Browse files Browse the repository at this point in the history
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 <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
Auto-Submit: Robert Findley <[email protected]>
  • Loading branch information
findleyr authored and gopherbot committed Feb 12, 2024
1 parent 50b4f1b commit c5643e9
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 8 deletions.
14 changes: 11 additions & 3 deletions gopls/internal/server/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ func (s *server) newFolder(ctx context.Context, folder protocol.DocumentURI, nam
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
Expand All @@ -493,15 +493,23 @@ func (s *server) newFolder(ctx context.Context, folder protocol.DocumentURI, nam
}, 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",
}},
},
Expand Down
5 changes: 4 additions & 1 deletion gopls/internal/test/integration/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ func (c *Client) WorkspaceFolders(context.Context) ([]protocol.WorkspaceFolder,
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
Expand Down
36 changes: 32 additions & 4 deletions gopls/internal/test/integration/fake/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ type EditorConfig struct {
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.
Expand Down Expand Up @@ -216,7 +222,7 @@ func (e *Editor) Client() *Client {
}

// 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
Expand All @@ -229,7 +235,7 @@ func makeSettings(sandbox *Sandbox, config EditorConfig) map[string]interface{}
env[k] = v
}

settings := map[string]interface{}{
settings := map[string]any{
"env": env,

// Use verbose progress reporting so that integration tests can assert on
Expand All @@ -248,6 +254,28 @@ func makeSettings(sandbox *Sandbox, config EditorConfig) map[string]interface{}
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
}

Expand All @@ -261,7 +289,7 @@ func (e *Editor) initialize(ctx context.Context) error {
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)
Expand Down
42 changes: 42 additions & 0 deletions gopls/internal/test/integration/misc/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,48 @@ var FooErr = errors.New("foo")
})
}

// 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.
Expand Down
18 changes: 18 additions & 0 deletions gopls/internal/test/integration/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,29 @@ func WorkspaceFolders(relFolders ...string) RunOption {
// 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.
Expand Down
41 changes: 41 additions & 0 deletions gopls/internal/test/marker/testdata/configuration/static.txt
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c5643e9

Please sign in to comment.