Skip to content

Commit

Permalink
gopls/internal/regtest/misc: test Implementations + vendor
Browse files Browse the repository at this point in the history
This change adds a test for golang/go#56169, whose cause and fix
were the same as for golang/go#55995, which was fixed in CL 454315.

Updates golang/go#55995
Fixes golang/go#56169

Change-Id: I6d2cf964be77606de3e945c2e5ecdee82892ee99
Reviewed-on: https://go-review.googlesource.com/c/tools/+/454637
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
  • Loading branch information
adonovan committed Dec 2, 2022
1 parent f540ee6 commit 6002d6e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions gopls/internal/regtest/misc/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func TestGoToCrashingDefinition_Issue49223(t *testing.T) {
// TestVendoringInvalidatesMetadata ensures that gopls uses the
// correct metadata even after an external 'go mod vendor' command
// causes packages to move; see issue #55995.
// See also TestImplementationsInVendor, which tests the same fix.
func TestVendoringInvalidatesMetadata(t *testing.T) {
testenv.NeedsGo1Point(t, 14)
const proxy = `
Expand Down
87 changes: 87 additions & 0 deletions gopls/internal/regtest/misc/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ package misc

import (
"fmt"
"os"
"sort"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
"golang.org/x/tools/gopls/internal/lsp/protocol"
. "golang.org/x/tools/gopls/internal/lsp/regtest"
"golang.org/x/tools/internal/testenv"
)

func TestStdlibReferences(t *testing.T) {
Expand Down Expand Up @@ -288,3 +290,88 @@ func _() {
}
})
}

// This is a regression test for Issue #56169, in which interface
// implementations in vendored modules were not found. The actual fix
// was the same as for #55995; see TestVendoringInvalidatesMetadata.
func TestImplementationsInVendor(t *testing.T) {
testenv.NeedsGo1Point(t, 14)
const proxy = `
-- other.com/b@v1.0.0/go.mod --
module other.com/b
go 1.14
-- other.com/b@v1.0.0/b.go --
package b
type B int
func (B) F() {}
`
const src = `
-- go.mod --
module example.com/a
go 1.14
require other.com/b v1.0.0
-- go.sum --
other.com/b v1.0.0 h1:9WyCKS+BLAMRQM0CegP6zqP2beP+ShTbPaARpNY31II=
other.com/b v1.0.0/go.mod h1:TgHQFucl04oGT+vrUm/liAzukYHNxCwKNkQZEyn3m9g=
-- a.go --
package a
import "other.com/b"
type I interface { F() }
var _ b.B
`
WithOptions(
ProxyFiles(proxy),
Modes(Default), // fails in 'experimental' mode
).Run(t, src, func(t *testing.T, env *Env) {
// Enable to debug go.sum mismatch, which may appear as
// "module lookup disabled by GOPROXY=off", confusingly.
if false {
env.DumpGoSum(".")
}

checkVendor := func(locs []protocol.Location, wantVendor bool) {
if len(locs) != 1 {
t.Errorf("got %d locations, want 1", len(locs))
} else if strings.Contains(string(locs[0].URI), "/vendor/") != wantVendor {
t.Errorf("got location %s, wantVendor=%t", locs[0], wantVendor)
}
}

env.OpenFile("a.go")
refPos := env.RegexpSearch("a.go", "I") // find "I" reference

// Initially, a.I has one implementation b.B in
// the module cache, not the vendor tree.
checkVendor(env.Implementations("a.go", refPos), false)

// Run 'go mod vendor' outside the editor.
if err := env.Sandbox.RunGoCommand(env.Ctx, ".", "mod", []string{"vendor"}, true); err != nil {
t.Fatalf("go mod vendor: %v", err)
}

// Synchronize changes to watched files.
env.Await(env.DoneWithChangeWatchedFiles())

// Now, b.B is found in the vendor tree.
checkVendor(env.Implementations("a.go", refPos), true)

// Delete the vendor tree.
if err := os.RemoveAll(env.Sandbox.Workdir.AbsPath("vendor")); err != nil {
t.Fatal(err)
}
// Notify the server of the deletion.
if err := env.Sandbox.Workdir.CheckForFileChanges(env.Ctx); err != nil {
t.Fatal(err)
}

// Synchronize again.
env.Await(env.DoneWithChangeWatchedFiles())

// b.B is once again defined in the module cache.
checkVendor(env.Implementations("a.go", refPos), false)
})
}

0 comments on commit 6002d6e

Please sign in to comment.