Bug 1942153 - Which cannot be used with unbound variable checking
Summary: Which cannot be used with unbound variable checking
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Fedora
Classification: Fedora
Component: which
Version: rawhide
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Than Ngo
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
: 1948033 (view as bug list)
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2021-03-23 18:47 UTC by Zdenek Kabelac
Modified: 2021-05-06 13:31 UTC (History)
7 users (show)

Fixed In Version: which-2.21-26.fc34
Doc Type: If docs needed, set a value
Doc Text:
Clone Of:
Environment:
Last Closed: 2021-04-26 12:23:32 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)

Description Zdenek Kabelac 2021-03-23 18:47:15 UTC
Whenever I run which in bash with enabled 'unbound variable protection'
(aka set -euE -o pipefail)

I'm getting this:

environment: line 1: _declare: unbound variable


----/etc/profile.d/which2.sh----

# Initialization script for bash, sh, mksh and ksh

_declare="declare -f"
_opt="-f"

if [ "$0" = "ksh" ] || [ "$0" = "-ksh" ] || [ "$0" = "mksh" ] || [ "$0" = "-mksh" ] ; then
  _declare="typeset -f"
  _opt=""
fi
 
which ()
{
(alias; eval ${_declare}) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
}

export ${_opt} which

Comment 1 Zdenek Kabelac 2021-03-23 18:47:51 UTC
Version in use:

which-2.21-23.fc35.x86_64

Comment 2 Zdenek Kabelac 2021-03-23 18:52:13 UTC
Downgrade to version  which-2.21-22.fc35.x86_64  solve the problem for now...


--- which2.sh	2021-03-18 17:50:28.000000000 +0100
+++ which2.sh.bug	2021-03-23 19:49:12.579664153 +0100
@@ -1,11 +1,16 @@
 # Initialization script for bash, sh, mksh and ksh
+
+_declare="declare -f"
+_opt="-f"
+
+if [ "$0" = "ksh" ] || [ "$0" = "-ksh" ] || [ "$0" = "mksh" ] || [ "$0" = "-mksh" ] ; then
+  _declare="typeset -f"
+  _opt=""
+fi
+ 
 which ()
 {
-  if [ "$0" = "ksh" -o "$0" = "-ksh" -o "$0" = "mksh" -o "$0" = "-mksh" ] ; then
-    (alias; typeset -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@
-    export which
-  else
-    (alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@
-    export -f which
-  fi
+(alias; eval ${_declare}) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
 }
+
+export ${_opt} which

Comment 3 Esa Varemo 2021-03-23 19:29:14 UTC
The `export -f` introduced by the new script also throws errors with ZSH (not sure if I should open another bug or not).

To reproduce:

```
podman run --rm -ti fedora:34 bash -c "dnf install -y which zsh; zsh"
[skipping dnf output]
/etc/profile.d/which2.sh:export:16: invalid option(s)
[root@0fd8d9951815]/#
```

The export builtin in zsh doesn't have `-f` and in general doesn't support exporting functions

Comment 4 Than Ngo 2021-03-25 19:32:42 UTC
it's fixed in which-2.21-24.fc35.x86_64

Comment 5 Zdenek Kabelac 2021-04-06 19:41:28 UTC
Reopening as it still does not work:

which-2.21-24.fc35.x86_64   still reports:

environment: line 1: _declare: unbound variable


Thus command cannot be used in strict bash environment.

Comment 6 Craig 2021-04-09 20:38:24 UTC
*** Bug 1948033 has been marked as a duplicate of this bug. ***

Comment 7 Craig 2021-04-09 20:45:07 UTC
I'm also seeing this issue using which-2.21-24.fc34.x86_64

Comment 8 redhat 2021-04-26 08:16:31 UTC
For zsh the problem moved a bit since the fix by Than Ngo https://bugzilla.redhat.com/show_bug.cgi?id=1942153#c4, but zsh is still complaining.

```
podman run --rm -ti fedora:34 bash -c "dnf install -y which zsh; zsh"
[skipping dnf output]
basename: missing operand
Try 'basename --help' for more information.
/etc/profile.d/which2.sh:export:18: invalid option(s)
```

because $SHELL is not set to /usr/bin/zsh at that time.
When I locally started zsh from a bash and printed SHELL, then $SHELL was still set to /bin/bash and the if clause did not work properly. Hence, then I got

```
zsh
/etc/profile.d/which2.sh:export:18: invalid option(s)
```

(without the basename error).

Comment 9 Than Ngo 2021-04-26 12:05:31 UTC
could (In reply to Zdenek Kabelac from comment #5)
> Reopening as it still does not work:
> 
> which-2.21-24.fc35.x86_64   still reports:
> 
> environment: line 1: _declare: unbound variable
> 
> 
> Thus command cannot be used in strict bash environment.

Zdenek, could you download and test new build ?

  https://koji.fedoraproject.org/koji/taskinfo?taskID=66715419

Thanks

Comment 10 Zdenek Kabelac 2021-04-26 12:13:53 UTC
(In reply to Than Ngo from comment #9)
> could (In reply to Zdenek Kabelac from comment #5)
> > Reopening as it still does not work:
> > 
> > which-2.21-24.fc35.x86_64   still reports:
> > 
> > environment: line 1: _declare: unbound variable
> > 
> > 
> > Thus command cannot be used in strict bash environment.
> 
> Zdenek, could you download and test new build ?
> 
>   https://koji.fedoraproject.org/koji/taskinfo?taskID=66715419


I've installed and checked some cases and it appears to be working for me.

Comment 11 Than Ngo 2021-04-26 12:23:32 UTC
Zdenek, thanks for the testing and feedback!

Comment 12 redhat 2021-04-26 13:18:10 UTC
(In reply to Than Ngo from comment #11)
> Zdenek, thanks for the testing and feedback!

Thank you for fixing it. I can confirm it's working for the zsh case, too.

Comment 13 Simon Gerhards 2021-05-01 11:21:46 UTC
This also affects Fedora 34.

The fix from Rawhide does not fix it completely for me. It still produces an error if "set -o pipefail" is used in addition to unbound variable checking.

The following script reproduces the problem here:
"""
#!/usr/bin/bash
set -uo pipefail
which bash
"""

Running it results in the following output:
"""
environment: line 1: which_declare: unbound variable
/usr/bin/bash
"""

Comment 14 Than Ngo 2021-05-04 07:27:31 UTC
(In reply to Simon Gerhards from comment #13)
> This also affects Fedora 34.
> 
> The fix from Rawhide does not fix it completely for me. It still produces an
> error if "set -o pipefail" is used in addition to unbound variable checking.
> 
> The following script reproduces the problem here:
> """
> #!/usr/bin/bash
> set -uo pipefail
> which bash
> """
> 
> Running it results in the following output:
> """
> environment: line 1: which_declare: unbound variable
> /usr/bin/bash
> """

it's fixed in which-2.21-26.fc35, built in rawhide. Could you please test it?

Thanks

Comment 15 Fedora Update System 2021-05-04 12:04:59 UTC
FEDORA-2021-9c4af8a655 has been submitted as an update to Fedora 34. https://bodhi.fedoraproject.org/updates/FEDORA-2021-9c4af8a655

Comment 16 Fedora Update System 2021-05-05 01:50:47 UTC
FEDORA-2021-9c4af8a655 has been pushed to the Fedora 34 testing repository.
Soon you'll be able to install the update with the following command:
`sudo dnf upgrade --enablerepo=updates-testing --advisory=FEDORA-2021-9c4af8a655`
You can provide feedback for this update here: https://bodhi.fedoraproject.org/updates/FEDORA-2021-9c4af8a655

See also https://fedoraproject.org/wiki/QA:Updates_Testing for more information on how to test updates.

Comment 17 Fedora Update System 2021-05-06 01:01:37 UTC
FEDORA-2021-9c4af8a655 has been pushed to the Fedora 34 stable repository.
If problem still persists, please make note of it in this bug report.

Comment 18 Simon Gerhards 2021-05-06 06:16:50 UTC
The fix works for me.

Thanks!

Comment 19 Andrea (a.k.a. valdar) 2021-05-06 13:31:04 UTC
I can confirm the fix as well.

Thanks!


Note You need to log in before you can comment on or make changes to this bug.