Skip to content

Astral ty support #224

@kuator

Description

@kuator

With current configuration, ty doesn't work out of the box, I guess a new hook needs to be added, but I'm not sure.

Minimal reproducible config:
initi.lua content:

-- Run this file as `nvim --clean -u minimal.lua`

for name, url in pairs({
    -- ADD PLUGINS _NECESSARY_ TO REPRODUCE THE ISSUE, e.g:
    "https://github.com/neovim/nvim-lspconfig",
    "https://github.com/linux-cultist/venv-selector.nvim",
}) do
    local install_path = vim.fn.fnamemodify("nvim_issue/" .. name, ":p")
    if vim.fn.isdirectory(install_path) == 0 then
        vim.fn.system({ "git", "clone", "--depth=1", url, install_path })
    end
    vim.opt.runtimepath:append(install_path)
end

require("venv-selector").setup({
    search = {
        tmp_venvs = {
            command = "fd /bin/python$ /tmp/venvs/ --full-path",
        },
    },
})

vim.lsp.enable("ty")

-- ADD INIT.LUA SETTINGS _NECESSARY_ FOR REPRODUCING THE ISSUE
local lspattach = vim.api.nvim_create_augroup("LspAttach", { clear = true })
vim.api.nvim_create_autocmd({ "LspAttach" }, {
    group = lspattach,
    callback = function(ev)
        local options = { noremap = true, silent = true }
        local client = vim.lsp.get_client_by_id(ev.data.client_id)
        local bufnr = ev.buf

        vim.keymap.set("n", "gd", vim.lsp.buf.definition, options)
        vim.bo[bufnr].omnifunc = "v:lua.vim.lsp.omnifunc"
    end,
})

Steps

$ pipx install uv
$ pipx install ty
$ uv init test_django_project
Using CPython 3.12.11
Creating virtual environment at: /tmp/tmp_venv
Activate with: source /tmp/tmp_venv/bin/activate
$ cd test_django_project
$ uv venv /tmp/venvs/tmp_venv
$ source /tmp/venvs/tmp_venv/bin/activate
$ uv add --active django
$ echo $VIRTUAL_ENV
/tmp/venvs/tmp_venv
$ django-admin startproject main .
$ django-admin startapp articles
$ deactivate
$ nvim --clean -u init.lua articles/views.py

Now I choose the tmp-venv I created and go-to-definition doesn't work and server is complaining that it can't resolve the import:

Image

Now, if you activate the environment before running nvim:
$ source /tmp/venvs/tmp_venv/bin/activate
$ rm ~/.cache/venv-selector/venvs2.json
$ nvim --clean -u init.lua articles/views.py

Now it works even without venv-selector, I try to do go-to-definition on the render function and it works
Image

Astral ty documentation says that it respects the VIRTUAL_ENV variable when looking for third party modules but it doesn't seem to be the case when using venv-selector. Maybe venv-selector sets VIRTUAL_ENV after the lsp get run, I'm not sure.
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions