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

Need help getting leptosfmt to work with rust-analyzer #283

Closed
Alt-iOS opened this issue Mar 10, 2024 · 14 comments
Closed

Need help getting leptosfmt to work with rust-analyzer #283

Alt-iOS opened this issue Mar 10, 2024 · 14 comments
Labels
bug Something isn't working

Comments

@Alt-iOS
Copy link

Alt-iOS commented Mar 10, 2024

Neovim version (nvim -v)

NVIM v0.9.5

Operating system/version

macos 14

Output of :checkhealth rustaceanvim

rustaceanvim: require("rustaceanvim.health").check()

Checking for Lua dependencies ~
- OK [mfussenegger/nvim-dap](https://github.com/mfussenegger/nvim-dap) installed.

Checking external dependencies ~
- OK rust-analyzer: found rust-analyzer 0.3.1868-standalone (037924c4d 2024-03-03)
- OK Cargo: found cargo 1.78.0-nightly (f772ec022 2024-03-01)
- OK rustc: found rustc 1.78.0-nightly (b6d2d841b 2024-03-05)
- OK debug adapter: found codelldb 

Checking config ~
- OK No errors found in config.

Checking for conflicting plugins ~
- OK No conflicting plugins detected.

Checking for tree-sitter parser ~
- OK tree-sitter parser for Rust detected.

How to reproduce the issue

- create a new leptos project
- select a file as projectroot
- put the following in the rust-analyzer.json
- {
  "rust-analyzer.rust.analyzerTargetDir": true,
  "rust-analyzer.rustfmt.overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"]
}


### Expected behaviour

It reformats the file back to the original state

### Actual behaviour

Nothing happens

### The minimal config used to reproduce this issue.

```json 
 {
  "rust-analyzer.rust.analyzerTargetDir": true,
  "rust-analyzer.rustfmt.overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"]
}
@Alt-iOS Alt-iOS added the bug Something isn't working label Mar 10, 2024
@Alt-iOS
Copy link
Author

Alt-iOS commented Mar 10, 2024

Sorry, followed the method with the json file but I still cant get it to work

@mrcjkb
Copy link
Owner

mrcjkb commented Mar 10, 2024

Hey 👋

  • Do you have leptosfmt installed properly and is it on your PATH? You can check this with :lua =vim.fn.executable('leptosfmt') (it should output 1) and try running :!leptosfmt --help in cmd mode.

  • Can you please try configuring this via the vim.g.rustaceanvim table in your init.lua first, so that we can rule out issues with loading the config from json?

  • Are you following the leptosfmt instructions correctly?

It says:

And you must configure rustfmt to use the correct edition, place a rustfmt.toml file in the root of your project:

edition = "2021"
# (optional) other config...

but I don't see this in your issue description.

  • If you can reproduce this with the configuration in vim.g.rustaceanvim, then I suggest asking the leptosfmt maintainers
  • If you cannot reproduce this with the configuration in vim.g.rustaceanvim, then please follow the documentation on troubleshooting with a minimal config and post the minimal config here (as was requested in the issue form - a json file is not a minimal config). Also, please provide more detailed steps to reproduce (e.g. an unformatted rust file and the expected outcome).

@mrcjkb mrcjkb changed the title .nvim.lua doesnt seem to have effect Need help getting leptosfmt to work with rust-analyzer Mar 10, 2024
@mrcjkb
Copy link
Owner

mrcjkb commented Mar 10, 2024

Also, see this comment

@Alt-iOS
Copy link
Author

Alt-iOS commented Mar 11, 2024

HI, first of all, thanks for your help, an sorry should have been more specific, I have leptosfmt working on the project, it works from terminal, but when using it to integrate it with rustaceanvim is when it fails, in vscode with this settings.json it does work. I changed the tardetDir structure, it was wrong on the first one, my bad, also I fixed it in the vimg.g.rustaceanvim but the issue still persists

settings.json

 "rust-analyzer.cargo.targetDir": true,
    "rust-analyzer.rustfmt.overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"],
    "rust-analyzer.checkOnSave": true,

about exrc and what I did got working, idk maybe it was because yesterday was really late for me but I couldnt make heads or tails of :help exrc nonetheless what I meant is I got it to load the .nvim.lua for me, this is the file:

vim.g.rustaceanvim = {
	-- Plugin configuration
	tools = {},
	-- LSP configuration
	server = {
		on_attach = function(client, bufnr)
			-- you can also put keymaps in here
		end,
		default_settings = {
			-- rust-analyzer language server configuration
			["rust-analyzer"] = {
				["cargo.targetDir"] = true,
				["rustfmt.overrideCommand"] = { "leptosfmt", "--stdin", "--rustfmt" },
				-- Additional settings appended here
				cargo = {
					allFeatures = true,
					loadOutDirsFromCheck = true,
					runBuildScripts = true,
				},
				checkOnSave = {
					allFeatures = true,
					command = "clippy",
					extraArgs = { "--no-deps" },
				},
				procMacro = {
					enable = true,
					ignored = {
						["async-trait"] = { "async_trait" },
						["napi-derive"] = { "napi" },
						["async-recursion"] = { "async_recursion" },
					},
				},
			},
		},
	},
	-- DAP configuration
	dap = {},
}

Nonetheless it still doesnt work with the formatter and the target dir doesnt change, this is also my init.lua

require("config.lazy")
vim.opt.exrc = true
vim.g.rustaceanvim = {
  -- ...
  server = {
    ---@param project_root string Path to the project root
    settings = function(project_root)
      local ra = require("rustaceanvim.config.server")
      return ra.load_rust_analyzer_settings(project_root, {
        settings_file_pattern = "rust-analyzer.json",
      })
    end,
  },
}

I am using LazyVim distro so its possible something is colliding but I cant figure it out for the life of me, again really thanks for the help

Just for reference here I mention the part of the lazyvim defualts that might be conflicting but I dont know enough to know if they would collide, append each other, sorry I am somewhat new with lua/nvim configs

@Alt-iOS
Copy link
Author

Alt-iOS commented Mar 11, 2024

Hi, just wanted to mention I also tried this version of the rust-analyzer.json, as to avoid duplication of keys in case the name of the file is taken as one

{
  "cargo.targetDir": true,
  "rustfmt.overrideCommand": ["leptosfmt", "--stdin", "--rustfmt"],
  "checkOnSave": true
}

@mrcjkb
Copy link
Owner

mrcjkb commented Mar 11, 2024

LazyVim has a lot of plugins that could potentially conflict with rustaceanvim.

I also had a look at the extras.lang.rust module and saw that it sets vim.g.rustaceanvim in its config function, which could indeed be preventing exrc from working:

https://github.com/LazyVim/LazyVim/blob/78e6405f90eeb76fdf8f1a51f9b8a81d2647a698/lua/lazyvim/plugins/extras/lang/rust.lua#L83

It should be:

config = function(_, opts)
    vim.g.rustaceanvim = vim.tbl_deep_extend("keep", vim.g.rustaceanvim or {}, opts or {})
end,

otherwise it will just overwrite whatever you set.

I don't have the capacity to support every distro and configuration that comes with loads of plugins, because every configuration is different and very difficult to reproduce reliably.
That's why I requested you try to reproduce this with plain neovim (not LazyVim) and the minimal config provided by this repository (see the last bullet point of my previous comment).
Once we have found or ruled out a rustaceanvim bug by doing that, we can move forward.

@Alt-iOS
Copy link
Author

Alt-iOS commented Mar 11, 2024

Sorry I missed the part about the minimal config, I am trying to get the minimal config tu run but sadly I get an error saying that module lazy has not been found,

  1. Copy the minimal.lua file
  2. run the command in the readme
mkdir -p /tmp/minimal/
NVIM_DATA_MINIMAL="/tmp/minimal" NVIM_APP_NAME="nvim-minimal" nvim -u NORC -u minimal.lua

heres the full error

rror detected while processing /Users/victor/minimal.lua:
E5113: Error while calling lua chunk: /Users/victor/minimal.lua:38: module 'lazy' not found:
        no field package.preload['lazy']
        no file './lazy.lua'
        no file '/opt/homebrew/share/luajit-2.1/lazy.lua'
        no file '/usr/local/share/lua/5.1/lazy.lua'
        no file '/usr/local/share/lua/5.1/lazy/init.lua'
        no file '/opt/homebrew/share/lua/5.1/lazy.lua'
        no file '/opt/homebrew/share/lua/5.1/lazy/init.lua'
        no file './lazy.so'
        no file '/usr/local/lib/lua/5.1/lazy.so'
        no file '/opt/homebrew/lib/lua/5.1/lazy.so'
        no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
        [C]: in function 'require'
        /Users/victor/minimal.lua:38: in main chunk

Am I missing a step? 😅

@mrcjkb
Copy link
Owner

mrcjkb commented Mar 11, 2024

NVIM_DATA_MINIMAL="/tmp/minimal" NVIM_APP_NAME="nvim-minimal" nvim -u NORC -u minimal.lua

🤔 that's very strange.
I just tried it out again and it worked fine (but I'm on Linux).
It looks like bootstrapping lazy.nvim didn't work for you. It should clone it into the /tmp/minimal directory.

@Alt-iOS
Copy link
Author

Alt-iOS commented Mar 11, 2024

I am on mac (so thats probably why its causing problems) and it doesnt seem to work, how could I go about it and install it manually? (Heres a screen of /tmp/minimal/lazy)
Screenshot 2024-03-11 at 12 12 38 PM

@mrcjkb
Copy link
Owner

mrcjkb commented Mar 11, 2024

I am on mac (so thats probably why its causing problems) and it doesnt seem to work, how could I go about it and install it manually? (Heres a screen of /tmp/minimal/lazy) Screenshot 2024-03-11 at 12 12 38 PM

I suppose you could try manually cloning lazy.nvim into /tmp/minimal

@mrcjkb
Copy link
Owner

mrcjkb commented Mar 12, 2024

This PR may fix the exrc issue for LazyVim: LazyVim/LazyVim#2720.

@Alt-iOS
Copy link
Author

Alt-iOS commented Mar 13, 2024

Hey man, thanks a lot!

@mrcjkb
Copy link
Owner

mrcjkb commented Apr 1, 2024

@Alt-iOS were you able to solve your problem?

@Alt-iOS
Copy link
Author

Alt-iOS commented Apr 1, 2024

Hiiii, thanks yeah, with the PR you so kindly submitted to LazyVim, the rust-analyzer.json worked again

@mrcjkb mrcjkb closed this as completed Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants