golang.org/x/tools/gopls@v0.15.3/internal/test/integration/workspace/multi_folder_test.go (about)

     1  // Copyright 2023 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  	"testing"
     9  
    10  	. "golang.org/x/tools/gopls/internal/test/integration"
    11  )
    12  
    13  // TODO(rfindley): update the marker tests to support the concept of multiple
    14  // workspace folders, and move this there.
    15  func TestMultiView_Diagnostics(t *testing.T) {
    16  	// In the past, gopls would only diagnose one View at a time
    17  	// (the last to have changed).
    18  	//
    19  	// This test verifies that gopls can maintain diagnostics for multiple Views.
    20  	const files = `
    21  
    22  -- a/go.mod --
    23  module golang.org/lsptests/a
    24  
    25  go 1.20
    26  -- a/a.go --
    27  package a
    28  
    29  func _() {
    30  	x := 1 // unused
    31  }
    32  -- b/go.mod --
    33  module golang.org/lsptests/b
    34  
    35  go 1.20
    36  -- b/b.go --
    37  package b
    38  
    39  func _() {
    40  	y := 2 // unused
    41  }
    42  `
    43  
    44  	WithOptions(
    45  		WorkspaceFolders("a", "b"),
    46  	).Run(t, files, func(t *testing.T, env *Env) {
    47  		env.OnceMet(
    48  			InitialWorkspaceLoad,
    49  			Diagnostics(env.AtRegexp("a/a.go", "x")),
    50  			Diagnostics(env.AtRegexp("b/b.go", "y")),
    51  		)
    52  	})
    53  }
    54  
    55  func TestMultiView_LocalReplace(t *testing.T) {
    56  	// This is a regression test for #66145, where gopls attempted to load a
    57  	// package in a locally replaced module as a workspace package, resulting in
    58  	// spurious import diagnostics because the module graph had been pruned.
    59  
    60  	const proxy = `
    61  -- example.com/c@v1.2.3/go.mod --
    62  module example.com/c
    63  
    64  go 1.20
    65  
    66  -- example.com/c@v1.2.3/c.go --
    67  package c
    68  
    69  const C = 3
    70  
    71  `
    72  	// In the past, gopls would only diagnose one View at a time
    73  	// (the last to have changed).
    74  	//
    75  	// This test verifies that gopls can maintain diagnostics for multiple Views.
    76  	const files = `
    77  -- a/go.mod --
    78  module golang.org/lsptests/a
    79  
    80  go 1.20
    81  
    82  require golang.org/lsptests/b v1.2.3
    83  
    84  replace golang.org/lsptests/b => ../b
    85  
    86  -- a/a.go --
    87  package a
    88  
    89  import "golang.org/lsptests/b"
    90  
    91  const A = b.B - 1
    92  
    93  -- b/go.mod --
    94  module golang.org/lsptests/b
    95  
    96  go 1.20
    97  
    98  require example.com/c v1.2.3
    99  
   100  -- b/go.sum --
   101  example.com/c v1.2.3 h1:hsOPhoHQLZPEn7l3kNya3fR3SfqW0/rafZMP8ave6fg=
   102  example.com/c v1.2.3/go.mod h1:4uG6Y5qX88LrEd4KfRoiguHZIbdLKUEHD1wXqPyrHcA=
   103  -- b/b.go --
   104  package b
   105  
   106  const B = 2
   107  
   108  -- b/unrelated/u.go --
   109  package unrelated
   110  
   111  import "example.com/c"
   112  
   113  const U = c.C
   114  `
   115  
   116  	WithOptions(
   117  		WorkspaceFolders("a", "b"),
   118  		ProxyFiles(proxy),
   119  	).Run(t, files, func(t *testing.T, env *Env) {
   120  		// Opening unrelated first ensures that when we compute workspace packages
   121  		// for the "a" workspace, it includes the unrelated package, which will be
   122  		// unloadable from a as there is no a/go.sum.
   123  		env.OpenFile("b/unrelated/u.go")
   124  		env.AfterChange()
   125  		env.OpenFile("a/a.go")
   126  		env.AfterChange(NoDiagnostics())
   127  	})
   128  }