2019-12-16 13:40:24 -07:00
|
|
|
// 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"
|
2019-12-19 08:20:43 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/telemetry"
|
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
2019-12-16 13:40:24 -07:00
|
|
|
errors "golang.org/x/xerrors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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) {
|
2020-01-10 11:20:44 -07:00
|
|
|
// Check the Go version by running go list with GO111MODULE=off.
|
|
|
|
// If the output is anything other than "go1.14\n", assume -modfile is not supported.
|
|
|
|
// Borrowed from internal/imports/mod.go:620
|
2019-12-23 09:07:19 -07:00
|
|
|
const format = `{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}`
|
2020-01-10 11:20:44 -07:00
|
|
|
stdout, err := source.InvokeGo(ctx, folder, append(env, "GO111MODULE=off"), "list", "-e", "-f", format)
|
2019-12-16 13:40:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
2020-01-10 11:20:44 -07:00
|
|
|
// If the output is not go1.14 or an empty string, then it could be an error.
|
2019-12-16 13:40:24 -07:00
|
|
|
lines := strings.Split(stdout.String(), "\n")
|
2020-01-10 11:20:44 -07:00
|
|
|
if len(lines) < 2 && stdout.String() != "" {
|
|
|
|
log.Error(ctx, "unexpected stdout when checking for go1.14", errors.Errorf("%q", stdout), telemetry.Directory.Of(folder))
|
|
|
|
return "", false, nil
|
2019-12-16 13:40:24 -07:00
|
|
|
}
|
2019-12-23 09:07:19 -07:00
|
|
|
// Get the go.mod file associated with this module.
|
|
|
|
b, err := source.InvokeGo(ctx, folder, env, "env", "GOMOD")
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
modfile := strings.TrimSpace(b.String())
|
|
|
|
if modfile == os.DevNull {
|
|
|
|
return "", false, errors.Errorf("go env GOMOD did not detect a go.mod file in this folder")
|
|
|
|
}
|
|
|
|
return modfile, lines[0] == "go1.14", nil
|
2019-12-16 13:40:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The function getModfiles will return the go.mod files associated with the directory that is passed in.
|
2019-12-19 08:20:43 -07:00
|
|
|
func getModfiles(ctx context.Context, folder string, options source.Options) (*modfiles, error) {
|
2019-12-20 11:50:28 -07:00
|
|
|
if !options.TempModfile {
|
2019-12-19 08:20:43 -07:00
|
|
|
log.Print(ctx, "using the -modfile flag is disabled", telemetry.Directory.Of(folder))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
modfile, flagExists, err := modfileFlagExists(ctx, folder, options.Env)
|
2019-12-16 13:40:24 -07:00
|
|
|
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")
|
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
// Copy the current go.mod file into the temporary go.mod file.
|
2019-12-23 09:07:19 -07:00
|
|
|
tempFile, err := ioutil.TempFile("", "go.*.mod")
|
2019-12-16 13:40:24 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-23 09:07:19 -07:00
|
|
|
defer tempFile.Close()
|
2019-12-16 13:40:24 -07:00
|
|
|
origFile, err := os.Open(modfile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer origFile.Close()
|
2019-12-23 09:07:19 -07:00
|
|
|
if _, err := io.Copy(tempFile, origFile); err != nil {
|
2019-12-16 13:40:24 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
copySumFile(modfile, tempFile.Name())
|
2019-12-23 09:07:19 -07:00
|
|
|
return &modfiles{real: modfile, temp: tempFile.Name()}, nil
|
2019-12-16 13:40:24 -07:00
|
|
|
}
|
2019-12-17 14:13:33 -07:00
|
|
|
|
|
|
|
func copySumFile(realFile, tempFile string) {
|
|
|
|
realSum := realFile[0:len(realFile)-3] + "sum"
|
|
|
|
tempSum := tempFile[0:len(tempFile)-3] + "sum"
|
|
|
|
stat, err := os.Stat(realSum)
|
|
|
|
if err != nil || !stat.Mode().IsRegular() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
contents, err := ioutil.ReadFile(realSum)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ioutil.WriteFile(tempSum, contents, stat.Mode())
|
|
|
|
}
|