-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/lsp: use the -modfile flag to update a different go.mod file
In the upcoming Go 1.14 release, there is an introduction of the -modfile flag which allows a user to run a go command but choose where to direct the go.mod file updates. The information about this can be found here: golang/go#34506. This change starts setting up the infrastructure to handle the seperate modfile rather than keep changing a user's go.mod file. To support versions of Go that are not 1.14, we run a modified "go list" command that checks the release tags to see if 1.14 is contained. Updates golang/go#31999 Change-Id: Icb71b6402ec4fa07e5f6f1a63954c25520e860b0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/211538 Run-TryBot: Rohan Challa <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
- Loading branch information
1 parent
210e553
commit 62a9628
Showing
5 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// 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" | ||
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, env []string) (*modfiles, error) { | ||
modfile, flagExists, err := modfileFlagExists(ctx, folder, 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters