@@ -2,7 +2,6 @@ package main
2
2
3
3
import (
4
4
"fmt"
5
- "golang.org/x/mod/semver"
6
5
"io/ioutil"
7
6
"log"
8
7
"net/url"
@@ -13,6 +12,8 @@ import (
13
12
"runtime"
14
13
"strings"
15
14
15
+ "golang.org/x/mod/semver"
16
+
16
17
"github.com/github/codeql-go/extractor/autobuilder"
17
18
"github.com/github/codeql-go/extractor/util"
18
19
)
@@ -289,6 +290,77 @@ func main() {
289
290
}
290
291
}
291
292
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
+
292
364
// if `LGTM_INDEX_NEED_GOPATH` is set, it overrides the value for `needGopath` inferred above
293
365
if needGopathOverride := os .Getenv ("LGTM_INDEX_NEED_GOPATH" ); needGopathOverride != "" {
294
366
inLGTM = true
0 commit comments