tweag / ormolu

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

Include default dependencies when source file not mentioned in cabal file #1033

Closed brandonchinn178 closed 1 year ago

brandonchinn178 commented 1 year ago

Resolves #1032

Technically, only the first commit is necessary to solve the issue. But I refactored the CabalInfo subsystem to ensure that the former CabalFileNotFound + CabalFileDidNotMention branches used the same logic. Specifically, this change allows refineConfig to be the sole source of truth for defining the Cabal information for both a cabal file not existing and a cabal file not mentioning a source file (previously, this was two different locations: parseCabalInfo was doing it for CabalFileDidNotMention and refineConfig was doing it for CabalFileNotFound).

Test

Add the following Haskell file:

foo =
  unlines $
    hello <> world : ["Line " ++ show x | x <- [10 .. 20]]
  where
    hello = "hello"
    world = " world"

Running ormolu on it formats the same in both cases:

  1. The Haskell file is in an empty directory
  2. The Haskell file is in a directory with a Cabal file, but it's not mentioned in the Cabal file
mrkkrp commented 1 year ago

Thanks! I merged only the first commit though because I'm not sure about the benefits vs mental cost of the others.

brandonchinn178 commented 1 year ago

@mrkkrp Thanks! I do think the refactor is an improvement for overall understandability. e.g. it's a bit confusing IMO for CabalInfo to be different for the same cabal file but different source files. Do you mind if I open a separate PR to discuss that?

mrkkrp commented 1 year ago

CabalInfo could be construed as "Cabal information for this particular module we are formatting". Feel free to open a separate PR though.