diff --git a/gopls/internal/lsp/cache/load.go b/gopls/internal/lsp/cache/load.go index 078f7cb12f7..a932567b72e 100644 --- a/gopls/internal/lsp/cache/load.go +++ b/gopls/internal/lsp/cache/load.go @@ -93,7 +93,7 @@ func (s *Snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc case viewLoadScope: // If we are outside of GOPATH, a module, or some other known // build system, don't load subdirectories. - if !s.validBuildConfiguration() { + if s.view.typ == AdHocView { query = append(query, "./") } else { query = append(query, "./...") @@ -329,22 +329,13 @@ func (s *Snapshot) workspaceLayoutError(ctx context.Context) (error, []*Diagnost // If the snapshot does not have a valid build configuration, it may be // that the user has opened a directory that contains multiple modules. // Check for that an warn about it. - if !s.validBuildConfiguration() { - var msg string - if s.view.folder.Env.GoVersion >= 18 { - msg = `gopls was not able to find modules in your workspace. + if s.view.typ == AdHocView { + msg := `gopls was not able to find modules in your workspace. When outside of GOPATH, gopls needs to know which modules you are working on. You can fix this by opening your workspace to a folder inside a Go module, or by using a go.work file to specify multiple modules. See the documentation for more information on setting up your workspace: https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.` - } else { - msg = `gopls requires a module at the root of your workspace. -You can work with multiple modules by upgrading to Go 1.18 or later, and using -go workspaces (go.work files). -See the documentation for more information on setting up your workspace: -https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.` - } return fmt.Errorf(msg), s.applyCriticalErrorToFiles(ctx, msg, openFiles) } diff --git a/gopls/internal/lsp/cache/pkg.go b/gopls/internal/lsp/cache/pkg.go index 12031347046..19b974f90c2 100644 --- a/gopls/internal/lsp/cache/pkg.go +++ b/gopls/internal/lsp/cache/pkg.go @@ -106,7 +106,7 @@ type ( dir string // dir containing the go.mod file modulePath string // parsed module path } - viewLoadScope protocol.DocumentURI // load the workspace + viewLoadScope struct{} // load the workspace ) // Implement the loadScope interface. diff --git a/gopls/internal/lsp/cache/snapshot.go b/gopls/internal/lsp/cache/snapshot.go index 14c06744475..760991bf3ec 100644 --- a/gopls/internal/lsp/cache/snapshot.go +++ b/gopls/internal/lsp/cache/snapshot.go @@ -351,26 +351,6 @@ func (s *Snapshot) Templates() map[protocol.DocumentURI]file.Handle { return tmpls } -func (s *Snapshot) validBuildConfiguration() bool { - // Since we only really understand the `go` command, if the user has a - // different GOPACKAGESDRIVER, assume that their configuration is valid. - if s.view.typ == GoPackagesDriverView { - return true - } - - // Check if the user is working within a module or if we have found - // multiple modules in the workspace. - if len(s.view.workspaceModFiles) > 0 { - return true - } - - if s.view.typ == GOPATHView { - return true - } - - return false -} - // config returns the configuration used for the snapshot's interaction with // the go/packages API. It uses the given working directory. // @@ -1431,7 +1411,7 @@ If you are using modules, please open your editor to a directory in your module. If you believe this warning is incorrect, please file an issue: https://github.com/golang/go/issues/new.` func shouldShowAdHocPackagesWarning(snapshot *Snapshot, active []*metadata.Package) string { - if !snapshot.validBuildConfiguration() { + if snapshot.view.typ == AdHocView { for _, mp := range active { // A blank entry in DepsByImpPath // indicates a missing dependency. @@ -1528,10 +1508,9 @@ func (s *Snapshot) reloadWorkspace(ctx context.Context) error { return nil } - // If the view's build configuration is invalid, we cannot reload by - // package path. Just reload the directory instead. - if !s.validBuildConfiguration() { - scopes = []loadScope{viewLoadScope("LOAD_INVALID_VIEW")} + // For an ad-hoc view, we cannot reload by package path. Just reload the view. + if s.view.typ == AdHocView { + scopes = []loadScope{viewLoadScope{}} } err := s.load(ctx, false, scopes...) diff --git a/gopls/internal/lsp/cache/view.go b/gopls/internal/lsp/cache/view.go index 05dce692f28..2bb8367ad9e 100644 --- a/gopls/internal/lsp/cache/view.go +++ b/gopls/internal/lsp/cache/view.go @@ -410,34 +410,26 @@ func (s *Session) UpdateFolders(ctx context.Context, newFolders []*Folder) error // viewEnv returns a string describing the environment of a newly created view. // // It must not be called concurrently with any other view methods. -// -// TODO(golang/go#57979): revisit this function and its uses once the dust -// settles. +// TODO(rfindley): rethink this function, or inline sole call. func viewEnv(v *View) string { - env := v.folder.Options.EnvSlice() - buildFlags := append([]string{}, v.folder.Options.BuildFlags...) - var buf bytes.Buffer fmt.Fprintf(&buf, `go info for %v -(go dir %s) +(view type %v) +(root dir %s) (go version %s) -(valid build configuration = %v) (build flags: %v) +(go env: %+v) +(env overlay: %v) `, v.folder.Dir.Path(), + v.typ, v.root.Path(), strings.TrimRight(v.folder.Env.GoVersionOutput, "\n"), - v.snapshot.validBuildConfiguration(), - buildFlags, + v.folder.Options.BuildFlags, + *v.snapshot.view.folder.Env, + v.snapshot.view.envOverlay, ) - for _, v := range env { - s := strings.SplitN(v, "=", 2) - if len(s) != 2 { - continue - } - } - return buf.String() } @@ -715,7 +707,7 @@ func (s *Snapshot) loadWorkspace(ctx context.Context, firstAttempt bool) (loadEr scopes = append(scopes, moduleLoadScope{dir: moduleDir, modulePath: parsed.File.Module.Mod.Path}) } } else { - scopes = append(scopes, viewLoadScope("LOAD_VIEW")) + scopes = append(scopes, viewLoadScope{}) } // If we're loading anything, ensure we also load builtin, diff --git a/gopls/internal/test/integration/workspace/workspace_test.go b/gopls/internal/test/integration/workspace/workspace_test.go index da935093574..319487d9776 100644 --- a/gopls/internal/test/integration/workspace/workspace_test.go +++ b/gopls/internal/test/integration/workspace/workspace_test.go @@ -1007,7 +1007,7 @@ package main // Confirm that the build configuration is seen as valid, // even though there are technically multiple go.mod files in the // worskpace. - LogMatching(protocol.Info, ".*valid build configuration = true.*", 1, false), + LogMatching(protocol.Info, ".*view type GoModView.*", 1, false), ) }) }