Skip to content

Commit 7cfb6e4

Browse files
committed
Replace bind-mount with volume as Bazel Cache
Since bind-mounting the Bazel cache from the host causes various problems (problematic access rights; incompatibilities with CodeSpaces) without major benefits, we use now a regular named volume as persistent cache.
1 parent 1c8be1b commit 7cfb6e4

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

src/s-core-devcontainer/.devcontainer/s-core-local/devcontainer-feature.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
"postCreateCommand": "if [ -f .bazelversion ] && [ \"$(cat .bazelversion)\" != \"$(dpkg --list | grep 'ii bazel ' | awk '{print $3}')\" ]; then sudo apt-get update && sudo apt-get install -y --allow-downgrades bazel=$(cat .bazelversion); fi",
3737
"mounts": [
3838
{
39-
"source": "${localEnv:HOME}/.cache/bazel", // default Bazel cache directory
39+
"source": "eclipse-s-core-bazel-cache",
4040
"target": "/var/cache/bazel",
41-
"type": "bind"
41+
"type": "volume"
4242
}
4343
]
4444
}

src/s-core-devcontainer/.devcontainer/s-core-local/on_create_command.sh

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

4-
# In some scenarios (like CodeSpaces), apparently not all (or even any?)
5-
# bind mounts to the host are done and /var/cache/bazel does not exist.
6-
# See devcontainer-feature.json, where this mount is defined.
7-
# Hence, if /var/cache/bazel exists, it was mounted from the host,
8-
# and we configure Bazel to use /var/cache/bazel as cache.
9-
# This way, Bazel re-uses a bind-mounted cache from the host machine, while
10-
# still using the default cache (${HOME}/.cache/bazel) if no such cache exists.
4+
# Enable persistent Bazel cache
5+
#
6+
# Usually, a volume is mounted to /var/cache/bazel (see
7+
# devcontainer-feature.json). This shall be used as Bazel cache, which is then
8+
# preserved across container re-starts. Since the volume has a fixed
9+
# name ("eclipse-s-core-bazel-cache"), it is even used across all Eclipse
10+
# S-CORE DevContainer instances.
1111
if [ -d /var/cache/bazel ]; then
12-
echo "Configuring Bazel to use /var/cache/bazel as cache..."
12+
echo "Bazel Cache: /var/cache/bazel exists. Checking ownership and configuring Bazel to use it as cache..."
13+
current_owner_group=$(stat -c "%U:%G" /var/cache/bazel)
14+
current_user_group="$(id -un):$(id -gn)"
15+
if [ "${current_owner_group}" = "${current_user_group}" ]; then
16+
echo "Bazel Cache: /var/cache/bazel is already owned by ${current_user_group}. "
17+
else
18+
echo "Bazel Cache: /var/cache/bazel is not owned by ${current_owner_group}. Setting ownership (this may take a few seconds) ..."
19+
sudo chown -R "${current_user_group}" /var/cache/bazel
20+
fi
21+
echo "Bazel Cache: Configuring Bazel to use /var/cache/bazel as cache..."
1322
echo "startup --output_user_root=/var/cache/bazel" >> ~/.bazelrc
1423
fi
1524

0 commit comments

Comments
 (0)