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

     1  // Copyright 2022 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  	"fmt"
     9  	"path/filepath"
    10  	"testing"
    11  
    12  	. "golang.org/x/tools/gopls/internal/test/integration"
    13  )
    14  
    15  // Test that setting go.work via environment variables or settings works.
    16  func TestUseGoWorkOutsideTheWorkspace(t *testing.T) {
    17  	// As discussed in
    18  	// https://github.com/golang/go/issues/59458#issuecomment-1513794691, we must
    19  	// use \-separated paths in go.work use directives for this test to work
    20  	// correctly on windows.
    21  	var files = fmt.Sprintf(`
    22  -- work/a/go.mod --
    23  module a.com
    24  
    25  go 1.12
    26  -- work/a/a.go --
    27  package a
    28  -- work/b/go.mod --
    29  module b.com
    30  
    31  go 1.12
    32  -- work/b/b.go --
    33  package b
    34  
    35  func _() {
    36  	x := 1 // unused
    37  }
    38  -- other/c/go.mod --
    39  module c.com
    40  
    41  go 1.18
    42  -- other/c/c.go --
    43  package c
    44  -- config/go.work --
    45  go 1.18
    46  
    47  use (
    48  	%s
    49  	%s
    50  	%s
    51  )
    52  `,
    53  		filepath.Join("$SANDBOX_WORKDIR", "work", "a"),
    54  		filepath.Join("$SANDBOX_WORKDIR", "work", "b"),
    55  		filepath.Join("$SANDBOX_WORKDIR", "other", "c"),
    56  	)
    57  
    58  	WithOptions(
    59  		WorkspaceFolders("work"), // use a nested workspace dir, so that GOWORK is outside the workspace
    60  		EnvVars{"GOWORK": filepath.Join("$SANDBOX_WORKDIR", "config", "go.work")},
    61  	).Run(t, files, func(t *testing.T, env *Env) {
    62  		// When we have an explicit GOWORK set, we should get a file watch request.
    63  		env.OnceMet(
    64  			InitialWorkspaceLoad,
    65  			FileWatchMatching(`other`),
    66  			FileWatchMatching(`config.go\.work`),
    67  		)
    68  		env.Await(FileWatchMatching(`config.go\.work`))
    69  		// Even though work/b is not open, we should get its diagnostics as it is
    70  		// included in the workspace.
    71  		env.OpenFile("work/a/a.go")
    72  		env.AfterChange(
    73  			Diagnostics(env.AtRegexp("work/b/b.go", "x := 1"), WithMessage("not used")),
    74  		)
    75  	})
    76  }