Skip to content

failing to source virtualenvwrapper on alpine linux (ash) #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
agowa opened this issue Sep 2, 2024 · 9 comments · Fixed by #117
Open

failing to source virtualenvwrapper on alpine linux (ash) #105

agowa opened this issue Sep 2, 2024 · 9 comments · Fixed by #117

Comments

@agowa
Copy link

agowa commented Sep 2, 2024

Hi, currently the virtualenvwrapper.sh script fails to be sourced on alpine linux.

This line of code

export VIRTUALENVWRAPPER_SCRIPT="${.sh.file}"

currently causes -ash: /usr/bin/virtualenvwrapper.sh: line 109: syntax error: bad substitution when using the ash shell on alpine linux.

@agowa
Copy link
Author

agowa commented Sep 2, 2024

May be the same issue as with fish shell however I've not tried it there. Just guessing from the surrounding code.
#100

@ferdnyc
Copy link
Contributor

ferdnyc commented Mar 24, 2025

${.sh.file} is ksh syntax, but ksh support was dropped in commit d736549 so that branch probably should've been taken out too.

That still won't help your shell, though, because without a default VIRTUALENVWRAPPER_SCRIPT won't get set.

Really, using $0 should probably be the default -- that will even work in Bash, under most cases, although $BASH_SOURCE is certainly better if it's available.

@ferdnyc
Copy link
Contributor

ferdnyc commented Mar 24, 2025

Although, interestingly, when sourcing a script it seems that $0 is the ONLY thing that works for Zsh, and $BASH_SOURCE is the only thing that works for Bash.

so I guess the first question is, if you save this script to a file test_arg0.sh:

echo "0: $0"
echo "BASH_SOURCE: ${BASH_SOURCE}"
echo "ZSH_ARGZERO: ${ZSH_ARGZERO}"
echo "ZSH_SCRIPT: ${ZSH_SCRIPT}"

# Feel free to add any additional variables
# your shell's documentation indicates might be useful.

Then what do you get in ash for each of these scenarios?

  1. (At any shell prompt):
    ash test_arg0.sh
  2. (At an ash shell prompt):
    source test_arg0.sh

Here's what I get

Zsh

$ zsh test_arg0.sh
0: test_arg0.sh
BASH_SOURCE: 
ZSH_ARGZERO: test_arg0.sh
ZSH_SCRIPT: test_arg0.sh

$ source test_arg0.sh
0: test_arg0.sh
BASH_SOURCE: 
ZSH_ARGZERO: zsh
ZSH_SCRIPT: 

Bash

$ bash test_arg0.sh 
0: test_arg0.sh
BASH_SOURCE: test_arg0.sh
ZSH_ARGZERO: 
ZSH_SCRIPT: 

$ source test_arg0.sh 
0: bash
BASH_SOURCE: test_arg0.sh
ZSH_ARGZERO: 
ZSH_SCRIPT: 

ferdnyc added a commit to ferdnyc/virtualenvwrapper that referenced this issue Mar 24, 2025
- `${.sh.file}` was ksh syntax, but ksh support was dropped in commit
  d736549
- Instead of testing only for certain specific shells (and excluding
  all others), use `$BASH_SOURCE` if it's set, otherwise fall back
  to `$0` which should work with more shells (hopefully).

  Fixes python-virtualenvwrapper#105
@ferdnyc
Copy link
Contributor

ferdnyc commented Mar 24, 2025

May be the same issue as with fish shell however I've not tried it there. Just guessing from the surrounding code.
#100

I don't think fish is ever gonna work. It dumps out with an error anytime a variable reference uses curly braces! (What are they THINKING??)

$  source test_arg0.sh
test_arg0.sh (line 2): Variables cannot be bracketed. In fish, please use "$BASH_SOURCE".
echo "BASH_SOURCE: ${BASH_SOURCE}"

@dhellmann
Copy link
Contributor

I think there's an alternative implementation for fish.

I've never heard of the ash shell. If its syntax can be supported without breaking anything, I could review patches.

@ferdnyc
Copy link
Contributor

ferdnyc commented Mar 30, 2025

@DHELLMAN See #117, though I don't know whether that's a complete solution. It at least gives ash and others like it a fighting chance, by no longer defaulting to KSH syntax for all shells that aren't Bash or Zsh.

ferdnyc added a commit to ferdnyc/virtualenvwrapper that referenced this issue Mar 31, 2025
- `${.sh.file}` was ksh syntax, but ksh support was dropped in commit
  d736549
- Instead of testing only for certain specific shells (and excluding
  all others), use `$BASH_SOURCE` if it's set, otherwise fall back
  to `$0` which should work with more shells (hopefully).

  Fixes python-virtualenvwrapper#105
@ferdnyc
Copy link
Contributor

ferdnyc commented Mar 31, 2025

I just remembered that alpine publishes docker containers, which makes it really easy to test the ash shell on a different system. So...

$ docker pull alpine:latest
$ docker run -t -i alpine:latest /bin/ash
/ # cat > test_arg0.sh
echo "0: $0"
echo "BASH_SOURCE: ${BASH_SOURCE}"
echo "ZSH_ARGZERO: ${ZSH_ARGZERO}"
echo "ZSH_SCRIPT: ${ZSH_SCRIPT}"
/ # source test_arg0.sh
0: /bin/ash
BASH_SOURCE: 
ZSH_ARGZERO: 
ZSH_SCRIPT: 

Conclusion: None of the existing methods will work for Ash. $0 shows us the wrong path. So we need to look for other supported options.

Looking at the man page, I don't see any documented method of determining the path where a sourced script is located. $0 is all it supports, and that remains the path to the shell binary.

@mergify mergify bot closed this as completed in #117 Mar 31, 2025
@mergify mergify bot closed this as completed in 23c86d9 Mar 31, 2025
@dhellmann dhellmann reopened this Mar 31, 2025
@ferdnyc
Copy link
Contributor

ferdnyc commented Mar 31, 2025

Ah, crap, that's my fault, I shouldn't have marked that PR as "fixes" this issue, since it doesn't.

...Thanks, you beat me to it.

@ferdnyc
Copy link
Contributor

ferdnyc commented Mar 31, 2025

I've posted a StackExchange question asking how this might be solved.

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 a pull request may close this issue.

3 participants