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

Autocompletion wants me to build the project #123

Open
mrphil2105 opened this issue Apr 17, 2024 · 19 comments
Open

Autocompletion wants me to build the project #123

mrphil2105 opened this issue Apr 17, 2024 · 19 comments
Labels
bug Something isn't working

Comments

@mrphil2105
Copy link

Describe the bug

When I try to use autocomplete in VSCode in a .axaml file the only option that shows up is "Build the project". Building the project does not help. I am actually using Neovim with the LSP, but I tried it in VSCode as well, and the issue persists.

To Reproduce

The project consists of a root folder with an .sln file and 4 other folders containing .csproj files. One of the folders contains an Avalonia project. This is the project I am trying to use the extension with: BachelorProject

Avalonia for VS Code

v0.0.26

Avalonia version

v11.0.10 and tested v11.0.2 as well

VS Code version

v1.88.1

Relevant log output

2024-04-17 17:36:20.659 [info] Creating language service
2024-04-17 17:36:20.659 [info] Starting Avalonia Language Server...
2024-04-17 17:36:20.659 [info] [Trace - 17:36:20] Sending request 'initialize - (0)'.
2024-04-17 17:36:21.459 [info] [Trace - 17:36:21] Received response 'initialize - (0)' in 808ms.
2024-04-17 17:36:21.459 [info] [Trace - 17:36:21] Sending notification 'initialized'.
2024-04-17 17:36:21.470 [info] [Trace - 17:36:21] Sending notification 'workspace/didChangeConfiguration'.
2024-04-17 17:36:21.519 [info] [Trace - 17:36:21] Received request 'client/registerCapability - (1)'.
2024-04-17 17:36:21.519 [info] [Trace - 17:36:21] Sending response 'client/registerCapability - (1)'. Processing request took 2ms
2024-04-17 17:36:21.519 [info] [Trace - 17:36:21] Sending notification 'textDocument/didOpen'.
2024-04-17 17:36:21.541 [info] [Trace - 17:36:21] Received request 'workspace/configuration - (2)'.
2024-04-17 17:36:21.541 [info] [Trace - 17:36:21] Sending response 'workspace/configuration - (2)'. Processing request took 1ms
2024-04-17 17:36:21.595 [info] [Trace - 17:36:21] Received request 'workspace/configuration - (3)'.
2024-04-17 17:36:21.596 [info] [Trace - 17:36:21] Sending response 'workspace/configuration - (3)'. Processing request took 0ms
2024-04-17 17:36:21.643 [info] [Trace - 17:36:21] Received request 'workspace/configuration - (4)'.
2024-04-17 17:36:21.643 [info] [Trace - 17:36:21] Sending response 'workspace/configuration - (4)'. Processing request took 0ms
2024-04-17 17:36:21.668 [info] [Trace - 17:36:21] Received notification 'window/logMessage'.
2024-04-17 17:36:21.668 [info] [Info  - 17:36:21] AvaloniaLanguageServer.Handlers.TextDocumentSyncHandler: ** DidOpenText: file:///home/mrphil2105/CSharp%20Projects/BachelorProject/AvaloniaApp/Views/MainView.axaml | Uri='file:///home/mrphil2105/CSharp%20Projects/BachelorProject/AvaloniaApp/Views/MainView.axaml'
2024-04-17 17:36:25.105 [info] [Trace - 17:36:25] Sending notification 'textDocument/didChange'.
2024-04-17 17:36:25.893 [info] [Trace - 17:36:25] Sending notification 'textDocument/didChange'.
2024-04-17 17:36:25.901 [info] [Trace - 17:36:25] Sending request 'textDocument/completion - (1)'.
2024-04-17 17:36:25.974 [info] [Trace - 17:36:25] Received response 'textDocument/completion - (1)' in 74ms.
2024-04-17 17:36:25.992 [info] [Trace - 17:36:25] Sending request 'completionItem/resolve - (2)'.
2024-04-17 17:36:26.019 [info] [Trace - 17:36:26] Received response 'completionItem/resolve - (2)' in 27ms.
2024-04-17 17:36:28.497 [info] [Trace - 17:36:28] Sending notification 'textDocument/didChange'.
2024-04-17 17:36:28.551 [info] [EXT - INFO] Solution data path path: /tmp/Apachi.sln.json
2024-04-17 17:36:28.594 [info] parser process args: /bin/sh,-c,dotnet "/home/mrphil2105/.vscode-oss/extensions/avaloniateam.vscode-avalonia-0.0.26-universal/solutionParserTool/SolutionParser.dll" "/home/mrphil2105/CSharp Projects/BachelorProject/Apachi.sln" --sdk 7.0
2024-04-17 17:36:28.915 [info] parser process exited 255

Additional context

In Neovim I used the following to set up the LSP:

local configs = require("lspconfig.configs")
local root_pattern = require("lspconfig.util").root_pattern
if not configs.avalonia then
    configs.avalonia = {
        default_config = {
            cmd = { "dotnet", "/path/to/AvaloniaLanguageServer.dll" },
            root_dir = root_pattern("*.csproj"),
            filetypes = { "axaml", "xml" },
        },
    }
end

require("lspconfig").avalonia.setup(coq.lsp_ensure_capabilities {
    on_attach = on_attach, -- My attach function.
    settings = {},
})

P.S. In the log output I can see that it runs with --sdk 7.0. Does this mean I cannot use .NET 8.0 and have to use .NET 7.0?

@mrphil2105 mrphil2105 added the bug Something isn't working label Apr 17, 2024
@10Peppermill
Copy link

I was having a similar issue and fixed it by following the trouble shooting in item two of this disucssion. To save you a click; Close VSCode, navigate to project folder in terminal, call dotnet build, reopen in vscode.

@mrphil2105
Copy link
Author

mrphil2105 commented Apr 19, 2024

I was having a similar issue and fixed it by following the trouble shooting in item two of this disucssion. To save you a click; Close VSCode, navigate to project folder in terminal, call dotnet build, reopen in vscode.

I mentioned in my initial comment above that building the project does not help. Reopening VSCode or Neovim after also doesn't work. I think the LSP fails to detect that the project has been built.

@lucadecamillis
Copy link

I had the same issue here. What helped me was to install the following two extensions in vscode:

After that autocomplete started to work.

@mrphil2105
Copy link
Author

I had the same issue here. What helped me was to install the following two extensions in vscode:

* https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csdevkit

* https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.vscodeintellicode-csharp

After that autocomplete started to work.

I wonder what the equivalent fix is in Neovim, as I don't actually use VSCode for any development.

@DolmaAndKebab
Copy link

DolmaAndKebab commented May 14, 2024

Tried to do the same thing...

vim.api.nvim_create_autocmd({"BufNewFile", "BufRead"}, {
	pattern={"*.axaml"},
	group=vim.api.nvim_create_augroup("axaml-ls", { clear = true } ),
	callback = function()
		local lsp_bin_dir = "~/.vscode/extensions/avaloniateam.vscode-avalonia-0.0.31/avaloniaServer/"
		local lsp_bin_path = "~/.vscode/extensions/avaloniateam.vscode-avalonia-0.0.31/avaloniaServer/AvaloniaLanguageServer.dll";
		if vim.fn.isdirectory(lsp_bin_dir) then
			vim.cmd.setfiletype("xml")
			vim.lsp.start({
				name = "axaml",
				cmd = {"dotnet", lsp_bin_path, "--stdio"},
				root_dir = vim.fn.getcwd(),
				capabilities = capabilities,
			})
		else
			print("Failed to start Avalona Language Server because the lsp bin dir is incorrect!")
		end
	end
})

And it doesnt work however I managed to fix it in vscode..

It seems that the LSP or maybe the extension I am unsure.. But it seems to be asking the .Net Install Extension in vscode to install dotnet 8 and probably something else who knows I fixed it in vscode by giving my dotnet bin path to .Net Install.

I pretty much opened up "User profile settings (JSON)"

@mrphil2105
Copy link
Author

Tried to do the same thing...

vim.api.nvim_create_autocmd({"BufNewFile", "BufRead"}, {
	pattern={"*.axaml"},
	group=vim.api.nvim_create_augroup("axaml-ls", { clear = true } ),
	callback = function()
		local lsp_bin_dir = "~/.vscode/extensions/avaloniateam.vscode-avalonia-0.0.31/avaloniaServer/"
		local lsp_bin_path = "~/.vscode/extensions/avaloniateam.vscode-avalonia-0.0.31/avaloniaServer/AvaloniaLanguageServer.dll";
		if vim.fn.isdirectory(lsp_bin_dir) then
			vim.cmd.setfiletype("xml")
			vim.lsp.start({
				name = "axaml",
				cmd = {"dotnet", lsp_bin_path, "--stdio"},
				root_dir = vim.fn.getcwd(),
				capabilities = capabilities,
			})
		else
			print("Failed to start Avalona Language Server because the lsp bin dir is incorrect!")
		end
	end
})

And it doesnt work however I managed to fix it in vscode..

It seems that the LSP or maybe the extension I am unsure.. But it seems to be asking the .Net Install Extension in vscode to install dotnet 8 and probably something else who knows I fixed it in vscode by giving my dotnet bin path to .Net Install.

I pretty much opened up "User profile settings (JSON)"

I tried in VSCode and gave the .NET path. It still didn't work though. And even if it did it wouldn't help me since I never use VSCode.

@DolmaAndKebab
Copy link

DolmaAndKebab commented May 14, 2024

Heres what I did to fix it for vscode.. Sadly I want to use Neovim but I cant seem to get it too work.

Step 1

Open up settings.json..

Ctrl + Shift + p
Then search for
Open User Settings (JSON)

Step 2

Add the following to your settings.json

 }
    "dotnetAcquisitionExtension.existingDotnetPath": [
        {"extensionId": "AvaloniaTeam.vscode-avalonia", "path": "/usr/bin/dotnet"}
    ],
    "dotnetAcquisitionExtension.installTimeoutValue": 180,
    "editor.minimap.enabled": false
}

REPLACE /usr/bin/dotnet WITH THE BIN PATH TO DOTNET

In order to find the dotnet bin path open up the terminal and run
whereis dotnet and get the path and replace /usr/bin/dotnet

@mrphil2105
Copy link
Author

Now I wonder how you can replicate this in Neovim. If I didn't use Neovim I'd use Rider instead of VSCode, which has official Avalonia support.

@DolmaAndKebab
Copy link

I made it work once but then I did something by mistake and it broke..

I tried remaking it and even moving the LSP.

vim.api.nvim_create_autocmd({"BufNewFile", "BufRead"}, {
	pattern={"*.axaml"},
	group=vim.api.nvim_create_augroup("axaml-ls", { clear = true } ),
	callback = function()
		if vim.fn.isdirectory("~/.config/nvim/LSP-Servers/") then
			local lsp_bin_dir = "~/.config/nvim/LSP-Servers/avaloniaServer"
			local lsp_bin_path = "~/.config/nvim/LSP-Servers/avaloniaServer/AvaloniaLanguageServer.dll";
			if vim.fn.isdirectory(lsp_bin_dir) then
				vim.cmd.setfiletype("xml")
				vim.lsp.start({
					name = "axaml",
					cmd = {"dotnet", lsp_bin_path},
					root_dir = vim.fn.getcwd(),
					capabilities = capabilities,
				})
			else
				print("Failed to start Avalona Language Server because the lsp bin dir does not exist!")
			end
		else
			print("Failed to start Avalonia Language Server. LSP-Servers directory does not exist!")
		end
	end
})

But sadly it just responds with something like "Client 1 exit with 1 and quit with 0"

@DolmaAndKebab
Copy link

Well I guess some how someone made it work..

https://github.com/Johanw123/avalonia.nvim

@mrphil2105
Copy link
Author

mrphil2105 commented May 14, 2024

Well I guess some how someone made it work..

https://github.com/Johanw123/avalonia.nvim

That one is only for previewer, as far as I can see.

@DolmaAndKebab
Copy link

It shows a example for LSP.

@DolmaAndKebab
Copy link

I messed around a bit and managed to get some of the LSP to work now the LSP just keeps recommending the same completion according to the LSP logs.

local Avalonia_LSP_Path_dir = vim.fn.expand("~/.config/nvim/LSP/avaloniaServer/")
local Avalonia_LSP_Path = vim.fn.expand("~/.config/nvim/LSP/avaloniaServer/AvaloniaLanguageServer.dll")

function _ISFILE(name, root)
	if type(name) ~= "string" then
		return false
	end
	if type(root) ~= "string" then
		return false
	end
	return vim.fn.findfile(name, root)
end

vim.api.nvim_create_autocmd({ "BufNewFile", "BufRead" }, {
	pattern = { "*.axaml" },
	group = vim.api.nvim_create_augroup("Avalonia_LSP", { clear = true }),
	callback = function()
		if vim.fn.isdirectory(Avalonia_LSP_Path_dir) then
			if not _ISFILE("AvaloniaLanguageServer.dll", Avalonia_LSP_Path_dir) then
				print("Avalonia LSP does not exist!")
				return
			end
			vim.cmd.setfiletype("xml")

			-- Setting the root_dir
			local root_dir = lspconfig.util.root_pattern("*.csproj", "*.sln")(vim.fn.getcwd())
			if not root_dir then
				print("Root directory not found!")
				return
			end

			-- For debugging
			print("Root directory: " .. root_dir)

			-- Building the project

			print("Building the project!")
			vim.cmd("!dotnet build")

			vim.lsp.start({
				name = "Avalonia_LS",
				cmd = { "dotnet", Avalonia_LSP_Path },
				root_dir = root_dir,
				filetypes = { "axaml", "xml" },
				capabilities = capabilities, -- Your capabilities
			})
			-- For debugging
			vim.lsp.set_log_level("debug")
			local log_path = vim.lsp.get_log_path()
			print("LSP log path: " .. log_path)
		else
			print("Avalonia LSP Dir not found!.. Provided path: " + Avalonia_LSP_Path_dir)
		end
	end,
})

vim.api.nvim_create_user_command("BuildProject", function()
	vim.cmd("!dotnet build")
end, {})

@MeKebabMan
Copy link

Fix!

Open up vscode and when it asks you to Build the project just press enter and let it do it's thing after that close vscode and open up neovim with your avalonia LSP config and it should work!

@MeKebabMan
Copy link

I found out that the Avalonia LSP requires some files in the Bin and OBJ directories. So its able to create those files in VScode but it is not able to in Neovim so what you can do is create a template that already contains the files required for the LSP and each time you use Avalonia instead of doing dotnet new avalonia.app you can just do git clone https://user/my-template. The template should contain the right files for the LSP to work correctly! This can be easily fixed in neovim now let me try explaining how it should work in neovim!

So what you can do is every time the Avalonia LSP Attaches and there's a axaml file you can use LUA to create the required files for the avalonia LSP meaning the user or you will never have to tab into vscode just to build the project!

How I found this out??

I started by creating a avalonia app and then tracking files being created & deleted I found out that some files were modified and new files were created after the vscode extension attempted to build the project..

I managed to get it working in neovim!!!!

@mrphil2105
Copy link
Author

mrphil2105 commented Jun 24, 2024

Selecting 'Build the project' in VSCode and pressing Enter did not work for me. But then I noticed that it runs the build command with --sdk 7.0 and I only had 8.0 installed. Installing 7.0 made the build command work (keep in mind that a regular build with dotnet build did not work as mentioned before). This made it create the files in obj you mentioned and now the LSP works in Neovim! A bit strange since the VSCode extension page mentions that you need 8.0 and not 7.0. Any idea why it does not use 8.0 and whether building my project with 8.0 will break the LSP somehow?

EDIT: Nevermind, turns of the version of the extension I installed in VSCode was not uploaded by the Avalonia team, and was an older version (0.0.26).

@MeKebabMan
Copy link

I mean you can just create a bash script to create the files that it requires.. But who in the world has enough time to figure all of that out? its been 3 weeks and I haven't tested it and to be honest if it's this annoying to get a LSP to work then I might aswell just use GTK4.

@VNNCC
Copy link

VNNCC commented Jul 23, 2024

Just came across this issue.

I am using VSCode and .NET 8.0.400-preview.0.24324.5 and believe that my fix will only be applicable for those using a preview version.

TL;DR

Identified and fixed issues with the Build project button, preview feature, and XAML IntelliSense. The fix involved updating the Microsoft.Build.dll libraries in the solution parser tool directory of the extension (...\.vscode-insiders\extensions\avaloniateam.vscode-avalonia-0.0.31\solutionParserTool\), which resolved the errors.

Detailed Description

Issues Addressed by the Fix

  • Build project button not functioning
  • Preview feature not working
  • XAML IntelliSense issues

Fix

Upon reviewing the logs, I identified the following entries:

[info] Command avalonia.previewProcess, [SomeFile].axaml
[info] [EXT - INFO] Solution data path path: ../[SolutionName].sln.json
[info] parser process args: C:\Windows\system32\cmd.exe,/d,/s,/c,"dotnet "....vscode-insiders\extensions\avaloniateam.vscode-avalonia-0.0.31\solutionParserTool\SolutionParser.dll" "...[SolutionName].sln""

The entry for Solution data path path caught my attention. Upon inspecting it, I found the following content:

{
  "solution": "../[SolutionName].sln",
  "projects": [],
  "files": []
}

This content was clearly incorrect.

To address this, I executed the following command:

dotnet "...\.vscode-insiders\extensions\avaloniateam.vscode-avalonia-0.0.31\solutionParserTool\SolutionParser.dll" "...\[SolutionName].sln"

The result was an error:

Error parsing project [ProjectName]: Invalid static method invocation syntax: "[MSBuild]::SubstringByAsciiChars($(MSBuildProjectFile), 0, 8)". Method '[MSBuild]::SubstringByAsciiChars' not found. Static method invocation should be of the form: $([FullTypeName]::Method()), e.g. $([System.IO.Path]::Combine(`a`, `b`)). Check that all parameters are defined, are of the correct type, and are specified in the right order.  ...\dotnet\sdk\8.0.400-preview.0.24324.5\Microsoft.Common.CurrentVersion.targets

Upon examining the Microsoft.Common.CurrentVersion.targets file, I found the following:

<MSBuildCopyMarkerName Condition="'$(MSBuildCopyMarkerName.Length)' &gt; '17'">$([MSBuild]::SubstringByAsciiChars($(MSBuildProjectFile), 0, 8)).$([MSBuild]::StableStringHash($(MSBuildProjectFile)).ToString("X8"))</MSBuildCopyMarkerName>

I compared this with the version .NET 8.0.206:

-<MSBuildCopyMarkerName Condition="'$(MSBuildCopyMarkerName.Length)' &gt; '17'">$(MSBuildProjectFile.Substring(0,8)).$([MSBuild]::StableStringHash($(MSBuildProjectFile)).ToString("X8"))</MSBuildCopyMarkerName>
+<MSBuildCopyMarkerName Condition="'$(MSBuildCopyMarkerName.Length)' &gt; '17'">$([MSBuild]::SubstringByAsciiChars($(MSBuildProjectFile), 0, 8)).$([MSBuild]::StableStringHash($(MSBuildProjectFile)).ToString("X8"))</MSBuildCopyMarkerName>

As seen, there was a change.

I then examined the code to determine which library was used for project information collection, which was Microsoft.Build.dll.

By copying the .NET preview Microsoft.Build.*.dll libraries from ..\dotnet\sdk\8.0.400-preview.0.24324.5\ into the following directory ...\.vscode-insiders\extensions\avaloniateam.vscode-avalonia-0.0.31\solutionParserTool\ the [SolutionName].sln.json file was successfully generated, resolving the IntelliSense and preview issues.

@MeKebabMan
Copy link

Yay

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

6 participants