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

Inconsistency in Ormolu formatting #213

Closed
sir4ur0n opened this issue Jul 20, 2020 · 2 comments · Fixed by #257
Closed

Inconsistency in Ormolu formatting #213

sir4ur0n opened this issue Jul 20, 2020 · 2 comments · Fixed by #257
Labels
component: plugins type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. type: enhancement New feature or request

Comments

@sir4ur0n
Copy link
Collaborator

sir4ur0n commented Jul 20, 2020

Hey, sorry for opening yet another issue, I kinda feel guilty 😅 I noticed what appears to be a bug, and I took the opportunity to ask additional formatter-related questions, I can push them to other issues if it's simpler for you to manage.

I just tested in VS Code "Format document", which I assume relies on Ormolu (I configured the Ormolu formatter in languageServerHaskell.formattingProvider).

1 - Format is bugged, and inconsistent

We see an inconsistency between applying "Format" from HLS (in VS Code) or applying Ormolu directly in CLI.

Consider this code:

module Foo where

import Prelude
import GHC.Generics (Generic)
import Data.Text (Text)

data Foo
  = Foo
      { _name :: Text,
        _description :: Maybe Text
      }
  deriving (Generic, Show, Eq)

Applying reformatting from VS Code (and I checked in HLS Output view that it uses Ormolu), this gets reformatted as such:

module Foo where

import GHC.Generics (Generic)
import Prelude
import Data.Text (Text)

data Foo = Foo
  { _name :: Text,
    _description :: Maybe Text
  }
  deriving (Generic, Show, Eq)

Note in particular that import order was modified, yet it's not completely alphabetical

Now run the formatter again in VS Code (yes, without modifying anything) and the second time, import order is correct! 💥

module Foo where

import Data.Text (Text)
import GHC.Generics (Generic)
import Prelude

data Foo = Foo
  { _name :: Text,
    _description :: Maybe Text
  }
  deriving (Generic, Show, Eq)

Now when running Ormolu in CLI, the original piece of code gets reformatted to:

module Foo where

import Data.Text (Text)
import GHC.Generics (Generic)
import Prelude

data Foo = Foo
  { _name :: Text,
    _description :: Maybe Text
  }
  deriving (Generic, Show, Eq)

i.e. Ormolu in CLI run once behaves the same as formatting twice in a row in VS Code 💥

Edit: the situation seems to be even worse. Here's an additional example:

import Data.Text (Text)
import GHC.Generics (Generic)
import Prelude
import Data.Time.Calendar
import Data.Aeson

Even after 50 reformats, the import order remains:

import Data.Aeson
import Data.Text (Text)
import GHC.Generics (Generic)
import Data.Time.Calendar
import Prelude

While Ormolu in CLI correctly returns:

import Data.Aeson
import Data.Text (Text)
import Data.Time.Calendar
import GHC.Generics (Generic)
import Prelude

2 - Is it a packaged version of Ormolu, or does it use the path one?

3 - Custom options

If I am correct, how would one pass custom options to Ormolu, like --ghc-opt -XBangPatterns? So far we were using Nix + a custom shell wrapper to force our own options as such:

ormolu-wrapped = symlinkJoin {
  name = "ormolu";
  paths = [ ormolu ];
  buildInputs = [ pkgs.makeWrapper ];
  postBuild = ''
    wrapProgram $out/bin/ormolu \
      --add-flags "\
        --ghc-opt -XBangPatterns \
        --ghc-opt -XTypeApplications \
      "
  '';
};

Would this still work/get picked up by HLS?

4 - Formatter (plugins) versions in HLS logs

Shouldn't the HLS logs display the formatter version? Currently there's no way to know which one is picked/used
I think the perfect opportunity would be at startup while listing plugins:

Starting (haskell-language-server)LSP server...
  with arguments: Arguments {argLSP = True, argsCwd = Nothing, argFiles = [], argsVersion = False, argsShakeProfiling = Nothing, argsTesting = False, argsExamplePlugin = False, argsDebugOn = False, argsLogFile = Nothing, argsThreads = 0}
  with plugins: [PluginId "brittany",PluginId "floskell",PluginId "ghcide",PluginId "ormolu",PluginId "pragmas",PluginId "stylish-haskell"]

Thank you again for the help, quick replies, and overall support 🙏 Let me know if I can help (with my modest understanding of this project)

@fendor
Copy link
Collaborator

fendor commented Jul 20, 2020

Hi! Thank you for this nice bug report and you should not guilty at all! It is nice to get such detailed bug reports.

I dont have details, just the following Information:

@jneira jneira added component: plugins type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. type: enhancement New feature or request labels Jul 23, 2020
@jneira
Copy link
Member

jneira commented Jul 23, 2020

Shouldn't the HLS logs display the formatter version? Currently there's no way to know which one is picked/used
I think the perfect opportunity would be at startup while listing plugins

That is a good idea. Anyway the ourmulu version used in master would be 0.1.2.0, you can check it in the .cabal file:

... having in account that there is no version greater than 0.1.2.0 (https://hackage.haskell.org/package/ormolu)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: plugins type: bug Something isn't right: doesn't work as intended, documentation is missing/outdated, etc.. type: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants