Skip to content

fix(install.sh): script-scope tmpdir so the EXIT trap doesn't trip set -u#273

Closed
manzil-infinity180 wants to merge 1 commit into
mainfrom
fix/install-sh-tmpdir-trap
Closed

fix(install.sh): script-scope tmpdir so the EXIT trap doesn't trip set -u#273
manzil-infinity180 wants to merge 1 commit into
mainfrom
fix/install-sh-tmpdir-trap

Conversation

@manzil-infinity180
Copy link
Copy Markdown
Contributor

What

The installer aborts with bash: tmpdir: unbound variable (exit 1) after a successful install:

cilock v2.0.0-rc7 installed.
  $ cilock version
  ...
bash: line 161: tmpdir: unbound variable

Root cause

install.sh runs under set -euo pipefail. tmpdir was declared local inside main(), but the cleanup trap … EXIT fires in the global scope after main returns — where that local no longer exists. With -u (nounset), expanding the now-unset $tmpdir aborts the script.

Two consequences: the temp dir is never cleaned up (the rm never runs on the real path), and curl … | bash exits non-zero even though the binary installed fine — breaking any caller that checks the exit code.

Fix

Declare tmpdir at script scope (not local) and register a cleanup() EXIT trap once at the top, so the trap can see the real path and remove it. ${tmpdir:-} guards early exits.

+tmpdir=""
+cleanup() { [ -n "${tmpdir:-}" ] && rm -rf "$tmpdir"; }
+trap cleanup EXIT
 ...
-  local tmpdir
   tmpdir="$(mktemp -d)"
-  trap 'rm -rf "$tmpdir"' EXIT

Verification

Minimal repro of the two patterns under set -u:

  • before: prints success, then tmpdir: unbound variable, exit 1 (matches the report).
  • after: exit 0, temp dir cleaned up.

bash -n passes.

Signed-off-by: Rahul Vishwakarma <rahulvs2809@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant