Skip to content

Commit 1bb006f

Browse files
committed
Move defer statements to the right place
It turns out that extracting defer statements into a separate function changes behaviour.
1 parent 641f16b commit 1bb006f

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func tryUpdateGoModAndGoSum(modMode ModMode, depMode DependencyInstallerMode) {
356356
}
357357
}
358358

359-
func moveToTemporaryGopath(srcdir string, importpath string) {
359+
func moveToTemporaryGopath(srcdir string, importpath string) (string, []string, string, string, string) {
360360
// a temporary directory where everything is moved while the correct
361361
// directory structure is created.
362362
scratch, err := ioutil.TempDir(srcdir, "scratch")
@@ -408,10 +408,11 @@ func moveToTemporaryGopath(srcdir string, importpath string) {
408408
log.Fatalf("Failed to rename %s to %s: %s\n", scratch, newdir, err.Error())
409409
}
410410

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+
}
413413

414-
err = os.Chdir(newdir)
414+
func createPathTransformerFile(newdir string) *os.File {
415+
err := os.Chdir(newdir)
415416
if err != nil {
416417
log.Fatalf("Failed to chdir into %s: %s\n", newdir, err.Error())
417418
}
@@ -422,8 +423,11 @@ func moveToTemporaryGopath(srcdir string, importpath string) {
422423
if err != nil {
423424
log.Fatalf("Unable to create path transformer file: %s.", err.Error())
424425
}
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")
427431
if err != nil {
428432
log.Fatalf("Unable to write path transformer file: %s.", err.Error())
429433
}
@@ -435,7 +439,9 @@ func moveToTemporaryGopath(srcdir string, importpath string) {
435439
if err != nil {
436440
log.Fatalf("Unable to set SEMMLE_PATH_TRANSFORMER environment variable: %s.\n", err.Error())
437441
}
442+
}
438443

444+
func setGopath(root string) {
439445
// set/extend GOPATH
440446
oldGopath := os.Getenv("GOPATH")
441447
var newGopath string
@@ -447,7 +453,7 @@ func moveToTemporaryGopath(srcdir string, importpath string) {
447453
} else {
448454
newGopath = root
449455
}
450-
err = os.Setenv("GOPATH", newGopath)
456+
err := os.Setenv("GOPATH", newGopath)
451457
if err != nil {
452458
log.Fatalf("Unable to set GOPATH to %s: %s\n", newGopath, err.Error())
453459
}
@@ -634,7 +640,16 @@ func main() {
634640
inLGTM := os.Getenv("LGTM_SRC") != "" || os.Getenv("LGTM_INDEX_NEED_GOPATH") != ""
635641

636642
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)
638653
}
639654

640655
// check whether an explicit dependency installation command was provided

0 commit comments

Comments
 (0)