Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 28c6974

Browse files
Sauyon Leesmowton
authored andcommitted
Add workaround for go 1.14 explicit vendoring requirement
This only applies for module files for which no Go version has been specified; Go will assume these should be parsed with the latest Go version, which will cause them to fail if the vendor directory has been generated with an old version of Go, as the vendor/modules.txt will not meet the new requirements for consistency.
1 parent 34d5e97 commit 28c6974

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@ const (
141141
Glide
142142
)
143143

144+
// addVersionToMod add a go version directive, e.g. `go 1.14` to a `go.mod` file.
145+
func addVersionToMod(goMod []byte, version string) bool {
146+
cmd := exec.Command("go", "mod", "edit", "-go="+version)
147+
return run(cmd)
148+
}
149+
144150
func main() {
145151
if len(os.Args) > 1 {
146152
usage()
@@ -184,6 +190,34 @@ func main() {
184190
// if a vendor/modules.txt file exists, we assume that there are vendored Go dependencies, and
185191
// skip the dependency installation step and run the extractor with `-mod=vendor`
186192
hasVendor := util.FileExists("vendor/modules.txt")
193+
if hasVendor {
194+
// fix go vendor issues with go versions >= 1.14 when no go version is specified in the go.mod
195+
// if this is the case, and dependencies were vendored with an old go version (and therefore
196+
// do not contain a '## explicit' annotation, the go command will fail and refuse to do any
197+
// work
198+
//
199+
// we work around this by adding an explicit go version of 1.13, which is the last version
200+
// where this is not an issue
201+
if depMode == GoGetWithModules {
202+
goMod, err := ioutil.ReadFile("go.mod")
203+
if err != nil {
204+
log.Println("Failed to read go.mod to check for missing Go version")
205+
} else if versionRe := regexp.MustCompile(`(?m)^go[ \t\r]+[0-9]+\.[0-9]+$`); !versionRe.Match(goMod) {
206+
// if the go.mod does not contain a version line
207+
modulesTxt, err := ioutil.ReadFile("vendor/modules.txt")
208+
if err != nil {
209+
log.Println("Failed to read vendor/modules.txt to check for mismatched Go version")
210+
} else if explicitRe := regexp.MustCompile("(?m)^## explicit$"); !explicitRe.Match(modulesTxt) {
211+
// and the modules.txt does not contain an explicit annotation
212+
log.Println("Adding a version directive to the go.mod file as the modules.txt does not have explicit annotations")
213+
if !addVersionToMod(goMod, "1.13") {
214+
log.Println("Failed to add a version to the go.mod file to fix explicitly required package bug; not using vendored dependencies")
215+
hasVendor = false
216+
}
217+
}
218+
}
219+
}
220+
}
187221

188222
// if `LGTM_INDEX_NEED_GOPATH` is set, it overrides the value for `needGopath` inferred above
189223
if needGopathOverride := os.Getenv("LGTM_INDEX_NEED_GOPATH"); needGopathOverride != "" {

0 commit comments

Comments
 (0)