Unverified Commit a8372ab8 authored by Jeremy Morrell's avatar Jeremy Morrell Committed by GitHub

Rework output when caching directories (#559)

* Rework output when caching directories

This splits the logic when the buildpack saves and restores the cache
into two separate branches:

- when the user hasn't specified specific directories to cache (most everyone)
- when the user has given a list of specific directories to cache (power users)

This gives us more control over the default messaging, which has
included messaging for bower_components forever, even though most users
will never use bower, nor should we encourage them to.

These changes silence and minimize the messaging for the default case
parent 79b88795
...@@ -9,7 +9,6 @@ unset GIT_DIR # Avoid GIT_DIR leak from previous build steps ...@@ -9,7 +9,6 @@ unset GIT_DIR # Avoid GIT_DIR leak from previous build steps
### Constants ### Constants
DEFAULT_CACHE="node_modules bower_components"
BPLOG_PREFIX="buildpack.nodejs" BPLOG_PREFIX="buildpack.nodejs"
### Configure directories ### Configure directories
...@@ -141,6 +140,7 @@ install_bins | output "$LOG_FILE" ...@@ -141,6 +140,7 @@ install_bins | output "$LOG_FILE"
restore_cache() { restore_cache() {
local cache_status="$(get_cache_status)" local cache_status="$(get_cache_status)"
local cache_directories="$(get_cache_directories)"
if $YARN; then if $YARN; then
if [ -e "$BUILD_DIR/node_modules" ]; then if [ -e "$BUILD_DIR/node_modules" ]; then
...@@ -148,23 +148,38 @@ restore_cache() { ...@@ -148,23 +148,38 @@ restore_cache() {
rm -rf "$BUILD_DIR/node_modules" rm -rf "$BUILD_DIR/node_modules"
fi fi
fi fi
if [ "$cache_status" == "valid" ]; then
local cache_directories=$(get_cache_directories) if [[ "$cache_status" == "disabled" ]]; then
if [ "$cache_directories" == "" ]; then header "Restoring cache"
echo "Loading 2 from cacheDirectories (default):" echo "Caching has been disabled because NODE_MODULES_CACHE=${NODE_MODULES_CACHE}"
restore_cache_directories "$BUILD_DIR" "$CACHE_DIR" "$DEFAULT_CACHE" elif [[ "$cache_status" == "valid" ]]; then
header "Restoring cache"
if [[ "$cache_directories" == "" ]]; then
restore_default_cache_directories "$BUILD_DIR" "$CACHE_DIR"
else
restore_custom_cache_directories "$BUILD_DIR" "$CACHE_DIR" $cache_directories
fi
elif [[ "$cache_status" == "new-signature" ]]; then
header "Restoring cache"
if [[ "$cache_directories" == "" ]]; then
echo "Cached directories were not restored because of a change in versions of node, npm, or yarn"
echo "Module installation may take longer for this build"
else else
echo "Loading $(echo $cache_directories | wc -w | xargs) from cacheDirectories (package.json):" # If the user has specified custom cache directories, be more explicit
restore_cache_directories "$BUILD_DIR" "$CACHE_DIR" $cache_directories echo "Invalidating cache due to a change in node, npm, or yarn versions"
echo "Will not restore the following directories for this build:"
for directory in $(< $cache_directories); do
echo " $directory"
done
fi fi
else else
echo "Skipping cache restore ($cache_status)" # No cache exists, be silent
:
fi fi
mcount "cache.$cache_status" mcount "cache.$cache_status"
} }
header "Restoring cache" | output "$LOG_FILE"
restore_cache | output "$LOG_FILE" restore_cache | output "$LOG_FILE"
build_dependencies() { build_dependencies() {
...@@ -195,21 +210,21 @@ build_dependencies | output "$LOG_FILE" ...@@ -195,21 +210,21 @@ build_dependencies | output "$LOG_FILE"
cache_build() { cache_build() {
local cache_directories=$(get_cache_directories) local cache_directories=$(get_cache_directories)
echo "Clearing previous node cache"
clear_cache clear_cache
if ! ${NODE_MODULES_CACHE:-true}; then if ! ${NODE_MODULES_CACHE:-true}; then
echo "Skipping cache save (disabled by config)" # we've already warned that caching is disabled in the restore step
elif [ "$cache_directories" == "" ]; then # so be silent here
echo "Saving 2 cacheDirectories (default):" :
save_cache_directories "$BUILD_DIR" "$CACHE_DIR" "$DEFAULT_CACHE" elif [[ "$cache_directories" == "" ]]; then
header "Caching build"
save_default_cache_directories "$BUILD_DIR" "$CACHE_DIR"
else else
echo "Saving $(echo $cache_directories | wc -w | xargs) cacheDirectories (package.json):" header "Caching build"
save_cache_directories "$BUILD_DIR" "$CACHE_DIR" $cache_directories save_custom_cache_directories "$BUILD_DIR" "$CACHE_DIR" $cache_directories
fi fi
save_signature save_signature
} }
header "Caching build"
cache_build | output "$LOG_FILE" cache_build | output "$LOG_FILE"
prune_devdependencies() { prune_devdependencies() {
......
...@@ -39,10 +39,34 @@ get_cache_directories() { ...@@ -39,10 +39,34 @@ get_cache_directories() {
fi fi
} }
restore_cache_directories() { restore_default_cache_directories() {
local build_dir=${1:-} local build_dir=${1:-}
local cache_dir=${2:-} local cache_dir=${2:-}
# node_modules
if [[ -e "$build_dir/node_modules" ]]; then
echo "- node_modules is checked into source control and cannot be cached"
elif [[ -e "$cache_dir/node/node_modules" ]]; then
echo "- node_modules"
mkdir -p $(dirname "$build_dir/node_modules")
mv "$cache_dir/node/node_modules" "$build_dir/node_modules"
else
echo "- node_modules (not cached - skipping)"
fi
# bower_components, should be silent if it is not in the cache
if [[ -e "$cache_dir/node/bower_components" ]]; then
echo "- bower_components"
fi
}
restore_custom_cache_directories() {
local build_dir=${1:-}
local cache_dir=${2:-}
local cache_directories="${@:3}"
echo "Loading $(echo $cache_directories | wc -w | xargs) from cacheDirectories (package.json):"
for cachepath in ${@:3}; do for cachepath in ${@:3}; do
if [ -e "$build_dir/$cachepath" ]; then if [ -e "$build_dir/$cachepath" ]; then
echo "- $cachepath (exists - skipping)" echo "- $cachepath (exists - skipping)"
...@@ -63,10 +87,37 @@ clear_cache() { ...@@ -63,10 +87,37 @@ clear_cache() {
mkdir -p $CACHE_DIR/node mkdir -p $CACHE_DIR/node
} }
save_cache_directories() { save_default_cache_directories() {
local build_dir=${1:-} local build_dir=${1:-}
local cache_dir=${2:-} local cache_dir=${2:-}
# node_modules
if [[ -e "$build_dir/node_modules" ]]; then
echo "- node_modules"
mkdir -p "$cache_dir/node/node_modules"
cp -a "$build_dir/node_modules" $(dirname "$cache_dir/node/node_modules")
else
# this can happen if there are no dependencies
mcount "cache.no-node-modules"
echo "- node_modules (nothing to cache)"
fi
# bower_components
if [[ -e "$build_dir/bower_components" ]]; then
mcount "cache.saved-bower-components"
echo "- bower_components"
mkdir -p "$cache_dir/node/bower_components"
cp -a "$build_dir/bower_components" $(dirname "$cache_dir/node/bower_components")
fi
}
save_custom_cache_directories() {
local build_dir=${1:-}
local cache_dir=${2:-}
local cache_directories="${@:3}"
echo "Saving $(echo $cache_directories | wc -w | xargs) cacheDirectories (package.json):"
for cachepath in ${@:3}; do for cachepath in ${@:3}; do
if [ -e "$build_dir/$cachepath" ]; then if [ -e "$build_dir/$cachepath" ]; then
echo "- $cachepath" echo "- $cachepath"
......
...@@ -28,13 +28,11 @@ testDisableCache() { ...@@ -28,13 +28,11 @@ testDisableCache() {
compile "node-modules-cache-2" $cache $env_dir compile "node-modules-cache-2" $cache $env_dir
assertCaptured "lodash@1.0.0" assertCaptured "lodash@1.0.0"
assertCaptured "Saving 2 cacheDirectories"
assertCapturedSuccess assertCapturedSuccess
echo "false" > $env_dir/NODE_MODULES_CACHE echo "false" > $env_dir/NODE_MODULES_CACHE
compile "node-modules-cache-2" $cache $env_dir compile "node-modules-cache-2" $cache $env_dir
assertCaptured "lodash@1.3.1" assertCaptured "lodash@1.3.1"
assertNotCaptured "Saving 2 cacheDirectories"
assertCapturedSuccess assertCapturedSuccess
} }
...@@ -42,9 +40,7 @@ testNodeModulesCached() { ...@@ -42,9 +40,7 @@ testNodeModulesCached() {
cache=$(mktmpdir) cache=$(mktmpdir)
compile "caching" $cache compile "caching" $cache
assertCaptured "Saving 2 cacheDirectories (default)"
assertCaptured "- node_modules" assertCaptured "- node_modules"
assertCaptured "- bower_components (nothing to cache)"
assertEquals "1" "$(ls -1 $cache/node/node_modules | grep express | wc -l | tr -d ' ')" assertEquals "1" "$(ls -1 $cache/node/node_modules | grep express | wc -l | tr -d ' ')"
assertCapturedSuccess assertCapturedSuccess
} }
...@@ -86,12 +82,12 @@ testBuildWithCache() { ...@@ -86,12 +82,12 @@ testBuildWithCache() {
cache=$(mktmpdir) cache=$(mktmpdir)
compile "stable-node" $cache compile "stable-node" $cache
assertCaptured "Skipping cache restore (not-found)" assertNotCaptured "Restoring cache"
assertEquals "1" "$(ls -1 $cache/node/node_modules | grep hashish | wc -l | tr -d ' ')" assertEquals "1" "$(ls -1 $cache/node/node_modules | grep hashish | wc -l | tr -d ' ')"
assertCapturedSuccess assertCapturedSuccess
compile "stable-node" $cache compile "stable-node" $cache
assertNotCaptured "- node_modules (not cached - skipping)" assertCaptured "- node_modules"
assertFileContains "${STACK}" "${cache}/node/signature" assertFileContains "${STACK}" "${cache}/node/signature"
assertCapturedSuccess assertCapturedSuccess
...@@ -110,7 +106,7 @@ testCacheWithPrebuild() { ...@@ -110,7 +106,7 @@ testCacheWithPrebuild() {
assertCapturedSuccess assertCapturedSuccess
compile "cache-prebuild" $cache $env_dir compile "cache-prebuild" $cache $env_dir
assertCaptured "Skipping cache restore (new-signature" assertCaptured "Cached directories were not restored because of a change in versions of node"
assertCapturedSuccess assertCapturedSuccess
} }
...@@ -451,7 +447,7 @@ testSignatureInvalidation() { ...@@ -451,7 +447,7 @@ testSignatureInvalidation() {
compile "node-0.12.7" $cache compile "node-0.12.7" $cache
assertCaptured "Downloading and installing node 0.12.7" assertCaptured "Downloading and installing node 0.12.7"
assertCaptured "Skipping cache restore (new-signature" assertCaptured "Cached directories were not restored because of a change in versions of node"
assertCapturedSuccess assertCapturedSuccess
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment