Merge "Fix invalid json when product variables have quotes" am: ab8581a1f8

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2175955

Change-Id: Icca744c4a87625cbe7b03db9ee3b29efaf1acd26
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot
2022-08-05 19:58:53 +00:00
committed by Automerger Merge Worker

View File

@@ -18,6 +18,7 @@ package android
// product variables necessary for soong_build's operation. // product variables necessary for soong_build's operation.
import ( import (
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@@ -305,6 +306,9 @@ func saveToBazelConfigFile(config *productVariables, outDir string) error {
if err != nil { if err != nil {
return fmt.Errorf("cannot marshal config data: %s", err.Error()) return fmt.Errorf("cannot marshal config data: %s", err.Error())
} }
// The backslashes need to be escaped because this text is going to be put
// inside a Starlark string literal.
configJson = bytes.ReplaceAll(configJson, []byte("\\"), []byte("\\\\"))
bzl := []string{ bzl := []string{
bazel.GeneratedBazelFileWarning, bazel.GeneratedBazelFileWarning,
@@ -317,11 +321,11 @@ product_var_constraints = _product_var_constraints
arch_variant_product_var_constraints = _arch_variant_product_var_constraints arch_variant_product_var_constraints = _arch_variant_product_var_constraints
`, `,
} }
err = ioutil.WriteFile(filepath.Join(dir, "product_variables.bzl"), []byte(strings.Join(bzl, "\n")), 0644) err = os.WriteFile(filepath.Join(dir, "product_variables.bzl"), []byte(strings.Join(bzl, "\n")), 0644)
if err != nil { if err != nil {
return fmt.Errorf("Could not write .bzl config file %s", err) return fmt.Errorf("Could not write .bzl config file %s", err)
} }
err = ioutil.WriteFile(filepath.Join(dir, "BUILD"), []byte(bazel.GeneratedBazelFileWarning), 0644) err = os.WriteFile(filepath.Join(dir, "BUILD"), []byte(bazel.GeneratedBazelFileWarning), 0644)
if err != nil { if err != nil {
return fmt.Errorf("Could not write BUILD config file %s", err) return fmt.Errorf("Could not write BUILD config file %s", err)
} }