Skip to content

Commit

Permalink
partially call bazel info to skip cache eviction
Browse files Browse the repository at this point in the history
  • Loading branch information
SinimaWath committed Oct 4, 2024
1 parent 778a4da commit cc27a5c
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 17 deletions.
45 changes: 45 additions & 0 deletions internal/bazel/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ type Bazel interface {
WriteToStderr(v bool)
WriteToStdout(v bool)
Info() (map[string]string, error)
InfoCommand(args ...string) ([]string, error)
Query(args ...string) (*blaze_query.QueryResult, error)
CQuery(args ...string) (*analysis.CqueryResult, error)
Build(args ...string) (*bytes.Buffer, error)
Expand Down Expand Up @@ -258,6 +259,33 @@ func (b *bazel) Info() (map[string]string, error) {
return b.processInfo(stdoutBuffer.String())
}

func (b *bazel) InfoCommand(commands ...string) ([]string, error) {
b.WriteToStderr(false)
b.WriteToStdout(false)

infoArgs := append([]string(nil), commands...)

stdoutBuffer, _ := b.newCommand("info", infoArgs...)

// This gofunction only prints if 'bazel info' takes longer than 8 seconds
doneCh := make(chan struct{})
defer close(doneCh)
go func() {
select {
case <-doneCh:
// Do nothing since we're done.
case <-time.After(8 * time.Second):
log.Logf("Running `bazel info`... it's being a little slow")
}
}()

err := b.cmd.Run()
if err != nil {
return nil, err
}
return b.processInfoCommand(stdoutBuffer.String())
}

func (b *bazel) processInfo(info string) (map[string]string, error) {
lines := strings.Split(info, "\n")
output := make(map[string]string, 0)
Expand All @@ -274,6 +302,23 @@ func (b *bazel) processInfo(info string) (map[string]string, error) {
return output, nil
}

func (b *bazel) processInfoCommand(info string) ([]string, error) {
lines := strings.Split(info, "\n")
output := []string{}

for _, line := range lines {
if line == "" || strings.Contains(line, "Starting local Bazel server and connecting to it...") {
continue
}
data := strings.SplitN(line, ": ", 2)
if len(data) < 2 {
return nil, errors.New("Bazel info returned a non key-value pair")
}
output = append(output, data[1])
}
return output, nil
}

// Executes a query language expression over a specified subgraph of the
// build dependency graph.
//
Expand Down
4 changes: 4 additions & 0 deletions internal/bazel/testing/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (b *MockBazel) Info() (map[string]string, error) {
b.actions = append(b.actions, []string{"Info"})
return map[string]string{}, nil
}
func (b *MockBazel) InfoCommand(args ...string) ([]string, error) {
b.actions = append(b.actions, append([]string{"InfoCommand"}, args...))
return []string{}, nil
}
func (b *MockBazel) AddQueryResponse(query string, res *blaze_query.QueryResult) {
if b.queryResponse == nil {
b.queryResponse = map[string]*blaze_query.QueryResult{}
Expand Down
15 changes: 13 additions & 2 deletions internal/ibazel/ibazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,8 @@ func New(version string) (*IBazel, error) {
lifecycleHooks,
}

info, _ := i.getInfo()
for _, l := range i.lifecycleListeners {
l.Initialize(&info)
l.Initialize(i.getInfo)
}

go func() {
Expand Down Expand Up @@ -480,6 +479,18 @@ func (i *IBazel) getInfo() (map[string]string, error) {
return res, nil
}

func (i *IBazel) getInfoByKeys(keys ...string) ([]string, error) {
b := i.newBazel()

res, err := b.InfoCommand(keys...)
if err != nil {
log.Errorf("Error getting Bazel info %v", err)
return nil, err
}

return res, nil
}

func (i *IBazel) queryForSourceFiles(query string) ([]string, error) {
b := i.newBazel()

Expand Down
14 changes: 5 additions & 9 deletions internal/ibazel/ibazel_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package ibazel

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -28,17 +27,14 @@ import (
)

func (i *IBazel) realLocalRepositoryPaths() (map[string]string, error) {
info, err := i.getInfo()
if err != nil {
log.Errorf("Error finding bazel info: %v\n", err)
info, err := i.getInfoByKeys("output_base", "install_base")
if err != nil || info == nil {
log.Errorf("Error finding bazel info output_base install_base: %v\n", err)
return nil, err
}

outputBase := info["output_base"]
installBase := info["install_base"]
if false {
return nil, fmt.Errorf("`bazel info` didn't include install_base")
}
outputBase, installBase := info[0], info[1]

externalPath := filepath.Join(outputBase, "external")

files, err := ioutil.ReadDir(externalPath)
Expand Down
2 changes: 1 addition & 1 deletion internal/ibazel/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
type Lifecycle interface {
// Initialize is called once it is known that this lifecycle client is going
// to be used.
Initialize(info *map[string]string)
Initialize(func() (map[string]string, error))

// TargetDecider takes a protobuf rule and performs setup if it matches the
// listener's expectations.
Expand Down
2 changes: 1 addition & 1 deletion internal/ibazel/lifecycle_hooks/lifecycle_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func New() *LifecycleHooks {
}
}

func (l *LifecycleHooks) Initialize(info *map[string]string) {}
func (l *LifecycleHooks) Initialize(func() (map[string]string, error)) {}

func (l *LifecycleHooks) TargetDecider(rule *blaze_query.Rule) {}

Expand Down
2 changes: 1 addition & 1 deletion internal/ibazel/live_reload/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (l *LiveReloadServer) AddEventsListener(listener Events) {
l.eventListeners = append(l.eventListeners, listener)
}

func (l *LiveReloadServer) Initialize(info *map[string]string) {}
func (l *LiveReloadServer) Initialize(func() (map[string]string, error)) {}

func (l *LiveReloadServer) Cleanup() {
if l.lrserver != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/ibazel/output_runner/output_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func New() *OutputRunner {
return i
}

func (i *OutputRunner) Initialize(info *map[string]string) {}
func (i *OutputRunner) Initialize(func() (map[string]string, error)) {}

func (i *OutputRunner) TargetDecider(rule *blaze_query.Rule) {}

Expand Down
10 changes: 8 additions & 2 deletions internal/ibazel/profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func New(version string) *Profiler {
return p
}

func (i *Profiler) Initialize(info *map[string]string) {
func (i *Profiler) Initialize(getInfo func() (map[string]string, error)) {
if *profileDev == "" {
return
}
Expand All @@ -114,9 +114,15 @@ func (i *Profiler) Initialize(info *map[string]string) {

log.Errorf("Profile output: %s", *profileDev)

info, err := getInfo()
if err != nil {
log.Errorf("Failed to get info: %v", err)
return
}

i.iterationBuildStart = true
i.newIteration()
i.startEvent(info)
i.startEvent(&info)
i.startProfilerServer()
}

Expand Down

0 comments on commit cc27a5c

Please sign in to comment.