Skip to content
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

Env's name disappears from the prompt when spawning a subshell #458

Open
startakovsky opened this issue Apr 15, 2023 · 6 comments
Open

Env's name disappears from the prompt when spawning a subshell #458

startakovsky opened this issue Apr 15, 2023 · 6 comments

Comments

@startakovsky
Copy link

I am using zshell (default for Mac) and have pyenv installed. The way pyenv works is that it will automatically switch between virtual environments depending on which folder I'm in and which folder I switch to using the .python-version file. The code that enables this is run inside my ~/.zshrc file, and it's pretty boilerplate for pyenv.

I can tell my environment is running because I have something like this at the beginning of my prompt (myenvironment) folder $ in my zsh.

But when I open a new session by typing $ zsh -l, I don't have the same prompt, (myenvironment) goes away.

So what I do to fix things is type cd .., hit enter, then cd - hit enter, and we're done.

Interestingly, it won't actually work if I do cd .. && cd - or cd .. ; cd -, but when I do what I described, I get the prompt above.

What I am looking for is a way to do this automatically... any tips? Is this a bug that can be fixed or some way to invoke the environment change... there are downstream effects of this, and it's not just the prompt that I care about, I just assume it's out of scope for this ticket.

@native-api native-api changed the title How to trigger automatically trigger changing directories. Env's name disappears from the prompt when spawning a subshell Apr 16, 2023
@native-api native-api transferred this issue from pyenv/pyenv Apr 16, 2023
@native-api
Copy link
Member

Diagnosed the issue. The environment is actually still active in the subshell (all the revelant envvars are set), it just disappears from the prompt because PS1 is reset by /etc/zshrc's code.

@startakovsky
Copy link
Author

Thanks @native-api. I think more is going on that what you describe based on this inconsistent behavior with conda, a tool that comes with the anaconda installation via pyenv. See below for a demo of what I am talking about.

~ mkdir code/show-native-api
➜  ~ cd code/show-native-api
➜  show-native-api pyenv local anaconda3-2023.03
(anaconda3-2023.03) ➜  show-native-api ls -la
total 8
drwxr-xr-x   3 steven  staff   96 Apr 17 18:58 .
drwxr-xr-x  26 steven  staff  832 Apr 17 18:57 ..
-rw-r--r--   1 steven  staff   18 Apr 17 18:58 .python-version
(anaconda3-2023.03) ➜  show-native-api conda create --name show-native-api
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /Users/steven/.pyenv/versions/anaconda3-2023.03/envs/show-native-api



Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate show-native-api
#
# To deactivate an active environment, use
#
#     $ conda deactivate

(anaconda3-2023.03) ➜  show-native-api conda activate show-native-api
(show-native-api) (anaconda3-2023.03) ➜  show-native-api echo "pretty prompt"
pretty prompt
(show-native-api) (anaconda3-2023.03) ➜  show-native-api zsh -l
➜  show-native-api echo "no prompt, but yes pyenv activate?"
no prompt, but yes pyenv activate?
➜  show-native-api pyenv version
anaconda3-2023.03 (set by /Users/steven/code/show-native-api/.python-version)
➜  show-native-api echo "seems activated, let's activate the conda virtual envir
onment"
seems activated, let's activate the conda virtual environment
➜  show-native-api conda activate show-native-api

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


➜  show-native-api echo "not good"
not good
➜  show-native-api cd .. && cd -
~/code/show-native-api
➜  show-native-api conda activate show-native-api

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.


➜  show-native-api echo "still not good"
still not good
➜  show-native-api cd ..
➜  code cd -
~/code/show-native-api
(anaconda3-2023.03) ➜  show-native-api conda activate show-native-api
(show-native-api) (anaconda3-2023.03) ➜  show-native-api echo "now it's good"
now it's good

@native-api
Copy link
Member

native-api commented Apr 18, 2023

In any case, I was writing a fix by detecting this situation (PS1 is reset to the original value), but is stumped by the fact that when pyenv-sh-activate code runs, PS1 appears to the blank (expands to a blank value) -- so I cannot check it.

Do you know anything about that? This seems to be another one of Zsh's quirks. Googling and checking the man pages came up empty so far.

@startakovsky
Copy link
Author

No, I didn't know this, but I do see the message from time to time that they are not going to show the environment name by default in the future, and don't know if it's related to me not seeing it. But as you can see from the terminal output, it's not even about how it's activated, it's that things are different before doing cd .. cd - and not able to be streamlined with cd .. && cd - . Namely, it's this conda activate that seems to not work unless time is spend outside of the directory.

@startakovsky
Copy link
Author

Also, my .zshrc file is very boilerplate, I have oh-my-zsh installed and nothing custom to conda init. As a matter of fact it is interesting, somehow conda init is being run or the environment is being activated in some more robust way when you exit and reenter the directory.

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="robbyrussell"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
eval "$(pyenv init -)"
source ~/.iterm2_shell_integration.zsh
export NVM_DIR="$HOME/.nvm"
  [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh"  # This loads nvm
  [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm"  # This loads nvm bash_completion
# place this after nvm initialization!
load_nvmrc
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
eval "$(rbenv init - zsh)"
export GEM_HOME="$(ruby -e 'puts Gem.user_dir')"
export PATH="$PATH:$GEM_HOME/bin"

autoload -U add-zsh-hook
add-zsh-hook chpwd load_nvmrc

@native-api
Copy link
Member

➜  show-native-api conda activate show-native-api

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.

Found the reason for this. Since version 4, Conda doesn't copy itself to alternate environments but rather creates a shell function conda. Which, unlike envvars, does not survive spawning a new shell.

Now, why do you expect a virtual environment to survive spawning a new shell? This doesn't work with base virtualenv, venv or conda, either.

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

No branches or pull requests

2 participants