Skip to content

Commit

Permalink
Display difference in configuration in verbose mode (#22)
Browse files Browse the repository at this point in the history
This is pretty ugly, but it's really useful for debugging.

It only displays differences when a target changed from exactly one
configuration to exactly one different configuration - in other cases,
it's hard to tell which configurations paired up and should be diffed.
  • Loading branch information
illicitonion committed Sep 14, 2022
1 parent 8602ef3 commit d9d5911
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 7 deletions.
8 changes: 4 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ http_archive(

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

go_rules_dependencies()

go_register_toolchains(version = "1.18")

load("//:third_party/go/deps.bzl", "go_dependencies")

# gazelle:repository_macro third_party/go/deps.bzl%go_dependencies
go_dependencies()

go_rules_dependencies()

go_register_toolchains(version = "1.18")

load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

gazelle_dependencies()
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ require (
github.com/aristanetworks/goarista v0.0.0-20220211174905-526022c8b178
github.com/bazelbuild/bazel-gazelle v0.25.1-0.20220406134132-bd319f810c16
github.com/otiai10/copy v1.7.1-0.20211223015809-9aae5f77261f
github.com/wI2L/jsondiff v0.2.0
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb
google.golang.org/protobuf v1.27.1
)

require (
github.com/tidwall/gjson v1.14.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/tools v0.1.10 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.3 h1:7JgpsBaN0uMkyju4tbYHu0mnM55hNKVYLsXmwr15NQI=
github.com/otiai10/mint v1.3.3/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/tidwall/gjson v1.14.0 h1:6aeJ0bzojgWLa82gDQHcx3S0Lr/O51I9bJ5nv6JFx5w=
github.com/tidwall/gjson v1.14.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/wI2L/jsondiff v0.2.0 h1:dE00WemBa1uCjrzQUUTE/17I6m5qAaN0EMFOg2Ynr/k=
github.com/wI2L/jsondiff v0.2.0/go.mod h1:axTcwtBkY4TsKuV+RgoMhHyHKKFRI6nnjRLi8LLYQnA=
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb h1:pC9Okm6BVmxEw76PUu0XUbOTQ92JX11hfvqTjAV3qxM=
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
Expand Down
2 changes: 2 additions & 0 deletions pkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "pkg",
srcs = [
"bazel.go",
"configurations.go",
"hash_cache.go",
"target_determinator.go",
"targets_list.go",
Expand All @@ -18,6 +19,7 @@ go_library(
"//third_party/protobuf/bazel/build",
"@bazel_gazelle//label:go_default_library",
"@com_github_aristanetworks_goarista//path",
"@com_github_wi2l_jsondiff//:jsondiff",
"@org_golang_google_protobuf//encoding/protojson",
"@org_golang_google_protobuf//proto",
],
Expand Down
59 changes: 59 additions & 0 deletions pkg/configurations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pkg

import (
"bytes"
"encoding/json"
"fmt"

"github.com/wI2L/jsondiff"
)

func diffConfigurations(l, r singleConfigurationOutput) (string, error) {
patch, err := jsondiff.Compare(l, r)
if err != nil {
return "", fmt.Errorf("failed to diff configurations %v and %v: %w", l.ConfigHash, r.ConfigHash, err)
}
v, err := json.Marshal(patch)
if err != nil {
return "", fmt.Errorf("failed to marshal patch diffing configurations %v and %v: %w", l.ConfigHash, r.ConfigHash, err)
}
return string(v), nil
}

// singleConfigurationOutput is a JSON-deserializing struct based on the observed output of `bazel config`.
// There are a few extra fields we don't represent, but they don't seem relevant to how we currently interpret the data.
// Feel free to add more in the future!
type singleConfigurationOutput struct {
ConfigHash string
Fragments json.RawMessage
FragmentOptions json.RawMessage
}

func getConfigurationDetails(context *Context) (map[Configuration]singleConfigurationOutput, error) {
var stdout bytes.Buffer
var stderr bytes.Buffer

returnVal, err := context.BazelCmd.Execute(
BazelCmdConfig{Dir: context.WorkspacePath, Stdout: &stdout, Stderr: &stderr},
"--output_base", context.BazelOutputBase, "config", "--output=json", "--dump_all")

if returnVal != 0 || err != nil {
return nil, fmt.Errorf("failed to run bazel config --output=json --dump_all: %w. Stderr:\n%v", err, stderr.String())
}

content := stdout.Bytes()

var configurations []singleConfigurationOutput
if err := json.Unmarshal(content, &configurations); err != nil {
return nil, fmt.Errorf("failed to unmarshal config stdout: %w", err)
}
m := make(map[Configuration]singleConfigurationOutput)
for _, c := range configurations {
configuration := Configuration(c.ConfigHash)
if _, ok := m[configuration]; ok {
return nil, fmt.Errorf("saw duplicate configuration for %q", configuration)
}
m[configuration] = c
}
return m, nil
}
9 changes: 8 additions & 1 deletion pkg/target_determinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ type QueryResults struct {
TargetHashCache *TargetHashCache
BazelRelease string
// QueryError is whatever error was returned when running the cquery to get these results.
QueryError error
QueryError error
configurations map[Configuration]singleConfigurationOutput
}

func (queryInfo *QueryResults) PrefillCache() error {
Expand Down Expand Up @@ -635,12 +636,18 @@ func doQueryDeps(context *Context, targets TargetsList) (*QueryResults, error) {
labelsToConfigurations: processedLabelsToConfigurations,
}

configurations, err := getConfigurationDetails(context)
if err != nil {
return nil, fmt.Errorf("failed to interpret configurations output: %w", err)
}

queryResults := &QueryResults{
MatchingTargets: matchingTargets,
TransitiveConfiguredTargets: transitiveConfiguredTargets,
TargetHashCache: NewTargetHashCache(transitiveConfiguredTargets, bazelRelease),
BazelRelease: bazelRelease,
QueryError: nil,
configurations: configurations,
}
return queryResults, nil
}
Expand Down
18 changes: 16 additions & 2 deletions pkg/walker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,23 @@ func DiffSingleLabel(beforeMetadata, afterMetadata *QueryResults, includeDiffere
callback(label, differences, configuredTarget)
return nil
} else if !beforeMetadata.MatchingTargets.ContainsLabelAndConfiguration(label, configuration) {
collectDifference(Difference{
difference := Difference{
Category: "NewConfiguration",
})
}
if includeDifferences {
configurationsBefore := beforeMetadata.MatchingTargets.ConfigurationsFor(label)
configurationsAfter := afterMetadata.MatchingTargets.ConfigurationsFor(label)
if len(configurationsBefore) == 1 && len(configurationsAfter) == 1 {
diff, _ := diffConfigurations(beforeMetadata.configurations[configurationsBefore[0]], afterMetadata.configurations[configurationsAfter[0]])
difference = Difference{
Category: "ChangedConfiguration",
Before: string(configurationsBefore[0]),
After: string(configurationsAfter[0]),
Key: diff,
}
}
}
collectDifference(difference)
callback(label, differences, configuredTarget)
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static junit.framework.TestCase.fail;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public class TargetDeterminatorSpecificFlagsTest {
Expand Down Expand Up @@ -157,6 +158,16 @@ public void testWorktreeCreation() throws Exception {

}

@Test
public void changedConfigurationVerbose() throws Exception {
TestdataRepo.gitCheckout(testDir, Commits.BAZELRC_AFFECTING_JAVA);
String output = getOutput(Commits.TWO_LANGUAGES_OF_TESTS, "//java/example:ExampleTest", false, true, List.of("--verbose"));
// This isn't great output, and we shouldn't worry about changing its format in the future,
// but this test is to ensure we return a result including a hint as to what changed the
// configuration.
assertThat(output, containsString("-source 7 -target 7"));
}

private Set<Label> getTargets(String commitBefore, String targets) throws Exception {
return getTargets(commitBefore, targets, false, true);
}
Expand Down
24 changes: 24 additions & 0 deletions third_party/go/deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,36 @@ def go_dependencies():
sum = "h1:fj5tQ8acgNUr6O8LEplsxDhUIe2573iLkJc+PqnzZTI=",
version = "v0.0.0-20191217153810-f85b25db303b",
)
go_repository(
name = "com_github_tidwall_gjson",
importpath = "github.com/tidwall/gjson",
sum = "h1:6aeJ0bzojgWLa82gDQHcx3S0Lr/O51I9bJ5nv6JFx5w=",
version = "v1.14.0",
)
go_repository(
name = "com_github_tidwall_match",
importpath = "github.com/tidwall/match",
sum = "h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=",
version = "v1.1.1",
)
go_repository(
name = "com_github_tidwall_pretty",
importpath = "github.com/tidwall/pretty",
sum = "h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=",
version = "v1.2.0",
)
go_repository(
name = "com_github_tjfoc_gmsm",
importpath = "github.com/tjfoc/gmsm",
sum = "h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=",
version = "v1.4.1",
)
go_repository(
name = "com_github_wi2l_jsondiff",
importpath = "github.com/wI2L/jsondiff",
sum = "h1:dE00WemBa1uCjrzQUUTE/17I6m5qAaN0EMFOg2Ynr/k=",
version = "v0.2.0",
)
go_repository(
name = "com_github_xtaci_kcp_go",
importpath = "github.com/xtaci/kcp-go",
Expand Down

0 comments on commit d9d5911

Please sign in to comment.