Skip to content

Commit

Permalink
Make repository_chache non-experimental and enable by default
Browse files Browse the repository at this point in the history
Make the --experimental_repository_cache option no longer experimental and enable
the cache by default. The default location is in the a separate directory under the
install base (under .cache/bazel user's home directory); this means it is shared
between workspaces und not between different versions of bazel. We plan to also
share it between different bazel versions in the future.

Fixes bazelbuild#4676.

RELNOTES:
  repository_cache is no longer experimental and enabled by default.

Change-Id: I8499c2d1811e0fe8d830796e07cd6f8bc75e48ba
PiperOrigin-RevId: 187172766
  • Loading branch information
aehlig authored and Copybara-Service committed Feb 27, 2018
1 parent db80298 commit 6e09338
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import com.google.devtools.build.lib.bazel.rules.workspace.NewGitRepositoryRule;
import com.google.devtools.build.lib.bazel.rules.workspace.NewHttpArchiveRule;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.pkgcache.PackageCacheOptions;
import com.google.devtools.build.lib.rules.repository.LocalRepositoryFunction;
import com.google.devtools.build.lib.rules.repository.LocalRepositoryRule;
Expand All @@ -73,12 +74,14 @@
import com.google.devtools.build.lib.skyframe.SkyValueDirtinessChecker;
import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.build.skyframe.SkyKey;
import com.google.devtools.build.skyframe.SkyValue;
import com.google.devtools.common.options.OptionsBase;
import com.google.devtools.common.options.OptionsProvider;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -91,6 +94,9 @@
*/
public class BazelRepositoryModule extends BlazeModule {

// Default location (relative to output user root) of the repository cache.
public static final String DEFAULT_CACHE_LOCATION = "cache/repos/v1";

// A map of repository handlers that can be looked up by rule class name.
private final ImmutableMap<String, RepositoryFunction> repositoryHandlers;
private final AtomicBoolean isFetch = new AtomicBoolean(false);
Expand Down Expand Up @@ -210,7 +216,23 @@ public void beforeCommand(CommandEnvironment env) {
}
repositoryCache.setRepositoryCachePath(repositoryCachePath);
} else {
repositoryCache.setRepositoryCachePath(null);
Path repositoryCachePath =
env.getDirectories()
.getServerDirectories()
.getOutputUserRoot()
.getRelative(DEFAULT_CACHE_LOCATION);
try {
FileSystemUtils.createDirectoryAndParents(repositoryCachePath);
repositoryCache.setRepositoryCachePath(repositoryCachePath);
} catch (IOException e) {
env.getReporter()
.handle(
Event.warn(
"Failed to set up cache at "
+ repositoryCachePath.toString()
+ ": "
+ e.getMessage()));
}
}

if (repoOptions.experimentalDistdir != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
public class RepositoryOptions extends OptionsBase {

@Option(
name = "experimental_repository_cache",
name = "repository_cache",
oldName = "experimental_repository_cache",
defaultValue = "null",
documentationCategory = OptionDocumentationCategory.BAZEL_CLIENT_OPTIONS,
effectTags = {OptionEffectTag.BAZEL_INTERNAL_CONFIGURATION},
Expand Down
73 changes: 69 additions & 4 deletions src/test/shell/bazel/external_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ EOF
}

function test_repository_cache_relative_path() {
# Verify that --experimental_repository_cache works for query and caches soly
# Verify that --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")
Expand Down Expand Up @@ -1144,7 +1144,7 @@ http_archive(
)
EOF
# Use the external repository once to make sure it is cached.
bazel build --experimental_repository_cache="../cache" '@ext//:bar' \
bazel build --repository_cache="../cache" '@ext//:bar' \
|| fail "expected sucess"

# Now "go offline" and clean local resources.
Expand All @@ -1154,7 +1154,7 @@ EOF

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

Expand All @@ -1172,11 +1172,76 @@ http_archive(
)
EOF
bazel query 'deps("@ext//:bar")' \
--experimental_repository_cache="../cache" > "${TEST_log}" \
--repository_cache="../cache" > "${TEST_log}" \
|| fail "Expected success"
expect_log '@ext//:foo'
}

test_default_cache()
{
# Verify that the default 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 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 '@ext//:bar' || fail "expected sucess"

# Now "go offline" and clean local resources.
rm -f "${WRKDIR}/ext.zip"
bazel clean --expunge

# The value should still be available from the repository cache
bazel query 'deps("@ext//:bar")' > "${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")' > "${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 6e09338

Please sign in to comment.