internal/lsp: add gopls setting to disable use of -modfile flag behavior

This CL adds a "disableTempModfile" boolean that can be turned on or off.
While we are adding support for go.mod files in gopls, this flag will allow
users to opt out of using the -modfile flag that is enabled in Go 1.14. This
flag might be removed at a future point, the decision still needs to be made.

Updates golang/go#31999

Change-Id: Ic90229333e988fcc6d461ab1ee47bce1114bd965
Reviewed-on: https://go-review.googlesource.com/c/tools/+/212139
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/cache/modfiles.go b/internal/lsp/cache/modfiles.go
index c85a0c3..6896552 100644
--- a/internal/lsp/cache/modfiles.go
+++ b/internal/lsp/cache/modfiles.go
@@ -12,6 +12,8 @@
 	"strings"
 
 	"golang.org/x/tools/internal/lsp/source"
+	"golang.org/x/tools/internal/lsp/telemetry"
+	"golang.org/x/tools/internal/telemetry/log"
 	errors "golang.org/x/xerrors"
 )
 
@@ -34,8 +36,12 @@
 }
 
 // The function getModfiles will return the go.mod files associated with the directory that is passed in.
-func getModfiles(ctx context.Context, folder string, env []string) (*modfiles, error) {
-	modfile, flagExists, err := modfileFlagExists(ctx, folder, env)
+func getModfiles(ctx context.Context, folder string, options source.Options) (*modfiles, error) {
+	if options.DisableTempModfile {
+		log.Print(ctx, "using the -modfile flag is disabled", telemetry.Directory.Of(folder))
+		return nil, nil
+	}
+	modfile, flagExists, err := modfileFlagExists(ctx, folder, options.Env)
 	if err != nil {
 		return nil, err
 	}
diff --git a/internal/lsp/cache/session.go b/internal/lsp/cache/session.go
index c8389ba..6d5a41d 100644
--- a/internal/lsp/cache/session.go
+++ b/internal/lsp/cache/session.go
@@ -80,7 +80,7 @@
 	baseCtx := trace.Detach(xcontext.Detach(ctx))
 	backgroundCtx, cancel := context.WithCancel(baseCtx)
 
-	modfiles, err := getModfiles(ctx, folder.Filename(), options.Env)
+	modfiles, err := getModfiles(ctx, folder.Filename(), options)
 	if err != nil {
 		log.Error(ctx, "error getting modfiles", err, telemetry.Directory.Of(folder))
 	}
diff --git a/internal/lsp/source/options.go b/internal/lsp/source/options.go
index 7d76cd6..a69e1a8 100644
--- a/internal/lsp/source/options.go
+++ b/internal/lsp/source/options.go
@@ -67,10 +67,11 @@
 			Literal:       true,
 			Budget:        100 * time.Millisecond,
 		},
-		ComputeEdits: myers.ComputeEdits,
-		Analyzers:    defaultAnalyzers,
-		GoDiff:       true,
-		LinkTarget:   "pkg.go.dev",
+		ComputeEdits:       myers.ComputeEdits,
+		Analyzers:          defaultAnalyzers,
+		GoDiff:             true,
+		LinkTarget:         "pkg.go.dev",
+		DisableTempModfile: true,
 	}
 )
 
@@ -113,6 +114,11 @@
 
 	VerboseOutput bool
 
+	// WARNING: This configuration will be changed in the future.
+	// It only exists while this feature is under development.
+	// Disable use of the -modfile flag in Go 1.14.
+	DisableTempModfile bool
+
 	LinkTarget string
 }
 
@@ -317,6 +323,9 @@
 	case "verboseOutput":
 		result.setBool(&o.VerboseOutput)
 
+	case "disableTempModfile":
+		result.setBool(&o.DisableTempModfile)
+
 	// Deprecated settings.
 	case "wantSuggestedFixes":
 		result.State = OptionDeprecated