Skip to content

Commit 388a518

Browse files
authored
Fix unbound variable warning (heroku#1632)
* Fix variable scope ``` /var/lib/buildpack/bin/support/bash_functions.sh: line 53: heroku_buildpack_ruby_dir: unbound variable ``` * Prefer local variable The local `stack` is set from `STACK` earlier. Prefer it everywhere in the function. * Test behavior change * CHANGELOG * Change bash variable scope The local `heroku_buildpack_ruby_dir` is not available inside of the function `atexit`. Moving the code out of a function means the local doesn't need to be present at exit. * Remove vendored buildpack detection The old buildpack logic would pre-vendor a Ruby version at deploy in `vendor/ruby/$stack/$arch` of the buildpack dir. There were problems when the Ruby buildpack was specified to run twice so a check was added for that condition. The logic was changed to always download the Ruby verison and not pre-vendor details in the PR heroku#1481. Since that's no longer happening, we can avoid the check and just create a tempdir every time. This simplifies the logic. * Remove stack as it's no longer used * Inline bash function Fixing the previously hidden warning introduced a new error: ``` remote: -----> Ruby app detected remote: /tmp/codon/tmp/buildpacks/ed3d993430eab40cdfcb6130d0909c19a1b9ae24/bin/compile: line 39: /tmp/tmp.Uu0SIOeXEb/bin/ruby: No such file or directory ``` What's happening here is that the `trap` to cleanup the bootstrap dir was firing on subshell exit, and since we were capturing the output of that function via `bootstrap_ruby=$(...)` it was creating the directory and immediately deleting it. This was previously not a problem as the local variable scoping prevented the `rm -rf` from firing correctly. But fixing that variable scoping error introduced this problem. We can fix by moving that trap out of a sub-shell. Since this function is really only 3 lines long at this point, there's not much benefit in having it as a function, especially if it cannot provide the automatic cleanup behavior as desired.
1 parent 1199310 commit 388a518

6 files changed

Lines changed: 15 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [Unreleased]
44

5+
- Fix `heroku_buildpack_ruby_dir: unbound variable` warning (https://github.com/heroku/heroku-buildpack-ruby/pull/1632)
56

67
## [v317] - 2025-08-07
78

bin/compile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ BUILD_DIR=$1
88
CACHE_DIR=$2
99
ENV_DIR=$3
1010
BIN_DIR=$(cd "$(dirname "$0")" || exit; pwd) # absolute path
11-
BUILDPACK_DIR=$(dirname "$BIN_DIR")
1211

1312
# shellcheck source=bin/support/bash_functions.sh
1413
source "$BIN_DIR/support/bash_functions.sh"
1514

1615
checks::ensure_supported_stack "${STACK:?Required env var STACK is not set}"
1716

18-
bootstrap_ruby_dir=$(install_bootstrap_ruby "$BIN_DIR" "$BUILDPACK_DIR")
17+
bootstrap_ruby_dir=$(mktemp -d)
18+
"$BIN_DIR"/support/download_ruby "$BIN_DIR" "$bootstrap_ruby_dir"
19+
trap 'rm -rf "$bootstrap_ruby_dir"' EXIT
20+
1921
export PATH="$bootstrap_ruby_dir/bin/:$PATH"
2022
unset GEM_PATH
2123

bin/support/bash_functions.sh

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,6 @@ curl_retry_on_18() {
1010
return $ec
1111
}
1212

13-
# This function will install a version of Ruby onto the
14-
# system for the buildpack to use. It coordinates download
15-
# and setting appropriate env vars for execution
16-
#
17-
# Example:
18-
#
19-
# install_bootstrap_ruby "$BIN_DIR" "$BUILDPACK_DIR"
20-
#
21-
# Takes two arguments, the first is the location of the buildpack's
22-
# `bin` directory. This is where the `download_ruby` script can be
23-
# found. The second argument is the root directory where Ruby
24-
# can be installed.
25-
#
26-
# This function relies on the env var `$STACK` being set. This
27-
# is set in codon outside of the buildpack. An example of a stack
28-
# would be "heroku-24".
29-
install_bootstrap_ruby()
30-
{
31-
local bin_dir=$1
32-
local buildpack_dir=$2
33-
local stack="${STACK:?Required env var STACK is not set}"
34-
35-
# Multi-arch aware stack support
36-
if [ "$stack" == "heroku-24" ]; then
37-
local arch
38-
arch=$(dpkg --print-architecture)
39-
local heroku_buildpack_ruby_dir="$buildpack_dir/vendor/ruby/$STACK/$arch"
40-
else
41-
local heroku_buildpack_ruby_dir="$buildpack_dir/vendor/ruby/$STACK"
42-
fi
43-
44-
# The -d flag checks to see if a file exists and is a directory.
45-
# This directory may be non-empty if a previous compile has
46-
# already placed a Ruby executable here.
47-
if [ ! -d "$heroku_buildpack_ruby_dir" ]; then
48-
heroku_buildpack_ruby_dir=$(mktemp -d)
49-
# bootstrap ruby
50-
"$bin_dir"/support/download_ruby "$bin_dir" "$heroku_buildpack_ruby_dir"
51-
function atexit {
52-
# shellcheck disable=SC2317
53-
rm -rf "$heroku_buildpack_ruby_dir"
54-
}
55-
trap atexit EXIT
56-
fi
57-
58-
echo "$heroku_buildpack_ruby_dir"
59-
}
60-
6113
which_java()
6214
{
6315
which java > /dev/null

bin/test

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
set -euo pipefail
66

77
BIN_DIR=$(cd "$(dirname "$0")" || exit; pwd) # absolute path
8-
BUILDPACK_DIR=$(dirname "$BIN_DIR")
98

109
# shellcheck source=bin/support/bash_functions.sh
1110
source "$BIN_DIR/support/bash_functions.sh"
1211

13-
bootstrap_ruby_dir=$(install_bootstrap_ruby "$BIN_DIR" "$BUILDPACK_DIR")
12+
bootstrap_ruby_dir=$(mktemp -d)
13+
"$BIN_DIR"/support/download_ruby "$BIN_DIR" "$bootstrap_ruby_dir"
14+
trap 'rm -rf "$bootstrap_ruby_dir"' EXIT
15+
1416
export PATH="$bootstrap_ruby_dir/bin/:$PATH"
1517
unset GEM_PATH
1618

bin/test-compile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ BUILD_DIR=$1
66
CACHE_DIR=$2
77
ENV_DIR=$3
88
BIN_DIR=$(cd "$(dirname "$0")" || exit; pwd) # absolute path
9-
BUILDPACK_DIR=$(dirname "$BIN_DIR")
109

1110
# shellcheck source=bin/support/bash_functions.sh
1211
source "$BIN_DIR/support/bash_functions.sh"
1312

14-
bootstrap_ruby_dir=$(install_bootstrap_ruby "$BIN_DIR" "$BUILDPACK_DIR")
13+
bootstrap_ruby_dir=$(mktemp -d)
14+
"$BIN_DIR"/support/download_ruby "$BIN_DIR" "$bootstrap_ruby_dir"
15+
trap 'rm -rf "$bootstrap_ruby_dir"' EXIT
16+
1517
export PATH="$bootstrap_ruby_dir/bin/:$PATH"
1618
unset GEM_PATH
1719

spec/hatchet/ruby_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@
246246
end
247247

248248
app.deploy do |app|
249+
expect(app.output).to_not include("unbound variable")
250+
249251
# Intentionally different than the default ruby version
250252
expect(app.output).to match("#{version}")
251253
expect(app.run("ruby -v")).to match("#{version}")

0 commit comments

Comments
 (0)