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

VSCode constantly freezes with Saving 'file.zig': Running 'Zig Language' Formatter #1881

Closed
timfayz opened this issue May 1, 2024 · 6 comments
Labels
bug Something isn't working editor:vscode

Comments

@timfayz
Copy link

timfayz commented May 1, 2024

Zig Version

0.12.0

Zig Language Server Version

ZLS 0.12.0

Client / Code Editor / Extensions

Visual Studio Code-1.88.1

Steps to Reproduce and Observed Behavior

I use zls as part of the Zig extension for vscode. The problem is that zls constantly freezes when attempting to format a file before saving. It happens more frequently when there are syntax errors. Aside from that, I don't see any significant patterns. When it happens, vscode pops up a notification on the right, stating Saving 'some_file.zig': Running 'Zig Language' Formatter, and I am unable to save the file (other than continuing editing it). As a result, I find myself constantly pressing cmd shift p to open the command palette and running Developer: Reload Window to forcefully "reset" the editor.

I have the following configuration:

  "zig.path": "zig",
  "zig.zls.path": "/Users/timfayz/Library/Application Support/Code/User/globalStorage/ziglang.vscode-zig/zls_install/zls",
  "zig.zls.path": "/opt/homebrew/bin/zls",
  "zig.zls.enableBuildOnSave": false,
  "zig.initialSetupDone": true,
  "zig.zls.checkForUpdate": true,
  "zig.zls.enableAutofix": true,

Note that there are two instances of zig.zls.path. This is to show that I have tried both: the zls installed by the extension itself and the one I manually installed through brew install zls. Unfortunately, the problem persists regardless of which binary I use. I don't remember exactly when the problem started, but it dates back to zls version fd9d2642e27ac65ea96fb37a27e02c9e2d269e23, committed on Oct 22, 2023 (so no fuzz about latest 0.12.0 release, I simply tired of this problem and decided to report now). Despite trying various zig and zls updates, uninstalling and reinstalling the extension, or reopening the editor, the issue always remains.

Expected Behavior

No freezing.

Relevant log output

info : ( main ): Enabled debug logging.
info : ( main ): Starting ZLS 0.12.0 @ '/opt/homebrew/bin/zls'
info : (server): Client is 'Visual Studio Code-1.88.1'
debug: (server): Offset Encoding: utf-16
info : (server): No config file zls.json found. This is not an error.
info : (server): Set config option 'builtin_path' to '/Users/timfayz/Library/Caches/zls/builtin.zig'
info : (server): Set config option 'zig_lib_path' to '/opt/homebrew/Cellar/zig/0.12.0/lib/zig'
info : (server): Set config option 'zig_exe_path' to '/opt/homebrew/bin/zig'
info : (server): Set config option 'build_runner_path' to '/Users/timfayz/Library/Caches/zls/build_runner_0.12.0.zig'
info : (server): Set config option 'global_cache_path' to '/Users/timfayz/Library/Caches/zls'
info : (server): Set config option 'build_runner_global_cache_path' to '/Users/timfayz/.zig/cache'
debug: (server): Took 185ms to process request-0-initialize on Thread 3780493
debug: (server): Dynamically registering method 'workspace/didChangeConfiguration'
debug: (server): Took 0ms to process notification-initialized on Thread 3780493
debug: (store ): Opened document `file:///Users/timfayz/Cloud/dev/ccl/src/tokenizer.zig`
debug: (server): Took 0ms to process notification-textDocument/didOpen on Thread 3780493
debug: (server): Took 0ms to process response-register-workspace/didChangeConfiguration on Thread 3780493
debug: (server): Took 0ms to process request-1-textDocument/documentSymbol on Thread 3780495
debug: (server): Took 20ms to process request-2-textDocument/codeAction on Thread 3780493
info : (server): Set config option 'semantic_tokens' to 'partial'
debug: (server): Took 45ms to process response-i_haz_configuration on Thread 3780493
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/std.zig`
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/mem.zig`
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/debug.zig`
debug: (server): Took 4ms to process request-3-textDocument/inlayHint on Thread 3780497
debug: (server): Took 0ms to process response-semantic_tokens_refresh on Thread 3780493
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/ascii.zig`
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io.zig`
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io/Reader.zig`
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/log.zig`
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/mem/Allocator.zig`
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/testing.zig`
debug: (store ): Opened document `file:///opt/homebrew/Cellar/zig/0.12.0/lib/zig/std/io/fixed_buffer_stream.zig`
debug: (server): Took 4ms to process request-4-textDocument/semanticTokens/full on Thread 3780496
... (last two messages on freeze)
debug: (server): Took 0ms to process notification-textDocument/willSave on Thread 3780496
debug: (server): Took 0ms to process request-1244-textDocument/willSaveWaitUntil on Thread 3780494
@timfayz timfayz added the bug Something isn't working label May 1, 2024
@Techatrix
Copy link
Member

By default, the vscode-zig extension provides formatting by itself and does not rely on ZLS unless the zig.formattingProvider config option is set to zls. There have been other recent reports about the vscode-zig formatting not working properly but me and @SuperAuguste have not been able to reliably reproduce or identify the cause of this issue.

One possible reason could be the fact that zig fmt has recently become just in time compiled when it is first used. This means that formatting for the first time will take some time until the formatter has been compiled. This can be easily noticed when running zig fmt --help with a new Zig version or cleared cache.

Until this has been resolved, you can switch to ZLS as the formatting provider which does not have this issue as far as I can tell.

@timfayz
Copy link
Author

timfayz commented May 1, 2024

Lately, I've been getting this freeze almost every minute or so. I gave "zig.formattingProvider": "zls" a try, and it seems it's working! My saviour! Thank you! The question is then what kind of formatter I was using? The option was set to extension. Also, does it mean the "zig.zls.*" options are irrelevant unless switched to the zls provider?

@Techatrix
Copy link
Member

Both formatting providers behave identical. extension means that the vscode-zig extension runs zig fmt behind the scenes. zls means that the vscode-zig extension will ask ZLS to format the file and ZLS will run the standard library code that implements zig fmt (ZLS basically contains the zig fmt command). The zig.formattingProvider is only about formatting the zig.zls.* are about all the other ZLS features that are not formatting.

@timfayz
Copy link
Author

timfayz commented May 2, 2024

The zig.formattingProvider is only about formatting [and] the zig.zls.* are about all the other ZLS features that are not formatting.

Sorry, I didn't really understand the answer. Isn't, say, zig.zls.enableAutofix part of the formatting? If not, then what is it part of?

Also, I tried to disable the formatting altogether but realized that I can't (through options) unless I disable the extension itself. Here are my options (relevant parts):

  "zig.formattingProvider": "off",
  "zig.zls.enableBuildOnSave": false,
  "editor.formatOnSave": false,

Interesting to note that when it comes to formatting other files (eg. json), the formatOnSave set to false effectively disables the feature. So it seems the extension does not respect the global option and formatting is executed no matter what.

Update: "zig.formattingProvider": "off" indeed disables formatting (I just needed to reload the editor). However, solely using "editor.formatOnSave": false doesn't disable it.

@Techatrix
Copy link
Member

Sorry, I didn't really understand the answer. Isn't, say, zig.zls.enableAutofix part of the formatting? If not, then what is it part of?

Autofix may appear as a feature build on top of formatting but they are actually independent. You can use autofix without formatting and the other way round.
When talking about formatting, I refer to all changes that zig fmt does. All other text edits you see are autofix from ZLS and don't rely on zig fmt or the zig executable in general.

The zig.formattingProvider only concerns itself with formatting and not autofix.

However, solely using "editor.formatOnSave": false doesn't disable it.

The vscode-zig extension overrides the editor.formatOnSave option for Zig which takes priority over the "ordinary" editor.formatOnSave option.
To disable formatting on Zig files in VS Code, the following needs to be added to settings.json:

"[zig]": {
  "editor.formatOnSave": false
}

@Techatrix
Copy link
Member

This should have hopefully been fixed by ziglang/vscode-zig#222 which is in the vscode-zig 0.5.9 release.
If this still happens, feel free to report this to vscode-zig.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working editor:vscode
Projects
None yet
Development

No branches or pull requests

2 participants