Fix replacements of namespace module srcs in genrule

expandSrcsForBazel always prefixed : in OriginalModuleName. The
exceptions to this are filegroups that appear in a different soong
namespace. For these cases, we were not correctly substituting the soong
module name with the equivalent bazel label.

Test: go test ./bp2build
Change-Id: If090f3f8819835177c1f4d191b3eef6bb6e30ace
diff --git a/android/bazel_paths.go b/android/bazel_paths.go
index 7992564..02ae5ca 100644
--- a/android/bazel_paths.go
+++ b/android/bazel_paths.go
@@ -378,7 +378,13 @@
 		if m, tag := SrcIsModuleWithTag(p); m != "" {
 			l := getOtherModuleLabel(ctx, m, tag, BazelModuleLabel)
 			if l != nil && !InList(l.Label, expandedExcludes) {
-				l.OriginalModuleName = fmt.Sprintf(":%s", m)
+				if strings.HasPrefix(m, "//") {
+					// this is a module in a soong namespace
+					// It appears as //<namespace>:<module_name> in srcs, and not ://<namespace>:<module_name>
+					l.OriginalModuleName = m
+				} else {
+					l.OriginalModuleName = fmt.Sprintf(":%s", m)
+				}
 				labels.Includes = append(labels.Includes, *l)
 			}
 		} else {
diff --git a/bp2build/genrule_conversion_test.go b/bp2build/genrule_conversion_test.go
index 5ca8bd7..2a10a14 100644
--- a/bp2build/genrule_conversion_test.go
+++ b/bp2build/genrule_conversion_test.go
@@ -27,6 +27,8 @@
 
 func registerGenruleModuleTypes(ctx android.RegistrationContext) {
 	ctx.RegisterModuleType("genrule_defaults", func() android.Module { return genrule.DefaultsFactory() })
+	ctx.RegisterModuleType("cc_binary", func() android.Module { return cc.BinaryFactory() })
+	ctx.RegisterModuleType("soong_namespace", func() android.Module { return android.NamespaceFactory() })
 }
 
 func runGenruleTestCase(t *testing.T, tc Bp2buildTestCase) {
@@ -913,3 +915,40 @@
 		})
 	}
 }
+
+func TestGenruleWithModulesInNamespaces(t *testing.T) {
+	bp := `
+genrule {
+	name: "mygenrule",
+	cmd: "echo $(location //mynamespace:mymodule) > $(out)",
+	srcs: ["//mynamespace:mymodule"],
+	out: ["myout"],
+}
+`
+	fs := map[string]string{
+		"mynamespace/Android.bp":     `soong_namespace {}`,
+		"mynamespace/dir/Android.bp": `cc_binary {name: "mymodule"}`,
+	}
+	expectedBazelTargets := []string{
+		MakeBazelTargetNoRestrictions("genrule", "mygenrule", AttrNameToString{
+			// The fully qualified soong label is <namespace>:<module_name>
+			// - here the prefix is mynamespace
+			// The fully qualifed bazel label is <package>:<module_name>
+			// - here the prefix is mynamespace/dir, since there is a BUILD file at each level of this FS path
+			"cmd":  `"echo $(location //mynamespace/dir:mymodule) > $(OUTS)"`,
+			"outs": `["myout"]`,
+			"srcs": `["//mynamespace/dir:mymodule"]`,
+		}),
+	}
+
+	t.Run("genrule that uses module from a different namespace", func(t *testing.T) {
+		runGenruleTestCase(t, Bp2buildTestCase{
+			Blueprint:                  bp,
+			Filesystem:                 fs,
+			ModuleTypeUnderTest:        "genrule",
+			ModuleTypeUnderTestFactory: genrule.GenRuleFactory,
+			ExpectedBazelTargets:       expectedBazelTargets,
+		})
+	})
+
+}