ale icon indicating copy to clipboard operation
ale copied to clipboard

ZLS language server isn't started unless a project root is found

Open techatrix opened this issue 11 months ago • 6 comments

Information

VIM version

NVIM v0.11.5 Build type: Release

Operating System: Linux 6.12.62, NixOS 25.11, x86_64

What went wrong

ALE does not start the ZLS language server in files that have no build.zig file in any parent directory. The :ALEInfo output will contain the following to indicate that it did not start ZLS:

 LSP Error Messages:

(Errors for zls)
Failed to find project root, language server won't start.

This has been initially reported to ZLS because it prevents LSP features from working inside the Zig standard library since it doesn't have a build.zig.

There are some other issues I found that mentioned the "Failed to find project root, language server won't start." error but none of the seemed to asked whether it was necessary to find a project root to start the language server. I guess this is specific to each language server but ZLS does support to be run without a project root or workspace folder. So perhaps start ZLS anyway if possible?

Reproducing the bug

  1. Install Zig and ZLS
  2. Open a Zig file that doesn't have a build.zig in any parent directory. Like the Zig standard library.
  3. Try to use commands like :ALEGoToDefinition which do not work.

:ALEInfo

Expand
 Current Filetype: zig
Available Linters: ['zlint', 'zls']
  Enabled Linters: ['zls']
  Ignored Linters: []
 Suggested Fixers:
  'remove_trailing_lines' - Remove all blank lines at the end of a file.
  'trim_whitespace' - Remove all trailing whitespace characters at the end of every line.
  'zigfmt' - Official formatter for Zig
 
 Linter Variables:
let g:ale_zig_zls_config = {}
let g:ale_zig_zls_executable = '/home/techatrix/repos/zls/my/zls.sh'
 
 Global Variables:
let g:ale_cache_executable_check_failures = v:null
let g:ale_change_sign_column_color = v:null
let g:ale_command_wrapper = v:null
let g:ale_completion_delay = v:null
let g:ale_completion_enabled = v:false
let g:ale_completion_max_suggestions = v:null
let g:ale_disable_lsp = 'auto'
let g:ale_echo_cursor = v:true
let g:ale_echo_msg_error_str = 'Error'
let g:ale_echo_msg_format = '%code: %%s'
let g:ale_echo_msg_info_str = 'Info'
let g:ale_echo_msg_warning_str = 'Warning'
let g:ale_enabled = 1
let g:ale_fix_on_save = v:false
let g:ale_fixers = {}
let g:ale_history_enabled = v:true
let g:ale_info_default_mode = 'preview'
let g:ale_history_log_output = v:true
let g:ale_keep_list_window_open = 0
let g:ale_lint_delay = 200
let g:ale_lint_on_enter = v:true
let g:ale_lint_on_filetype_changed = v:true
let g:ale_lint_on_insert_leave = v:true
let g:ale_lint_on_save = v:true
let g:ale_lint_on_text_changed = 'normal'
let g:ale_linter_aliases = {}
let g:ale_linters = {'zig': ['zls']}
let g:ale_linters_explicit = v:false
let g:ale_linters_ignore = {}
let g:ale_list_vertical = v:false
let g:ale_list_window_size = 10
let g:ale_loclist_msg_format = '%code: %%s'
let g:ale_max_buffer_history_size = v:null
let g:ale_max_signs = v:null
let g:ale_maximum_file_size = v:null
let g:ale_open_list = v:false
let g:ale_pattern_options = v:null
let g:ale_pattern_options_enabled = v:null
let g:ale_root = {}
let g:ale_set_balloons = v:false
let g:ale_set_highlights = v:true
let g:ale_set_loclist = v:true
let g:ale_set_quickfix = v:false
let g:ale_set_signs = v:true
let g:ale_sign_column_always = v:null
let g:ale_sign_error = v:null
let g:ale_sign_info = v:null
let g:ale_sign_offset = v:null
let g:ale_sign_style_error = v:null
let g:ale_sign_style_warning = v:null
let g:ale_sign_warning = v:null
let g:ale_sign_highlight_linenrs = v:null
let g:ale_type_map = {}
let g:ale_use_neovim_diagnostics_api = v:true
let g:ale_use_global_executables = v:null
let g:ale_virtualtext_cursor = 'all'
let g:ale_warn_about_trailing_blank_lines = v:true
let g:ale_warn_about_trailing_whitespace = v:true
 
 LSP Error Messages:

(Errors for zls)
Failed to find project root, language server won't start.
Failed to find project root, language server won't start.
  Command History:

techatrix avatar Dec 27 '25 01:12 techatrix

Are there any other files ALE can look for to determine the root of the Zig project? without this ALE would start a new ZLS instance for every single Zig file open in vim that depending on how resource hungry is ZLS can quickly exhaust you computer RAM/CPU.

The specific part that needs to be modified is https://github.com/dense-analysis/ale/blob/8eb4803da99a575bc827a6c814e63b1053b7002f/ale_linters/zig/zls.vim#L8.

hsanson avatar Dec 29 '25 03:12 hsanson

Not really. A build.zig, build.zig.zon or .git are the only reasonable indicators to estimate a project root for Zig. But none of these apply to the standard library which editors generally just treat as a set of standalone files. Does ALE not support running an LSP server instance without knowing a project root? Could it not share one server instance across all projects or at least share one instance for all project-less files?

If ZLS is just started to handle some requests on a single file without being given any LSP workspace directories then it unlikely to consume many resources but any solution that would avoid having one process per file would still be nice.

I also just found https://github.com/dense-analysis/ale/issues/4577. This issue essentially describes the same problem but with a different example (single C++ file compared to Zig standard library). But at it's core, it can apply to any language where you may encounter a standalone source file.

techatrix avatar Dec 29 '25 12:12 techatrix

I tested a few different things, and what worked was just setting the project_root to .

At first I tried to remove project_root, and zls threw errors.

As pointed out, I'm not sure if the instance of zls is the same when I go to the std library using 'Go To Definition', or if a new instance is started. I couldn't find any settings to set to tell it to persist. Someone who knows the codebase better would need to confirm. I can do a PR if necessary. It was removing the function all-together and setting 'project_root' to '.'

in /ale_linters/zig/zls.vim

call ale#linter#Define('zig', {
\   'name': 'zls',
\   'lsp': 'stdio',
\   'lsp_config': {b -> ale#Var(b, 'zig_zls_config')},
\   'executable': {b -> ale#Var(b, 'zig_zls_executable')},
\   'command': '%e',
\   'project_root': '.',
\})

coffeebe4code avatar Dec 30 '25 21:12 coffeebe4code

Simply modify the ale_linters#zig#zls#GetProjectRoo() method to fallback to current file directory instead of returning an empty '' string:


function! ale_linters#zig#zls#GetProjectRoot(buffer) abort
    let l:build_rs = ale#path#FindNearestFile(a:buffer, 'build.zig')

    return !empty(l:build_rs)
    \     ?  fnamemodify(l:build_rs, ':h')
    \     :  expand('#' . a:buffer . ':p:h')
endfunction

If a build.zig file is found it will work as it already does and if not found it will fallback to the directory of the current file.

Since we are using STDIO to communicate with zls, with this change ALE will spawn a new instance of the language server for every file open. I recomend to check how much resources zls require per spawned instance to see if it is acceptable. Using top or htop is easy to see the zls instances and how much resources they use.

hsanson avatar Jan 03 '26 10:01 hsanson

It would only do this if a build.zig is not found, and the only scenario that does that is the std library. So a persons project should work great, and only slightly slower from my tests in stdlib files.

I can make a PR after checking it on my ale repo

coffeebe4code avatar Jan 18 '26 03:01 coffeebe4code

@hsanson hoping you could review the PR. I got the tests passing. For both cwd, and build.zig.

coffeebe4code avatar Jan 24 '26 20:01 coffeebe4code