Improve aquery-related validation and error handling

Also introduce test suite for aquery handling

Test: m nothing
Test: lunch aosp_flame && USE_BAZEL_ANALYSIS=1 m libc
Change-Id: I2493d42782099ea0b575968fca38bce6f0d59015
This commit is contained in:
Chris Parsons
2021-01-15 12:22:41 -05:00
parent 0dd19125eb
commit 4f069891ae
4 changed files with 481 additions and 13 deletions

View File

@@ -83,11 +83,15 @@ type BuildStatement struct {
// AqueryBuildStatements returns an array of BuildStatements which should be registered (and output
// to a ninja file) to correspond one-to-one with the given action graph json proto (from a bazel
// aquery invocation).
func AqueryBuildStatements(aqueryJsonProto []byte) []BuildStatement {
func AqueryBuildStatements(aqueryJsonProto []byte) ([]BuildStatement, error) {
buildStatements := []BuildStatement{}
var aqueryResult actionGraphContainer
json.Unmarshal(aqueryJsonProto, &aqueryResult)
err := json.Unmarshal(aqueryJsonProto, &aqueryResult)
if err != nil {
return nil, err
}
pathFragments := map[int]pathFragment{}
for _, pathFragment := range aqueryResult.PathFragments {
@@ -97,8 +101,7 @@ func AqueryBuildStatements(aqueryJsonProto []byte) []BuildStatement {
for _, artifact := range aqueryResult.Artifacts {
artifactPath, err := expandPathFragment(artifact.PathFragmentId, pathFragments)
if err != nil {
// TODO(cparsons): Better error handling.
panic(err.Error())
return nil, err
}
artifactIdToPath[artifact.Id] = artifactPath
}
@@ -110,15 +113,24 @@ func AqueryBuildStatements(aqueryJsonProto []byte) []BuildStatement {
for _, actionEntry := range aqueryResult.Actions {
outputPaths := []string{}
for _, outputId := range actionEntry.OutputIds {
// TODO(cparsons): Validate the id is present.
outputPaths = append(outputPaths, artifactIdToPath[outputId])
outputPath, exists := artifactIdToPath[outputId]
if !exists {
return nil, fmt.Errorf("undefined outputId %d", outputId)
}
outputPaths = append(outputPaths, outputPath)
}
inputPaths := []string{}
for _, inputDepSetId := range actionEntry.InputDepSetIds {
// TODO(cparsons): Validate the id is present.
for _, inputId := range depsetIdToArtifactIds[inputDepSetId] {
// TODO(cparsons): Validate the id is present.
inputPaths = append(inputPaths, artifactIdToPath[inputId])
inputArtifacts, exists := depsetIdToArtifactIds[inputDepSetId]
if !exists {
return nil, fmt.Errorf("undefined input depsetId %d", inputDepSetId)
}
for _, inputId := range inputArtifacts {
inputPath, exists := artifactIdToPath[inputId]
if !exists {
return nil, fmt.Errorf("undefined input artifactId %d", inputId)
}
inputPaths = append(inputPaths, inputPath)
}
}
buildStatement := BuildStatement{
@@ -130,7 +142,7 @@ func AqueryBuildStatements(aqueryJsonProto []byte) []BuildStatement {
buildStatements = append(buildStatements, buildStatement)
}
return buildStatements
return buildStatements, nil
}
func expandPathFragment(id int, pathFragmentsMap map[int]pathFragment) (string, error) {
@@ -140,7 +152,7 @@ func expandPathFragment(id int, pathFragmentsMap map[int]pathFragment) (string,
for currId > 0 {
currFragment, ok := pathFragmentsMap[currId]
if !ok {
return "", fmt.Errorf("undefined path fragment id '%s'", currId)
return "", fmt.Errorf("undefined path fragment id %d", currId)
}
labels = append([]string{currFragment.Label}, labels...)
currId = currFragment.ParentId