linux-cultist / venv-selector.nvim

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

VenvSelectCached Command is not working #75

Closed mohit2152sharma closed 6 months ago

mohit2152sharma commented 8 months ago

So I am using miniconda for my virtual environments and I have the plugin set up as follows:

{
    "linux-cultist/venv-selector.nvim",
    opts = {
      search = false,
      anaconda_envs_path = os.getenv "HOME" .. "/miniconda3/envs",
      auto_refresh = true,
      -- search_venv_managers = false,
      search_workspace = false,
    },
    keys = { { "<leader>lv", "<cmd>:VenvSelect<cr>", desc = "Select VirtualEnv" } },
  },

I cd into my python project and select a virtual environment. But when I come back again to that directory, and run the command VenvSelectCached it throws the error, Not an editor command. However, if I select the virtual environment and then run VenvSelectCached it works fine.

Not sure if I am doing everything correct. I don't know know much lua, but when I was browsing through the functions, (this one), it seems to like, we need to pass cache_file in the opts?

linux-cultist commented 8 months ago

Ok thank you. Sorry for late response, it's been busy over here lately. I will have a look at this one as soon as possible. :)

linux-cultist commented 7 months ago

Sorry for the delays on this one. I managed to catch a very bad cold for the last week here, so haven't been at my computer for almost a week now. Hopefully soon I can have a look at this and figure out what's going on. I didn't write the code for VenvselectCached myself, it was a PR by another user a long time ago, so I have to look into it a bit and see what could be the issue. :)

mohit2152sharma commented 7 months ago

Hey, no issues, please take care of your health. It's not blocking anything for me, so it's okay if takes time. No need to rush, at the cost of your health.

mennohofste commented 6 months ago

Are you running the VenvSelectCached command directly?

If so, this happens because lazy.nvim loads the plugin lazily, so the commands are not loaded on startup. You can only run the VenvSelectCached command only after the plugin is loaded.

I can come up with 3 solutions:

  1. bind the command to a key similar to your VenvSelect. Lazy loads the plugin when you press the key
  2. run the lua command lua require("venv-selector").retrieve_from_cache(), the require essential loads the plugin
  3. add lazy = false to your config to avoid lazy loading the pluging (https://github.com/folke/lazy.nvim#-plugin-spec)

There are probably more solutions. I imagine adding an event trigger to your config could also work, though I could not get it to work for me immediately.

Hope this helps!

voider1 commented 6 months ago

@mennohofste For me both of the commands are not working, I tried all your suggestions, but to no avail. Any other suggestions?

mohit2152sharma commented 6 months ago

@mennohofste , thanks a lot. The issue was exactly, what you explained. I went ahead with option 3 for the time being, but as I am reading more about lua, I feel like there should be a way to automatically load the virtual environment, when you cd into a project directory (if it's available in cache).. I was able to do that by modifying the config function, now whenever I cd into a known directory, it automatically loads the virtual environment. I am using the following setup.


{
    "linux-cultist/venv-selector.nvim",
    lazy = false,
    config = function()
      local venv = require "venv-selector"
      venv.setup {
        search = false,
        anaconda_envs_path = os.getenv "HOME" .. "/miniconda3/envs",
        auto_refresh = true,
        search_workspace = false,
      }
      venv.retrieve_from_cache()
    end,
    keys = {
      { "<leader>lv", "<cmd>:VenvSelect<cr>", desc = "Select VirtualEnv" },
      { "<leader>lc", "<cmd>:VenvSelectCached<cr>", desc = "Select Cached VirtualEnv" },
    },
  }

@voider1 , it may be because of how you are setting up the plugin (as it was for me), could you share, more details about how you are setting it up?

@voider1 , I am closing this issue, please open a separate issue for your use case

linux-cultist commented 6 months ago

Amazing to see this closed, thanks a lot @mennohofste and @mohit2152sharma for figuring out what the issue was. :)