Merge "Delete bazel-related files while bazel is off" into main am: 9e5abfecc9

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/2810453

Change-Id: Id51828985013577efa18b99724ffeea96d02db35
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/ui/build/soong.go b/ui/build/soong.go
index 5ae8430..abdfeda 100644
--- a/ui/build/soong.go
+++ b/ui/build/soong.go
@@ -632,6 +632,10 @@
 
 		if config.BazelBuildEnabled() || config.Bp2Build() {
 			checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(bp2buildFilesTag))
+		} else {
+			// Remove bazel files in the event that bazel is disabled for the build.
+			// These files may have been left over from a previous bazel-enabled build.
+			cleanBazelFiles(config)
 		}
 
 		if config.JsonModuleGraph() {
@@ -790,6 +794,18 @@
 	}
 }
 
+func cleanBazelFiles(config Config) {
+	files := []string{
+		shared.JoinPath(config.SoongOutDir(), "bp2build"),
+		shared.JoinPath(config.SoongOutDir(), "workspace"),
+		shared.JoinPath(config.SoongOutDir(), bazel.SoongInjectionDirName),
+		shared.JoinPath(config.OutDir(), "bazel")}
+
+	for _, f := range files {
+		os.RemoveAll(f)
+	}
+}
+
 func runMicrofactory(ctx Context, config Config, name string, pkg string, mapping map[string]string) {
 	ctx.BeginTrace(metrics.RunSoong, name)
 	defer ctx.EndTrace()