From 082c5f31cc6d8bf1e28a552970d45b3f609fe82b Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Thu, 4 Aug 2022 15:49:20 -0700 Subject: [PATCH] Fix invalid json when product variables have quotes Go's json encoder will escape quotes with backslashes. But we put the encoded json into a Starlark string literal, which will evaluate the \" into just a regular quote, so they won't be escaped when the json.parse() gets to them. Escape all backslashes so this doesn't happen. Test: lunch sdk_phone_x86_64-userdebug; b build //build/bazel/examples/apex/minimal:build.bazel.examples.apex.minimal Change-Id: I473e6c42968fcf73d47dec61670956a7ac9a6c88 --- android/config.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/android/config.go b/android/config.go index 8c01beed0..84c17dee3 100644 --- a/android/config.go +++ b/android/config.go @@ -18,6 +18,7 @@ package android // product variables necessary for soong_build's operation. import ( + "bytes" "encoding/json" "errors" "fmt" @@ -305,6 +306,9 @@ func saveToBazelConfigFile(config *productVariables, outDir string) error { if err != nil { 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{ bazel.GeneratedBazelFileWarning, @@ -317,11 +321,11 @@ product_var_constraints = _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 { 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 { return fmt.Errorf("Could not write BUILD config file %s", err) }