Skip to content

Commit acbc0e3

Browse files
committed
internal/lsp: modify how we check for go 1.14 for modfiles
When the tempModfile flag was enabled, there would be some go commands that would run without the -modfile flag. This change adds the config's buildFlags to the invokeGo command in go/packages and updates how we are checking if the go version is 1.14 within the modfileFlagExists function. Fixes golang/go#36247 Change-Id: I436666c3fcad33e85ba00aec5f227fe4a0e638b8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/212477 Run-TryBot: Rohan Challa <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent 11e9d9c commit acbc0e3

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

go/packages/golist.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ func golistDriver(cfg *Config, rootsDirs func() *goInfo, words ...string) (*driv
673673

674674
// Run "go list" for complete
675675
// information on the specified packages.
676-
buf, err := invokeGo(cfg, golistargs(cfg, words)...)
676+
buf, err := invokeGo(cfg, "list", golistargs(cfg, words)...)
677677
if err != nil {
678678
return nil, err
679679
}
@@ -877,7 +877,7 @@ func absJoin(dir string, fileses ...[]string) (res []string) {
877877
func golistargs(cfg *Config, words []string) []string {
878878
const findFlags = NeedImports | NeedTypes | NeedSyntax | NeedTypesInfo
879879
fullargs := []string{
880-
"list", "-e", "-json",
880+
"-e", "-json",
881881
fmt.Sprintf("-compiled=%t", cfg.Mode&(NeedCompiledGoFiles|NeedSyntax|NeedTypesInfo|NeedTypesSizes) != 0),
882882
fmt.Sprintf("-test=%t", cfg.Tests),
883883
fmt.Sprintf("-export=%t", usesExportData(cfg)),
@@ -893,10 +893,13 @@ func golistargs(cfg *Config, words []string) []string {
893893
}
894894

895895
// invokeGo returns the stdout of a go command invocation.
896-
func invokeGo(cfg *Config, args ...string) (*bytes.Buffer, error) {
896+
func invokeGo(cfg *Config, verb string, args ...string) (*bytes.Buffer, error) {
897897
stdout := new(bytes.Buffer)
898898
stderr := new(bytes.Buffer)
899-
cmd := exec.CommandContext(cfg.Context, "go", args...)
899+
goArgs := []string{verb}
900+
goArgs = append(goArgs, cfg.BuildFlags...)
901+
goArgs = append(goArgs, args...)
902+
cmd := exec.CommandContext(cfg.Context, "go", goArgs...)
900903
// On darwin the cwd gets resolved to the real path, which breaks anything that
901904
// expects the working directory to keep the original path, including the
902905
// go command when dealing with modules.

internal/lsp/cache/modfiles.go

+25-13
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,37 @@ import (
1717
errors "golang.org/x/xerrors"
1818
)
1919

20-
// Borrowed from (internal/imports/mod.go:620)
2120
// This function will return the main go.mod file for this folder if it exists and whether the -modfile
2221
// flag exists for this version of go.
2322
func modfileFlagExists(ctx context.Context, folder string, env []string) (string, bool, error) {
24-
const format = `{{.GoMod}}
25-
{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}
26-
`
27-
stdout, err := source.InvokeGo(ctx, folder, env, "list", "-m", "-f", format)
23+
var tempEnv []string
24+
for i := range env {
25+
tempEnv = append(tempEnv, env[i])
26+
if strings.Contains(env[i], "GO111MODULE") {
27+
tempEnv[i] = "GO111MODULE=off"
28+
}
29+
}
30+
// Borrowed from (internal/imports/mod.go:620)
31+
const format = `{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}`
32+
// Check the go version by running "go list" with modules off.
33+
stdout, err := source.InvokeGo(ctx, folder, tempEnv, "list", "-f", format)
2834
if err != nil {
2935
return "", false, err
3036
}
3137
lines := strings.Split(stdout.String(), "\n")
3238
if len(lines) < 2 {
3339
return "", false, errors.Errorf("unexpected stdout: %q", stdout)
3440
}
35-
return lines[0], lines[1] == "go1.14", nil
41+
// Get the go.mod file associated with this module.
42+
b, err := source.InvokeGo(ctx, folder, env, "env", "GOMOD")
43+
if err != nil {
44+
return "", false, err
45+
}
46+
modfile := strings.TrimSpace(b.String())
47+
if modfile == os.DevNull {
48+
return "", false, errors.Errorf("go env GOMOD did not detect a go.mod file in this folder")
49+
}
50+
return modfile, lines[0] == "go1.14", nil
3651
}
3752

3853
// The function getModfiles will return the go.mod files associated with the directory that is passed in.
@@ -51,22 +66,19 @@ func getModfiles(ctx context.Context, folder string, options source.Options) (*m
5166
if modfile == "" || modfile == os.DevNull {
5267
return nil, errors.Errorf("go env GOMOD cannot detect a go.mod file in this folder")
5368
}
54-
f, err := ioutil.TempFile("", "go.*.mod")
69+
tempFile, err := ioutil.TempFile("", "go.*.mod")
5570
if err != nil {
5671
return nil, err
5772
}
58-
defer f.Close()
73+
defer tempFile.Close()
5974
// Copy the current go.mod file into the temporary go.mod file.
6075
origFile, err := os.Open(modfile)
6176
if err != nil {
6277
return nil, err
6378
}
6479
defer origFile.Close()
65-
if _, err := io.Copy(f, origFile); err != nil {
66-
return nil, err
67-
}
68-
if err := f.Close(); err != nil {
80+
if _, err := io.Copy(tempFile, origFile); err != nil {
6981
return nil, err
7082
}
71-
return &modfiles{real: modfile, temp: f.Name()}, nil
83+
return &modfiles{real: modfile, temp: tempFile.Name()}, nil
7284
}

0 commit comments

Comments
 (0)