Allow new_ rules to overwrited BUILD files in downloaded repos

My previous change carefully checked that the file was a symlink before
removing it and added a test with local repositories... and it of course isn't
a symlink with downloaded repositories and crashes.

Fixes #1697.

--
MOS_MIGRATED_REVID=134536130
diff --git a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java
index 1e6429d..bec9ca1 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/repository/RepositoryFunction.java
@@ -40,6 +40,7 @@
 import com.google.devtools.build.lib.vfs.Path;
 import com.google.devtools.build.lib.vfs.PathFragment;
 import com.google.devtools.build.lib.vfs.RootedPath;
+import com.google.devtools.build.lib.vfs.Symlinks;
 import com.google.devtools.build.skyframe.SkyFunction;
 import com.google.devtools.build.skyframe.SkyFunction.Environment;
 import com.google.devtools.build.skyframe.SkyFunctionException;
@@ -198,9 +199,10 @@
       Path repositoryDirectory, String contents) throws RepositoryFunctionException {
     Path buildFilePath = repositoryDirectory.getRelative("BUILD");
     try {
-      // Make sure we're not overwriting an existing BUILD file.
-      if (buildFilePath.exists()) {
-        Preconditions.checkState(buildFilePath.isSymbolicLink());
+      // The repository could have an existing BUILD file that's either a regular file (for remote
+      // repositories) or a symlink (for local repositories). Either way, we want to remove it and
+      // write our own.
+      if (buildFilePath.exists(Symlinks.NOFOLLOW)) {
         buildFilePath.delete();
       }
       FileSystemUtils.writeContentAsLatin1(buildFilePath, contents);
diff --git a/src/test/shell/bazel/external_integration_test.sh b/src/test/shell/bazel/external_integration_test.sh
index 33f2573..477e165 100755
--- a/src/test/shell/bazel/external_integration_test.sh
+++ b/src/test/shell/bazel/external_integration_test.sh
@@ -720,7 +720,8 @@
 function test_changing_build_file() {
   echo "abc" > w
   echo "def" > w.new
-  tar czf x.tar.gz w w.new
+  echo "I'm a build file" > BUILD
+  tar czf x.tar.gz w w.new BUILD
   local sha256=$(sha256sum x.tar.gz | cut -f 1 -d ' ')
   serve_file x.tar.gz