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