Add --ignore_all_rc_files startup options.
This overrides --bazelrc and --[no]master_bazelrc regardless of order. Like --bazelrc and --[no]master_bazelrc, it cannot be mentioned in an rc file, this would be a contradiction. This flag is useful for testing, and for having a version-agnostic way to turn off all rc files, such as in the canonical command line reporting. Now that blazerc and bazelrc are separate, this is necessary.
If explicit values for --bazelrc or --master_bazelrc are provided which are now ignored, Bazel will warn the user.
#4502
Alternatives considered - We could avoid this flag but would need to have some well-documented, reusable list of the startup flags that effectively come to the same effect. This would be necessary in our integration tests and in the CommandLineEvent and other places where rc files need to be completely disabled for correctness. We decided that this startup option was more straightforward and usable for both users and Bazel devs: it shouldn't be used when more fine-grained control is needed, but provides warnings if users are likely to be confused by the outcome.
RELNOTES: None.
PiperOrigin-RevId: 196750704
diff --git a/src/test/cpp/rc_file_test.cc b/src/test/cpp/rc_file_test.cc
index 70b3f93..5900db1 100644
--- a/src/test/cpp/rc_file_test.cc
+++ b/src/test/cpp/rc_file_test.cc
@@ -229,6 +229,96 @@
using ParseOptionsTest = RcFileTest;
+TEST_F(ParseOptionsTest, IgnoreAllRcFilesIgnoresAllMasterAndUserRcFiles) {
+ // Put fake options in different expected rc files, to check that none of them
+ // are read.
+ std::string user_workspace_rc;
+ ASSERT_TRUE(
+ SetUpUserRcFileInWorkspace("startup --userfoo", &user_workspace_rc));
+ std::string workspace_rc;
+ ASSERT_TRUE(SetUpMasterRcFileInWorkspace("startup --workspacemasterfoo",
+ &workspace_rc));
+ std::string binary_rc;
+ ASSERT_TRUE(SetUpMasterRcFileAlongsideBinary("startup --binarymasterfoo",
+ &binary_rc));
+
+ const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
+ "build"};
+ // Expect no error due to the incorrect options, as non of them should have
+ // been loaded.
+ std::string error;
+ EXPECT_EQ(blaze_exit_code::SUCCESS,
+ option_processor_->ParseOptions(args, workspace_, cwd_, &error));
+ ASSERT_EQ("", error);
+
+ // Check that the startup options' provenance message contains nothing
+ testing::internal::CaptureStderr();
+ option_processor_->PrintStartupOptionsProvenanceMessage();
+ const std::string& output = testing::internal::GetCapturedStderr();
+
+ EXPECT_EQ(output, "");
+}
+
+TEST_F(ParseOptionsTest, LaterIgnoreRcFileValueWins) {
+ std::string workspace_rc;
+ ASSERT_TRUE(SetUpMasterRcFileInWorkspace("startup --workspacemasterfoo",
+ &workspace_rc));
+
+ const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
+ "--noignore_all_rc_files", "build"};
+ std::string error;
+ EXPECT_EQ(blaze_exit_code::BAD_ARGV,
+ option_processor_->ParseOptions(args, workspace_, cwd_, &error));
+ ASSERT_EQ(
+ "Unknown startup option: '--workspacemasterfoo'.\n For more info, run "
+ "'bazel help startup_options'.",
+ error);
+
+ // Check that the startup options' provenance message contains the provenance
+ // of the incorrect option.
+ testing::internal::CaptureStderr();
+ option_processor_->PrintStartupOptionsProvenanceMessage();
+ const std::string& output = testing::internal::GetCapturedStderr();
+
+ EXPECT_THAT(output,
+ MatchesRegex("INFO: Reading 'startup' options from .*bazel.rc: "
+ "--workspacemasterfoo\n"));
+}
+
+TEST_F(ParseOptionsTest, IgnoreAllRcFilesIgnoresCommandLineRcFileToo) {
+ // Put fake options in different expected rc files, to check that none of them
+ // are read.
+ std::string workspace_rc;
+ ASSERT_TRUE(SetUpMasterRcFileInWorkspace("startup --workspacemasterfoo",
+ &workspace_rc));
+ std::string binary_rc;
+ ASSERT_TRUE(SetUpMasterRcFileAlongsideBinary("startup --binarymasterfoo",
+ &binary_rc));
+ const std::string cmdline_rc_path =
+ blaze_util::JoinPath(workspace_, "mybazelrc");
+ ASSERT_TRUE(
+ blaze_util::MakeDirectories(blaze_util::Dirname(cmdline_rc_path), 0755));
+ ASSERT_TRUE(
+ blaze_util::WriteFile("startup --userfoo", cmdline_rc_path, 0755));
+
+ const std::vector<std::string> args = {binary_path_, "--ignore_all_rc_files",
+ "--bazelrc=" + cmdline_rc_path,
+ "build"};
+ // Expect no error due to the incorrect options, as non of them should have
+ // been loaded.
+ std::string error;
+ EXPECT_EQ(blaze_exit_code::SUCCESS,
+ option_processor_->ParseOptions(args, workspace_, cwd_, &error));
+ ASSERT_EQ("", error);
+
+ // Check that the startup options' provenance message contains nothing
+ testing::internal::CaptureStderr();
+ option_processor_->PrintStartupOptionsProvenanceMessage();
+ const std::string& output = testing::internal::GetCapturedStderr();
+
+ EXPECT_EQ(output, "");
+}
+
TEST_F(ParseOptionsTest, CommandLineBazelrcHasUnknownOption) {
const std::string cmdline_rc_path =
blaze_util::JoinPath(workspace_, "mybazelrc");