internal/lsp/cache: fix -mod=mod for workspace module setups

When we're in workspace module mode, there may not be a go.mod at the
root of the workspace, so checking modURI != "" doesn't tell us whether
module mode is on. Add a flag to workspaceMode which is exactly that.

There are probably many more places that should be updated, but since
the design is in flux I don't want to cause more churn than necessary.
This is enough to unblock -mod=readonly by default.

(I missed this in CL 253799 because I was running the wrong Go Version.)

Change-Id: I718ee44c7a2547c90f9d62393aba390c9b4b0f46
Reviewed-on: https://go-review.googlesource.com/c/tools/+/254756
Trust: Heschi Kreinick <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2020-09-14 18:31:33 -04:00
parent 6498648128
commit 2db8f0ff89
4 changed files with 23 additions and 12 deletions

View File

@ -91,7 +91,7 @@ func (s *snapshot) load(ctx context.Context, scopes ...interface{}) error {
cfg := s.config(ctx)
cleanup := func() {}
var modFH, sumFH source.FileHandle
var err error
if s.view.modURI != "" {
@ -108,7 +108,7 @@ func (s *snapshot) load(ctx context.Context, scopes ...interface{}) error {
}
switch {
case s.view.workspaceMode&workspaceModule != 0:
case s.view.workspaceMode&usesWorkspaceModule != 0:
var (
tmpDir span.URI
err error

View File

@ -257,7 +257,7 @@ func (v *View) findAndBuildWorkspaceModule(ctx context.Context, options source.O
return nil
}
v.workspaceMode |= workspaceModule
v.workspaceMode |= usesWorkspaceModule | moduleMode
// Walk the view's folder to find all modules in the view.
root := v.root.Filename()

View File

@ -217,10 +217,13 @@ func (s *snapshot) goCommandInvocation(ctx context.Context, cfg *packages.Config
}
cfg.BuildFlags = append(cfg.BuildFlags, fmt.Sprintf("-modfile=%s", tmpURI.Filename()))
}
if s.view.modURI != "" && verb != "mod" && verb != "get" {
modFH, err := s.GetFile(ctx, s.view.modURI)
if err != nil {
return "", nil, nil, cleanup, err
if verb != "mod" && verb != "get" {
var modFH source.FileHandle
if s.view.modURI != "" {
modFH, err = s.GetFile(ctx, s.view.modURI)
if err != nil {
return "", nil, nil, cleanup, err
}
}
modMod, err := s.view.needsModEqualsMod(ctx, modFH)
if err != nil {

View File

@ -159,14 +159,14 @@ type View struct {
type workspaceMode int
const (
standard workspaceMode = 1 << iota
moduleMode workspaceMode = 1 << iota
// tempModfile indicates whether or not the -modfile flag should be used.
tempModfile
// workspaceModule indicates support for the experimental workspace module
// usesWorkspaceModule indicates support for the experimental workspace module
// feature.
workspaceModule
usesWorkspaceModule
)
type builtinPackageHandle struct {
@ -818,6 +818,9 @@ func (v *View) setBuildInformation(ctx context.Context, options source.Options)
if err != nil {
return err
}
if modFile != "" {
v.workspaceMode |= moduleMode
}
if modFile == os.DevNull {
return nil
}
@ -836,7 +839,6 @@ func (v *View) setBuildInformation(ctx context.Context, options source.Options)
if !options.TempModfile || v.modURI == "" {
return nil
}
v.workspaceMode = standard
if v.goversion >= 14 {
v.workspaceMode |= tempModfile
}
@ -1020,7 +1022,7 @@ func (v *View) goVersion(ctx context.Context, env []string) (int, error) {
var modFlagRegexp = regexp.MustCompile(`-mod[ =](\w+)`)
func (v *View) needsModEqualsMod(ctx context.Context, modFH source.FileHandle) (bool, error) {
if v.goversion < 16 || modFH == nil {
if v.goversion < 16 || v.workspaceMode&moduleMode == 0 {
return false, nil
}
@ -1036,6 +1038,12 @@ func (v *View) needsModEqualsMod(ctx context.Context, modFH source.FileHandle) (
return modFlag == "vendor", nil
}
// In workspace module mode, there may not be a go.mod file.
// TODO: Once vendor mode is designed, update to check if it's on, however that works.
if modFH == nil {
return true, nil
}
modBytes, err := modFH.Read()
if err != nil {
return false, err