Skip to content

Commit

Permalink
gopls: remove the workspace_metadata command
Browse files Browse the repository at this point in the history
Remove the workspace_metadata command, as VS Code no longer needs this
to run workspace commands (it can use go.work instead).

Updates golang/go#44696

Change-Id: Ife579a15e64969c4301e4508e18b7c8a8b633b9f
Reviewed-on: https://go-review.googlesource.com/c/tools/+/382235
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
findleyr committed Feb 4, 2022
1 parent 25d2ab2 commit 3e30e21
Show file tree
Hide file tree
Showing 14 changed files with 12 additions and 230 deletions.
17 changes: 0 additions & 17 deletions gopls/doc/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,21 +350,4 @@ Args:
}
```

### **Query workspace metadata**
Identifier: `gopls.workspace_metadata`

Query the server for information about active workspaces.

Result:

```
{
// All workspaces for this session.
"Workspaces": []{
"Name": string,
"ModuleDir": string,
},
}
```

<!-- END Commands: DO NOT MANUALLY EDIT THIS SECTION -->
77 changes: 0 additions & 77 deletions gopls/internal/regtest/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
package workspace

import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"sort"
"strings"
Expand All @@ -17,7 +15,6 @@ import (
. "golang.org/x/tools/internal/lsp/regtest"
"golang.org/x/tools/internal/lsp/source"

"golang.org/x/tools/internal/lsp/command"
"golang.org/x/tools/internal/lsp/fake"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/testenv"
Expand Down Expand Up @@ -852,80 +849,6 @@ func main() {
})
}

func TestWorkspaceDirAccess(t *testing.T) {
const multiModule = `
-- moda/a/go.mod --
module a.com
go 1.15
-- moda/a/a.go --
package main
func main() {
fmt.Println("Hello")
}
-- modb/go.mod --
module b.com
go 1.16
-- modb/b/b.go --
package main
func main() {
fmt.Println("World")
}
`
WithOptions(
Modes(Experimental),
SendPID(),
).Run(t, multiModule, func(t *testing.T, env *Env) {
params := &protocol.ExecuteCommandParams{
Command: command.WorkspaceMetadata.ID(),
Arguments: []json.RawMessage{json.RawMessage("{}")},
}
var result command.WorkspaceMetadataResult
env.ExecuteCommand(params, &result)

if n := len(result.Workspaces); n != 1 {
env.T.Fatalf("got %d workspaces, want 1", n)
}
// Don't factor this out of Server.addFolders. vscode-go expects this
// directory.
modPath := filepath.Join(result.Workspaces[0].ModuleDir, "go.mod")
gotb, err := ioutil.ReadFile(modPath)
if err != nil {
t.Fatalf("reading expected workspace modfile: %v", err)
}
got := string(gotb)
for _, want := range []string{"go 1.16", "a.com v1.9999999.0-goplsworkspace", "b.com v1.9999999.0-goplsworkspace"} {
if !strings.Contains(got, want) {
// want before got here, since the go.mod is multi-line
t.Fatalf("workspace go.mod missing %q. got:\n%s", want, got)
}
}
workdir := env.Sandbox.Workdir.RootURI().SpanURI().Filename()
env.WriteWorkspaceFile("gopls.mod", fmt.Sprintf(`
module gopls-workspace
require (
a.com v1.9999999.0-goplsworkspace
)
replace a.com => %s/moda/a
`, workdir))
env.Await(env.DoneWithChangeWatchedFiles())
gotb, err = ioutil.ReadFile(modPath)
if err != nil {
t.Fatalf("reading expected workspace modfile: %v", err)
}
got = string(gotb)
want := "b.com v1.9999999.0-goplsworkspace"
if strings.Contains(got, want) {
t.Fatalf("workspace go.mod contains unexpected %q. got:\n%s", want, got)
}
})
}

func TestDirectoryFiltersLoads(t *testing.T) {
// exclude, and its error, should be excluded from the workspace.
const files = `
Expand Down
14 changes: 4 additions & 10 deletions internal/lsp/cache/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ func (s *Session) Cache() interface{} {
return s.cache
}

func (s *Session) NewView(ctx context.Context, name string, folder, tempWorkspace span.URI, options *source.Options) (source.View, source.Snapshot, func(), error) {
func (s *Session) NewView(ctx context.Context, name string, folder span.URI, options *source.Options) (source.View, source.Snapshot, func(), error) {
s.viewMu.Lock()
defer s.viewMu.Unlock()
for _, view := range s.views {
if span.CompareURI(view.folder, folder) == 0 {
return nil, nil, nil, source.ErrViewExists
}
}
view, snapshot, release, err := s.createView(ctx, name, folder, tempWorkspace, options, 0)
view, snapshot, release, err := s.createView(ctx, name, folder, options, 0)
if err != nil {
return nil, nil, func() {}, err
}
Expand All @@ -173,7 +173,7 @@ func (s *Session) NewView(ctx context.Context, name string, folder, tempWorkspac
return view, snapshot, release, nil
}

func (s *Session) createView(ctx context.Context, name string, folder, tempWorkspace span.URI, options *source.Options, snapshotID uint64) (*View, *snapshot, func(), error) {
func (s *Session) createView(ctx context.Context, name string, folder span.URI, options *source.Options, snapshotID uint64) (*View, *snapshot, func(), error) {
index := atomic.AddInt64(&viewIndex, 1)

if s.cache.options != nil {
Expand Down Expand Up @@ -218,7 +218,6 @@ func (s *Session) createView(ctx context.Context, name string, folder, tempWorks
filesByBase: map[string][]*fileBase{},
rootURI: root,
workspaceInformation: *ws,
tempWorkspace: tempWorkspace,
}
v.importsState = &importsState{
ctx: backgroundCtx,
Expand Down Expand Up @@ -257,11 +256,6 @@ func (s *Session) createView(ctx context.Context, name string, folder, tempWorks
go func() {
defer release()
snapshot.initialize(initCtx, true)
// Ensure that the view workspace is written at least once following
// initialization.
if err := v.updateWorkspace(initCtx); err != nil {
event.Error(ctx, "copying workspace dir", err)
}
}()
return v, snapshot, snapshot.generation.Acquire(), nil
}
Expand Down Expand Up @@ -381,7 +375,7 @@ func (s *Session) updateView(ctx context.Context, view *View, options *source.Op
return nil, err
}

v, _, release, err := s.createView(ctx, view.name, view.folder, view.tempWorkspace, options, snapshotID)
v, _, release, err := s.createView(ctx, view.name, view.folder, options, snapshotID)
release()

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ func (ac *unappliedChanges) GetFile(ctx context.Context, uri span.URI) (source.F
return ac.originalSnapshot.GetFile(ctx, uri)
}

func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileChange, forceReloadMetadata bool) (*snapshot, bool) {
func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileChange, forceReloadMetadata bool) *snapshot {
var vendorChanged bool
newWorkspace, workspaceChanged, workspaceReload := s.workspace.invalidate(ctx, changes, &unappliedChanges{
originalSnapshot: s,
Expand Down Expand Up @@ -2044,7 +2044,7 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
result.initializeOnce = &sync.Once{}
}
}
return result, workspaceChanged
return result
}

// guessPackageIDsForURI returns all packages related to uri. If we haven't
Expand Down
61 changes: 1 addition & 60 deletions internal/lsp/cache/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ type View struct {
// workspaceInformation tracks various details about this view's
// environment variables, go version, and use of modules.
workspaceInformation

// tempWorkspace is a temporary directory dedicated to holding the latest
// version of the workspace go.mod file. (TODO: also go.sum file)
tempWorkspace span.URI
}

type workspaceInformation struct {
Expand Down Expand Up @@ -231,10 +227,6 @@ func (v *View) Folder() span.URI {
return v.folder
}

func (v *View) TempWorkspace() span.URI {
return v.tempWorkspace
}

func (v *View) Options() *source.Options {
v.optionsMu.Lock()
defer v.optionsMu.Unlock()
Expand Down Expand Up @@ -734,63 +726,12 @@ func (v *View) invalidateContent(ctx context.Context, changes map[span.URI]*file

oldSnapshot := v.snapshot

var workspaceChanged bool
v.snapshot, workspaceChanged = oldSnapshot.clone(ctx, v.baseCtx, changes, forceReloadMetadata)
if workspaceChanged {
if err := v.updateWorkspaceLocked(ctx); err != nil {
event.Error(ctx, "copying workspace dir", err)
}
}
v.snapshot = oldSnapshot.clone(ctx, v.baseCtx, changes, forceReloadMetadata)
go oldSnapshot.generation.Destroy("View.invalidateContent")

return v.snapshot, v.snapshot.generation.Acquire()
}

func (v *View) updateWorkspace(ctx context.Context) error {
if v.tempWorkspace == "" {
return nil
}
v.snapshotMu.Lock()
defer v.snapshotMu.Unlock()
return v.updateWorkspaceLocked(ctx)
}

// updateWorkspaceLocked should only be called when v.snapshotMu is held. It
// guarantees that workspace module content will be copied to v.tempWorkace at
// some point in the future. We do not guarantee that the temp workspace sees
// all changes to the workspace module, only that it is eventually consistent
// with the workspace module of the latest snapshot.
func (v *View) updateWorkspaceLocked(ctx context.Context) error {
if v.snapshot == nil {
return errors.New("view is shutting down")
}

release := v.snapshot.generation.Acquire()
defer release()
src, err := v.snapshot.getWorkspaceDir(ctx)
if err != nil {
return err
}
for _, name := range []string{"go.mod", "go.sum"} {
srcname := filepath.Join(src.Filename(), name)
srcf, err := os.Open(srcname)
if err != nil {
return errors.Errorf("opening snapshot %s: %w", name, err)
}
defer srcf.Close()
dstname := filepath.Join(v.tempWorkspace.Filename(), name)
dstf, err := os.Create(dstname)
if err != nil {
return errors.Errorf("truncating view %s: %w", name, err)
}
defer dstf.Close()
if _, err := io.Copy(dstf, srcf); err != nil {
return errors.Errorf("copying %s: %w", name, err)
}
}
return nil
}

func (s *Session) getWorkspaceInformation(ctx context.Context, folder span.URI, options *source.Options) (*workspaceInformation, error) {
if err := checkPathCase(folder.Filename()); err != nil {
return nil, errors.Errorf("invalid workspace folder path: %w; check that the casing of the configured workspace folder path agrees with the casing reported by the operating system", err)
Expand Down
11 changes: 0 additions & 11 deletions internal/lsp/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,17 +701,6 @@ func (c *commandHandler) AddImport(ctx context.Context, args command.AddImportAr
})
}

func (c *commandHandler) WorkspaceMetadata(ctx context.Context) (command.WorkspaceMetadataResult, error) {
var result command.WorkspaceMetadataResult
for _, view := range c.s.session.Views() {
result.Workspaces = append(result.Workspaces, command.Workspace{
Name: view.Name(),
ModuleDir: view.TempWorkspace().Filename(),
})
}
return result, nil
}

func (c *commandHandler) StartDebugging(ctx context.Context, args command.DebuggingArgs) (result command.DebuggingResult, _ error) {
addr := args.Addr
if addr == "" {
Expand Down
16 changes: 0 additions & 16 deletions internal/lsp/command/command_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions internal/lsp/command/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,6 @@ type Interface interface {
// themselves.
AddImport(context.Context, AddImportArgs) error

// WorkspaceMetadata: Query workspace metadata
//
// Query the server for information about active workspaces.
WorkspaceMetadata(context.Context) (WorkspaceMetadataResult, error)

// StartDebugging: Start the gopls debug server
//
// Start the gopls debug server if it isn't running, and return the debug
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/lsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func testLSP(t *testing.T, datum *tests.Data) {
tests.DefaultOptions(options)
session.SetOptions(options)
options.SetEnvSlice(datum.Config.Env)
view, snapshot, release, err := session.NewView(ctx, datum.Config.Dir, span.URIFromPath(datum.Config.Dir), "", options)
view, snapshot, release, err := session.NewView(ctx, datum.Config.Dir, span.URIFromPath(datum.Config.Dir), options)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lsp/mod/mod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestModfileRemainsUnchanged(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, _, release, err := session.NewView(ctx, "diagnostics_test", span.URIFromPath(folder), "", options)
_, _, release, err := session.NewView(ctx, "diagnostics_test", span.URIFromPath(folder), options)
release()
if err != nil {
t.Fatal(err)
Expand Down
6 changes: 0 additions & 6 deletions internal/lsp/source/api_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/lsp/source/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func testSource(t *testing.T, datum *tests.Data) {
options := source.DefaultOptions().Clone()
tests.DefaultOptions(options)
options.SetEnvSlice(datum.Config.Env)
view, _, release, err := session.NewView(ctx, "source_test", span.URIFromPath(datum.Config.Dir), "", options)
view, _, release, err := session.NewView(ctx, "source_test", span.URIFromPath(datum.Config.Dir), options)
release()
if err != nil {
t.Fatal(err)
Expand Down
Loading

0 comments on commit 3e30e21

Please sign in to comment.