Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not working Goto Definition hotkey with project with yarn pnp #177

Closed
utrumo opened this issue Jun 23, 2024 · 15 comments · Fixed by #179
Closed

Not working Goto Definition hotkey with project with yarn pnp #177

utrumo opened this issue Jun 23, 2024 · 15 comments · Fixed by #179

Comments

@utrumo
Copy link

utrumo commented Jun 23, 2024

Here is another problem related to yarn plug and play with the same conditions as here #169

I get an error when calling the go to definition command:

Error executing vim.schedule lua callback: /usr/share/nvim/runtime/lua/vim/lsp/util.lua:132: index out of range
stack traceback:
	[C]: in function 'str_utfindex'
	/usr/share/nvim/runtime/lua/vim/lsp/util.lua:132: in function '_str_utfindex_enc'
	...nvim/lazy/telescope.nvim/lua/telescope/builtin/__lsp.lua:108: in function 'item_to_location'
	...nvim/lazy/telescope.nvim/lua/telescope/builtin/__lsp.lua:207: in function 'handler'
	/usr/share/nvim/runtime/lua/vim/lsp/client.lua:687: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>

If I add to .yarnrc.yml:
nodeLinker: node-modules

and run yarn install
packages are installed in the node_module - go to definition command works well.
In vscode, in both cases, everything works without errors

image

My config:
https://github.com/utrumo/myConf/blob/master/nvim/lua/plugins/typescript.lua
https://www.lazyvim.org/extras/editor/telescope
https://www.lazyvim.org/extras/lang/typescript

@yioneko
Copy link
Owner

yioneko commented Jun 24, 2024

I made a PR to partially resolve the issue. This means that some additional adjustments needs to be done on nvim side. Though the mutation of zipfile uri could be done on server side before sending to nvim, but I'd like not to implement that since keeping compatible with specific client's non-standard behavior is a bad practice from my view. So the current solution is not ideal and Iet me know what do you think about it.

@utrumo
Copy link
Author

utrumo commented Jun 25, 2024

@yioneko, please tell me how I can try to run your edits on my computer?
I immediately could not figure out how to install vtsls from your branch via mason.nvim

The yarn documentation advises using the vim-rzip plugin. Could that be the problem?
https://yarnpkg.com/getting-started/editor-sdks

@yioneko
Copy link
Owner

yioneko commented Jun 25, 2024

I just published a new verision 0.2.5-alpha.0 to npm and you can directly use that for testing.

Regarding vim-rzip: I think no, as that plugin also uses vim builtin zip plugin under the hood.

@utrumo
Copy link
Author

utrumo commented Jun 26, 2024

I can't make go to definition work.

I updated vtsls

MasonInstall [email protected]

added autocmd, which is described in your pull request,
I tried in my config in lazy.lua to comment on the lines "gzip" and "zipPlugin" in the disabled_plugins section,
as well as disable vim-rzip plugin.

I still get an error:

Error executing vim.schedule lua callback: /usr/share/nvim/runtime/lua/vim/lsp/util.lua:1801: index out of range
stack traceback:
	[C]: in function '_str_byteindex_enc'
	/usr/share/nvim/runtime/lua/vim/lsp/util.lua:1801: in function 'locations_to_items'
	...nvim/lazy/telescope.nvim/lua/telescope/builtin/__lsp.lua:197: in function 'handler'
	/usr/share/nvim/runtime/lua/vim/lsp/client.lua:687: in function ''
	vim/_editor.lua: in function <vim/_editor.lua:0>

Can you help me figure out what I'm doing wrong?

@yioneko
Copy link
Owner

yioneko commented Jun 26, 2024

Can you verify the zip plugin is actually working? If you start editing a .zip archive in nvim, it will list the file contained in the zip.

@utrumo
Copy link
Author

utrumo commented Jun 26, 2024

Yes, I open zip archives inside the same .yarn/cache and their contents are available in nvim without the new autocmd command and with it, no difference. Only go to definition does not work

@yioneko
Copy link
Owner

yioneko commented Jun 26, 2024

What's your lsp config for it? Maybe you didn't set init_options.hostInfo = "neovim". It is required to inform server to send zipfile: uri insted of zip: (it is for vscode).

@utrumo
Copy link
Author

utrumo commented Jun 26, 2024

Exactly! I did not set this option. Can you tell me how to set it up?

@yioneko
Copy link
Owner

yioneko commented Jun 26, 2024

It depends on what you use for lsp config, but generally it is as simple as setting init_options is your setup call:

require("lspconfig").vtsls.setup({
  init_options = {
    hostInfo = "neovim",
  },
  settings = ...
})

If you still cannot figure out how to do that, you can provide your lsp setup for it.

@utrumo
Copy link
Author

utrumo commented Jun 26, 2024

It worked! Go to definition earned.
True, previewing the contents of the file does not work.
The presence of an autocommand does not affect it in any way.

Here is my minimum config that turned out:

return {
  "neovim/nvim-lspconfig",
  opts = {
    servers = {
      vtsls = {
        init_options = { hostInfo = "neovim" },
        settings = {
          typescript = {
            tsdk = "./.yarn/sdks/typescript/lib",
          },
        },
      },
    },
  },
}

And here's the full one, where I turned off automatic formatting to avoid conflicts with prettier. Can you tell me if I did it correctly?

return {
  {
    "neovim/nvim-lspconfig",
    opts = {
      inlay_hints = { enabled = false },
      servers = {
        vtsls = {
          init_options = { hostInfo = "neovim" },
          settings = {
            typescript = {
              tsdk = "./.yarn/sdks/typescript/lib",
            },
          },
        },
      },
      setup = {
        vtsls = function(_, _opts)
          require("lazyvim.util").lsp.on_attach(function(client)
            if client.name == "vtsls" then
              client.server_capabilities.documentFormattingProvider = false
            end
          end)
        end,
      },
    },
  },
  {
    "stevearc/conform.nvim",
    ---@class ConformOpts
    opts = {
      formatters = {
        prettier = {
          command = function(self, bufnr)
            local util = require("conform.util")
            local cmd = util.find_executable({ ".yarn/sdks/prettier/bin/prettier.cjs" }, "")(self, bufnr)

            if cmd ~= "" then
              return cmd
            end

            -- return type of util.from_node_modules is fun(self: conform.FormatterConfig, ctx: conform.Context): string
            ---@diagnostic disable-next-line
            return util.from_node_modules("prettier")(self, bufnr)
          end,
        },
      },
    },
  },
}

Is there any other way to preview the files, or is this a problem with a completely different plugin?
image

@yioneko
Copy link
Owner

yioneko commented Jun 26, 2024

Yes, the setup is correct. It looks like you are using Telescope, which I tested to have the same problem. I didn't dive deep into it as I tried fzf-lua where the preview works fine, so I think this is the problem on Telescope side.

@utrumo
Copy link
Author

utrumo commented Jun 30, 2024

Thank you. The inclusion of editor.fzf in LazyExtra solved the problem. Plugin
telescope was replaced by fzf-lua and preview earned.

No additional auto-command was needed:

vim.api.nvim_create_autocmd("BufReadCmd", {
  pattern = "zipfile:/[^/]*",
  callback = function(args)
    -- the uri is like zipfile:/a/b/c
    local uri = args.match --[[@as string]]
    -- transform it to be zipfile:///a/b/c
    vim.fn["zip#Read"](uri:gsub("^zipfile:", "zipfile://"), 1)
    local printInspect = function(value)
      print(vim.inspect(value))
    end
    printInspect(uri)
  end,
})

go to definition works even without it on 0.2.5-alpha.0

@utrumo utrumo closed this as completed Jun 30, 2024
@yioneko
Copy link
Owner

yioneko commented Jul 1, 2024

I did some trick to make the zipfile:/// returned in some cases, so the snippet is not always needed if your usage is within these cases. For other cases like diagnostics and code actions on the zip file, the snippet could come to save.

@utrumo
Copy link
Author

utrumo commented Jul 2, 2024

In what cases is the auto command exactly needed, how can I check that it works?

@yioneko
Copy link
Owner

yioneko commented Jul 2, 2024

Never mind, I created a new test package and checked it again and now everything works as expected without the autocmd. Maybe I did something wrong before. Thanks for your support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants