linux-cultist / venv-selector.nvim

Allows selection of python virtual environment from within neovim
MIT License
375 stars 40 forks source link

Opening a terminal with miniconda env causes a puzzling warning message #147

Open barikata1984 opened 1 week ago

barikata1984 commented 1 week ago

Hi all,

I am using this plugin to autoload miniconda envs per project. I notice that there is a new regexp branch and it is recommended to move to that branch. So I did it. After following this instruction on #117, I believe that I could notice this plugin of the location of miniconda envs and actually I can set a virtual env per project. But, when I activate a terminal with this plugin, I get the following warning message now although I haven't have it with the main branch:

[WARNING]: Console output during zsh initialization detected.

When using Powerlevel10k with instant prompt, console output during zsh
initialization may indicate issues.

You can:

  - Recommended: Change ~/.zshrc so that it does not perform console I/O
    after the instant prompt preamble. See the link below for details.

    * You will not see this error message again.
    * Zsh will start quickly and prompt will update smoothly.

  - Suppress this warning either by running p10k configure or by manually
    defining the following parameter:

      typeset -g POWERLEVEL9K_INSTANT_PROMPT=quiet

    * You will not see this error message again.
    * Zsh will start quickly but prompt will jump down after initialization.

  - Disable instant prompt either by running p10k configure or by manually
    defining the following parameter:

      typeset -g POWERLEVEL9K_INSTANT_PROMPT=off

    * You will not see this error message again.
    * Zsh will start slowly.

  - Do nothing.

    * You will see this error message every time you start zsh.
    * Zsh will start quickly but prompt will jump down after initialization.

For details, see:
https://github.com/romkatv/powerlevel10k/blob/master/README.md#instant-prompt

-- console output produced during zsh initialization follows --

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "/home/atsushi/miniconda3/lib/python3.12/site-packages/conda/exception_handler.py", lin
e 18, in __call__
        return func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
      File "/home/atsushi/miniconda3/lib/python3.12/site-packages/conda/cli/main.py", line 110, in
 main_sourced
        print(activator.execute(), end="")
              ^^^^^^^^^^^^^^^^^^^
      File "/home/atsushi/miniconda3/lib/python3.12/site-packages/conda/activate.py", line 212, in
 execute
        return getattr(self, self.command)()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/atsushi/miniconda3/lib/python3.12/site-packages/conda/activate.py", line 177, in
 activate
        builder_result = self.build_activate(self.env_name_or_prefix)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/atsushi/miniconda3/lib/python3.12/site-packages/conda/activate.py", line 355, in
 build_activate
        return self._build_activate_stack(env_name_or_prefix, False)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/atsushi/miniconda3/lib/python3.12/site-packages/conda/activate.py", line 445, in
 _build_activate_stack
        deactivate_scripts = self._get_deactivate_scripts(old_conda_prefix)
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/home/atsushi/miniconda3/lib/python3.12/site-packages/conda/activate.py", line 808, in
 _get_deactivate_scripts
        for entry in os.scandir(join(prefix, "etc", "conda", "deactivate.d"))
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<frozen posixpath>", line 76, in join
    TypeError: expected str, bytes or os.PathLike object, not NoneType

`$ /home/atsushi/miniconda3/bin/conda shell.posix activate base`

  environment variables:
                 CIO_TEST=<not set>
        CONDA_DEFAULT_ENV=base
                CONDA_EXE=/home/atsushi/miniconda3/bin/conda
    CONDA_PROMPT_MODIFIER=(base)
         CONDA_PYTHON_EXE=/home/atsushi/miniconda3/bin/python
               CONDA_ROOT=/home/atsushi/miniconda3
              CONDA_SHLVL=1
           CURL_CA_BUNDLE=<not set>
          LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda-11.7/lib64:/usr/local/cuda-
                          11.8/lib64:/usr/local/cuda-12.1/lib64
               LD_PRELOAD=<not set>
                     PATH=/home/atsushi/miniconda3/envs/nemd/bin:/home/atsushi/.local/share/nvim
                          /mason/bin:/home/atsushi/miniconda3/bin:/home/atsushi/miniconda3/conda
                          bin:/usr/local/cuda/bin:/home/atsushi/.local/bin:/usr/local/sbin:/usr/
                          local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/s
                          nap/bin:/snap/bin
       REQUESTS_CA_BUNDLE=<not set>
            SSL_CERT_FILE=<not set>
               WINDOWPATH=2

     active environment : None
            shell level : 1
       user config file : /home/atsushi/.condarc
 populated config files : /home/atsushi/.condarc
          conda version : 24.5.0
    conda-build version : not installed
         python version : 3.12.4.final.0
                 solver : libmamba (default)
       virtual packages : __archspec=1=skylake
                          __conda=24.5.0=0
                          __cuda=12.4=0
                          __glibc=2.35=0
                          __linux=6.5.0=0
                          __unix=0=0
       base environment : /home/atsushi/miniconda3  (writable)
      conda av data dir : /home/atsushi/miniconda3/etc/conda
  conda av metadata url : None
           channel URLs : https://conda.anaconda.org/conda-forge/linux-64
                          https://conda.anaconda.org/conda-forge/noarch
                          https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /home/atsushi/miniconda3/pkgs
                          /home/atsushi/.conda/pkgs
       envs directories : /home/atsushi/miniconda3/envs
                          /home/atsushi/.conda/envs
               platform : linux-64
             user-agent : conda/24.5.0 requests/2.32.3 CPython/3.12.4 Linux/6.5.0-41-generic ubunt
u/22.04.4 glibc/2.35 solver/libmamba conda-libmamba-solver/24.1.0 libmambapy/1.5.8
                UID:GID : 1000:1000
             netrc file : /home/atsushi/.netrc
           offline mode : False

An unexpected error has occurred. Conda has prepared the above report.
If you suspect this error is being caused by a malfunctioning plugin,
consider using the --no-plugins option to turn off plugins.

Example: conda --no-plugins install <package>

Alternatively, you can set the CONDA_NO_PLUGINS environment variable on
the command line to run the command without plugins enabled.

Example: CONDA_NO_PLUGINS=true conda install <package>

At first glance, it is raised by either powerlevel10k or conda. But this warning only appears on a project to which a virtual env is registered with this plugin. The message also appears when I call a built-in terminal with :terminal. l believe that how an interactive shell is called by this plugin, or by neovim, would be somewhat different from calling the shell from a terminal app (in my case wezterm), which would trigger the warning. Does anybody come up with an idea to clear the warning without just suppressing it?

Below is my plugin config:

return {
  'linux-cultist/venv-selector.nvim',
  dependencies = {
    'neovim/nvim-lspconfig',
    'mfussenegger/nvim-dap',
    'mfussenegger/nvim-dap-python', --optional
    { 'nvim-telescope/telescope.nvim', branch = '0.1.x', dependencies = { 'nvim-lua/plenary.nvim' } },
  },
  lazy = false,
  branch = 'regexp', -- -dev/add_miniconda', -- This is the regexp branch, use this for the new version
  config = function()
    require('venv-selector').setup { -- ()
      settings = {
        search = {
          anaconda_envs = {
            command = 'fd bin/python$ ~/miniconda3/envs --full-path --color never -E /proc', -- change path here to your anaconda envs
          },
          anaconda_base = {
            command = 'fd /python$ ~/miniconda3/bin --full-path --color never -E /proc', -- change path here to your anaconda base
          },
        },
      },
    }
  end,
  keys = {
    { '<leader>,v', '<cmd>VenvSelect<cr>' },
    { '<leader>,vc', '<cmd>VenvSelectCached<cr>' },
  },
}

and my .zshrc just in case bcoz it is referred to in the message (exports are noted on .zprofile).

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
  source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi

# 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

# 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"
alias wezterm='flatpak run org.wezfurlong.wezterm'

# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh

source ~/.files/powerlevel10k/powerlevel10k.zsh-theme

# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
# !! And should not be moved to .zprofile to have the !!
# !! toggleterm neovim plugin execute conda commands !!
__conda_setup="$('/home/atsushi/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/home/atsushi/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/home/atsushi/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/home/atsushi/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

Thanks in advance!

barikata1984 commented 1 week ago

Just an additional info. When I run echo $CONDA_PREFIX on a normal terminal, it returns my miniconda location like /home/USER/miniconda3. But when I run it on a VenvSelect-ed terminal, it returns nothing, or maybe an empty string. From this observation, the base miniconda env would not be loaded correctly. I hope this observation rings a bell inside someone's mind.

barikata1984 commented 1 week ago

Okay, after running conda config --set auto_activate_base false, the warning disappears. Obviously, it does not activate the base env automatically. It works in my use case, but I want to keep this issue open for a while because I am unsure whether this is an expected behaviour.

I see one important change you need to do however - for your own searches using miniconda, try to add type = "anaconda" as the second parameter under your command for a search. See here in the default search for anaconda venvs how it should look like:

https://github.com/linux-cultist/venv-selector.nvim/blob/regexp/lua/venv-selector/config.lua

This is how the plugin knows it should set CONDA_PREFIX and that's why it's missing for you inside a terminal launched from inside neovim.

linux-cultist commented 1 week ago

Thanks for the detailed issue!

I haven't tried the plugin with miniconda myself, but I'm a bit surprised it doesn't just work, like it does with anaconda. So the issue is because miniconda wants to activate the base environment also every time you activate another venv?

barikata1984 commented 1 week ago

@linux-cultist

In my understanding, it is more due to the conflict between the base and another env; when conda config --set auto_activate_base true, conda tries to activate the base env at the time of a shell spawned while this plugin also tries to activate another env. This simultaneous env activation would get conda or the shell unsure about which env label should be sent to the console output, which is the problematic process mentioned in the warning.

Something curious is such env activation did not cause such a warning with main branch afaik

barikata1984 commented 1 week ago

Btw, I made a fork repo to auto-load miniconda link. I can make a PR but adjusting the format is somewhat tedious. Tell me you need the PR or not and what type of lua format I should follow

linux-cultist commented 5 days ago

Would be nice with a PR of course. I can fix the formatting after it's been merged so you can focus on the code.

Or just link to whatever special things in the code you did to get it to work properly. :) I'm on vacation right now but I will look at this in a few weeks!

barikata1984 commented 4 days ago

I made a PR #152. Pls check it. I thought adding some note about conda config --set auto_activate_base true but have not been sure where I should put it. Let me know where I should put it if necessary