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

Commit 5cc695f

Browse files
committed
Autobuilder: fall back when os.Executable fails
This can happen under tracing, perhaps because of github/codeql-tracer#29
1 parent f49ff27 commit 5cc695f

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

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

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"golang.org/x/mod/semver"
67
"io/ioutil"
@@ -206,6 +207,51 @@ func checkVendor() bool {
206207
return true
207208
}
208209

210+
func getOsToolsSubdir() (string, error) {
211+
switch runtime.GOOS {
212+
case "darwin":
213+
return "osx64", nil
214+
case "linux":
215+
return "linux64", nil
216+
case "windows":
217+
return "win64", nil
218+
}
219+
return "", errors.New("Unknown OS: " + runtime.GOOS)
220+
}
221+
222+
func getExtractorDir() (string, error) {
223+
mypath, err := os.Executable()
224+
if err == nil {
225+
return filepath.Dir(mypath), nil
226+
}
227+
log.Printf("Could not determine path of autobuilder: %v.\n", err)
228+
229+
// Fall back to rebuilding our own path from the extractor root:
230+
extractorRoot := os.Getenv("CODEQL_EXTRACTOR_GO_ROOT")
231+
if extractorRoot == "" {
232+
return "", errors.New("CODEQL_EXTRACTOR_GO_ROOT not set")
233+
}
234+
235+
osSubdir, err := getOsToolsSubdir()
236+
if err != nil {
237+
return "", err
238+
}
239+
240+
return filepath.Join(extractorRoot, "tools", osSubdir), nil
241+
}
242+
243+
func getExtractorPath() (string, error) {
244+
dirname, err := getExtractorDir()
245+
if err != nil {
246+
return "", err
247+
}
248+
extractor := filepath.Join(dirname, "go-extractor")
249+
if runtime.GOOS == "windows" {
250+
extractor = extractor + ".exe"
251+
}
252+
return extractor, nil
253+
}
254+
209255
func main() {
210256
if len(os.Args) > 1 {
211257
usage()
@@ -507,13 +553,9 @@ func main() {
507553
}
508554

509555
// extract
510-
mypath, err := os.Executable()
556+
extractor, err := getExtractorPath()
511557
if err != nil {
512-
log.Fatalf("Could not determine path of autobuilder: %v.\n", err)
513-
}
514-
extractor := filepath.Join(filepath.Dir(mypath), "go-extractor")
515-
if runtime.GOOS == "windows" {
516-
extractor = extractor + ".exe"
558+
log.Fatalf("Could not determine path of extractor: %v.\n", err)
517559
}
518560

519561
cwd, err := os.Getwd()

0 commit comments

Comments
 (0)