Skip to content

Compliled-in fourmolu 1.4.0.0 in HLS 2.4.0.0 formats differently than standalone compiled fourmolu 1.4.0.0 #3882

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

Open
lylek opened this issue Nov 27, 2023 · 7 comments
Labels
component: formatters status: blocked Not actionable, because blocked by upstream/GHC etc. type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc..

Comments

@lylek
Copy link

lylek commented Nov 27, 2023

Your environment

Which OS do you use?

Windows
Which version of GHC do you use and how did you install it?

9.6.3 from nix
How is your project built (alternative: link to the project)?
cabal

Which LSP client (editor/plugin) do you use?

VSCode + Haskell extension
Which version of HLS do you use and how did you install it?

2.4.0.0 from nix
Have you configured HLS in any way (especially: a hie.yaml file)?
No

Steps to reproduce

Create code that has an infix operator leading on a line, e.g.:

module Main where

import qualified Data.Map.Strict as M

main :: IO ()
main = do
  let foo =
        M.fromList
          $ zipWith
            (+)
            [0 .. 9]
            [10, 20 .. 30]
  pure ()

Format it via VSCode (Shift+Alt+F): It remains the same.

Format it on the command line using fourmolu path/to/Main.hs. It comes out with the $ on the end of the previous line:

main :: IO ()
main = do
  let foo =
        M.fromList $
          zipWith
            (+)
            [0 .. 9]
            [10, 20 .. 30]
  pure ()

Expected behaviour

HLS in VSCode should format it with the $ trailing after M.fromList.

Actual behaviour

HLS in VSCode formats it with the $ leading before zipWith.

Debug information

On the command line:

$ fourmolu --version
fourmolu 0.14.0.0
using ghc-lib-parser 9.6.3.20231014

In the HLS log within VSCode:

2023-11-27T17:37:51.110548Z | Debug | fourmolu: Using compiled in fourmolu-0.14.0.0

Note that it doesn't say what version of ghc-lib-parser it's using. Maybe that makes the difference? Was it compiled with ghc-lib-parser 9.6.2?

@lylek lylek added status: needs triage type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. labels Nov 27, 2023
@lylek
Copy link
Author

lylek commented Nov 27, 2023

I tried compiling fourmolu 1.4.0.0 using GHC 9.6.2 and it still put the $ on the line after M.fromList.

fourmolu 0.14.0.0
using ghc-lib-parser 9.6.2.20230523

So I guess the version of ghc-lib-parser isn't the issue.

@lylek
Copy link
Author

lylek commented Nov 27, 2023

I'm able to work around the problem by having HLS shell out to an external fourmolu instead of using the built-in one. Still want to know why the built-in one is not formatting correctly.

@fendor
Copy link
Collaborator

fendor commented Nov 27, 2023

Hi, thank you for your bug report!

There are many related issues #3788, #3454, #3421, I think this is a duplicate of one of these, but I haven't checked, yet. One of the oldest issues we have, #411

@lylek
Copy link
Author

lylek commented Nov 27, 2023

Hi, thank you for your bug report!

There are many related issues #3788, #3454, #3421, I think this is a duplicate of one of these, but I haven't checked, yet. One of the oldest issues we have, #411

Thanks, but I don't think this is quite a duplicate of any of those. Maybe #3788, but the conversation in that issue is leading toward the idea that it's a fixity issue, and there's no fixity involved here, as there's only one operator. The issues #3454 and #3421 are solely about fixity, and fixity is not the issue here.

As for #411, that seems to have been the motivation for allowing use of an external formatter, which does work for me. But that doesn't resolve the issue of why the built-in fourmolu is not formatting the same way as one compiled on its own, even if it's supposedly the same version as the built-in one.

I suspect that the fixity discussion is a red herring and perhaps this ticket provides additional evidence toward that idea.

What those tickets do show, though, is that this issue has been around for a long time.

@lylek
Copy link
Author

lylek commented Nov 27, 2023

Hmm, that's weird: seems as though other operators, even &, with the same fixity as $, are formatting to the left of the second line, even in the standalone fourmolu.

Is $ just an odd exception?

@lylek
Copy link
Author

lylek commented Nov 29, 2023

Through experimentation, I found that fourmolu treats infixr 0 specially. If an operator has that fixity, then it will format it to the end of the first line. All other operators get formatted to the start of the second line. Note that it's both the associativity r and the precedence 0 that trigger that special behavior.

Both the compiled-in fourmolu in HLS and the standalone fourmolu are reading the config file fourmolu.yaml. But the standalone fourmolu is defaulting $ to infixr 0 even if it's not specified in the config file. The compiled-in fourmolu is defaulting $ to some other fixity (not sure what), and therefore formatting it differently.

So that would be the thing to look for - what is the discrepancy that would make one build default the fixity for $, and the other not?

@lylek
Copy link
Author

lylek commented Nov 29, 2023

This is may be happening in the ourmolu part of the code, not the forked part for fourmolu.

@michaelpj michaelpj added the status: blocked Not actionable, because blocked by upstream/GHC etc. label Dec 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: formatters status: blocked Not actionable, because blocked by upstream/GHC etc. type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc..
Projects
None yet
Development

No branches or pull requests

3 participants