diff --git a/bp2build/build_conversion.go b/bp2build/build_conversion.go index d48d9759e..e93b3dca1 100644 --- a/bp2build/build_conversion.go +++ b/bp2build/build_conversion.go @@ -568,6 +568,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 ad88e9796..49897b3ca 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", )`, }, }