Skip to content

Commit

Permalink
Ignore read-only errors when updating the mtime of the install_base
Browse files Browse the repository at this point in the history
Currently if the `--install_base` path passed is not writable by the
user invoking Bazel, the Bazel client crashes[^1]:
```console
❯ bazel --install_base=/some/read/only/path version
FATAL: failed to set timestamp on '/some/read/only/path': (error: 30): Read-only file system
```

This happens because the Bazel client (unconditionally) attempts to
update the `mtime` of this path:
https://github.com/bazelbuild/bazel/blob/a3c677dfea2de636a719d50345a5a97af96fae60/src/main/cpp/blaze.cc#L1018-L1029

This commit updates the client to ignore such errors. See bazelbuild#20373 for
context.
  • Loading branch information
rrbutani committed Dec 5, 2023
1 parent dfcb9ef commit ed3d573
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/main/cpp/blaze.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
#include <unistd.h>
#endif

#if !defined(_WIN32)
#include <cerrno>
#else
#include <windows.h>
#endif

#include "src/main/cpp/archive_utils.h"
#include "src/main/cpp/blaze_util.h"
#include "src/main/cpp/blaze_util_platform.h"
Expand Down Expand Up @@ -1012,10 +1018,24 @@ static void EnsureCorrectRunningVersion(const StartupOptions &startup_options,
std::unique_ptr<blaze_util::IFileMtime> mtime(
blaze_util::CreateFileMtime());
if (!mtime->SetToNow(blaze_util::Path(startup_options.install_base))) {
string err = GetLastErrorString();
BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
<< "failed to set timestamp on '" << startup_options.install_base
<< "': " << err;
// Ignore permissions errors (i.e. if the install base is not writable):
bool raise_error = true;

#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__APPLE__) || defined(__CYGWIN__)
// On POSIX platforms, `SetToNow` is backed by `utime(2)` which can
// return `EROFS`/`EPERM`:
if (errno == EROFS || errno == EPERM) { raise_error = false; }
#elif defined(_WIN32)
// On Windows, `SetToNow` is backed by `CreateFileW` + `SetFileTime`:
if (::GetLastError() == ERROR_ACCESS_DENIED) { raise_error = false; }
#endif

if (raise_error) {
string err = GetLastErrorString();
BAZEL_DIE(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR)
<< "failed to set timestamp on '" << startup_options.install_base
<< "': " << err;
}
}
}
}
Expand Down

0 comments on commit ed3d573

Please sign in to comment.