Skip to content

Commit

Permalink
Flush cache with version change (#40)
Browse files Browse the repository at this point in the history
* nodejs- clear cache if node version changed

* copy functionality to npm yarn and ts

* store_node_version tests for npm nodejs and ts

* fix typo

* clear cache tests

* fixes to clearing cache tests

* dont need to mkdir

* shellcheck

* change version from 12 to 14

* change to use env.build instead of store.toml

* use env.build PREV_NODE_VERSION var

* address review comments

* stub node command, mock node 14, check for PREV_NODE_VERSION var

* use -n instead of ! -z
  • Loading branch information
lillianzhang331 authored May 5, 2021
1 parent 9d55ab9 commit 46dec4f
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 51 deletions.
2 changes: 2 additions & 0 deletions buildpacks/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Change node engine version from 12 to 14 ([#40](https://github.com/heroku/buildpacks-node/pull/40))
- Clear cache when node version changes ([#40](https://github.com/heroku/buildpacks-node/pull/40))

## [0.7.3] 2021/03/04
- Flush cache when stack image changes ([#28](https://github.com/heroku/buildpacks-node/pull/28))
Expand Down
6 changes: 6 additions & 0 deletions buildpacks/nodejs/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ rm -rf node_modules

clear_cache_on_stack_change "$layers_dir"

write_to_store_toml "$layers_dir"

set_up_environment "$layers_dir/nodejs"

bootstrap_buildpack "$layers_dir/bootstrap"

install_or_reuse_toolbox "$layers_dir/toolbox"
export PATH=$layers_dir/toolbox/bin:$PATH

store_node_version "$layers_dir/nodejs"

install_or_reuse_node "$layers_dir/nodejs" "$build_dir"
export PATH=$layers_dir/nodejs/bin:$PATH

clear_cache_on_node_version_change "$layers_dir" "$layers_dir/nodejs"

parse_package_json_engines "$layers_dir/package_manager_metadata" "$build_dir"

if [[ -f "yarn.lock" ]]; then
Expand Down
52 changes: 43 additions & 9 deletions buildpacks/nodejs/lib/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ source "$bp_dir/lib/utils/log.sh"
# shellcheck source=/dev/null
source "$bp_dir/lib/utils/toml.sh"

write_to_store_toml() {
local layers_dir=$1

if [[ ! -f "${layers_dir}/store.toml" ]]; then
touch "${layers_dir}/store.toml"
cat <<TOML >"${layers_dir}/store.toml"
[metadata]
last_stack = "$CNB_STACK_ID"
TOML
fi
}

clear_cache_on_stack_change() {
local layers_dir=$1

Expand All @@ -29,14 +41,6 @@ clear_cache_on_stack_change() {
rm -rf "${layers_dir:?}"/*
fi
fi

if [[ ! -f "${layers_dir}/store.toml" ]]; then
touch "${layers_dir}/store.toml"
cat <<TOML >"${layers_dir}/store.toml"
[metadata]
last_stack = "$CNB_STACK_ID"
TOML
fi
}

set_up_environment() {
Expand Down Expand Up @@ -68,6 +72,18 @@ install_or_reuse_toolbox() {
echo "launch = false" >>"${layer_dir}.toml"
}

store_node_version() {
local layer_dir=$1
local prev_node_version
# shellcheck disable=SC2002
prev_node_version=$(cat "${layer_dir}.toml" | grep version | xargs | cut -d " " -f3)
mkdir -p "${layer_dir}/env.build"
if [[ -s "${layer_dir}/env.build/PREV_NODE_VERSION.override" ]]; then
rm -rf "${layer_dir}/env.build/PREV_NODE_VERSION.override"
fi
echo -e "$prev_node_version\c" >>"${layer_dir}/env.build/PREV_NODE_VERSION.override"
}

install_or_reuse_node() {
local layer_dir=$1
local build_dir=$2
Expand All @@ -79,7 +95,7 @@ install_or_reuse_node() {
status "Installing Node"
info "Getting Node version"
engine_node=$(json_get_key "$build_dir/package.json" ".engines.node")
node_version=${engine_node:-12.x}
node_version=${engine_node:-14.x}

info "Resolving Node version"
resolved_data=$(resolve-version node "$node_version")
Expand All @@ -106,6 +122,24 @@ install_or_reuse_node() {
fi
}

clear_cache_on_node_version_change() {
local layers_dir=$1
local layer_dir=$2
local prev_node_version
local curr_node_version

curr_node_version="$(node -v)"
curr_node_version=${curr_node_version:1} #to truncate the "v" that is concatedated to version in node -v
if [[ -s "${layer_dir}/env.build/PREV_NODE_VERSION" ]]; then
prev_node_version=$(cat "${layer_dir}/env.build/PREV_NODE_VERSION")

if [[ "$curr_node_version" != "$prev_node_version" ]]; then
info "Deleting cache because node version changed from \"$prev_node_version\" to \"$curr_node_version\""
rm -rf "${layers_dir}/yarn" "${layers_dir}/yarn.toml"
fi
fi
}

parse_package_json_engines() {
local layer_dir=$1
local build_dir=$2
Expand Down
70 changes: 62 additions & 8 deletions buildpacks/nodejs/shpec/build_shpec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ rm_binaries() {

describe "lib/build.sh"
stub_command "info"
stub_command "node"
rm_binaries

layers_dir=$(create_temp_layer_dir)
Expand All @@ -55,14 +56,6 @@ describe "lib/build.sh"

export CNB_STACK_ID="heroku-20"

it "creates store.toml when not present"
assert file_absent "$layers_dir/store.toml"

clear_cache_on_stack_change "$layers_dir"

assert file_present "$layers_dir/store.toml"
end

it "does not delete layers with same stack"
assert file_present "$layers_dir/my_layer.toml"

Expand All @@ -71,6 +64,8 @@ describe "lib/build.sh"
assert file_present "$layers_dir/my_layer.toml"
end

write_to_store_toml "$layers_dir"

it "deletes layers when stack changes"
CNB_STACK_ID="heroku-22"

Expand All @@ -84,6 +79,51 @@ describe "lib/build.sh"
unset CNB_STACK_ID
end

describe "clear_cache_on_node_version_change"

touch "$layers_dir/yarn"

it "does not delete layers with same node version"
mkdir "${layers_dir}/nodejs"
mkdir "${layers_dir}/nodejs/env.build"

echo -e "$(node -v)\c" >>"${layers_dir}/nodejs/env.build/PREV_NODE_VERSION"

assert file_present "$layers_dir/yarn"

clear_cache_on_node_version_change "$layers_dir" "$layers_dir/nodejs"

assert file_present "$layers_dir/yarn"
end

it "deletes layers when node version changes"
rm -rf "${layers_dir}/nodejs/env.build/PREV_NODE_VERSION"
echo -e "different_version" >>"${layers_dir}/nodejs/env.build/PREV_NODE_VERSION"

assert file_present "$layers_dir/yarn"

clear_cache_on_node_version_change "$layers_dir" "$layers_dir/nodejs"

assert file_absent "$layers_dir/yarn"
end

end

describe "write_to_store_toml"

if [[ -s "$layers_dir/store.toml" ]]; then
rm -rf "$layers_dir/store.toml"
fi

it "creates store.toml when not present"
assert file_absent "$layers_dir/store.toml"

write_to_store_toml "$layers_dir"

assert file_present "$layers_dir/store.toml"
end
end

describe "boostrap_buildpack"
create_binaries "$layers_dir/bootstrap"

Expand Down Expand Up @@ -150,6 +190,19 @@ describe "lib/build.sh"
end
end

describe "store_node_version"
layers_dir=$(create_temp_layer_dir)

touch "${layers_dir}/nodejs.toml"
echo -e "[metadata]\nversion = \"test_version\"" > "${layers_dir}/nodejs.toml"

it "stores node version in PREV_NODE_VERSION env"
assert file_absent "$layers_dir/nodejs/env.build/PREV_NODE_VERSION.override"
store_node_version "$layers_dir/nodejs"
assert equal "$(cat "$layers_dir/nodejs/env.build/PREV_NODE_VERSION.override")" test_version
end
end

describe "install_or_reuse_node"
layers_dir=$(create_temp_layer_dir)
project_dir=$(create_temp_project_dir)
Expand Down Expand Up @@ -301,6 +354,7 @@ describe "lib/build.sh"
end

unstub_command "info"
unstub_command "node"
rm_binaries
end

Expand Down
4 changes: 4 additions & 0 deletions buildpacks/npm/bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ warn_prebuilt_modules "$build_dir"

run_prebuild "$build_dir"

clear_cache_on_node_version_change "$layers_dir"

write_to_store_toml "$layers_dir"

install_or_reuse_node_modules "$build_dir" "$layers_dir/node_modules"

run_build "$build_dir"
Expand Down
34 changes: 26 additions & 8 deletions buildpacks/npm/lib/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@ clear_cache_on_stack_change() {
rm -rf "${layers_dir:?}"/*
fi
fi

if [[ ! -f "${layers_dir}/store.toml" ]]; then
touch "${layers_dir}/store.toml"
cat <<TOML >"${layers_dir}/store.toml"
[metadata]
last_stack = "$CNB_STACK_ID"
TOML
fi
}

detect_package_lock() {
Expand Down Expand Up @@ -143,6 +135,32 @@ install_modules() {
fi
}

write_to_store_toml() {
local layers_dir=$1

if [[ ! -f "${layers_dir}/store.toml" ]]; then
touch "${layers_dir}/store.toml"
cat <<TOML >"${layers_dir}/store.toml"
[metadata]
last_stack = "$CNB_STACK_ID"
TOML
fi
}

clear_cache_on_node_version_change() {
local layers_dir=$1
local curr_node_version

curr_node_version="$(node -v)"
curr_node_version=${curr_node_version:1} #to truncate the "v" that is concatedated to version in node -v
if [[ -n "$PREV_NODE_VERSION" ]]; then
if [[ "$curr_node_version" != "$PREV_NODE_VERSION" ]]; then
info "Deleting cache because node version changed from \"$PREV_NODE_VERSION\" to \"$curr_node_version\""
rm -rf "${layers_dir:?}"/*
fi
fi
}

install_or_reuse_node_modules() {
local build_dir=$1
local layer_dir=$2
Expand Down
7 changes: 7 additions & 0 deletions buildpacks/npm/mocks/node/v14/bin/node
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env bash

node_version="v14.16.1"

if [[ "$1" == "-v" ]]; then
echo "$node_version"
fi
62 changes: 53 additions & 9 deletions buildpacks/npm/shpec/build_shpec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ use_npm() {
PATH="${shpec_dir}/../mocks/npm/v$1/bin:$CURRENT_PATH"
}

use_node() {
PATH="${shpec_dir}/../mocks/node/v$1/bin:$CURRENT_PATH"
}

describe "lib/build.sh"
install_tools
stub_command "node"

CURRENT_PATH=$PATH
layers_dir=$(create_temp_layer_dir)
Expand All @@ -64,14 +69,6 @@ describe "lib/build.sh"

export CNB_STACK_ID="heroku-20"

it "creates store.toml when not present"
assert file_absent "$layers_dir/store.toml"

clear_cache_on_stack_change "$layers_dir"

assert file_present "$layers_dir/store.toml"
end

it "does not delete layers with same stack"
assert file_present "$layers_dir/my_layer.toml"

Expand All @@ -80,8 +77,10 @@ describe "lib/build.sh"
assert file_present "$layers_dir/my_layer.toml"
end

write_to_store_toml "$layers_dir"

it "deletes layers when stack changes"
CNB_STACK_ID="heroku-22"
export CNB_STACK_ID="heroku-22"

assert file_present "$layers_dir/my_layer.toml"

Expand All @@ -93,6 +92,50 @@ describe "lib/build.sh"
unset CNB_STACK_ID
end

describe "clear_cache_on_node_version_change"

touch "$layers_dir/node_modules"

it "does not delete layers with same node version"
use_node 14
version="$(node -v)"
truncated_version=${version:1}
export PREV_NODE_VERSION="$truncated_version"

assert file_present "$layers_dir/node_modules"

clear_cache_on_node_version_change "$layers_dir"

assert file_present "$layers_dir/node_modules"
end

it "deletes layers when node version changes"
export PREV_NODE_VERSION="different_version"

assert file_present "$layers_dir/node_modules"

clear_cache_on_node_version_change "$layers_dir"

assert file_absent "$layers_dir/node_modules"
end
unset PREV_NODE_VERSION
end

describe "write_to_store_toml"

if [[ -s "$layers_dir/store.toml" ]]; then
rm -rf "$layers_dir/store.toml"
fi

it "creates store.toml when not present"
assert file_absent "$layers_dir/store.toml"

write_to_store_toml "$layers_dir"

assert file_present "$layers_dir/store.toml"
end
end

describe "prune_devdependencies"
project_dir=$(create_temp_project_dir)
use_npm 6
Expand Down Expand Up @@ -319,5 +362,6 @@ describe "lib/build.sh"
end

unstub_command "log_info"
unstub_command "node"
rm_tools_and_mocks
end
Loading

0 comments on commit 46dec4f

Please sign in to comment.