Skip to content

Commit b169f1b

Browse files
committed
Factor out code to fix go vendor issues
1 parent f872a11 commit b169f1b

File tree

1 file changed

+30
-26
lines changed

1 file changed

+30
-26
lines changed

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

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,35 @@ func getModMode(depMode DependencyInstallerMode) ModMode {
250250
return ModUnset
251251
}
252252

253+
func fixGoVendorIssues(modMode ModMode, depMode DependencyInstallerMode, goDirectiveFound bool) ModMode {
254+
if modMode == ModVendor {
255+
// fix go vendor issues with go versions >= 1.14 when no go version is specified in the go.mod
256+
// if this is the case, and dependencies were vendored with an old go version (and therefore
257+
// do not contain a '## explicit' annotation, the go command will fail and refuse to do any
258+
// work
259+
//
260+
// we work around this by adding an explicit go version of 1.13, which is the last version
261+
// where this is not an issue
262+
if depMode == GoGetWithModules {
263+
if !goDirectiveFound {
264+
// if the go.mod does not contain a version line
265+
modulesTxt, err := ioutil.ReadFile("vendor/modules.txt")
266+
if err != nil {
267+
log.Println("Failed to read vendor/modules.txt to check for mismatched Go version")
268+
} else if explicitRe := regexp.MustCompile("(?m)^## explicit$"); !explicitRe.Match(modulesTxt) {
269+
// and the modules.txt does not contain an explicit annotation
270+
log.Println("Adding a version directive to the go.mod file as the modules.txt does not have explicit annotations")
271+
if !addVersionToMod("1.13") {
272+
log.Println("Failed to add a version to the go.mod file to fix explicitly required package bug; not using vendored dependencies")
273+
return ModMod
274+
}
275+
}
276+
}
277+
}
278+
}
279+
return modMode
280+
}
281+
253282
func main() {
254283
if len(os.Args) > 1 {
255284
usage()
@@ -303,32 +332,7 @@ func main() {
303332
}
304333

305334
modMode := getModMode(depMode)
306-
307-
if modMode == ModVendor {
308-
// fix go vendor issues with go versions >= 1.14 when no go version is specified in the go.mod
309-
// if this is the case, and dependencies were vendored with an old go version (and therefore
310-
// do not contain a '## explicit' annotation, the go command will fail and refuse to do any
311-
// work
312-
//
313-
// we work around this by adding an explicit go version of 1.13, which is the last version
314-
// where this is not an issue
315-
if depMode == GoGetWithModules {
316-
if !goDirectiveFound {
317-
// if the go.mod does not contain a version line
318-
modulesTxt, err := ioutil.ReadFile("vendor/modules.txt")
319-
if err != nil {
320-
log.Println("Failed to read vendor/modules.txt to check for mismatched Go version")
321-
} else if explicitRe := regexp.MustCompile("(?m)^## explicit$"); !explicitRe.Match(modulesTxt) {
322-
// and the modules.txt does not contain an explicit annotation
323-
log.Println("Adding a version directive to the go.mod file as the modules.txt does not have explicit annotations")
324-
if !addVersionToMod("1.13") {
325-
log.Println("Failed to add a version to the go.mod file to fix explicitly required package bug; not using vendored dependencies")
326-
modMode = ModMod
327-
}
328-
}
329-
}
330-
}
331-
}
335+
modMode = fixGoVendorIssues(modMode, depMode, goDirectiveFound)
332336

333337
// Go 1.16 and later won't automatically attempt to update go.mod / go.sum during package loading, so try to update them here:
334338
if modMode != ModVendor && depMode == GoGetWithModules && semver.Compare(getEnvGoSemVer(), "1.16") >= 0 {

0 commit comments

Comments
 (0)