Skip to content

Commit

Permalink
Change the list of rc files accepted.
Browse files Browse the repository at this point in the history
The old list was, in order:
- %workspace%/tools/bazel.rc (unless --nomaster_bazelrc)
- %binary_dir%/bazel.bazelrc (unless --nomaster_bazelrc)
- system rc, /etc/bazel.bazelrc or in %ProgramData% for Windows (unless --nomaster_bazelrc)
- the first of the following gets called the "user" bazelrc
  - path passed by flag --bazelrc
  - %workspace%/.bazelrc
  - $HOME/.bazelrc

The new list is hopefully a bit more consistent, as:
- system rc (unless --nosystem_rc)
- workspace, %workspace%/.bazelrc (unless --noworkspace_rc)
- user, $HOME/.bazelrc (unless --nohome_rc)
- command-line provided, passed as --bazelrc or nothing if the flag is absent.

This list removes two less than useful locations, duplication in the Workspace directory, and the rc next to the bazel binary. This location made sense at Google but is generally nonsensical elsewhere so we are removing it. It also stops the user local rc file from being overriden by passing in a custom file in --bazelrc.

In both old and new, --ignore_all_rc_files disables all of the above.
For a transition period, any file that you would have loaded but was not read will cause a WARNING to be printed. If you want the old file to still be read without moving its location, you can always import it into one of the new standard locations, or create a symlink.

Closes #4502, except for cleanup to remove the warning after a transition period of 1 Bazel version has passed.

RELNOTES[INC]: New bazelrc file list.

PiperOrigin-RevId: 207189212
  • Loading branch information
cvcal authored and Copybara-Service committed Aug 2, 2018
1 parent 1ae4fc2 commit ec83598
Show file tree
Hide file tree
Showing 14 changed files with 523 additions and 278 deletions.
6 changes: 3 additions & 3 deletions scripts/bash_completion_test.sh
Expand Up @@ -406,9 +406,9 @@ test_basic_subcommand_expansion() {
'shutdown '
}

test_common_options() {
# 'Test common option completion'
assert_expansion '--h' \
test_common_startup_options() {
# 'Test common startup option completion'
assert_expansion '--hos' \
'--host_jvm_'
assert_expansion '--host_jvm_a' \
'--host_jvm_args='
Expand Down
50 changes: 26 additions & 24 deletions site/docs/user-manual.html
Expand Up @@ -134,41 +134,43 @@ <h3 id='bazelrc'>
Bazel allows you to specify options in a configuration file.
</p>

<h4>Where are <code>.bazelrc</code> files?</h4>

<h4>Where are the <code>.bazelrc</code> files?</h4>
<p>
Bazel looks for an optional configuration file in the following locations,
in order. It will stop searching once it has successfully found a file.
in the order shown below. The options are interpreted in this order, so
options in later files can override a value from an earlier file if a
conflict arises. All options to control which of these files are loaded are
startup options, which means they much occur after <code>bazel</code> and
before the command (<code>build</code>, etc).
</p>
<ol>
<li>
The path specified by the <code class='flag'>--bazelrc=<var>file</var></code>
startup option. If specified, this option must appear <em>before</em> the
command name (e.g. <code>build</code>)
Unless the <code class='flag'>--nosystem_rc</code> is present, Bazel looks for
the system .bazelrc file: on Unix, it lives at <code>/etc/bazel.bazelrc</code>,
and on Windows at <code>%%ProgramData%%/bazel.bazelrc</code>.

If another system-specified location is required, this value can be
changed by setting <code>BAZEL_SYSTEM_BAZELRC_PATH</code> in
<code>src/main/cpp:option_processor</code> and using this custom Bazel binary.
</li>
<li>
A file named <code>.bazelrc</code> in your base workspace directory
Unless the <code class='flag'>--noworkspace_rc</code> is present, Bazel looks
for the <code>.bazelrc</code> file in your workspace directory.
</li>
<li>
Unless the <code class='flag'>--nohome_rc</code> is present, Bazel looks for
the home, or user, bazelrc: the file <code>.bazelrc</code> in your home
directory.
</li>
<li>
A file named <code>.bazelrc</code> in your home directory
An additional .rc file can be specified by the
<code class='flag'>--bazelrc=<var>file</var></code> startup option. If this
option is not present, no additional file is loaded. Unlike in the three
default locations specified above, an incorrect path or non-existent file
will fail if passed explicitly.
</li>
</ol>
<p>
The option <code class='flag'>--bazelrc=/dev/null</code> effectively disables the
use of a configuration file. We strongly recommend that you use
this option when performing release builds, or automated tests that
invoke Bazel.
</p>

<p>
Aside from the optional configuration file described above, Bazel also looks
for a master rc file named <code>bazel.bazelrc</code> next to the binary, in
the workspace at <code>tools/bazel.rc</code> or system-wide at
<code>/etc/bazel.bazelrc</code>. These files are here to support
installation-wide options or options shared between users. These files do not
override one another; if all of these files exist, all of them will be loaded.
Reading of these files can be disabled using the
<code class='flag'>--nomaster_bazelrc</code> option.
</p>
<h4><code>.bazelrc</code> syntax and semantics</h4>
<p>
Like all UNIX "rc" files, the <code>.bazelrc</code> file is a text file with
Expand Down
64 changes: 61 additions & 3 deletions src/main/cpp/bazel_startup_options.cc
Expand Up @@ -25,8 +25,14 @@ BazelStartupOptions::BazelStartupOptions(
const WorkspaceLayout *workspace_layout)
: StartupOptions("Bazel", workspace_layout),
user_bazelrc_(""),
use_system_rc(true),
use_workspace_rc(true),
use_home_rc(true),
use_master_bazelrc_(true) {
RegisterNullaryStartupFlag("home_rc");
RegisterNullaryStartupFlag("master_bazelrc");
RegisterNullaryStartupFlag("system_rc");
RegisterNullaryStartupFlag("workspace_rc");
RegisterUnaryStartupFlag("bazelrc");
}

Expand All @@ -42,6 +48,48 @@ blaze_exit_code::ExitCode BazelStartupOptions::ProcessArgExtra(
return blaze_exit_code::BAD_ARGV;
}
user_bazelrc_ = *value;
} else if (GetNullaryOption(arg, "--system_rc")) {
if (!rcfile.empty()) {
*error = "Can't specify --system_rc in .bazelrc file.";
return blaze_exit_code::BAD_ARGV;
}
use_system_rc = true;
option_sources["system_rc"] = rcfile;
} else if (GetNullaryOption(arg, "--nosystem_rc")) {
if (!rcfile.empty()) {
*error = "Can't specify --nosystem_rc in .bazelrc file.";
return blaze_exit_code::BAD_ARGV;
}
use_system_rc = false;
option_sources["system_rc"] = rcfile;
} else if (GetNullaryOption(arg, "--workspace_rc")) {
if (!rcfile.empty()) {
*error = "Can't specify --workspace_rc in .bazelrc file.";
return blaze_exit_code::BAD_ARGV;
}
use_workspace_rc = true;
option_sources["workspace_rc"] = rcfile;
} else if (GetNullaryOption(arg, "--noworkspace_rc")) {
if (!rcfile.empty()) {
*error = "Can't specify --noworkspace_rc in .bazelrc file.";
return blaze_exit_code::BAD_ARGV;
}
use_workspace_rc = false;
option_sources["workspace_rc"] = rcfile;
} else if (GetNullaryOption(arg, "--home_rc")) {
if (!rcfile.empty()) {
*error = "Can't specify --home_rc in .bazelrc file.";
return blaze_exit_code::BAD_ARGV;
}
use_home_rc = true;
option_sources["home_rc"] = rcfile;
} else if (GetNullaryOption(arg, "--nohome_rc")) {
if (!rcfile.empty()) {
*error = "Can't specify --nohome_rc in .bazelrc file.";
return blaze_exit_code::BAD_ARGV;
}
use_home_rc = false;
option_sources["home_rc"] = rcfile;
} else if (GetNullaryOption(arg, "--master_bazelrc")) {
if (!rcfile.empty()) {
*error = "Can't specify --master_bazelrc in .bazelrc file.";
Expand Down Expand Up @@ -71,9 +119,19 @@ void BazelStartupOptions::MaybeLogStartupOptionWarnings() const {
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 "
if ((use_home_rc) &&
option_sources.find("home_rc") != option_sources.end()) {
BAZEL_LOG(WARNING) << "Explicit value of --home_rc is "
"ignored, since --ignore_all_rc_files is on.";
}
if ((use_system_rc) &&
option_sources.find("system_rc") != option_sources.end()) {
BAZEL_LOG(WARNING) << "Explicit value of --system_rc is "
"ignored, since --ignore_all_rc_files is on.";
}
if ((use_workspace_rc) &&
option_sources.find("workspace_rc") != option_sources.end()) {
BAZEL_LOG(WARNING) << "Explicit value of --workspace_rc is "
"ignored, since --ignore_all_rc_files is on.";
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/cpp/bazel_startup_options.h
Expand Up @@ -34,6 +34,10 @@ class BazelStartupOptions : public StartupOptions {

private:
std::string user_bazelrc_;
bool use_system_rc;
bool use_workspace_rc;
bool use_home_rc;
// TODO(b/36168162): Remove the master rc flag.
bool use_master_bazelrc_;
};

Expand Down
14 changes: 14 additions & 0 deletions src/main/cpp/option_processor-internal.h
Expand Up @@ -16,6 +16,7 @@
#define BAZEL_SRC_MAIN_CPP_OPTION_PROCESSOR_INTERNAL_H_

#include <algorithm>
#include <set>

#include "src/main/cpp/rc_file.h"
#include "src/main/cpp/util/exit_code.h"
Expand All @@ -30,6 +31,19 @@ namespace internal {
std::vector<std::string> DedupeBlazercPaths(
const std::vector<std::string>& paths);

// Get the legacy list of rc files that would have been loaded - this is to
// provide a useful warning if files are being ignored that were loaded in a
// previous version of Bazel.
// TODO(b/3616816): Remove this once the warning is no longer useful.
std::set<std::string> GetOldRcPaths(
const WorkspaceLayout* workspace_layout, const std::string& workspace,
const std::string& cwd, const std::string& path_to_binary,
const std::vector<std::string>& startup_args);

// Returns what the "user bazelrc" would have been in the legacy rc list.
std::string FindLegacyUserBazelrc(const char* cmd_line_rc_file,
const std::string& workspace);

std::string FindSystemWideRc();

std::string FindRcAlongsideBinary(const std::string& cwd,
Expand Down

0 comments on commit ec83598

Please sign in to comment.