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

Autobuilder: fall back when os.Executable fails #383

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions extractor/cli/go-autobuilder/go-autobuilder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"golang.org/x/mod/semver"
"io/ioutil"
Expand Down Expand Up @@ -206,6 +207,51 @@ func checkVendor() bool {
return true
}

func getOsToolsSubdir() (string, error) {
Copy link
Contributor

@aibaars aibaars Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is known asos.Getenv("CODEQL_PLATFORM")

Copy link
Contributor Author

@smowton smowton Oct 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but that was being filtered out, presumably also by the tracer. It was known to autobuild.sh but not its child go-autobuild.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matt-gretton-dann The tracer shouldn't filter out CODEQL_ variables, right?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tracer has a concept of critical variables which it tries to preserve in the environment. But also when the tracer is turned off (as is the case when we trace go-autouild) we remove those critical variables. I believe this was historically deemed appropriate to try and preserve the environment the user would expect

CODEQL_PLATFORM is one of those critical env. vars.

switch runtime.GOOS {
case "darwin":
return "osx64", nil
case "linux":
return "linux64", nil
case "windows":
return "win64", nil
}
return "", errors.New("Unknown OS: " + runtime.GOOS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I like the fmt.Errorf more than errors.New, but that's entirely a style thing. Also, I think "unsupported" is more accurate here.

Suggested change
return "", errors.New("Unknown OS: " + runtime.GOOS)
return "", fmt.Errorf("Unsupported OS: %s", runtime.GOOS)

}

func getExtractorDir() (string, error) {
mypath, err := os.Executable()
if err == nil {
return filepath.Dir(mypath), nil
}
log.Printf("Could not determine path of autobuilder: %v.\n", err)

// Fall back to rebuilding our own path from the extractor root:
extractorRoot := os.Getenv("CODEQL_EXTRACTOR_GO_ROOT")
if extractorRoot == "" {
return "", errors.New("CODEQL_EXTRACTOR_GO_ROOT not set")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably have a similar message here as when CODEQL_EXTRACTOR_GO_SOURCE_ARCHIVE_DIR is unset below.

}

osSubdir, err := getOsToolsSubdir()
if err != nil {
return "", err
}

return filepath.Join(extractorRoot, "tools", osSubdir), nil
}

func getExtractorPath() (string, error) {
dirname, err := getExtractorDir()
if err != nil {
return "", err
}
extractor := filepath.Join(dirname, "go-extractor")
if runtime.GOOS == "windows" {
extractor = extractor + ".exe"
}
return extractor, nil
}

func main() {
if len(os.Args) > 1 {
usage()
Expand Down Expand Up @@ -507,13 +553,9 @@ func main() {
}

// extract
mypath, err := os.Executable()
extractor, err := getExtractorPath()
if err != nil {
log.Fatalf("Could not determine path of autobuilder: %v.\n", err)
}
extractor := filepath.Join(filepath.Dir(mypath), "go-extractor")
if runtime.GOOS == "windows" {
extractor = extractor + ".exe"
log.Fatalf("Could not determine path of extractor: %v.\n", err)
}

cwd, err := os.Getwd()
Expand Down