AckslD / swenv.nvim

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

auto_venv crashes - attempt to index local 'content' (a nil value) #39

Closed b0lle closed 4 months ago

b0lle commented 5 months ago

Hey,

I want to give your plugin a try, because it seems to smoothen my workflow. Thanks for your contribution!

However, I was not able to use the auto_venv command. It raises an exception, when calling require('swenv.api').auto_venv():

E5108: Error executing lua .../.local/share/nvim/lazy/swenv.nvim/lua/swenv/project.lua:11: attempt to index local 'content' (a nil value)
stack traceback:
        .../.local/share/nvim/lazy/swenv.nvim/lua/swenv/project.lua:11: in function 'read_venv_name'
        ...nzel/.local/share/nvim/lazy/swenv.nvim/lua/swenv/api.lua:196: in function 'auto_venv'
        [string ":lua"]:1: in main chunk

The venv I created lives in the .venv folder on the root folder of the project, created by poetry (I also tried to create one with python -m venv .venv). I am on nvim v0.9.5 and installed the plugin two days ago:

    ● swenv.nvim 58.25ms  start
        dir    /Users/xxx/.local/share/nvim/lazy/swenv.nvim
        url    https://github.com/AckslD/swenv.nvim
        branch main
        commit c11eeaa
        readme README.md
        help   |swenv.txt|

project_nvim is installed and declared as reference:

return {
  'AckslD/swenv.nvim',
  dependencies = { 'ahmedkhalf/project.nvim' },
}
  ● project.nvim 15.3ms  swenv.nvim
        dir    /Users/xxx/.local/share/nvim/lazy/project.nvim
        url    https://github.com/ahmedkhalf/project.nvim
        branch main
        commit 8c6bad7
        readme README.md
        keys    <leader>sp 

I am on macos sonoma v14.0. Do you have any ideas whats wrong with the setup?

AckslD commented 5 months ago

Could you confirm what require("project_nvim.project").get_project_root() gives? And if .venv is in that path?

AckslD commented 5 months ago

In any case I think it would be good to not error if .venv is missing, so opened #40

b0lle commented 5 months ago

require("project_nvim.project").get_project_root() actually returns nothing although the venv is in the root folder :/ I guess I need to have a look at project_nvim settings ...

b0lle commented 4 months ago

Ok I digged a little deeper ... The project_nvim dependency workes fine, i just missed the print() statement around the get_project_root statement. Now it returns:

$ lua print(require('project_nvim.project').get_project_root())
> '/Users/hi-there/Code/my-project'

With this in mind I try to call the read_venv_name() function directly, as it happens in the auto_venv function here

$ lua print(require('swenv.project').read_venv_name('/Users/hi-there/Code/my-project'))
> E5108: Error executing lua .../.local/share/nvim/lazy/swenv.nvim/lua/swenv/project.lua:11: attempt to index local 'content' (a nil value)
stack traceback:
        .../.local/share/nvim/lazy/swenv.nvim/lua/swenv/project.lua:11: in function 'read_venv_name'
        [string ":lua"]:1: in main chun

So, my understanding is that read_venv_name found my .venv folder and tries to read it, but the content is somehow a nil value ... So why? I did a little test in lua on my machine:

> file = io.open('/Users/hi-there/Code/my-project/.venv', 'r')
> print(file)
file (0x1e6771ba0)
> if not file then
print("not file")
end
> content = file:read('*a')
> print(content)
nil

In the lua docs it says, that io.open() can only read files, but .venv is a directory.

May this be related to the fact that I use pyenv to handle my virtual environments? The .venv folder looks like

.rw-r--r--  40 hi-there 30 Apr 16:07 .gitignore
drwxr-xr-x   - hi-there 30 Apr 16:07 bin
drwxr-xr-x   - hi-there 30 Apr 16:07 lib
.rw-r--r-- 368 hi-there 30 Apr 16:07 pyvenv.cfg
b0lle commented 4 months ago

Well, if its a pyenv "issue" than the plugin could get its name from the pyvenv.cfg:

> file = io.open('/Users/hi-there/Code/testcontainer/.venv/pyvenv.cfg')
> file:read('*a')
home = /Users/hi-there/.pyenv/versions/3.10.11/bin
implementation = CPython
version_info = 3.10.11.final.0
virtualenv = 20.26.0
include-system-site-packages = false
base-prefix = /Users/hi-there/.pyenv/versions/3.10.11
base-exec-prefix = /Users/hi-there/.pyenv/versions/3.10.11
base-executable = /Users/hi-there/.pyenv/versions/3.10.11/bin/python3.10
prompt = my-package-py3.10
b0lle commented 4 months ago

I created https://github.com/AckslD/swenv.nvim/pull/41 which solves my issue and may be related to #32

AckslD commented 4 months ago

@b0lle, can this be closed?