Openembedded Core Discussions
 help / color / mirror / Atom feed
* [PATCH] oe-init-build-env: fix unbound variable errors under 'set -u'
@ 2026-11-11 19:56 Babanpreet Singh
  2026-11-23  9:25 ` Paul Barker
  0 siblings, 1 reply; 3+ messages in thread
From: Babanpreet Singh @ 2026-11-11 19:56 UTC (permalink / raw)
  To: openembedded-core; +Cc: Ross Burton, Babanpreet Singh

Sourcing oe-init-build-env from a shell running with 'set -u' fails:

  ./oe-init-build-env: line 29: BBSERVER: unbound variable

Give the variables that may be unset an explicit empty default, here
and in oe-buildenv-internal.

[YOCTO #15469]

Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
---
 oe-init-build-env            | 14 +++++++-------
 scripts/oe-buildenv-internal | 22 +++++++++++-----------
 2 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/oe-init-build-env b/oe-init-build-env
index 5a2bd0f4d9..660c6ad02e 100755
--- a/oe-init-build-env
+++ b/oe-init-build-env
@@ -14,9 +14,9 @@
 # when being sourced.  To workaround the shell limitation use "set <builddir>"
 # prior to sourcing this script.
 #
-if [ -n "$BASH_SOURCE" ]; then
+if [ -n "${BASH_SOURCE:-}" ]; then
     THIS_SCRIPT=$BASH_SOURCE
-elif [ -n "$ZSH_NAME" ]; then
+elif [ -n "${ZSH_NAME:-}" ]; then
     THIS_SCRIPT=$0
 else
     THIS_SCRIPT="$(pwd)/oe-init-build-env"
@@ -26,16 +26,16 @@ else
         exit 1
     fi
 fi
-if [ -n "$BBSERVER" ]; then
+if [ -n "${BBSERVER:-}" ]; then
     unset BBSERVER
 fi
 
-if [ -z "$ZSH_NAME" ] && [ "$0" = "$THIS_SCRIPT" ]; then
+if [ -z "${ZSH_NAME:-}" ] && [ "$0" = "$THIS_SCRIPT" ]; then
     echo "Error: This script needs to be sourced. Please run as '. $THIS_SCRIPT'" >&2
     exit 1
 fi
 
-if [ -z "$OEROOT" ]; then
+if [ -z "${OEROOT:-}" ]; then
     OEROOT=$(dirname "$THIS_SCRIPT")
     OEROOT=$(readlink -f "$OEROOT")
 fi
@@ -43,12 +43,12 @@ unset THIS_SCRIPT
 
 export OEROOT
 . "$OEROOT"/scripts/oe-buildenv-internal &&
-    TEMPLATECONF="$TEMPLATECONF" "$OEROOT"/scripts/oe-setup-builddir || {
+    TEMPLATECONF="${TEMPLATECONF:-}" "$OEROOT"/scripts/oe-setup-builddir || {
     unset OEROOT
     return 1
 }
 
 unset OEROOT
 
-[ -z "$BUILDDIR" ] || cd "$BUILDDIR"
+[ -z "${BUILDDIR:-}" ] || cd "$BUILDDIR"
 
diff --git a/scripts/oe-buildenv-internal b/scripts/oe-buildenv-internal
index 2fdb19565a..6b2e4ea518 100755
--- a/scripts/oe-buildenv-internal
+++ b/scripts/oe-buildenv-internal
@@ -19,12 +19,12 @@ if ! $(return >/dev/null 2>&1) ; then
 fi
 
 # It is assumed OEROOT is already defined when this is called
-if [ -z "$OEROOT" ]; then
+if [ -z "${OEROOT:-}" ]; then
     echo >&2 "Error: OEROOT is not defined!"
     return 1
 fi
 
-if [ -z "$OE_SKIP_SDK_CHECK" ] && [ -n "$OECORE_SDK_VERSION" ]; then
+if [ -z "${OE_SKIP_SDK_CHECK:-}" ] && [ -n "${OECORE_SDK_VERSION:-}" ]; then
     echo >&2 "Error: The OE SDK/ADT was detected as already being present in this shell environment. Please use a clean shell when sourcing this environment script."
     return 1
 fi
@@ -39,8 +39,8 @@ if [ "$py_v38_check" != "True" ]; then
 fi
 unset py_v38_check
 
-if [ -z "$BDIR" ]; then
-    if [ -z "$1" ]; then
+if [ -z "${BDIR:-}" ]; then
+    if [ -z "${1:-}" ]; then
         BDIR="build"
     else
         BDIR="$1"
@@ -61,7 +61,7 @@ if [ -z "$BDIR" ]; then
             return 1
         fi
     fi
-    if [ -n "$2" ]; then
+    if [ -n "${2:-}" ]; then
         BITBAKEDIR="$2"
     fi
 fi
@@ -72,9 +72,9 @@ else
 fi
 unset BDIR
 
-if [ -z "$BITBAKEDIR" ]; then
-    BITBAKEDIR="$OEROOT/bitbake$BBEXTRA"
-    test -d "$BITBAKEDIR" || BITBAKEDIR="$OEROOT/../bitbake$BBEXTRA"
+if [ -z "${BITBAKEDIR:-}" ]; then
+    BITBAKEDIR="$OEROOT/bitbake${BBEXTRA:-}"
+    test -d "$BITBAKEDIR" || BITBAKEDIR="$OEROOT/../bitbake${BBEXTRA:-}"
 fi
 
 BITBAKEDIR=$(readlink -f "$BITBAKEDIR")
@@ -89,11 +89,11 @@ if [ ! -d "$BITBAKEDIR" ]; then
 fi
 
 # Add BitBake's library to PYTHONPATH
-PYTHONPATH=$BITBAKEDIR/lib:$PYTHONPATH
+PYTHONPATH=$BITBAKEDIR/lib:${PYTHONPATH:-}
 export PYTHONPATH
 
 # Remove any paths added by sourcing this script before
-[ -n "$OE_ADDED_PATHS" ] && PATH=$(echo $PATH | sed -e "s#$OE_ADDED_PATHS##") ||
+[ -n "${OE_ADDED_PATHS:-}" ] && PATH=$(echo $PATH | sed -e "s#$OE_ADDED_PATHS##") ||
     PATH=$(echo $PATH | sed -e "s#$OEROOT/scripts:$BITBAKEDIR/bin:##")
 
 # Make sure our paths are at the beginning of $PATH
@@ -114,6 +114,6 @@ SDKMACHINE BB_NUMBER_THREADS BB_NO_NETWORK PARALLEL_MAKE GIT_PROXY_COMMAND \
 SOCKS5_PASSWD SOCKS5_USER SCREENDIR STAMPS_DIR BBPATH_EXTRA BB_SETSCENE_ENFORCE \
 BB_LOGCONFIG"
 
-BB_ENV_PASSTHROUGH_ADDITIONS="$(echo $BB_ENV_PASSTHROUGH_ADDITIONS $BB_ENV_PASSTHROUGH_ADDITIONS_OE | tr ' ' '\n' | LC_ALL=C sort --unique | tr '\n' ' ')"
+BB_ENV_PASSTHROUGH_ADDITIONS="$(echo ${BB_ENV_PASSTHROUGH_ADDITIONS:-} $BB_ENV_PASSTHROUGH_ADDITIONS_OE | tr ' ' '\n' | LC_ALL=C sort --unique | tr '\n' ' ')"
 
 export BB_ENV_PASSTHROUGH_ADDITIONS
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] oe-init-build-env: fix unbound variable errors under 'set -u'
  2026-11-11 19:56 [PATCH] oe-init-build-env: fix unbound variable errors under 'set -u' Babanpreet Singh
@ 2026-11-23  9:25 ` Paul Barker
  2026-11-24 19:10   ` [PATCH] oeqa/selftest/oescripts: add a test for oe-init-build-env under set -u Babanpreet Singh
  0 siblings, 1 reply; 3+ messages in thread
From: Paul Barker @ 2026-11-23  9:25 UTC (permalink / raw)
  To: Babanpreet Singh, openembedded-core; +Cc: Ross Burton

On Wed Nov 11, 2026 at 7:56 PM GMT, Babanpreet Singh wrote:
> Sourcing oe-init-build-env from a shell running with 'set -u' fails:
>
>   ./oe-init-build-env: line 29: BBSERVER: unbound variable
>
> Give the variables that may be unset an explicit empty default, here
> and in oe-buildenv-internal.
>
> [YOCTO #15469]
>
> Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
> ---
>  oe-init-build-env            | 14 +++++++-------
>  scripts/oe-buildenv-internal | 22 +++++++++++-----------
>  2 files changed, 18 insertions(+), 18 deletions(-)
>
> diff --git a/oe-init-build-env b/oe-init-build-env
> index 5a2bd0f4d9..660c6ad02e 100755
> --- a/oe-init-build-env
> +++ b/oe-init-build-env
> @@ -14,9 +14,9 @@
>  # when being sourced.  To workaround the shell limitation use "set <builddir>"
>  # prior to sourcing this script.
>  #
> -if [ -n "$BASH_SOURCE" ]; then
> +if [ -n "${BASH_SOURCE:-}" ]; then
>      THIS_SCRIPT=$BASH_SOURCE
> -elif [ -n "$ZSH_NAME" ]; then
> +elif [ -n "${ZSH_NAME:-}" ]; then
>      THIS_SCRIPT=$0
>  else
>      THIS_SCRIPT="$(pwd)/oe-init-build-env"
> @@ -26,16 +26,16 @@ else
>          exit 1
>      fi
>  fi
> -if [ -n "$BBSERVER" ]; then
> +if [ -n "${BBSERVER:-}" ]; then
>      unset BBSERVER
>  fi
>  
> -if [ -z "$ZSH_NAME" ] && [ "$0" = "$THIS_SCRIPT" ]; then
> +if [ -z "${ZSH_NAME:-}" ] && [ "$0" = "$THIS_SCRIPT" ]; then
>      echo "Error: This script needs to be sourced. Please run as '. $THIS_SCRIPT'" >&2
>      exit 1
>  fi
>  
> -if [ -z "$OEROOT" ]; then
> +if [ -z "${OEROOT:-}" ]; then
>      OEROOT=$(dirname "$THIS_SCRIPT")
>      OEROOT=$(readlink -f "$OEROOT")
>  fi
> @@ -43,12 +43,12 @@ unset THIS_SCRIPT
>  
>  export OEROOT
>  . "$OEROOT"/scripts/oe-buildenv-internal &&
> -    TEMPLATECONF="$TEMPLATECONF" "$OEROOT"/scripts/oe-setup-builddir || {
> +    TEMPLATECONF="${TEMPLATECONF:-}" "$OEROOT"/scripts/oe-setup-builddir || {
>      unset OEROOT
>      return 1
>  }
>  
>  unset OEROOT
>  
> -[ -z "$BUILDDIR" ] || cd "$BUILDDIR"
> +[ -z "${BUILDDIR:-}" ] || cd "$BUILDDIR"
>  
> diff --git a/scripts/oe-buildenv-internal b/scripts/oe-buildenv-internal
> index 2fdb19565a..6b2e4ea518 100755
> --- a/scripts/oe-buildenv-internal
> +++ b/scripts/oe-buildenv-internal
> @@ -19,12 +19,12 @@ if ! $(return >/dev/null 2>&1) ; then
>  fi
>  
>  # It is assumed OEROOT is already defined when this is called
> -if [ -z "$OEROOT" ]; then
> +if [ -z "${OEROOT:-}" ]; then
>      echo >&2 "Error: OEROOT is not defined!"
>      return 1
>  fi
>  
> -if [ -z "$OE_SKIP_SDK_CHECK" ] && [ -n "$OECORE_SDK_VERSION" ]; then
> +if [ -z "${OE_SKIP_SDK_CHECK:-}" ] && [ -n "${OECORE_SDK_VERSION:-}" ]; then
>      echo >&2 "Error: The OE SDK/ADT was detected as already being present in this shell environment. Please use a clean shell when sourcing this environment script."
>      return 1
>  fi
> @@ -39,8 +39,8 @@ if [ "$py_v38_check" != "True" ]; then
>  fi
>  unset py_v38_check
>  
> -if [ -z "$BDIR" ]; then
> -    if [ -z "$1" ]; then
> +if [ -z "${BDIR:-}" ]; then
> +    if [ -z "${1:-}" ]; then
>          BDIR="build"
>      else
>          BDIR="$1"
> @@ -61,7 +61,7 @@ if [ -z "$BDIR" ]; then
>              return 1
>          fi
>      fi
> -    if [ -n "$2" ]; then
> +    if [ -n "${2:-}" ]; then
>          BITBAKEDIR="$2"
>      fi
>  fi
> @@ -72,9 +72,9 @@ else
>  fi
>  unset BDIR
>  
> -if [ -z "$BITBAKEDIR" ]; then
> -    BITBAKEDIR="$OEROOT/bitbake$BBEXTRA"
> -    test -d "$BITBAKEDIR" || BITBAKEDIR="$OEROOT/../bitbake$BBEXTRA"
> +if [ -z "${BITBAKEDIR:-}" ]; then
> +    BITBAKEDIR="$OEROOT/bitbake${BBEXTRA:-}"
> +    test -d "$BITBAKEDIR" || BITBAKEDIR="$OEROOT/../bitbake${BBEXTRA:-}"
>  fi
>  
>  BITBAKEDIR=$(readlink -f "$BITBAKEDIR")
> @@ -89,11 +89,11 @@ if [ ! -d "$BITBAKEDIR" ]; then
>  fi
>  
>  # Add BitBake's library to PYTHONPATH
> -PYTHONPATH=$BITBAKEDIR/lib:$PYTHONPATH
> +PYTHONPATH=$BITBAKEDIR/lib:${PYTHONPATH:-}
>  export PYTHONPATH
>  
>  # Remove any paths added by sourcing this script before
> -[ -n "$OE_ADDED_PATHS" ] && PATH=$(echo $PATH | sed -e "s#$OE_ADDED_PATHS##") ||
> +[ -n "${OE_ADDED_PATHS:-}" ] && PATH=$(echo $PATH | sed -e "s#$OE_ADDED_PATHS##") ||
>      PATH=$(echo $PATH | sed -e "s#$OEROOT/scripts:$BITBAKEDIR/bin:##")
>  
>  # Make sure our paths are at the beginning of $PATH
> @@ -114,6 +114,6 @@ SDKMACHINE BB_NUMBER_THREADS BB_NO_NETWORK PARALLEL_MAKE GIT_PROXY_COMMAND \
>  SOCKS5_PASSWD SOCKS5_USER SCREENDIR STAMPS_DIR BBPATH_EXTRA BB_SETSCENE_ENFORCE \
>  BB_LOGCONFIG"
>  
> -BB_ENV_PASSTHROUGH_ADDITIONS="$(echo $BB_ENV_PASSTHROUGH_ADDITIONS $BB_ENV_PASSTHROUGH_ADDITIONS_OE | tr ' ' '\n' | LC_ALL=C sort --unique | tr '\n' ' ')"
> +BB_ENV_PASSTHROUGH_ADDITIONS="$(echo ${BB_ENV_PASSTHROUGH_ADDITIONS:-} $BB_ENV_PASSTHROUGH_ADDITIONS_OE | tr ' ' '\n' | LC_ALL=C sort --unique | tr '\n' ' ')"
>  
>  export BB_ENV_PASSTHROUGH_ADDITIONS

Hi,

This change looks good to me, but it does suggest we need a test case
here for sourcing oe-init-build-env in a script using `set -u`. Are you
able to write a test case?

Best regards,

-- 
Paul Barker


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] oeqa/selftest/oescripts: add a test for oe-init-build-env under set -u
  2026-11-23  9:25 ` Paul Barker
@ 2026-11-24 19:10   ` Babanpreet Singh
  0 siblings, 0 replies; 3+ messages in thread
From: Babanpreet Singh @ 2026-11-24 19:10 UTC (permalink / raw)
  To: openembedded-core; +Cc: Paul Barker, Ross Burton, Babanpreet Singh

Add a test that sources oe-init-build-env from bash and dash with
'set -u', to catch a regression of 81df4c8650 ("oe-init-build-env: fix
unbound variable errors under 'set -u'"). Both fail with that commit
reverted.

[YOCTO #15469]

Suggested-by: Paul Barker <paul@pbarker.dev>
AI-Generated: Uses Claude (claude-opus-5-5)
Signed-off-by: Babanpreet Singh <bbnpreetsingh@gmail.com>
---
 meta/lib/oeqa/selftest/cases/oescripts.py | 26 +++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/meta/lib/oeqa/selftest/cases/oescripts.py b/meta/lib/oeqa/selftest/cases/oescripts.py
index 3e0bd6f4ec..342e6531df 100644
--- a/meta/lib/oeqa/selftest/cases/oescripts.py
+++ b/meta/lib/oeqa/selftest/cases/oescripts.py
@@ -7,6 +7,7 @@
 import os
 import shutil
 import importlib
+import tempfile
 import unittest
 from oeqa.selftest.case import OESelftestTestCase
 from oeqa.utils.commands import runCmd, bitbake, get_bb_var
@@ -103,6 +104,31 @@ class OEGitproxyTests(OESelftestTestCase):
             self.skipTest("No \"dash\" found on test system.")
         self.run_oegitproxy(custom_shell=dash)
 
+class OEInitBuildEnvTests(OESelftestTestCase):
+
+    @classmethod
+    def setUpClass(cls):
+        super().setUpClass()
+        cls.corebase = get_bb_var('COREBASE')
+
+    def run_oe_init_build_env(self, shell):
+        # The environment oe-selftest runs in already sets some of the
+        # variables the script reads, so start from an empty one
+        with tempfile.TemporaryDirectory(prefix="oe-init-build-env-") as builddir:
+            script = 'set -u; . ./oe-init-build-env && echo "BUILDDIR=$BUILDDIR"'
+            result = runCmd([shell, '-c', script, shell, builddir],
+                            cwd=self.corebase, env={'PATH': os.environ['PATH']})
+            self.assertIn('BUILDDIR=' + os.path.realpath(builddir), result.output)
+
+    def test_oe_init_build_env_nounset_bash(self):
+        self.run_oe_init_build_env('bash')
+
+    def test_oe_init_build_env_nounset_dash(self):
+        dash = shutil.which("dash")
+        if dash is None:
+            self.skipTest("No \"dash\" found on test system.")
+        self.run_oe_init_build_env(dash)
+
 class OeRunNativeTest(OESelftestTestCase):
     def test_oe_run_native(self):
         bitbake("qemu-helper-native -c addto_recipe_sysroot")
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24 19:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-11 19:56 [PATCH] oe-init-build-env: fix unbound variable errors under 'set -u' Babanpreet Singh
2026-11-23  9:25 ` Paul Barker
2026-11-24 19:10   ` [PATCH] oeqa/selftest/oescripts: add a test for oe-init-build-env under set -u Babanpreet Singh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox