Temporarily restore processing of workspace-wide tools/bazel.rc file.

Fixes #6321.

Change-Id: I5f1e1ac4f9ae9b0001b6ea707c981686bf91aa12

Closes #6322.

Change-Id: If6b134c74a316e4119fb3f5894cca2f1f8be6882
PiperOrigin-RevId: 215932254
diff --git a/src/main/cpp/option_processor.cc b/src/main/cpp/option_processor.cc
index 04f7f4f..f8fe5c8 100644
--- a/src/main/cpp/option_processor.cc
+++ b/src/main/cpp/option_processor.cc
@@ -23,6 +23,7 @@
 #include <set>
 #include <sstream>
 #include <utility>
+#include <vector>
 
 #include "src/main/cpp/blaze_util.h"
 #include "src/main/cpp/blaze_util_platform.h"
@@ -322,6 +323,9 @@
       SearchNullaryOption(cmd_line->startup_args, "workspace_rc", true)) {
     const std::string workspaceRcFile =
         blaze_util::JoinPath(workspace, kRcBasename);
+    // Legacy behavior.
+    rc_files.push_back(workspace_layout->GetWorkspaceRcPath(
+        workspace, cmd_line->startup_args));
     rc_files.push_back(workspaceRcFile);
   }
 
@@ -397,16 +401,22 @@
       workspace_layout, workspace, cwd, cmd_line->path_to_binary,
       cmd_line->startup_args, internal::FindSystemWideRc(system_bazelrc_path_));
 
+  std::vector<std::string> lost_files;
+  for (auto it = old_files.begin(); it != old_files.end(); it++) {
+    // we record canonical file names in read_files, so we need to
+    // canonicalize old file name, but we still report uncanonicalized
+    // names in error messages.
+    std::string canonical_old_file = blaze_util::MakeCanonical(it->c_str());
+    if (read_files.find(canonical_old_file) == read_files.end()) {
+      lost_files.push_back(*it);
+    }
+  }
+
   //   std::vector<std::string> old_files = internal::GetOldRcPathsInOrder(
   //       workspace_layout, workspace, cwd, cmd_line->path_to_binary,
   //       cmd_line->startup_args);
   //
   //   std::sort(old_files.begin(), old_files.end());
-  std::vector<std::string> lost_files(old_files.size());
-  std::vector<std::string>::iterator end_iter = std::set_difference(
-      old_files.begin(), old_files.end(), read_files.begin(), read_files.end(),
-      lost_files.begin());
-  lost_files.resize(end_iter - lost_files.begin());
   if (!lost_files.empty()) {
     std::string joined_lost_rcs;
     blaze_util::JoinStrings(lost_files, '\n', &joined_lost_rcs);
@@ -417,6 +427,17 @@
         << joined_lost_rcs;
   }
 
+  std::string legacy_workspace_file =
+      workspace_layout->GetWorkspaceRcPath(workspace, cmd_line->startup_args);
+  if (old_files.find(legacy_workspace_file) != old_files.end()) {
+    BAZEL_LOG(WARNING)
+        << "Processed legacy workspace file "
+        << legacy_workspace_file
+        << ". This file will not be processed in the next release of Bazel."
+        << " Please read https://github.com/bazelbuild/bazel/issues/6319"
+        << " for further information, including how to upgrade.";
+  }
+
   return blaze_exit_code::SUCCESS;
 }
 
diff --git a/src/test/cpp/rc_file_test.cc b/src/test/cpp/rc_file_test.cc
index 711e7b6..ffce9ec 100644
--- a/src/test/cpp/rc_file_test.cc
+++ b/src/test/cpp/rc_file_test.cc
@@ -261,8 +261,38 @@
   // read as expected.
   EXPECT_THAT(output,
               HasSubstr("The following rc files are no longer being read"));
-  EXPECT_THAT(output, HasSubstr(workspace_rc));
   EXPECT_THAT(output, HasSubstr(binary_rc));
+
+  EXPECT_THAT(output, HasSubstr("Processed legacy workspace file"));
+  EXPECT_THAT(output, HasSubstr(workspace_rc));
+}
+
+TEST_F(GetRcFileTest, GetRcFilesWarnsAboutLegacyWorkspaceFile) {
+  std::string workspace_rc;
+  ASSERT_TRUE(SetUpLegacyMasterRcFileInWorkspace("", &workspace_rc));
+
+  const CommandLine cmd_line = CommandLine(binary_path_, {}, "build", {});
+  std::string error = "check that this string is not modified";
+  std::vector<std::unique_ptr<RcFile>> parsed_rcs;
+
+  testing::internal::CaptureStderr();
+  const blaze_exit_code::ExitCode exit_code =
+      option_processor_->GetRcFiles(workspace_layout_.get(), workspace_, cwd_,
+                                    &cmd_line, &parsed_rcs, &error);
+  const std::string output = testing::internal::GetCapturedStderr();
+
+  EXPECT_EQ(blaze_exit_code::SUCCESS, exit_code);
+  EXPECT_EQ("check that this string is not modified", error);
+
+  // tools/blaze.rc should be read...
+  EXPECT_THAT(
+      output,
+      Not(HasSubstr("The following rc files are no longer being read")));
+
+  // ... but reported specially.
+  // (cf https://github.com/bazelbuild/bazel/issues/6321).
+  EXPECT_THAT(output, HasSubstr("Processed legacy workspace file"));
+  EXPECT_THAT(output, HasSubstr(workspace_rc));
 }
 
 TEST_F(