From 58a12b8baeed0f946f5c2437b1c59d0a8e5f04ff Mon Sep 17 00:00:00 2001 From: Jingwen Chen Date: Tue, 30 Mar 2021 13:08:36 +0000 Subject: [PATCH] bp2build/queryview: codegen control sequences literally. See b/184026959 for an instance where a genrule's cmd contains \n, and the codegenerator generates an applied newline. It should be the original newline control sequence literal. Test: TH, bp2build unit test Fixes: 184026959 Change-Id: Idb99ea87c75dfa4cb3ae679f98d8f61cb5a5ab09 --- bp2build/build_conversion.go | 7 +++++++ bp2build/build_conversion_test.go | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go index 9c98c7661..3564da069 100644 --- a/bp2build/build_conversion.go +++ b/bp2build/build_conversion.go @@ -533,6 +533,13 @@ func isZero(value reflect.Value) bool { func escapeString(s string) string { s = strings.ReplaceAll(s, "\\", "\\\\") + + // b/184026959: Reverse the application of some common control sequences. + // These must be generated literally in the BUILD file. + s = strings.ReplaceAll(s, "\t", "\\t") + s = strings.ReplaceAll(s, "\n", "\\n") + s = strings.ReplaceAll(s, "\r", "\\r") + return strings.ReplaceAll(s, "\"", "\\\"") } diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go index b9b250a5c..3fa1e2acf 100644 --- a/bp2build/build_conversion_test.go +++ b/bp2build/build_conversion_test.go @@ -239,6 +239,22 @@ func TestGenerateBazelTargetModules(t *testing.T) { "b", ], string_prop = "a", +)`, + }, + { + bp: `custom { + name: "control_characters", + string_list_prop: ["\t", "\n"], + string_prop: "a\t\n\r", + bazel_module: { bp2build_available: true }, +}`, + expectedBazelTarget: `custom( + name = "control_characters", + string_list_prop = [ + "\t", + "\n", + ], + string_prop = "a\t\n\r", )`, }, }