Skip to content

Commit 22ccbba

Browse files
committed
Run go mod tidy -e if go.mod exists
1 parent a8eeef6 commit 22ccbba

File tree

1 file changed

+73
-1
lines changed

1 file changed

+73
-1
lines changed

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

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"golang.org/x/mod/semver"
65
"io/ioutil"
76
"log"
87
"net/url"
@@ -13,6 +12,8 @@ import (
1312
"runtime"
1413
"strings"
1514

15+
"golang.org/x/mod/semver"
16+
1617
"github.com/github/codeql-go/extractor/autobuilder"
1718
"github.com/github/codeql-go/extractor/util"
1819
)
@@ -289,6 +290,77 @@ func main() {
289290
}
290291
}
291292

293+
if depMode == GoGetWithModules {
294+
// stat go.mod and go.sum
295+
var beforeGoModFileInfo, beforeGoSumFileInfo os.FileInfo
296+
297+
beforeGoMod, beforeGoModerr := os.Open("go.mod")
298+
if beforeGoModerr == nil {
299+
var beforeGoModStatErr error
300+
beforeGoModFileInfo, beforeGoModStatErr = beforeGoMod.Stat()
301+
if beforeGoModStatErr != nil {
302+
log.Println("Failed to stat go.mod before running `go mod tidy -e`")
303+
}
304+
} else {
305+
log.Println("Failed to read go.mod before running `go mod tidy -e`")
306+
}
307+
beforeGoMod.Close()
308+
309+
beforeGoSum, beforeGoSumErr := os.Open("go.sum")
310+
if beforeGoSumErr == nil {
311+
var beforeGoSumStatErr error
312+
beforeGoSumFileInfo, beforeGoSumStatErr = beforeGoSum.Stat()
313+
if beforeGoSumStatErr != nil {
314+
log.Println("Failed to stat go.sum before running `go mod tidy -e`")
315+
}
316+
}
317+
// don't print a warning if beforeGoSumErr != nil as it may be that the
318+
// file doesn't exist
319+
beforeGoSum.Close()
320+
321+
// run `go mod tidy -e`
322+
res := util.RunCmd(exec.Command("go", "mod", "tidy", "-e"))
323+
324+
if !res {
325+
log.Println("Failed to run `go mod tidy -e`")
326+
} else {
327+
if beforeGoModFileInfo != nil {
328+
afterGoMod, afterGoModErr := os.Open("go.mod")
329+
if afterGoModErr != nil {
330+
log.Println("Failed to read go.mod after running `go mod tidy -e`")
331+
} else {
332+
afterGoModFileInfo, afterGoModStatErr := afterGoMod.Stat()
333+
if afterGoModStatErr != nil {
334+
log.Println("Failed to stat go.mod after running `go mod tidy -e`")
335+
} else {
336+
if afterGoModFileInfo.ModTime().After(beforeGoModFileInfo.ModTime()) {
337+
// if go.mod has been changed then notify the user
338+
log.Println("We have run `go mod tidy -e` and it altered go.mod. You may wish to check these changes into version control. ")
339+
}
340+
}
341+
}
342+
afterGoMod.Close()
343+
}
344+
345+
afterGoSum, afterGoSumErr := os.Open("go.sum")
346+
if afterGoSumErr != nil {
347+
log.Println("Failed to read go.sum after running `go mod tidy -e`")
348+
} else {
349+
afterGoSumFileInfo, afterGoSumStatErr := afterGoSum.Stat()
350+
if afterGoSumStatErr != nil {
351+
log.Println("Failed to stat go.sum after running `go mod tidy -e`")
352+
} else {
353+
if beforeGoSumErr != nil || afterGoSumFileInfo.ModTime().After(beforeGoSumFileInfo.ModTime()) {
354+
// if go.sum has been changed then notify the user
355+
log.Println("We have run `go mod tidy -e` and it altered go.sum. You may wish to check these changes into version control. ")
356+
}
357+
}
358+
}
359+
afterGoSum.Close()
360+
361+
}
362+
}
363+
292364
// if `LGTM_INDEX_NEED_GOPATH` is set, it overrides the value for `needGopath` inferred above
293365
if needGopathOverride := os.Getenv("LGTM_INDEX_NEED_GOPATH"); needGopathOverride != "" {
294366
inLGTM = true

0 commit comments

Comments
 (0)