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
diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go
index 9c98c76..3564da0 100644
--- a/bp2build/build_conversion.go
+++ b/bp2build/build_conversion.go
@@ -533,6 +533,13 @@
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 b9b250a..3fa1e2a 100644
--- a/bp2build/build_conversion_test.go
+++ b/bp2build/build_conversion_test.go
@@ -241,6 +241,22 @@
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",
+)`,
+ },
}
dir := "."