Debian Codegraph @

gopls/internal/test/integration/workspace/std_test.go

raw symbols SCIP
1 // Copyright 2024 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package workspace
6
7 import (
8 "os"
9 "os/exec"
10 "path/filepath"
11 "runtime"
12 "strings"
13 "testing"
14
15 . "golang.org/x/tools/gopls/internal/test/integration"
16 )
17
18 func TestStdWorkspace(t *testing.T) {
19 // This test checks that we actually load workspace packages when opening
20 // GOROOT.
21 //
22 // In golang/go#65801, we failed to do this because go/packages returns nil
23 // Module for std and cmd.
24 //
25 // Because this test loads std as a workspace, it may be slow on smaller
26 // builders.
27 if testing.Short() {
28 t.Skip("skipping with -short: loads GOROOT")
29 }
30
31 // The test also fails on Windows because an absolute path does not match
32 // (likely a misspelling due to slashes).
33 // TODO(rfindley): investigate and fix this on windows.
34 if runtime.GOOS == "windows" {
35 t.Skip("skipping on windows: fails to misspelled paths")
36 }
37
38 // Query GOROOT. This is slightly more precise than e.g. runtime.GOROOT, as
39 // it queries the go command in the environment.
40 cmd := exec.Command("go", "env", "GOROOT")
41 // Run with GOTOOLCHAIN=local so as to not be affected by toolchain upgrades
42 // in the current directory (which is affected by gopls' go.mod file).
43 // This was golang/go#70187
44 cmd.Env = append(os.Environ(), "GOTOOLCHAIN=local")
45 goroot, err := cmd.Output()
46 if err != nil {
47 t.Fatal(err)
48 }
49 stdDir := filepath.Join(strings.TrimSpace(string(goroot)), "src")
50 WithOptions(
51 Modes(Default), // This test may be slow. No reason to run it multiple times.
52 WorkspaceFolders(stdDir),
53 ).Run(t, "", func(t *testing.T, env *Env) {
54 // Find parser.ParseFile. Query with `'` to get an exact match.
55 syms := env.Symbol("'go/parser.ParseFile")
56 if len(syms) != 1 {
57 t.Fatalf("got %d symbols, want exactly 1. Symbols:\n%v", len(syms), syms)
58 }
59 parserPath := syms[0].Location.URI.Path()
60 env.OpenFile(parserPath)
61
62 // Find the reference to ast.File from the signature of ParseFile. This
63 // helps guard against matching a comment.
64 astFile := env.RegexpSearch(parserPath, `func ParseFile\(.*ast\.(File)`)
65 refs := env.References(astFile)
66
67 // If we've successfully loaded workspace packages for std, we should find
68 // a reference in go/types.
69 foundGoTypesReference := false
70 for _, ref := range refs {
71 if strings.Contains(string(ref.URI), "go/types") {
72 foundGoTypesReference = true
73 }
74 }
75 if !foundGoTypesReference {
76 t.Errorf("references(ast.File) did not return a go/types reference. Refs:\n%v", refs)
77 }
78 })
79 }