Skip to content

Commit

Permalink
experimental_repository_cache: support relative paths
Browse files Browse the repository at this point in the history
...and interpret them relative to the workspace directory.

Improves on #3516. In fact, fixes the original request of
supporting relative paths.

Change-Id: Ibbb6fd43179d589ad477427e47e26c773c7a04de
PiperOrigin-RevId: 185121629
  • Loading branch information
aehlig authored and Copybara-Service committed Feb 9, 2018
1 parent 1d46d62 commit 157caed
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,15 @@ public void beforeCommand(CommandEnvironment env) {
RepositoryOptions repoOptions = env.getOptions().getOptions(RepositoryOptions.class);
if (repoOptions != null) {
if (repoOptions.experimentalRepositoryCache != null) {
Path repositoryCachePath = filesystem.getPath(repoOptions.experimentalRepositoryCache);
Path repositoryCachePath;
if (repoOptions.experimentalRepositoryCache.isAbsolute()) {
repositoryCachePath = filesystem.getPath(repoOptions.experimentalRepositoryCache);
} else {
repositoryCachePath =
env.getBlazeWorkspace()
.getWorkspace()
.getRelative(repoOptions.experimentalRepositoryCache);
}
repositoryCache.setRepositoryCachePath(repositoryCachePath);
} else {
repositoryCache.setRepositoryCachePath(null);
Expand Down
2 changes: 1 addition & 1 deletion src/test/shell/bazel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ sh_test(
size = "large",
srcs = ["external_integration_test.sh"],
data = [":test-deps"],
shard_count = 6,
shard_count = 12,
)

sh_test(
Expand Down
73 changes: 73 additions & 0 deletions src/test/shell/bazel/external_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,79 @@ EOF
expect_log '@ext//:foo'
}

function test_repository_cache_relative_path() {
# Verify that --experimental_repository_cache works for query and caches soly
# based on the predicted hash, for a repository-cache location given as path
# relative to the WORKSPACE
WRKDIR=$(mktemp -d "${TEST_TMPDIR}/testXXXXXX")
cd "${WRKDIR}"
mkdir ext
cat > ext/BUILD <<'EOF'
genrule(
name="foo",
outs=["foo.txt"],
cmd="echo Hello World > $@",
)
genrule(
name="bar",
outs=["bar.txt"],
srcs=[":foo"],
cmd="cp $< $@",
)
EOF
zip ext.zip ext/*
rm -rf ext
sha256=$(sha256sum ext.zip | head -c 64)

rm -rf cache
mkdir cache

rm -rf main
mkdir main
cd main
cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name="ext",
strip_prefix="ext",
urls=["file://${WRKDIR}/ext.zip"],
sha256="${sha256}",
)
EOF
# Use the external repository once to make sure it is cached.
bazel build --experimental_repository_cache="../cache" '@ext//:bar' \
|| fail "expected sucess"

# Now "go offline" and clean local resources.
rm -f "${WRKDIR}/ext.zip"
bazel clean --expunge
bazel query 'deps("@ext//:bar")' && fail "Couldn't clean local cache" || :

# The value should still be available from the repository cache
bazel query 'deps("@ext//:bar")' \
--experimental_repository_cache="../cache" > "${TEST_log}" \
|| fail "Expected success"
expect_log '@ext//:foo'

# Clean again.
bazel clean --expunge
# Even with a different source URL, the cache sould be consulted.

cat > WORKSPACE <<EOF
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name="ext",
strip_prefix="ext",
urls=["http://doesnotexist.example.com/invalidpath/othername.zip"],
sha256="${sha256}",
)
EOF
bazel query 'deps("@ext//:bar")' \
--experimental_repository_cache="../cache" > "${TEST_log}" \
|| fail "Expected success"
expect_log '@ext//:foo'
}

function test_repository_cache() {
# Verify that --experimental_repository_cache works for query and caches soly
# based on the predicted hash.
Expand Down

0 comments on commit 157caed

Please sign in to comment.