Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 9ad2d6c

Browse files
committed
Factor default and custom install paths
These now follow the same route: * Run a default or custom build script * If needed, check if vendor/ is usable * If it isn't, or if their build failed, install dependencies using go get etc This commit shouldn't cause any behavioural change.
1 parent 859b427 commit 9ad2d6c

File tree

1 file changed

+62
-69
lines changed

1 file changed

+62
-69
lines changed

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

Lines changed: 62 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func main() {
383383

384384
// check whether an explicit dependency installation command was provided
385385
inst := util.Getenv("CODEQL_EXTRACTOR_GO_BUILD_COMMAND", "LGTM_INDEX_BUILD_COMMAND")
386-
var install *exec.Cmd
386+
shouldInstallDependencies := false
387387
if inst == "" {
388388
// if there is a build file, run the corresponding build tool
389389
buildSucceeded := tryBuild("Makefile", "make") ||
@@ -393,65 +393,9 @@ func main() {
393393
tryBuild("build", "./build") ||
394394
tryBuild("build.sh", "./build.sh")
395395

396-
if modMode == ModVendor {
397-
// test if running `go` with -mod=vendor works, and if it doesn't, try to fallback to -mod=mod
398-
// or not set if the go version < 1.14. Note we check this post-build in case the build brings
399-
// the vendor directory up to date.
400-
if !checkVendor() {
401-
modMode = modModIfSupported()
402-
log.Println("The vendor directory is not consistent with the go.mod; not using vendored dependencies.")
403-
}
404-
}
405-
406396
if !buildSucceeded {
407-
if modMode == ModVendor {
408-
log.Printf("Skipping dependency installation because a Go vendor directory was found.")
409-
} else {
410-
// automatically determine command to install dependencies
411-
if depMode == Dep {
412-
// set up the dep cache if SEMMLE_CACHE is set
413-
cacheDir := os.Getenv("SEMMLE_CACHE")
414-
if cacheDir != "" {
415-
depCacheDir := filepath.Join(cacheDir, "go", "dep")
416-
log.Printf("Attempting to create dep cache dir %s\n", depCacheDir)
417-
err := os.MkdirAll(depCacheDir, 0755)
418-
if err != nil {
419-
log.Printf("Failed to create dep cache directory: %s\n", err.Error())
420-
} else {
421-
log.Printf("Setting dep cache directory to %s\n", depCacheDir)
422-
err = os.Setenv("DEPCACHEDIR", depCacheDir)
423-
if err != nil {
424-
log.Println("Failed to set dep cache directory")
425-
} else {
426-
err = os.Setenv("DEPCACHEAGE", "720h") // 30 days
427-
if err != nil {
428-
log.Println("Failed to set dep cache age")
429-
}
430-
}
431-
}
432-
}
433-
434-
if util.FileExists("Gopkg.lock") {
435-
// if Gopkg.lock exists, don't update it and only vendor dependencies
436-
install = exec.Command("dep", "ensure", "-v", "-vendor-only")
437-
} else {
438-
install = exec.Command("dep", "ensure", "-v")
439-
}
440-
log.Println("Installing dependencies using `dep ensure`.")
441-
} else if depMode == Glide {
442-
install = exec.Command("glide", "install")
443-
log.Println("Installing dependencies using `glide install`")
444-
} else {
445-
if depMode == GoGetWithModules {
446-
// enable go modules if used
447-
os.Setenv("GO111MODULE", "on")
448-
}
449-
450-
// get dependencies
451-
install = exec.Command("go", "get", "-v", "./...")
452-
log.Println("Installing dependencies using `go get -v ./...`.")
453-
}
454-
}
397+
// Build failed; we'll try to install dependencies ourselves
398+
shouldInstallDependencies = true
455399
}
456400
} else {
457401
// write custom build commands into a script, then run it
@@ -482,21 +426,70 @@ func main() {
482426
log.Fatalf("Unable to close temporary script holding custom build commands: %s\n", err.Error())
483427
}
484428
os.Chmod(script.Name(), 0700)
485-
install = exec.Command(script.Name())
486429
log.Println("Installing dependencies using custom build command.")
430+
run(exec.Command(script.Name()))
431+
}
487432

488-
if modMode == ModVendor {
489-
// test if running `go` with -mod=vendor works, and if it doesn't, try to fallback to -mod=mod
490-
// or not set if the go version < 1.14.
491-
if !checkVendor() {
492-
modMode = modModIfSupported()
493-
log.Println("The vendor directory is not consistent with the go.mod; not using vendored dependencies.")
494-
}
433+
if modMode == ModVendor {
434+
// test if running `go` with -mod=vendor works, and if it doesn't, try to fallback to -mod=mod
435+
// or not set if the go version < 1.14. Note we check this post-build in case the build brings
436+
// the vendor directory up to date.
437+
if !checkVendor() {
438+
modMode = modModIfSupported()
439+
log.Println("The vendor directory is not consistent with the go.mod; not using vendored dependencies.")
495440
}
496441
}
497442

498-
if install != nil {
499-
run(install)
443+
if shouldInstallDependencies {
444+
if modMode == ModVendor {
445+
log.Printf("Skipping dependency installation because a Go vendor directory was found.")
446+
} else {
447+
// automatically determine command to install dependencies
448+
var install *exec.Cmd
449+
if depMode == Dep {
450+
// set up the dep cache if SEMMLE_CACHE is set
451+
cacheDir := os.Getenv("SEMMLE_CACHE")
452+
if cacheDir != "" {
453+
depCacheDir := filepath.Join(cacheDir, "go", "dep")
454+
log.Printf("Attempting to create dep cache dir %s\n", depCacheDir)
455+
err := os.MkdirAll(depCacheDir, 0755)
456+
if err != nil {
457+
log.Printf("Failed to create dep cache directory: %s\n", err.Error())
458+
} else {
459+
log.Printf("Setting dep cache directory to %s\n", depCacheDir)
460+
err = os.Setenv("DEPCACHEDIR", depCacheDir)
461+
if err != nil {
462+
log.Println("Failed to set dep cache directory")
463+
} else {
464+
err = os.Setenv("DEPCACHEAGE", "720h") // 30 days
465+
if err != nil {
466+
log.Println("Failed to set dep cache age")
467+
}
468+
}
469+
}
470+
}
471+
472+
if util.FileExists("Gopkg.lock") {
473+
// if Gopkg.lock exists, don't update it and only vendor dependencies
474+
install = exec.Command("dep", "ensure", "-v", "-vendor-only")
475+
} else {
476+
install = exec.Command("dep", "ensure", "-v")
477+
}
478+
log.Println("Installing dependencies using `dep ensure`.")
479+
} else if depMode == Glide {
480+
install = exec.Command("glide", "install")
481+
log.Println("Installing dependencies using `glide install`")
482+
} else {
483+
if depMode == GoGetWithModules {
484+
// enable go modules if used
485+
os.Setenv("GO111MODULE", "on")
486+
}
487+
// get dependencies
488+
install = exec.Command("go", "get", "-v", "./...")
489+
log.Println("Installing dependencies using `go get -v ./...`.")
490+
}
491+
run(install)
492+
}
500493
}
501494

502495
// extract

0 commit comments

Comments
 (0)