Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect XDG directory settings on macOS #25205

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions site/en/remote/output-directories.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ The solution that's currently implemented:
subdirectory thereof. In other words, Bazel must be invoked from inside a
[repository](../external/overview#repository). Otherwise, an error is
reported.
* The _outputRoot_ directory defaults to `${XDG_CACHE_HOME}/bazel` (or
`~/.cache/bazel`, if the `XDG_CACHE_HOME` environment variable is not set) on
Linux, `/private/var/tmp` on macOS, and on Windows it defaults to `%HOME%` if
* The _outputRoot_ directory defaults to `~/.cache/bazel` on Linux,
`/private/var/tmp` on macOS, and on Windows it defaults to `%HOME%` if
set, else `%USERPROFILE%` if set, else the result of calling
`SHGetKnownFolderPath()` with the `FOLDERID_Profile` flag set. If the
environment variable `$TEST_TMPDIR` is set, as in a test of Bazel itself,
then that value overrides the default.
environment variable `$XDG_CACHE_HOME` is set on either Linux or
macOS, the value `${XDG_CACHE_HOME}/bazel` will override the default.
If the environment variable `$TEST_TMPDIR` is set, as in a test of Bazel
itself, then that value overrides any defaults.
* The Bazel user's build state is located beneath `outputRoot/_bazel_$USER`.
This is called the _outputUserRoot_ directory.
* Beneath the `outputUserRoot` directory there is an `install` directory, and in
Expand Down
8 changes: 7 additions & 1 deletion src/main/cpp/blaze_util_darwin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,13 @@ static string DescriptionFromCFError(CFErrorRef cf_err) {
return UTF8StringFromCFStringRef(cf_err_string);
}

string GetCacheDir() { return "/var/tmp"; }
string GetCacheDir() {
string xdg_cache_home = GetPathEnv("XDG_CACHE_HOME");
if (!xdg_cache_home.empty()) {
return blaze_util::JoinPath(xdg_cache_home, "bazel");
}
return "/var/tmp";
}

void WarnFilesystemType(const blaze_util::Path &output_base) {
// Check to see if we are on a non-local drive.
Expand Down
63 changes: 63 additions & 0 deletions src/test/cpp/bazel_startup_options_test.cc
aaronsky marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,69 @@ TEST_F(BazelStartupOptionsTest, UpdateConfigurationOnDarwin) {
"1629dee48cc4e53161f9b2be8614e062"),
startup_options_->output_base);
}

TEST_F(BazelStartupOptionsTest, UpdateConfigurationOnDarwinWithTestTmpdir) {
SetEnv("USER", "gandalf");
SetEnv("HOME", "/nonexistent/home");
SetEnv("XDG_CACHE_HOME", "/nonexistent/cache");
SetEnv("TEST_TMPDIR", "/nonexistent/tmpdir");
ReinitStartupOptions();
UpdateConfiguration();

ASSERT_EQ(blaze_util::Path("/nonexistent/tmpdir/_bazel_gandalf"),
startup_options_->output_user_root);
ASSERT_EQ(blaze_util::Path("/nonexistent/tmpdir/_bazel_gandalf/install/deadbeef"),
startup_options_->install_base);
ASSERT_EQ(blaze_util::Path("/nonexistent/tmpdir/_bazel_gandalf/"
"1629dee48cc4e53161f9b2be8614e062"),
startup_options_->output_base);
}

TEST_F(BazelStartupOptionsTest, UpdateConfigurationOnDarwinWithXdgCacheHome) {
SetEnv("USER", "gandalf");
SetEnv("HOME", "/nonexistent/home");
SetEnv("XDG_CACHE_HOME", "/nonexistent/cache");
UnsetEnv("TEST_TMPDIR");
ReinitStartupOptions();
UpdateConfiguration();

ASSERT_EQ(blaze_util::Path("/nonexistent/cache/bazel/_bazel_gandalf"),
startup_options_->output_user_root);
ASSERT_EQ(blaze_util::Path("/nonexistent/cache/bazel/_bazel_gandalf/install/deadbeef"),
startup_options_->install_base);
ASSERT_EQ(blaze_util::Path("/nonexistent/cache/bazel/_bazel_gandalf/"
"1629dee48cc4e53161f9b2be8614e062"),
startup_options_->output_base);
}

TEST_F(BazelStartupOptionsTest, UpdateConfigurationOnDarwinNoShellExpansion) {
SetEnv("USER", "gandalf");
SetEnv("TEST_TMPDIR", "~/\"$foo/test\"");
SetEnv("XDG_CACHE_HOME", "~/cache${bar}");
SetEnv("HOME", "~/home$(echo baz)");

ReinitStartupOptions();
UpdateConfiguration();

ASSERT_EQ(blaze_util::Path(blaze_util::GetCwd() + "/~/\"$foo/test\"" +
"/_bazel_gandalf"),
startup_options_->output_user_root);

UnsetEnv("TEST_TMPDIR");
ReinitStartupOptions();
UpdateConfiguration();

ASSERT_EQ(blaze_util::Path(blaze_util::GetCwd() +
"/~/cache${bar}/bazel/_bazel_gandalf"),
startup_options_->output_user_root);

UnsetEnv("XDG_CACHE_HOME");
ReinitStartupOptions();
UpdateConfiguration();

ASSERT_EQ(blaze_util::Path("/var/tmp/_bazel_gandalf"),
startup_options_->output_user_root);
}
#endif // __APPLE__

#if defined(__WIN32__) || defined(__CYGWIN__)
Expand Down