AckslD / swenv.nvim

Tiny plugin to quickly switch python virtual environments from within neovim without restarting.
206 stars 29 forks source link

Error in swenv.api.lua When CONDA_EXE Environment Variable Is Not Set #18

Closed Aricg closed 11 months ago

Aricg commented 1 year ago

Description:

When the CONDA_EXE environment variable is not set, the function get_conda_base() in swenv.api.lua throws an error, breaking the plugin functionality.

Environment:

OS: macOS (M1) NVIM v0.9.1

Steps to Reproduce:

Start LunarVim without having CONDA_EXE set in your environment. Trigger the swenv plugin feature. Observe the error. Actual Behavior:

The following error is displayed:

E5108: Error executing lua ...lunarvim/site/pack/lazy/opt/swenv.nvim/lua/swenv/api.lua:12: attempt to index local 'conda_exe_path' (a userdata value)
stack traceback:
        ...lunarvim/site/pack/lazy/opt/swenv.nvim/lua/swenv/api.lua:12: in function 'get_conda_base'
        ...lunarvim/site/pack/lazy/opt/swenv.nvim/lua/swenv/api.lua:55: in function 'get_venvs'
        ...lunarvim/site/pack/lazy/opt/swenv.nvim/lua/swenv/api.lua:81: in function 'pick_venv'
        [string ":lua"]:1: in main chunk

Expected Behavior:

If CONDA_EXE is not set, the plugin should either skip the conda-specific logic or fail gracefully with a clear error message.

Possible Solution:

Commenting out the conda-specific logic solves the issue.


M.get_venvs = function(venvs_path)
  local success, Path = pcall(require, 'plenary.path')
  if not success then
    vim.notify('Could not require plenary: ' .. Path, vim.log.levels.WARN)
    return
  end
  local scan_dir = require('plenary.scandir').scan_dir

  local venvs = {}

  -- CONDA
--   local conda_base_path = get_conda_base(vim.fn.getenv('CONDA_EXE'))
--   local conda_paths = scan_dir(conda_base_path .. '/envs', { depth = 1, only_dirs = true, silent = true })
-- 
--   for _, path in ipairs(conda_paths) do
--     table.insert(venvs, {
--       name = Path:new(path):make_relative(conda_base_path .. '/envs'),
--       path = path,
--       source = 'conda',
--     })
--   end

  -- VENV
  local paths = scan_dir(venvs_path, { depth = 1, only_dirs = true, silent = true })
  for _, path in ipairs(paths) do
    table.insert(venvs, {
      -- TODO how does one get the name of the file directly?
      name = Path:new(path):make_relative(venvs_path),
      path = path,
      source = 'venv',
    })
  end

  return venvs
end

It may be necessary to include a check for CONDA_EXE and bypass the conda logic if it is not set.

AckslD commented 1 year ago

@Aricg are you maybe on an old version? Should have been fixed by #9 I think

fxdgear commented 11 months ago

I just installed lunarvim today and am trying to get started with trying to understand it and I'm still getting this error too.

I'm going to make the assumption that it's going to be pulling from the main branch by default? So I should be pulling what ever fixed was applied in #9 right?

My other question is related to the fact that I use both poetry and pipenv and both of these libraries create the virtualenvs in different locations.

how can I specify the both of these?

fxdgear commented 11 months ago

Turns out I wasn't using this repo. I was instead following a blog post which used a fork! :(

AckslD commented 11 months ago

Thanks for letting me know @fxdgear. Maybe it was the same for @Aricg. Closing this but feel free to reopen.