mirror of https://github.com/golang/go.git
internal/lsp: allow for multiple ad-hoc packages in the workspace
Add the scope to the command-line-arguments package ID and path so that multiple command-line-arguments packages can coexist in the workspace. Fixes golang/go#47584 Change-Id: Icbfe90d67627f384c54f352e46270ab2bf4240bd Reviewed-on: https://go-review.googlesource.com/c/tools/+/341611 Trust: Rebecca Stambler <rstambler@golang.org> Run-TryBot: Rebecca Stambler <rstambler@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
15a2ab852e
commit
9f4956114f
|
|
@ -0,0 +1,44 @@
|
|||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package misc
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
. "golang.org/x/tools/internal/lsp/regtest"
|
||||
)
|
||||
|
||||
func TestMultipleAdHocPackages(t *testing.T) {
|
||||
Run(t, `
|
||||
-- a/a.go --
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fmt.Println("")
|
||||
}
|
||||
-- a/b.go --
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() () {
|
||||
fmt.Println("")
|
||||
}
|
||||
`, func(t *testing.T, env *Env) {
|
||||
env.OpenFile("a/a.go")
|
||||
if list := env.Completion("a/a.go", env.RegexpSearch("a/a.go", "Println")); list == nil || len(list.Items) == 0 {
|
||||
t.Fatal("expected completions, got none")
|
||||
}
|
||||
env.OpenFile("a/b.go")
|
||||
if list := env.Completion("a/b.go", env.RegexpSearch("a/b.go", "Println")); list == nil || len(list.Items) == 0 {
|
||||
t.Fatal("expected completions, got none")
|
||||
}
|
||||
if list := env.Completion("a/a.go", env.RegexpSearch("a/a.go", "Println")); list == nil || len(list.Items) == 0 {
|
||||
t.Fatal("expected completions, got none")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -205,7 +205,7 @@ func (s *snapshot) load(ctx context.Context, allowNetwork bool, scopes ...interf
|
|||
}
|
||||
// Set the metadata for this package.
|
||||
s.mu.Lock()
|
||||
m, err := s.setMetadataLocked(ctx, packagePath(pkg.PkgPath), pkg, cfg, map[packageID]struct{}{})
|
||||
m, err := s.setMetadataLocked(ctx, packagePath(pkg.PkgPath), pkg, cfg, query, map[packageID]struct{}{})
|
||||
s.mu.Unlock()
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
@ -403,8 +403,13 @@ func getWorkspaceDir(ctx context.Context, h *memoize.Handle, g *memoize.Generati
|
|||
// setMetadataLocked extracts metadata from pkg and records it in s. It
|
||||
// recurses through pkg.Imports to ensure that metadata exists for all
|
||||
// dependencies.
|
||||
func (s *snapshot) setMetadataLocked(ctx context.Context, pkgPath packagePath, pkg *packages.Package, cfg *packages.Config, seen map[packageID]struct{}) (*metadata, error) {
|
||||
func (s *snapshot) setMetadataLocked(ctx context.Context, pkgPath packagePath, pkg *packages.Package, cfg *packages.Config, query []string, seen map[packageID]struct{}) (*metadata, error) {
|
||||
id := packageID(pkg.ID)
|
||||
if source.IsCommandLineArguments(pkg.ID) {
|
||||
suffix := ":" + strings.Join(query, ",")
|
||||
id = packageID(string(id) + suffix)
|
||||
pkgPath = packagePath(string(pkgPath) + suffix)
|
||||
}
|
||||
if _, ok := seen[id]; ok {
|
||||
return nil, errors.Errorf("import cycle detected: %q", id)
|
||||
}
|
||||
|
|
@ -465,7 +470,7 @@ func (s *snapshot) setMetadataLocked(ctx context.Context, pkgPath packagePath, p
|
|||
continue
|
||||
}
|
||||
if s.noValidMetadataForIDLocked(importID) {
|
||||
if _, err := s.setMetadataLocked(ctx, importPkgPath, importPkg, cfg, copied); err != nil {
|
||||
if _, err := s.setMetadataLocked(ctx, importPkgPath, importPkg, cfg, query, copied); err != nil {
|
||||
event.Error(ctx, "error in dependency", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue