mirror of
https://github.com/golang/go
synced 2024-11-19 02:34:44 -07:00
3aa5a36464
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>
73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
// Copyright 2019 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"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"
|
|
)
|
|
|
|
// Borrowed from (internal/imports/mod.go:620)
|
|
// This function will return the main go.mod file for this folder if it exists and whether the -modfile
|
|
// flag exists for this version of go.
|
|
func modfileFlagExists(ctx context.Context, folder string, env []string) (string, bool, error) {
|
|
const format = `{{.GoMod}}
|
|
{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}
|
|
`
|
|
stdout, err := source.InvokeGo(ctx, folder, env, "list", "-m", "-f", format)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
lines := strings.Split(stdout.String(), "\n")
|
|
if len(lines) < 2 {
|
|
return "", false, errors.Errorf("unexpected stdout: %q", stdout)
|
|
}
|
|
return lines[0], lines[1] == "go1.14", nil
|
|
}
|
|
|
|
// The function getModfiles will return the go.mod files associated with the directory that is passed in.
|
|
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
|
|
}
|
|
if !flagExists {
|
|
return nil, nil
|
|
}
|
|
if modfile == "" || modfile == os.DevNull {
|
|
return nil, errors.Errorf("go env GOMOD cannot detect a go.mod file in this folder")
|
|
}
|
|
f, err := ioutil.TempFile("", "go.*.mod")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer f.Close()
|
|
// Copy the current go.mod file into the temporary go.mod file.
|
|
origFile, err := os.Open(modfile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer origFile.Close()
|
|
if _, err := io.Copy(f, origFile); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &modfiles{real: modfile, temp: f.Name()}, nil
|
|
}
|