-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
99 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,92 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bazelbuild/rules_go/go/runfiles" | ||
) | ||
|
||
var GoBinRlocationPath = "not set" | ||
var ConfigRlocationPath = "not set" | ||
|
||
// Produced by gazelle's go_deps extension. | ||
type Config struct { | ||
GoEnv map[string]string `json:"go_env"` | ||
} | ||
|
||
func main() { | ||
goBin, err := runfiles.Rlocation(GoBinRlocationPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// The go binary lies at $GOROOT/bin/go. | ||
goRoot, err := filepath.Abs(filepath.Dir(filepath.Dir(goBin))) | ||
|
||
cfg, err := parseConfig() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
env, err := getGoEnv(goBin, cfg) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
args := append([]string{goBin}, os.Args[1:]...) | ||
log.Fatal(runProcess(args, env, os.Getenv("BUILD_WORKING_DIRECTORY"))) | ||
} | ||
|
||
func parseConfig() (Config, error) { | ||
var cfg *Config | ||
// Special value set when rules_go is loaded as a WORKSPACE repo, in which | ||
// the cfg file isn't available from Gazelle. | ||
if ConfigRlocationPath == "WORKSPACE" { | ||
return Config{}, nil | ||
} | ||
cfgJsonPath, err := runfiles.Rlocation(ConfigRlocationPath) | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
cfgJson, err := os.ReadFile(cfgJsonPath) | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
err = json.Unmarshal(cfgJson, &cfg) | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
return *cfg, nil | ||
} | ||
|
||
func getGoEnv(goBin string, cfg Config) ([]string, error) { | ||
env := os.Environ() | ||
var filteredEnv []string | ||
for i := 0; i < len(env); i++ { | ||
if !strings.HasPrefix(env[i], "GOROOT=") { | ||
filteredEnv = append(filteredEnv, env[i]) | ||
} | ||
for k, v := range cfg.GoEnv { | ||
env = append(env, k+"="+v) | ||
} | ||
filteredEnv = append(filteredEnv, "GOROOT="+goRoot) | ||
|
||
err = os.Chdir(os.Getenv("BUILD_WORKING_DIRECTORY")) | ||
// The go binary lies at $GOROOT/bin/go. | ||
goRoot, err := filepath.Abs(filepath.Dir(filepath.Dir(goBin))) | ||
if err != nil { | ||
log.Fatal(err) | ||
return nil, err | ||
} | ||
|
||
args := append([]string{goBin}, os.Args[1:]...) | ||
log.Fatal(ReplaceWithProcess(args, filteredEnv)) | ||
// Override GOROOT to point to the hermetic Go SDK. | ||
return append(env, "GOROOT="+goRoot), nil | ||
} | ||
|
||
func runProcess(args, env []string, dir string) error { | ||
cmd := exec.Command(args[0], args[1:]...) | ||
cmd.Dir = dir | ||
cmd.Stdout = os.Stdout | ||
cmd.Stderr = os.Stderr | ||
cmd.Env = env | ||
err := cmd.Run() | ||
if exitErr, ok := err.(*exec.ExitError); ok { | ||
os.Exit(exitErr.ExitCode()) | ||
} else if err == nil { | ||
os.Exit(0) | ||
} | ||
return err | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
6.0.0 | ||
6.4.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters