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/main/cpp/bazel_startup_options.cc b/src/main/cpp/bazel_startup_options.cc index f3138f7..950647e 100644 --- a/src/main/cpp/bazel_startup_options.cc +++ b/src/main/cpp/bazel_startup_options.cc
@@ -16,13 +16,16 @@ #include <cassert> #include "src/main/cpp/blaze_util.h" +#include "src/main/cpp/util/logging.h" #include "src/main/cpp/workspace_layout.h" namespace blaze { BazelStartupOptions::BazelStartupOptions( const WorkspaceLayout *workspace_layout) - : StartupOptions("Bazel", workspace_layout) { + : StartupOptions("Bazel", workspace_layout), + user_bazelrc_(""), + use_master_bazelrc_(true) { RegisterNullaryStartupFlag("master_bazelrc"); RegisterUnaryStartupFlag("bazelrc"); } @@ -38,12 +41,20 @@ *error = "Can't specify --bazelrc in the .bazelrc file."; return blaze_exit_code::BAD_ARGV; } - } else if (GetNullaryOption(arg, "--nomaster_bazelrc") || - GetNullaryOption(arg, "--master_bazelrc")) { + user_bazelrc_ = *value; + } else if (GetNullaryOption(arg, "--master_bazelrc")) { if (!rcfile.empty()) { - *error = "Can't specify --[no]master_bazelrc in .bazelrc file."; + *error = "Can't specify --master_bazelrc in .bazelrc file."; return blaze_exit_code::BAD_ARGV; } + use_master_bazelrc_ = true; + option_sources["blazerc"] = rcfile; + } else if (GetNullaryOption(arg, "--nomaster_bazelrc")) { + if (!rcfile.empty()) { + *error = "Can't specify --nomaster_bazelrc in .bazelrc file."; + return blaze_exit_code::BAD_ARGV; + } + use_master_bazelrc_ = false; option_sources["blazerc"] = rcfile; } else { *is_processed = false; @@ -54,4 +65,18 @@ return blaze_exit_code::SUCCESS; } +void BazelStartupOptions::MaybeLogStartupOptionWarnings() const { + if (ignore_all_rc_files) { + if (!user_bazelrc_.empty()) { + BAZEL_LOG(WARNING) << "Value of --bazelrc is ignored, since " + "--ignore_all_rc_files is on."; + } + if ((use_master_bazelrc_) && + option_sources.find("blazerc") != option_sources.end()) { + BAZEL_LOG(WARNING) << "Explicit value of --master_bazelrc is " + "ignored, since --ignore_all_rc_files is on."; + } + } +} + } // namespace blaze