tweag / ormolu

A formatter for Haskell source code
https://ormolu-live.tweag.io
Other
950 stars 83 forks source link

Puts `qualified` at end of `import` leading to compile errors #992

Closed turbotimon closed 1 year ago

turbotimon commented 1 year ago

Describe the bug When i format this code

import Test.Hspec

import qualified Chapter_01_Spec
import qualified Chapter_02_Spec
import qualified Chapter_03_Spec
import qualified Chapter_04_Spec
import qualified Chapter_05_Spec
import qualified Chapter_06_Spec
import qualified Chapter_07_Spec
import qualified Chapter_08_Spec
import qualified Chapter_12_Spec

-- more things here

It gets formatted to this

import Chapter_01_Spec qualified
import Chapter_02_Spec qualified
import Chapter_03_Spec qualified
import Chapter_04_Spec qualified
import Chapter_05_Spec qualified
import Chapter_06_Spec qualified
import Chapter_07_Spec qualified
import Chapter_08_Spec qualified
import Chapter_12_Spec qualified
import Test.Hspec

leading to compile errors

Building test suite 'haskell-exercises-test' for haskell-exercises-0.1.0.0..

test\Spec.hs:1:24: error:
    Found `qualified' in postpositive position.
    To allow this, enable language extension 'ImportQualifiedPost'
  |
1 | import Chapter_01_Spec qualified
  |                        ^^^^^^^^^

...

To Reproduce

Expected behavior

A formatter should not format my code in a way it doesn't compile anymore (ImportQualifiedPost seems not to be standard compiler behaviour)

Environment

# stack.yaml
resolver: lts-20.10

packages:
- . 
-- haskell-exercises.cabal
cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.34.4.
--
-- see: https://github.com/sol/hpack

name:           haskell-exercises
version:        0.1.0.0
description:    xxx
homepage:       xxx
bug-reports:    xxx
author:         xxx
maintainer:     xxx
copyright:      xxx
license:        xxx
license-file:   LICENSE
build-type:     Simple
extra-source-files:
    README.md
    ChangeLog.md

source-repository head
  type: git
  location: xxx

library
  exposed-modules:
      Chapter_01
      Chapter_02
      Chapter_03
      Chapter_04
      Chapter_05
      Chapter_06
      Chapter_07
      Chapter_08
      Chapter_10
      Chapter_12
      Chapter_16
  other-modules:
      Paths_haskell_exercises
  hs-source-dirs:
      src
  build-depends:
      base >=4.7 && <5
  default-language: Haskell2010

executable haskell-exercises-exe
  main-is: Main.hs
  other-modules:
      Paths_haskell_exercises
  hs-source-dirs:
      app
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , haskell-exercises
  default-language: Haskell2010

test-suite haskell-exercises-test
  type: exitcode-stdio-1.0
  main-is: Spec.hs
  other-modules:
      Chapter_01_Spec
      Chapter_02_Spec
      Chapter_03_Spec
      Chapter_04_Spec
      Chapter_05_Spec
      Chapter_06_Spec
      Chapter_07_Spec
      Chapter_08_Spec
      Chapter_12_Spec
      Paths_haskell_exercises
  hs-source-dirs:
      test
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      QuickCheck
    , base >=4.7 && <5
    , genvalidity-hspec
    , haskell-exercises
    , hspec
  default-language: Haskell2010

Additional context

Changing the formatter in the plugin, e.g. to floskell does solve the problem

amesgen commented 1 year ago

Thanks for the report; can you post the respective .cabal file and the GHC version you are using?

turbotimon commented 1 year ago

sure i added all above

amesgen commented 1 year ago

Thanks for the update! Sadly, I can't seem to be able to reproduce this on x86_64-linux using GHC 9.2 and HLS 1.9.0.0 from nixpkgs :thinking:

One potential reason could be that HLS somehow tells Ormolu to use GHC2021 (which implies ImportQualifiedPost) instead of Haskell2010 (which you specify in the .cabal file). As GHC2021 was added in GHC 9.2 as the default language, it would be interesting whether you can reproduce it with GHC 9.0 (but don't feel obligated in any way to do this, purely optional).

If you just want to get your code to compile, you can add

{-# LANGUAGE ImportQualifiedPost #-}

to the modules in question; but that is of course just a workaround.

turbotimon commented 1 year ago

Thanks for your detailed answer, i will have a deeper look into it as soon as i find time. for the moment its the workaround