linux-cultist / venv-selector.nvim

Allows selection of python virtual environment from within neovim
MIT License
388 stars 41 forks source link

Why I don't see venv paths? #48

Closed syavabrazzzers closed 10 months ago

syavabrazzzers commented 11 months ago
image image image

This happens when i open a python file after any other?

This happens even when search options are enabled

linux-cultist commented 11 months ago

Hmm I hope its not related to the last commit where we made the Telegram viewer refresh itself in a different way...

I will see if I can reproduce this. You just open two python files after eachother? Do you run VenvSelect after each one?

syavabrazzzers commented 11 months ago

Hmm I hope its not related to the last commit where we made the Telegram viewer refresh itself in a different way...

I will see if I can reproduce this. You just open two python files after eachother? Do you run VenvSelect after each one?

first I just open some nvim configuration file, then I open the python file through the telescope

linux-cultist commented 11 months ago

Whats the output of the ":pwd" command if you run it in neovim when you dont get results?

syavabrazzzers commented 11 months ago

Whats the output of the ":pwd" command if you run it in neovim when you dont get results?

when python file opened after nvim config

image

python file has been open first

image

and I see venvs

image
linux-cultist commented 11 months ago

Yeah I think this is whats happening - its still looking in your pwd directory from the first file, goes up 2 parent folders, and finds nothing. When you open the second file, the pwd directory hasnt changed and its still looking in that one.

But i think my intention was that it would look at the currently opened file, and go 2 parent folders up from there, and search in all the directories from there.

I will have a look and see if this is a bug. I havent noticed this but thats because i run another plugin to set the pwd to the project folder. But that shouldnt be required.

Will look at this tonight! In the mean time, you can open neovim from your project folder or just change directory inside neovim, but thats annoying of course. :)

Thanks for reporting this!

syavabrazzzers commented 11 months ago

Yeah I think this is whats happening - its still looking in your pwd directory from the first file, goes up 2 parent folders, and finds nothing. When you open the second file, the pwd directory hasnt changed.

But i think my intention was that it would look at the currently opened file, and go 2 parent folders up from there, and search downwards.

I will have a look and see if this is a bug. I havent noticed this but thats because i run another plugin to set the pwd to the project folder. But that shouldnt be required.

Will look at this tonight! In the mean time, you can open neovim from your project folder or just change directory inside neovim, but thats annoying of course. :)

Okay, I will be waiting! :)

linux-cultist commented 11 months ago

You can see the exact commands its running if you are curious - just set enable_debug_output to true in the VenvSelect setup function, and you get messages (type :messages in nvim) showing the fd search parameters etc.

linux-cultist commented 11 months ago

I looked at this a bit and I cant reproduce the bug here. I open neovim, and I navigate to some file and run VenvSelect, and it correctly searches for venvs in the parent directory 2 steps up (and all folders below it).

If I then change directory to another location with "cd" inside neovim, open a file there, and runs VenvSelect there, it again correctly does the same - searches for venvs 2 steps up and all folders below it.

I can switch back and forth between these two buffers, and for each one, VenvSelect shows environments relative to the file in the buffer.

I cant reproduce what you get - you open a second file and there is no VenvSelect showing up? Can you send me the debug printout when you do these things? They should give some clues.

I did find one thing I dont like though - when there is no file in the current buffer, neovim goes to the home directory, then goes two steps up, and starts a search in the root of the file system. This takes forever on most computers, and VenvSelect keeps on running that search until it finishes, and wont show any telegram dialog boxes until its done.

I would like to add a timeout here in the future. Users probably never intend to search their entire computer.

linux-cultist commented 11 months ago

If you find the time to send me some debug output, I would love it, specially since i cant seem to reproduce this.. :/

linux-cultist commented 10 months ago

Im closing this but if you have the time later to send some debug output and some description of what you do, feel free to reopen. :)

mehalter commented 10 months ago

I have also found that with the latest commits I cannot load a single environment. Result from enabling debug output when simply running :VenvSelect in a folder that has a single folder with an environment named venv.

I did a bisect, that this is the first bad commit: https://github.com/linux-cultist/venv-selector.nvim/commit/af5a452ee89dc979ef6ee5704451a72580ac3847

Logs:

VenvSelect: 
{
  anaconda_base_path = vim.NIL,
  anaconda_envs_path = "/home/micah/.conda",
  auto_refresh = false,
  cache_dir = "/home/micah/.cache/venv-selector/",
  cache_file = "/home/micah/.cache/venv-selector/venvs.json",
  changed_venv_hooks = { <function 1>, <function 2>, <function 3> },
  dap_enabled = false,
  enable_debug_output = true,
  hatch_path = "~/.local/share/hatch/env/virtual",
  name = { "env", ".env", "venv", ".venv" },
  notify_user_on_activate = false,
  parents = 0,
  pipenv_path = "~/.local/share/virtualenvs",
  poetry_path = "~/.cache/pypoetry/virtualenvs",
  pyenv_path = "~/.pyenv/versions",
  search = true,
  search_venv_managers = true,
  search_workspace = true,
  venvwrapper_path = "/home/micah/.virtualenvs"
}
VenvSelect: Setting fd_binary_name to 'fd' since it was found on system.
VenvSelect: Telescope path: /home/micah/welp
VenvSelect: Looking for parent venvs in '/home/micah/welp' using the following parameters:
VenvSelect: 
{ "--absolute-path", "--color", "never", "-E", vim.NIL, "-E", "/home/micah/.conda", "-E", "/proc", "-HItd", "(^env$|^.env$|^venv$|^.venv$)", "/home/micah/welp" }
E5108: Error executing lua: .../nvim/lazy/venv-selector.nvim/lua/venv-selector/venv.lua:188: attempt to index local 'selected_venv' (a nil value)
stack traceback:
    .../nvim/lazy/venv-selector.nvim/lua/venv-selector/venv.lua:188: in function 'activate_venv'
    ...azy/venv-selector.nvim/lua/venv-selector/mytelescope.lua:157: in function 'key_func'
    ...hare/nvim/lazy/telescope.nvim/lua/telescope/mappings.lua:257: in function <...hare/nvim/lazy/telescope.nvim/lua/telescope/mappings.lua:256>
E5108: Error executing lua: .../nvim/lazy/venv-selector.nvim/lua/venv-selector/venv.lua:188: attempt to index local 'selected_venv' (a nil value)
stack traceback:
    .../nvim/lazy/venv-selector.nvim/lua/venv-selector/venv.lua:188: in function 'activate_venv'
    ...azy/venv-selector.nvim/lua/venv-selector/mytelescope.lua:157: in function 'key_func'
    ...hare/nvim/lazy/telescope.nvim/lua/telescope/mappings.lua:257: in function <...hare/nvim/lazy/telescope.nvim/lua/telescope/mappings.lua:256>
VenvSelect: No workspace folders found
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: There are 0 results to show:
linux-cultist commented 10 months ago

That commit changed how Conda works a little bit and VenvSelect now requires two environment variables for Conda environments - one for the base environment and one for the others venvs.

I can see that you get Anaconda base path = vim.nil in the debug output there also.

But it shouldn't error out like this no matter what config you have. Let me try and replicate this tonight. I will just create a folder, add a venv inside and then open vim when standing inside that folder. That's what you are doing right?

mehalter commented 10 months ago

Yeah that's what I'm doing! Also I do not use conda at all so that makes sense that environment variables wouldn't exist on my machine

mehalter commented 10 months ago

@linux-cultist I just did a test by installing conda and it seems to be happy now when I set up the base_path and envs_path. When I enable debugging I still do get the same two errors but my venv start showing up as well as the conda environments

linux-cultist commented 10 months ago

Ok thats good.

I will see if i can figure out what the errors are from. They sound like they would occur if you dont pick something in the telescope picker (because there are no results) and you press enter? Or do they always show up even if you dont press enter?

linux-cultist commented 10 months ago

I made a small change which i think may fix that error message you get, and instead just print: "No virtual env selected in telescope."

mehalter commented 10 months ago

What happens if there actually are virtual environments and it's not displaying them?

mehalter commented 10 months ago

Ah actually, the changes you made fixed the issue it appears and my virtual environments are correctly displaying now even without conda installed!

EDIT: This is incorrect, I removed my conda installation and it does not correctly find venv environments @linux-cultist

mehalter commented 10 months ago

Here are updated logs. It looks like currently if you do not have anaconda set up and base paths then it just doesn't work with anything else.

VenvSelect: 
{
  anaconda_base_path = "",
  anaconda_envs_path = "/home/micah/.conda/envs",
  auto_refresh = false,
  cache_dir = "/home/micah/.cache/venv-selector/",
  cache_file = "/home/micah/.cache/venv-selector/venvs.json",
  changed_venv_hooks = { <function 1>, <function 2>, <function 3> },
  dap_enabled = false,
  enable_debug_output = true,
  hatch_path = "~/.local/share/hatch/env/virtual",
  name = { "env", ".env", "venv", ".venv" },
  notify_user_on_activate = false,
  parents = 0,
  pipenv_path = "~/.local/share/virtualenvs",
  poetry_path = "~/.cache/pypoetry/virtualenvs",
  pyenv_path = "~/.pyenv/versions",
  search = true,
  search_venv_managers = true,
  search_workspace = true,
  venvwrapper_path = "~/.virtualenvs"
}
VenvSelect: Setting fd_binary_name to 'fd' since it was found on system.
VenvSelect: Telescope path: /home/micah/welp
VenvSelect: Looking for parent venvs in '/home/micah/welp' using the following parameters:
VenvSelect: 
{ "--absolute-path", "--color", "never", "-E", "", "-E", "/home/micah/.conda/envs", "-E", "/proc", "-HItd", "(^env$|^.env$|^venv$|^.venv$)", "/home/micah/welp" }
VenvSelect: No virtual env selected in telescope.
VenvSelect: No workspace folders found
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: There are 0 results to show:
Error executing vim.schedule lua callback: ...azy/venv-selector.nvim/lua/venv-selector/mytelescope.lua:79: attempt to index local 'picker' (a nil value)
stack traceback:
    ...azy/venv-selector.nvim/lua/venv-selector/mytelescope.lua:79: in function 'show_results'
    .../nvim/lazy/venv-selector.nvim/lua/venv-selector/venv.lua:59: in function 'find_other_venvs'
    .../nvim/lazy/venv-selector.nvim/lua/venv-selector/venv.lua:154: in function ''
    vim/_editor.lua: in function <vim/_editor.lua:0>

For reference, here is ls output:

13:36:57 ~/welp 1m 27s
➤ ls
venv
mehalter commented 10 months ago

It has to do with -E being added in there with an empty string (or nil) when there is no conda base path provided

13:38:30 ~/welp
➤ fd --absolute-path --color never -E /home/micah/.conda/envs -E /proc -HItd "(^env$|^.env$|^venv$|^.venv$)" .
/home/micah/welp/venv/

13:38:35 ~/welp
➤ fd --absolute-path --color never -E "" -E /home/micah/.conda/envs -E /proc -HItd "(^env$|^.env$|^venv$|^.venv$)" .
linux-cultist commented 10 months ago

Ok great, will fix tomorrow :)

linux-cultist commented 10 months ago

Made a quick attempt at fixing this tonight. See if the latest commit fixes the issue for you.

Will continue tomorrow otherwise with this. Dont like errors. :)

mehalter commented 10 months ago

No error messages but also not fixed sadly. It seems the first time I run it it doesn't work and then magically works the second time:

First run:

VenvSelect: 
{
  anaconda_base_path = "",
  anaconda_envs_path = "/home/micah/.conda/envs",
  auto_refresh = false,
  cache_dir = "/home/micah/.cache/venv-selector/",
  cache_file = "/home/micah/.cache/venv-selector/venvs.json",
  changed_venv_hooks = { <function 1>, <function 2>, <function 3> },
  dap_enabled = false,
  enable_debug_output = true,
  hatch_path = "~/.local/share/hatch/env/virtual",
  name = { "env", ".env", "venv", ".venv" },
  notify_user_on_activate = false,
  parents = 0,
  pipenv_path = "~/.local/share/virtualenvs",
  poetry_path = "~/.cache/pypoetry/virtualenvs",
  pyenv_path = "~/.pyenv/versions",
  search = true,
  search_venv_managers = true,
  search_workspace = true,
  venvwrapper_path = "~/.virtualenvs"
}
VenvSelect: Setting fd_binary_name to 'fd' since it was found on system.
VenvSelect: Telescope path: /home/micah/welp
VenvSelect: Looking for parent venvs in '/home/micah/welp' using the following parameters:
VenvSelect: 
{ "--absolute-path", "--color", "never", "-E", "/proc", "-HItd", "(^env$|^.env$|^venv$|^.venv$)", "/home/micah/welp" }
VenvSelect: No virtual env selected in telescope.

Second run:

VenvSelect: 
{
  anaconda_base_path = "",
  anaconda_envs_path = "/home/micah/.conda/envs",
  auto_refresh = false,
  cache_dir = "/home/micah/.cache/venv-selector/",
  cache_file = "/home/micah/.cache/venv-selector/venvs.json",
  changed_venv_hooks = { <function 1>, <function 2>, <function 3> },
  dap_enabled = false,
  enable_debug_output = true,
  hatch_path = "~/.local/share/hatch/env/virtual",
  name = { "env", ".env", "venv", ".venv" },
  notify_user_on_activate = false,
  parents = 0,
  pipenv_path = "~/.local/share/virtualenvs",
  poetry_path = "~/.cache/pypoetry/virtualenvs",
  pyenv_path = "~/.pyenv/versions",
  search = true,
  search_venv_managers = true,
  search_workspace = true,
  venvwrapper_path = "~/.virtualenvs"
}
VenvSelect: Setting fd_binary_name to 'fd' since it was found on system.
VenvSelect: Telescope path: /home/micah/welp
VenvSelect: Looking for parent venvs in '/home/micah/welp' using the following parameters:
VenvSelect: 
{ "--absolute-path", "--color", "never", "-E", "/proc", "-HItd", "(^env$|^.env$|^venv$|^.venv$)", "/home/micah/welp" }
VenvSelect: No virtual env selected in telescope.
VenvSelect: Found venv in parent search: /home/micah/welp/venv/
VenvSelect: No workspace folders found
VenvSelect: Found no workspaces to search for venvs.
VenvSelect: Found no venv manager directories to search for venvs.
VenvSelect: There are 1 results to show:
linux-cultist commented 10 months ago

Ok then I will take a proper look tomorrow at this. Thank you for the detailed debug info. :)

linux-cultist commented 10 months ago

Ive been trying to understand why this is happening.

Do you use LazyVim by any chance? I do right now, and it uses a vim.notify plugin to show nice notifications. For me, this causes VenvSelector to close the telegram picker the first time its run (when VenvSelect is run with debug_output_enabled = true), but not on future runs.

But I still get results. My log shows the same results every time I run VenvSelect, including the first time.

I cant reproduce this still. Can you try and see if you get this bug also when debug_output_enabled = false ?

mehalter commented 10 months ago

Ah yes it does acutally work as intended without the debugging output! My venv is showing up correctly even without conda things setup or installed. Thanks!

mehalter commented 10 months ago

Also I do not use LazyVim, but I do use nvim-notify

linux-cultist commented 10 months ago

Ok very good that it works now anyway. :) Such an interesting bug... It's related to 'nvim-notify' in some way but I can't figure it out. Maybe I change how debug output is produced in the future since it needs to be reliable.

Thank you for being so helpful and enjoy your evening. :)