stdenv: Don't stop set -u-ing

Before, we very carefully unapplied and reapplied `set -u` so the rest
of Nixpkgs could continue to not fail on undefined variables. Let's rip
off the band-aid.
This commit is contained in:
John Ericson 2019-10-29 19:43:34 -04:00
parent 373236ccff
commit 9df7efe0c6
2 changed files with 10 additions and 42 deletions

View file

@ -125,6 +125,13 @@
<link linkend="opt-networking.interfaces">networking.interfaces.&lt;name&gt;.…</link> options. <link linkend="opt-networking.interfaces">networking.interfaces.&lt;name&gt;.…</link> options.
</para> </para>
</listitem> </listitem>
<listitem>
<para>
The stdenv now runs all bash with <literal>set -u</literal>, to catch the use of undefined variables.
Before, it itself used <literal>set -u</literal> but was careful to unset it so other packages' code ran as before.
Now, all bash code is held to the same high standard, and the rather complex stateful manipulation of the options can be discarded.
</para>
</listitem>
</itemizedlist> </itemizedlist>
</section> </section>

View file

@ -17,10 +17,6 @@ fi
# code). The hooks for <hookName> are the shell function or variable # code). The hooks for <hookName> are the shell function or variable
# <hookName>, and the values of the shell array <hookName>Hooks. # <hookName>, and the values of the shell array <hookName>Hooks.
runHook() { runHook() {
local oldOpts="-u"
shopt -qo nounset || oldOpts="+u"
set -u # May be called from elsewhere, so do `set -u`.
local hookName="$1" local hookName="$1"
shift shift
local hooksSlice="${hookName%Hook}Hooks[@]" local hooksSlice="${hookName%Hook}Hooks[@]"
@ -30,10 +26,8 @@ runHook() {
# undefined. # undefined.
for hook in "_callImplicitHook 0 $hookName" ${!hooksSlice+"${!hooksSlice}"}; do for hook in "_callImplicitHook 0 $hookName" ${!hooksSlice+"${!hooksSlice}"}; do
_eval "$hook" "$@" _eval "$hook" "$@"
set -u # To balance `_eval`
done done
set "$oldOpts"
return 0 return 0
} }
@ -41,10 +35,6 @@ runHook() {
# Run all hooks with the specified name, until one succeeds (returns a # Run all hooks with the specified name, until one succeeds (returns a
# zero exit code). If none succeed, return a non-zero exit code. # zero exit code). If none succeed, return a non-zero exit code.
runOneHook() { runOneHook() {
local oldOpts="-u"
shopt -qo nounset || oldOpts="+u"
set -u # May be called from elsewhere, so do `set -u`.
local hookName="$1" local hookName="$1"
shift shift
local hooksSlice="${hookName%Hook}Hooks[@]" local hooksSlice="${hookName%Hook}Hooks[@]"
@ -56,10 +46,8 @@ runOneHook() {
ret=0 ret=0
break break
fi fi
set -u # To balance `_eval`
done done
set "$oldOpts"
return "$ret" return "$ret"
} }
@ -70,17 +58,13 @@ runOneHook() {
# environment variables) and from shell scripts (as functions). If you # environment variables) and from shell scripts (as functions). If you
# want to allow multiple hooks, use runHook instead. # want to allow multiple hooks, use runHook instead.
_callImplicitHook() { _callImplicitHook() {
set -u
local def="$1" local def="$1"
local hookName="$2" local hookName="$2"
if declare -F "$hookName" > /dev/null; then if declare -F "$hookName" > /dev/null; then
set +u
"$hookName" "$hookName"
elif type -p "$hookName" > /dev/null; then elif type -p "$hookName" > /dev/null; then
set +u
source "$hookName" source "$hookName"
elif [ -n "${!hookName:-}" ]; then elif [ -n "${!hookName:-}" ]; then
set +u
eval "${!hookName}" eval "${!hookName}"
else else
return "$def" return "$def"
@ -96,13 +80,10 @@ _callImplicitHook() {
# command can take them # command can take them
_eval() { _eval() {
if declare -F "$1" > /dev/null 2>&1; then if declare -F "$1" > /dev/null 2>&1; then
set +u
"$@" # including args "$@" # including args
else else
set +u
eval "$1" eval "$1"
fi fi
# `run*Hook` reenables `set -u`
} }
@ -190,12 +171,12 @@ addToSearchPath() {
# so it is defined here but tried after the hook. # so it is defined here but tried after the hook.
_addRpathPrefix() { _addRpathPrefix() {
if [ "${NIX_NO_SELF_RPATH:-0}" != 1 ]; then if [ "${NIX_NO_SELF_RPATH:-0}" != 1 ]; then
export NIX_LDFLAGS="-rpath $1/lib $NIX_LDFLAGS" export NIX_LDFLAGS="-rpath $1/lib ${NIX_LDFLAGS-}"
if [ -n "${NIX_LIB64_IN_SELF_RPATH:-}" ]; then if [ -n "${NIX_LIB64_IN_SELF_RPATH:-}" ]; then
export NIX_LDFLAGS="-rpath $1/lib64 $NIX_LDFLAGS" export NIX_LDFLAGS="-rpath $1/lib64 ${NIX_LDFLAGS-}"
fi fi
if [ -n "${NIX_LIB32_IN_SELF_RPATH:-}" ]; then if [ -n "${NIX_LIB32_IN_SELF_RPATH:-}" ]; then
export NIX_LDFLAGS="-rpath $1/lib32 $NIX_LDFLAGS" export NIX_LDFLAGS="-rpath $1/lib32 ${NIX_LDFLAGS-}"
fi fi
fi fi
} }
@ -489,11 +470,7 @@ activatePackage() {
(( "$hostOffset" <= "$targetOffset" )) || exit -1 (( "$hostOffset" <= "$targetOffset" )) || exit -1
if [ -f "$pkg" ]; then if [ -f "$pkg" ]; then
local oldOpts="-u"
shopt -qo nounset || oldOpts="+u"
set +u
source "$pkg" source "$pkg"
set "$oldOpts"
fi fi
# Only dependencies whose host platform is guaranteed to match the # Only dependencies whose host platform is guaranteed to match the
@ -512,11 +489,7 @@ activatePackage() {
fi fi
if [[ -f "$pkg/nix-support/setup-hook" ]]; then if [[ -f "$pkg/nix-support/setup-hook" ]]; then
local oldOpts="-u"
shopt -qo nounset || oldOpts="+u"
set +u
source "$pkg/nix-support/setup-hook" source "$pkg/nix-support/setup-hook"
set "$oldOpts"
fi fi
} }
@ -1264,19 +1237,11 @@ showPhaseHeader() {
genericBuild() { genericBuild() {
if [ -f "${buildCommandPath:-}" ]; then if [ -f "${buildCommandPath:-}" ]; then
local oldOpts="-u"
shopt -qo nounset || oldOpts="+u"
set +u
source "$buildCommandPath" source "$buildCommandPath"
set "$oldOpts"
return return
fi fi
if [ -n "${buildCommand:-}" ]; then if [ -n "${buildCommand:-}" ]; then
local oldOpts="-u"
shopt -qo nounset || oldOpts="+u"
set +u
eval "$buildCommand" eval "$buildCommand"
set "$oldOpts"
return return
fi fi
@ -1306,11 +1271,7 @@ genericBuild() {
# Evaluate the variable named $curPhase if it exists, otherwise the # Evaluate the variable named $curPhase if it exists, otherwise the
# function named $curPhase. # function named $curPhase.
local oldOpts="-u"
shopt -qo nounset || oldOpts="+u"
set +u
eval "${!curPhase:-$curPhase}" eval "${!curPhase:-$curPhase}"
set "$oldOpts"
if [ "$curPhase" = unpackPhase ]; then if [ "$curPhase" = unpackPhase ]; then
cd "${sourceRoot:-.}" cd "${sourceRoot:-.}"