Only mount dist dir to soong sandbox if it exists

The dist dir is created late in the build process. If a
soong sandbox attempts to mount it before it's created
the sandbox will create an empty file in its place.

Test: lunch aosp_cf_x86_phone-userdebug && make -j dist
Change-Id: Ie6513bf702de6e6322c78753d406d70ea3ccc04d
diff --git a/ui/build/sandbox_linux.go b/ui/build/sandbox_linux.go
index 5ca83cc..4c3bac3 100644
--- a/ui/build/sandbox_linux.go
+++ b/ui/build/sandbox_linux.go
@@ -79,7 +79,7 @@
 		sandboxConfig.outDir = absPath(c.ctx, c.config.OutDir())
 		sandboxConfig.distDir = absPath(c.ctx, c.config.DistDir())
 
-		cmd := exec.CommandContext(c.ctx.Context, nsjailPath,
+		sandboxArgs := []string{
 			"-H", "android-build",
 			"-e",
 			"-u", "nobody",
@@ -88,10 +88,21 @@
 			"-B", sandboxConfig.srcDir,
 			"-B", "/tmp",
 			"-B", sandboxConfig.outDir,
-			"-B", sandboxConfig.distDir,
+		}
+
+		if _, err := os.Stat(sandboxConfig.distDir); !os.IsNotExist(err) {
+			//Mount dist dir as read-write if it already exists
+			sandboxArgs = append(sandboxArgs, "-B",
+				sandboxConfig.distDir)
+		}
+
+		sandboxArgs = append(sandboxArgs,
 			"--disable_clone_newcgroup",
 			"--",
 			"/bin/bash", "-c", `if [ $(hostname) == "android-build" ]; then echo "Android" "Success"; else echo Failure; fi`)
+
+		cmd := exec.CommandContext(c.ctx.Context, nsjailPath, sandboxArgs...)
+
 		cmd.Env = c.config.Environment().Environ()
 
 		c.ctx.Verboseln(cmd.Args)
@@ -164,9 +175,6 @@
 		//Mount out dir as read-write
 		"-B", sandboxConfig.outDir,
 
-		//Mount dist dir as read-write
-		"-B", sandboxConfig.distDir,
-
 		// Mount a writable tmp dir
 		"-B", "/tmp",
 
@@ -178,6 +186,11 @@
 		"-q",
 	}
 
+	if _, err := os.Stat(sandboxConfig.distDir); !os.IsNotExist(err) {
+		//Mount dist dir as read-write if it already exists
+		sandboxArgs = append(sandboxArgs, "-B", sandboxConfig.distDir)
+	}
+
 	if c.Sandbox.AllowBuildBrokenUsesNetwork && c.config.BuildBrokenUsesNetwork() {
 		c.ctx.Printf("AllowBuildBrokenUsesNetwork: %v", c.Sandbox.AllowBuildBrokenUsesNetwork)
 		c.ctx.Printf("BuildBrokenUsesNetwork: %v", c.config.BuildBrokenUsesNetwork())