ionide / ionide-vscode-fsharp

VS Code plugin for F# development
http://ionide.io
MIT License
859 stars 277 forks source link

Eternal hang on format document #1606

Closed briandunn closed 2 years ago

briandunn commented 2 years ago

Describe the bug**

Calling the formatter either hangs forever or shows "No Fantomas install was found." Either "Install locally" or "Install globally" buttons do nothing. fantomas is in my $PATH and works via cli.

Steps to reproduce

  1. open an fsharp file
  2. format the file (save, or choose Format Document from the context menu)

Expected behaviour

The code is formatted by fantomas

Screenshots

image

Machine info

Additional context

Can't find anything in the logs that is obviously relevant.

open-collective-bot[bot] commented 2 years ago

Hey @briandunn :wave:,

Thank you for opening an issue. We will get back to you as soon as we can. Also, check out our OpenCollective and consider backing us.

https://opencollective.com/ionide

PS.: We offer backer support for all backers. Don't forget to add backer label when you start backing us :smile:

baronfel commented 2 years ago

Tagging @nojaf for investigation, please :)

nojaf commented 2 years ago

Hello Brian, thanks for reaching out. Having Fantomas on your $Path isn't necessarily going to cut it. You need a compatible version of Fantomas installed as local or global cli tool.

What do you get when you execute fantomas --version? And what do you get when you see dotnet tool list -g?

samritchie commented 2 years ago

I’m also seeing this problem - I’ve never had fantomas installed locally but it worked (bundled?) until last update.

If I install (the alpha version of) fantomas as a local tool I still see the same behaviour - the extension logs TextDocumentFormatting Request but never completes.

nojaf commented 2 years ago

Yes, the last update removed the bundled version. It might not seem like this from your point of view but this still solves more problems than it introduces.

Can you format your file using the cli?

samritchie commented 2 years ago

I understand the rationale behind this, but the messaging was poor - the changelog is vague and there’s no mention in the README, it just suddenly broke.

Fantomas works fine via the CLI, however running on a file via the context menu just shows a never-ending progress bar in the tabs.

nojaf commented 2 years ago

Could you try the following script please:

#r "nuget: Fantomas.Client"

open System.IO
open Fantomas.Client.Contracts
open Fantomas.Client.LSPFantomasService

let service = new LSPFantomasService() :> FantomasService

let file = @"C:\Users\fverdonck\Projects\HelloWorld\Math.fs"
let content = File.ReadAllText file

let response =
    service.FormatDocumentAsync { SourceCode = content; FilePath = file; Config = None }
    |> Async.AwaitTask
    |> Async.RunSynchronously

printfn "%A" response

This mimics what the tooling is doing when you press Format Document. What does response yield?

samritchie commented 2 years ago

Hi @nojaf, the script appears to work okay

samritchie commented 2 years ago

I should clarify I’ve never received the "No Fantomas install was found" message even when it wasn’t installed

nojaf commented 2 years ago

If you did not see "No Fantomas install was found", that would indicate that a compatible version was found. As supported by the script that is working. What code did you receive when running the script? Perhaps there is a hiccup between the response and the editor tooling processing it.

samritchie commented 2 years ago

No, I definitely had no fantomas install at all - I just verified by uninstalling fantomas and it has the same behaviour. I think it must be failing earlier in the process.

nojaf commented 2 years ago

Hey, sorry this may sound a bit rude but could you please try and answer my questions exactly. If I ask stuff like what does response yield and what code did you receive when running the script, I'd like to get those literal answers in order to help you.

I'm trying to troubleshoot this for you, hoping something rings a bell along the way but I have no real idea what your problem might be.

samritchie commented 2 years ago

Sorry, redacted FantomasResponse looks like this - it’s code 2

{ Code = 2
  FilePath = "<path to file>"
  Content = None }
nojaf commented 2 years ago

Thanks, Code 2 means that your file didn't need any formatting. So the behaviour that nothing happens is actually the correct one. However, TextDocumentFormatting Request should be completed though. @baronfel does anything come to mind when we return null in the LSP?

samritchie commented 2 years ago

For clarity, I was seeing exactly the same behaviour when the file did need formatting, and seeing the same behaviour when there was no fantomas install available.

samritchie commented 2 years ago

Just some additional details before I clock off. I’m on macOS 11.6.1, dotnet 5.0.401, ionide-fsharp 5.8.1, fantomas installed as a local tool 4.6.0-alpha-005 (although as noted same behaviour with no fantomas installed). It feels to me like ionide is struggling to find/run fantomas rather than a problem parsing the response.

nojaf commented 2 years ago

What do you get when you execute set DOTNET_CLI_UI_LANGUAGE=en-us" && dotnet tool list inside the terminal in your working directory? My syntax above might not be correct but you know set DOTNET_CLI_UI_LANGUAGE and run dotnet tool.

samritchie commented 2 years ago
-----------------------------------------------------------------------------------------------------------------
fake-cli           5.20.4-alpha.1642      fake         <project dir>/.config/dotnet-tools.json
fantomas-tool      4.6.0-alpha-005        fantomas      <project dir>/.config/dotnet-tools.json
briandunn commented 2 years ago

Hello Brian, thanks for reaching out. Having Fantomas on your $Path isn't necessarily going to cut it. You need a compatible version of Fantomas installed as local or global cli tool.

What do you get when you execute fantomas --version? And what do you get when you see dotnet tool list -g?

➜ fantomas --version
Fantomas v4.5.4
➜ dotnet tool list -g
Package Id          Version      Commands             
------------------------------------------------------
fake-cli            5.20.4       fake                 
fantomas-tool       4.5.4        fantomas             
fsautocomplete      0.47.2       dotnet-fsautocomplete

Looks like I need to upgrade to the 4.6 beta. will report back.

seanamos commented 2 years ago

Similar problem.

Running Format Document results in VSCode showing a loading bar at the top of the editor forever, the window remains responsive though. The loading bar is only fixed by reloading the window.

I have fantomas-tool 4.5.5 installed as a local dotnet tool.

nojaf commented 2 years ago

Hi, you need 4.6 as mentioned in the docs.

dotnet tool install --local fantomas-tool --version 4.6.0-alpha-005

Could you please try installing this exact version?

seanamos commented 2 years ago

After installing 4.6.0-alpha-005, I get the same problem.

nojaf commented 2 years ago

What operating system are you on?

nojaf commented 2 years ago

Also, is everybody on Ionide extension 5.8.1?

seanamos commented 2 years ago

I am on Linux (Pop!_OS) Ubuntu 20.04, using Ionide-fsharp 5.8.1.

nojaf commented 2 years ago

Someone just confirmed to me that on Ubuntu 21.10 (the Xubuntu flavour), it works as expected. Would anyone be willing to debug FsAutocomplete perhaps? This could give us some more insights into what is happening on the LSP side. I can provide some steps if anyone is interested.

recfab commented 2 years ago

I'm running into the same problem.

fantomas is installed as a tool, globally and locally, but not directly on the PATH

Environment information

Ionide.Ionide-fsharp vscode exteion version: 5.8.1

$  uname -a                                                                                                                                             ─╯
Darwin WS-126272a 19.6.0 Darwin Kernel Version 19.6.0: Thu Sep 16 20:58:47 PDT 2021; root:xnu-6153.141.40.1~1/RELEASE_X86_64 x86_64

$ fantomas --version                                                                                                                                   ─╯
zsh: command not found: fantomas

$ dotnet fantomas --version
Fantomas v4.6.0-alpha-005+e4a18aa21c5ca96053c720770e55cfd6f185b64

$ dotnet tool list
Package Id         Version              Commands      Manifest                                                       
---------------------------------------------------------------------------------------------------------------------
fantomas-tool      4.6.0-alpha-005      fantomas      /Users/nameny/code/personal/yggdrasil/.config/dotnet-tools.json

$ dotnet tool list -g
Package Id         Version              Commands
------------------------------------------------
fantomas-tool      4.6.0-alpha-004      fantomas

Testing script

I saved your testing script as ionide-vscode-fsharp-issue-1606.fsx and ran it against a file called Asciidoc.fsx

Here is the output when it does not need formatting:

$ dotnet fsi scripts/ionide-vscode-fsharp-issue-1606.fsx
{ Code = 2
  FilePath = "/Users/nameny/code/personal/yggdrasil/scripts/Asciidoc.fsx"
  Content = None }

and when it does:

$ dotnet fsi scripts/ionide-vscode-fsharp-issue-1606.fsx           
dotnet fsi scripts/ionide-vscode-fsharp-issue-1606.fsx
{ Code = 3
  FilePath = "/Users/nameny/code/personal/yggdrasil/scripts/Asciidoc.fsx"
  Content =
   Some
     "Parsing failed with errors: [|/Users/nameny/code/personal/yggdrasil/scripts/Asciidoc.fsx (6,3)-(6,4) parse error Unexpected symbol '|' in implementation file|]
And options: { SourceFiles = [|"/Users/nameny/code/personal/yggdrasil/scripts/Asciidoc.fsx"|]
  ConditionalCompilationDefines = []
  ErrorSeverityOptions = { WarnLevel = 3
                           GlobalWarnAsError = false
                           WarnOff = []
                           WarnOn = []
                           WarnAsError = []
                           WarnAsWarn = [] }
  LangVersionText = "default"
  IsInteractive = false
  LightSyntax = None
  CompilingFsLib = false
  IsExe = true }" } 

Running fantomas directly seems to work fine, on the same file

$ dotnet fantomas scripts/ionide-vscode-fsharp-issue-1606.fsx                                                                                         
Processing scripts/ionide-vscode-fsharp-issue-1606.fsx
scripts/ionide-vscode-fsharp-issue-1606.fsx has been written.
nojaf commented 2 years ago

Hello @recfab, thank you for this elaborate report. Could you share the contents of Asciidoc.fsx? I'd like to find out why you got Code = 3 for what code.

When you used dotnet fantomas scripts/ionide-vscode-fsharp-issue-1606.fsx the last time, that would be using the global version right (alpha 4)? Does the error also occur when you navigate to the /Users/nameny/code/personal/yggdrasil folder and format using the command line over there?

recfab commented 2 years ago

When you used dotnet fantomas scripts/ionide-vscode-fsharp-issue-1606.fsx the last time, that would be using the global version right (alpha 4)? Does the error also occur when you navigate to the /Users/nameny/code/personal/yggdrasil folder and format using the command line over there?

The flip actually: I ran it from within my repo, but ran it against the wrong file.

If I run it on the correct file, I get the same Code 3 response as the script, but that's because I'm an F# newb and messed up the syntax. After fixing the syntax so that it compiles, it works as expected. Given that, I'm not sure this is useful, but for completeness sake:

~ $ dotnet tool list -g
Package Id         Version      Commands
----------------------------------------
fantomas-tool      4.5.6        fantomas

~ $ dotnet fantomas ~/code/personal/yggdrasil/scripts/Asciidoc.fsx
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET program, but dotnet-fantomas does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

Today I learned: you can only use global tools as dotnet <command> if the executable is named dotnet-<command>. I can run it directly though

~ $ which fantomas
/Users/nameny/.dotnet/tools/fantomas

~ $ fantomas ~/code/personal/yggdrasil/scripts/Asciidoc.fsx
Processing /Users/nameny/code/personal/yggdrasil/scripts/Asciidoc.fsx
/Users/nameny/code/personal/yggdrasil/scripts/Asciidoc.fsx has been written.

And as a local tool:

~ $ cd ~/code/personal/yggdrasil

~/code/personal/yggdrasil $ dotnet tool list
Package Id         Version              Commands      Manifest
---------------------------------------------------------------------------------------------------------------------
fantomas-tool      4.6.0-alpha-005      fantomas      /Users/nameny/code/personal/yggdrasil/.config/dotnet-tools.json

~/code/personal/yggdrasil $ dotnet fantomas ./scripts/Asciidoc.fsx
Processing ./scripts/Asciidoc.fsx
./scripts/Asciidoc.fsx has been written.

Asciidoc.fsx contains a single sum type:

// The spec and corresponding TCK are not ready yet
// This file is partially based on the MR here:
// https://gitlab.eclipse.org/eclipse/asciidoc-lang/asciidoc-lang/-/merge_requests/6

type Node =
  | Preamble | Paragraph | Section | Verse
  | STEM | Sidebar | Quote | Open
  | Example | Listing | Literal | Admonition
  | Image | Video | Audio | Pass
  | AttributeReference | AttributeDefinition
  | Strong | Emphasis | Monospace | Subscript
  | Superscript | SingleQuotation | DoubleQuotation
  | Str | Anchor | OrderedList | UnorderedList | DescriptionList
  | InlineImage | InlineBreak | InlineButton | InlineMenu
  | InlineMacro | Table
nojaf commented 2 years ago

Thanks, if you open /Users/nameny/code/personal/yggdrasil in vscode I would expect formatting to work there. Other scenarios might not work as your global version 4.5.6 is not compatible with the latest changes in Ionide.

recfab commented 2 years ago

I updated the global tool to the correct version and made sure it was on the path (as fantomas) and it seems to work for me now. I had tried some other steps to create a repro, but I cannot get it to hang.

(In trying to reproduce this, I quit and restarted vscode repeatedly, toggling the FSharp.showProjectExplorerIn setting, and trying with and without fantomas on my PATH. About half the time, Ionide just didn't activate at all, but when that happens it doesn't hang, it just doesn't do anything, so I don't think it's related.)

seanamos commented 2 years ago

Installing the tool globally didn't fix the problem for me:

~ 
➜ dotnet tool install -g fantomas-tool --version 4.6.0-alpha-006 
You can invoke the tool using the following command: fantomas
Tool 'fantomas-tool' (version '4.6.0-alpha-006') was successfully installed.

~ took 13s 
➜ fantomas
Input path is missing...

Peek 2021-11-04 12-53

nojaf commented 2 years ago

Could you check if there is a Fantomas process started (after you invoked the Format Document command)? No idea, what command you need on your OS but in PowerShell this looks like:

Get-Process -Name "fantomas" | Format-Table Id, Name, CommandLine

   Id Name     CommandLine
   -- ----     -----------
27040 fantomas "fantomas" fantomas --daemon

If this process is present, formatting should work. If the process is not there, that might explain the hang in vscode.

Also, just to double-check, what does fantomas --version prompt? Should be 4.6.0-alpha-006.

seanamos commented 2 years ago

Running ps -aux | grep fantomas after running Format Document does not list a fantomas process.

While typing this though, bizarrely, it started to work after reloading VSCode multiple times.

➜ ps -aux | grep fantomas
seanamos  181682  6.1  0.4 4228632 76896 ?       Sl   17:59   0:01 dotnet fantomas --daemon
seanamos  181704  5.3  0.5 3238604 91992 ?       Sl   17:59   0:01 /usr/share/dotnet/dotnet /home/seanamos/.nuget/packages/fantomas-tool/4.6.0-alpha-005/tools/netcoreapp3.1/any/fantomas-tool.dll --daemon

I then killed the fantomas daemon process and reloaded VSCode and I cannot get it to work again. I tried about 20 times and the fantomas daemon process doesn't start.

nojaf commented 2 years ago

Killing the process manually is something that Ionide will not recover from and should never be done. I'm not sure what the impact of reloading VSCode is but I would expect things to only start working once you've exited and restarted the program. I still can't tell much more, only that some of the behaviour of this entire thread can be explained by the process not being there.

samritchie commented 2 years ago

I can confirm no fantomas process is running for me after I select "Format Document".

nojaf commented 2 years ago

@samritchie I believe you've installed the local tool right? Then the process will be dotnet with arguments fantomas --daemon. Out of curiosity, can you start this process manually? Run inside the workspace, dotnet fantomas --daemon. This should start the process but show no output. Could you verify that at least that works? Oh and run dotnet fantomas --version as well for me please.

samritchie commented 2 years ago

Yes, that’s correct, local tool. dotnet fantomas --daemon starts, checking processes from another shell shows /usr/local/share/dotnet/dotnet /Users/sam/.nuget/packages/fantomas-tool/4.6.0-alpha-006/tools/netcoreapp3.1/any/fantomas-tool.dll --daemon

dotnet fantomas --version returns Fantomas v4.6.0-alpha-006+c90333468261671f2f0e8e065009160b74a41662

samritchie commented 2 years ago

Found this issue - https://github.com/fsharp/FsAutoComplete/issues/845 - problem is the ".NET Core Test Explorer" extension; I disabled the extension & format document magically started working...

briandunn commented 2 years ago

Found this issue - fsharp/FsAutoComplete#845 - problem is the ".NET Core Test Explorer" extension; I disabled the extension & format document magically started working...

Brilliant @samritchie worked for me too thanks!

s6o commented 2 years ago

I did not have ".NET Core Test Explorer" installed, but was facing the same error message https://github.com/ionide/ionide-vscode-fsharp/issues/1606#issue-1039020462 after having updated to .NET 6 and ionide-fsharp 5.9.0

Upgrading fantomas-tool fixed it: dotnet tool update -g fantomas-tool --version 4.6.0-alpha-007

seanamos commented 2 years ago

I can confirm disabling ".NET Core Test Explorer" seems to fix formatting for me (I tested multiple times to make sure it wasn't a fluke). Enabling it again causes the eternal loading scenario again and the fantomas --daemon process failing to start.

Choc13 commented 2 years ago

FWIW, I could only get formatting working in VSCode again once I installed the latest version (4.6.0-alpha-007) as a local tool. When it was installed globally I was still seeing the formatter hang in VSCode, even with all other extensions disabled except for Ionide and after reinstalling fantomas and rebooting my machine.

nojaf commented 2 years ago

@Choc13 what does your global version of Fantomas give you when you do fantomas --version? Perhaps an incompatible version is still listed on your PATH somehow.

Choc13 commented 2 years ago

@nojaf I had 4.6.0-alpha-006 installed when I reported this IIRC. I had since removed the global tool, but fantomas / ionide had prompted me to install this again yesterday when working on some fsx script (for which I didn't have a local install), so I clicked the install globally option to see if that would work again. That seems to have installed v4.6.0-alpha-004+db3dc672e5ca5d8becefc0559e0c76bcd8d44e19 globally, however the formatting hung forever again. I then tried updating the global tool to latest v4.6.0-alpha-007+b8509dd595f943be409d9bba0a57d9103bdb2d2a and it still hangs.

If I then uninstall the global tool, reload VS Code and run the formatter it detects there is no fantomas installed and prompts me to add it. If I then choose to install locally it populates the tool manifest file with version 4.6.0-alpha-004 and formatting starts working fine again.

nojaf commented 2 years ago

Hmm, thank you for the details. What OS are you on?

Choc13 commented 2 years ago

macOS 12.0.1. Also dotnet SDK version is 6.0.100, but I only installed that a few days ago, so I believe this was happening when I was also working from 5.0.300

nojaf commented 2 years ago

Hi all, we are still working on this issue and I've gained some insight during a call yesterday where I could witness the problem firsthand.

That being said, could somebody try and download the latest version of Fantomas.Client and drop the Fantomas.Client.dll in the bin folder of your ionide install (For me on Windows this is C:\Users\fverdonck\.vscode\extensions\ionide.ionide-fsharp-5.10.1\bin).

Close vscode, reopen and try to format. At the very least a better error message should be shown in the logs.

Thanks in advance.

nojaf commented 2 years ago

@briandunn and others, the last version of Ionide has the recent improvements. Is this working out for anyone?

seanamos commented 2 years ago

@nojaf At least for me, it appears Format Document is working consistently now.