@@ -356,7 +356,7 @@ func tryUpdateGoModAndGoSum(modMode ModMode, depMode DependencyInstallerMode) {
356
356
}
357
357
}
358
358
359
- func moveToTemporaryGopath (srcdir string , importpath string ) {
359
+ func moveToTemporaryGopath (srcdir string , importpath string ) ( string , [] string , string , string , string ) {
360
360
// a temporary directory where everything is moved while the correct
361
361
// directory structure is created.
362
362
scratch , err := ioutil .TempDir (srcdir , "scratch" )
@@ -408,10 +408,11 @@ func moveToTemporaryGopath(srcdir string, importpath string) {
408
408
log .Fatalf ("Failed to rename %s to %s: %s\n " , scratch , newdir , err .Error ())
409
409
}
410
410
411
- // schedule restoring the contents of newdir to their original location after this function completes:
412
- defer restoreRepoLayout ( newdir , files , filepath . Base ( scratch ), srcdir )
411
+ return scratch , files , realSrc , root , newdir
412
+ }
413
413
414
- err = os .Chdir (newdir )
414
+ func createPathTransformerFile (newdir string ) * os.File {
415
+ err := os .Chdir (newdir )
415
416
if err != nil {
416
417
log .Fatalf ("Failed to chdir into %s: %s\n " , newdir , err .Error ())
417
418
}
@@ -422,8 +423,11 @@ func moveToTemporaryGopath(srcdir string, importpath string) {
422
423
if err != nil {
423
424
log .Fatalf ("Unable to create path transformer file: %s." , err .Error ())
424
425
}
425
- defer os .Remove (pt .Name ())
426
- _ , err = pt .WriteString ("#" + realSrc + "\n " + newdir + "//\n " )
426
+ return pt
427
+ }
428
+
429
+ func writePathTransformerFile (pt * os.File , realSrc , root , newdir string ) {
430
+ _ , err := pt .WriteString ("#" + realSrc + "\n " + newdir + "//\n " )
427
431
if err != nil {
428
432
log .Fatalf ("Unable to write path transformer file: %s." , err .Error ())
429
433
}
@@ -435,7 +439,9 @@ func moveToTemporaryGopath(srcdir string, importpath string) {
435
439
if err != nil {
436
440
log .Fatalf ("Unable to set SEMMLE_PATH_TRANSFORMER environment variable: %s.\n " , err .Error ())
437
441
}
442
+ }
438
443
444
+ func setGopath (root string ) {
439
445
// set/extend GOPATH
440
446
oldGopath := os .Getenv ("GOPATH" )
441
447
var newGopath string
@@ -447,7 +453,7 @@ func moveToTemporaryGopath(srcdir string, importpath string) {
447
453
} else {
448
454
newGopath = root
449
455
}
450
- err = os .Setenv ("GOPATH" , newGopath )
456
+ err : = os .Setenv ("GOPATH" , newGopath )
451
457
if err != nil {
452
458
log .Fatalf ("Unable to set GOPATH to %s: %s\n " , newGopath , err .Error ())
453
459
}
@@ -634,7 +640,16 @@ func main() {
634
640
inLGTM := os .Getenv ("LGTM_SRC" ) != "" || os .Getenv ("LGTM_INDEX_NEED_GOPATH" ) != ""
635
641
636
642
if inLGTM && needGopath {
637
- moveToTemporaryGopath (srcdir , importpath )
643
+ scratch , files , realSrc , root , newdir := moveToTemporaryGopath (srcdir , importpath )
644
+
645
+ // schedule restoring the contents of newdir to their original location after this function completes:
646
+ defer restoreRepoLayout (newdir , files , filepath .Base (scratch ), srcdir )
647
+
648
+ pt := createPathTransformerFile (newdir )
649
+ defer os .Remove (pt .Name ())
650
+
651
+ writePathTransformerFile (pt , realSrc , root , newdir )
652
+ setGopath (root )
638
653
}
639
654
640
655
// check whether an explicit dependency installation command was provided
0 commit comments